Skip to content

Commit

Permalink
fix(der_derive): tests with new implicit/explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
dishmaker committed Oct 9, 2024
1 parent f804da1 commit 86851cf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 42 deletions.
3 changes: 0 additions & 3 deletions der/src/asn1/any_custom_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ where
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Self, Self::Error> {
let header = Header::decode(reader)?;

// TODO(dishmaker): x509-cert tests fail due to read_nested:
// let value = T::decode_value(reader, header)?;

let value = reader.read_nested(header.length, |reader| T::decode_value(reader, header))?;

if header.tag.is_constructed() != value.tag().is_constructed() {
Expand Down
46 changes: 16 additions & 30 deletions der_derive/src/choice/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,23 @@ mod tests {

#[test]
fn explicit() {
for tag_number in [0, 1, 2, 3] {
for tag_number_u16 in [0, 1, 2, 3] {
for constructed in [false, true] {
let ident = Ident::new("ExplicitVariant", Span::call_site());
let attrs = FieldAttrs {
constructed,
class: Some(ClassNum::ContextSpecific(TagNumber(tag_number))),
class: Some(ClassNum::ContextSpecific(TagNumber(tag_number_u16))),
..Default::default()
};
assert_eq!(attrs.tag_mode, TagMode::Explicit);

let tag = TagOrPath::Tag(Tag::ContextSpecific {
constructed,
number: TagNumber(tag_number),
number: TagNumber(tag_number_u16),
});

let variant = ChoiceVariant { ident, attrs, tag };
let tag_number = TagNumber(tag_number).to_tokens();
let tag_number = TagNumber(tag_number_u16).to_tokens();

assert_eq!(
variant.to_decode_tokens().to_string(),
Expand All @@ -287,12 +287,9 @@ mod tests {
constructed: #constructed,
number: #tag_number,
} => Ok(Self::ExplicitVariant(
match ::der::asn1::ContextSpecific::<>::decode(reader)? {
field if field.tag_number == #tag_number => Some(field),
_ => None
}
::der::asn1::ContextSpecificExplicit::<#tag_number_u16, _>::decode_skipping(reader)?
.ok_or_else(|| {
der::Tag::ContextSpecific {
::der::Tag::ContextSpecific {
number: #tag_number,
constructed: #constructed
}
Expand All @@ -307,9 +304,7 @@ mod tests {
assert_eq!(
variant.to_encode_value_tokens().to_string(),
quote! {
Self::ExplicitVariant(variant) => ::der::asn1::ContextSpecificRef {
tag_number: #tag_number,
tag_mode: ::der::TagMode::Explicit,
Self::ExplicitVariant(variant) => ::der::asn1::ContextSpecificExplicitRef::<'_, #tag_number_u16, _> {
value: variant,
}
.encode_value(encoder),
Expand All @@ -320,9 +315,7 @@ mod tests {
assert_eq!(
variant.to_value_len_tokens().to_string(),
quote! {
Self::ExplicitVariant(variant) => ::der::asn1::ContextSpecificRef {
tag_number: #tag_number,
tag_mode: ::der::TagMode::Explicit,
Self::ExplicitVariant(variant) => ::der::asn1::ContextSpecificExplicitRef::<'_, #tag_number_u16, _> {
value: variant,
}
.value_len(),
Expand All @@ -346,24 +339,24 @@ mod tests {

#[test]
fn implicit() {
for tag_number in [0, 1, 2, 3] {
for tag_number_u16 in [0, 1, 2, 3] {
for constructed in [false, true] {
let ident = Ident::new("ImplicitVariant", Span::call_site());

let attrs = FieldAttrs {
constructed,
class: Some(ClassNum::ContextSpecific(TagNumber(tag_number))),
class: Some(ClassNum::ContextSpecific(TagNumber(tag_number_u16))),
tag_mode: TagMode::Implicit,
..Default::default()
};

let tag = TagOrPath::Tag(Tag::ContextSpecific {
constructed,
number: TagNumber(tag_number),
number: TagNumber(tag_number_u16),
});

let variant = ChoiceVariant { ident, attrs, tag };
let tag_number = TagNumber(tag_number).to_tokens();
let tag_number = TagNumber(tag_number_u16).to_tokens();

assert_eq!(
variant.to_decode_tokens().to_string(),
Expand All @@ -372,12 +365,9 @@ mod tests {
constructed: #constructed,
number: #tag_number,
} => Ok(Self::ImplicitVariant(
::der::asn1::ContextSpecific::<>::decode_implicit(
reader,
#tag_number
)?
::der::asn1::ContextSpecificImplicit::<#tag_number_u16, _>::decode_skipping(reader)?
.ok_or_else(|| {
der::Tag::ContextSpecific {
::der::Tag::ContextSpecific {
number: #tag_number,
constructed: #constructed
}
Expand All @@ -392,9 +382,7 @@ mod tests {
assert_eq!(
variant.to_encode_value_tokens().to_string(),
quote! {
Self::ImplicitVariant(variant) => ::der::asn1::ContextSpecificRef {
tag_number: #tag_number,
tag_mode: ::der::TagMode::Implicit,
Self::ImplicitVariant(variant) => ::der::asn1::ContextSpecificImplicitRef::<'_, #tag_number_u16, _> {
value: variant,
}
.encode_value(encoder),
Expand All @@ -405,9 +393,7 @@ mod tests {
assert_eq!(
variant.to_value_len_tokens().to_string(),
quote! {
Self::ImplicitVariant(variant) => ::der::asn1::ContextSpecificRef {
tag_number: #tag_number,
tag_mode: ::der::TagMode::Implicit,
Self::ImplicitVariant(variant) => ::der::asn1::ContextSpecificImplicitRef::<'_, #tag_number_u16, _> {
value: variant,
}
.value_len(),
Expand Down
13 changes: 4 additions & 9 deletions der_derive/src/sequence/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,10 @@ mod tests {
assert_eq!(
field.to_decode_tokens().to_string(),
quote! {
let implicit_field = ::der::asn1::ContextSpecific::<>::decode_implicit(
reader,
::der::TagNumber(0)
)?
let implicit_field = ::der::asn1::ContextSpecificImplicit::<0u16, _>::decode_skipping(reader)?
.ok_or_else(|| {
der::Tag::ContextSpecific {
number: ::der::TagNumber(0),
::der::Tag::ContextSpecific {
number: ::der::TagNumber(0u16),
constructed: false
}
.value_error()
Expand All @@ -341,9 +338,7 @@ mod tests {
assert_eq!(
field.to_encode_tokens().to_string(),
quote! {
::der::asn1::ContextSpecificRef {
tag_number: ::der::TagNumber(0),
tag_mode: ::der::TagMode::Implicit,
::der::asn1::ContextSpecificImplicitRef::<'_, 0u16, _> {
value: &self.implicit_field,
}
}
Expand Down

0 comments on commit 86851cf

Please sign in to comment.