Skip to content

Commit 02c666a

Browse files
committed
der: add test for application tags
1 parent 86851cf commit 02c666a

File tree

5 files changed

+181
-238
lines changed

5 files changed

+181
-238
lines changed

der/src/asn1/application.rs

+27
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,30 @@ pub type ApplicationExplicitRef<'a, const TAG: u16, T> =
1919
/// Application class, reference, IMPLICIT
2020
pub type ApplicationImplicitRef<'a, const TAG: u16, T> =
2121
CustomClassImplicitRef<'a, TAG, T, CLASS_APPLICATION>;
22+
23+
#[cfg(test)]
24+
#[allow(clippy::unwrap_used)]
25+
mod tests {
26+
use crate::{
27+
asn1::{context_specific::ContextSpecificExplicit, OctetStringRef},
28+
Decode, Encode,
29+
};
30+
use hex_literal::hex;
31+
32+
#[test]
33+
fn round_trip() {
34+
const EXAMPLE_BYTES: &[u8] = &hex!(
35+
"A2 06"
36+
"04 04"
37+
"01020304"
38+
);
39+
40+
let field =
41+
ContextSpecificExplicit::<2, OctetStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap();
42+
assert_eq!(field.value, OctetStringRef::new(&[1, 2, 3, 4]).unwrap());
43+
44+
let mut buf = [0u8; 128];
45+
let encoded = field.encode_to_slice(&mut buf).unwrap();
46+
assert_eq!(encoded, EXAMPLE_BYTES);
47+
}
48+
}

der/src/asn1/context_specific.rs

-22
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,6 @@ pub type ContextSpecificExplicitRef<'a, const TAG: u16, T> =
2222
pub type ContextSpecificImplicitRef<'a, const TAG: u16, T> =
2323
CustomClassImplicitRef<'a, TAG, T, CLASS_CONTEXT_SPECIFIC>;
2424

25-
// pub fn decode_implicit<'a, R: Reader<'a>, T: Tagged + DecodeValue<'a>>(
26-
// number: TagNumber,
27-
// reader: &mut R,
28-
// ) -> Result<Option<T>, T::Error> {
29-
// match AnyCustomClassImplicit::decode_skipping(Class::ContextSpecific, number, reader) {
30-
// Ok(Some(custom)) => Ok(Some(custom.value)),
31-
// Ok(None) => Ok(None),
32-
// Err(err) => Err(err),
33-
// }
34-
// }
35-
36-
// pub fn decode_explicit<'a, R: Reader<'a>, T: Decode<'a>>(
37-
// number: TagNumber,
38-
// reader: &mut R,
39-
// ) -> Result<Option<T>, T::Error> {
40-
// match AnyCustomClassExplicit::decode_skipping(Class::ContextSpecific, number, reader) {
41-
// Ok(Some(custom)) => Ok(Some(custom.value)),
42-
// Ok(None) => Ok(None),
43-
// Err(err) => Err(err),
44-
// }
45-
// }
46-
4725
#[cfg(test)]
4826
#[allow(clippy::unwrap_used)]
4927
mod tests {

der/src/asn1/private.rs

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ mod tests {
8080
use crate::{asn1::BitStringRef, Decode, Encode, SliceReader};
8181
use hex_literal::hex;
8282

83-
// Public key data from `pkcs8` crate's `ed25519-pkcs8-v2.der`
8483
const EXAMPLE_BYTES: &[u8] =
8584
&hex!("E123032100A3A7EAE3A8373830BC47E1167BC50E1DB551999651E0E2DC587623438EAC3F31");
8685

der/src/tag.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ fn parse_parts<'a, R: Reader<'a>>(first_byte: u8, reader: &mut R) -> Result<(boo
377377
Err(Error::new(ErrorKind::TagNumberInvalid, reader.position()))
378378
}
379379

380+
/// Length of encoded tag depends only on number it encodes
380381
fn tag_length(tag_number: u16) -> Length {
381382
if tag_number <= 30 {
382383
Length::ONE
@@ -389,6 +390,21 @@ fn tag_length(tag_number: u16) -> Length {
389390
}
390391
}
391392

393+
fn tag_class_number_bytes(
394+
class: Class,
395+
tag_number: TagNumber,
396+
constructed: bool,
397+
buf: &mut [u8; Tag::MAX_SIZE],
398+
) -> &[u8] {
399+
let mut first_byte = class as u8;
400+
if constructed {
401+
first_byte |= CONSTRUCTED_FLAG;
402+
}
403+
let num = tag_number.value();
404+
tag_number_bytes(first_byte, num, buf)
405+
}
406+
407+
/// Tag contains class bits, constructed flag and number
392408
#[allow(clippy::cast_possible_truncation)]
393409
fn tag_number_bytes(first_byte: u8, num: u16, buf: &mut [u8; Tag::MAX_SIZE]) -> &[u8] {
394410
if num <= 30 {
@@ -418,14 +434,9 @@ impl Encode for Tag {
418434
}
419435

420436
fn encode(&self, writer: &mut impl Writer) -> Result<()> {
421-
let mut first_byte = self.class() as u8;
422-
if self.is_constructed() {
423-
first_byte |= CONSTRUCTED_FLAG;
424-
}
425-
let num = self.number().value();
426-
427437
let mut buf = [0u8; Tag::MAX_SIZE];
428-
let tag_bytes = tag_number_bytes(first_byte, num, &mut buf);
438+
let tag_bytes =
439+
tag_class_number_bytes(self.class(), self.number(), self.is_constructed(), &mut buf);
429440
writer.write(tag_bytes)
430441
}
431442
}
@@ -499,7 +510,10 @@ impl fmt::Display for Tag {
499510

500511
impl fmt::Debug for Tag {
501512
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
502-
write!(f, "Tag(0x{:02x}: {})", self.number().value(), self)
513+
let mut buf = [0u8; Tag::MAX_SIZE];
514+
let tag_bytes =
515+
tag_class_number_bytes(self.class(), self.number(), self.is_constructed(), &mut buf);
516+
write!(f, "Tag({:02X?}: {})", tag_bytes, self)
503517
}
504518
}
505519

0 commit comments

Comments
 (0)