Skip to content

Commit

Permalink
Option: require T::Tagged, and check tag before constraints (Closes #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
chifflier committed Feb 21, 2024
1 parent c8292f4 commit 1ca9981
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/asn1_types/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ use crate::*;
impl<'a, T> FromBer<'a> for Option<T>
where
T: FromBer<'a>,
T: Tagged,
{
fn from_ber(bytes: &'a [u8]) -> ParseResult<Self> {
if bytes.is_empty() {
return Ok((bytes, None));
}
if let Ok((_, header)) = Header::from_ber(bytes) {
if T::TAG != header.tag {
// not the expected tag, early return
return Ok((bytes, None));
}
}
match T::from_ber(bytes) {
Ok((rem, t)) => Ok((rem, Some(t))),
Err(nom::Err::Error(Error::UnexpectedTag { .. })) => Ok((bytes, None)),
Err(e) => Err(e),
}
}
Expand All @@ -24,14 +30,20 @@ where
impl<'a, T> FromDer<'a> for Option<T>
where
T: FromDer<'a>,
T: Tagged,
{
fn from_der(bytes: &'a [u8]) -> ParseResult<Self> {
if bytes.is_empty() {
return Ok((bytes, None));
}
if let Ok((_, header)) = Header::from_der(bytes) {
if T::TAG != header.tag {
// not the expected tag, early return
return Ok((bytes, None));
}
}
match T::from_der(bytes) {
Ok((rem, t)) => Ok((rem, Some(t))),
Err(nom::Err::Error(Error::UnexpectedTag { .. })) => Ok((bytes, None)),
Err(e) => Err(e),
}
}
Expand Down
71 changes: 71 additions & 0 deletions tests/issue-27-option-struct-derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use asn1_rs::*;

#[derive(DerSequence, Debug, PartialEq)]
struct TestBool {
a: u16,
b: Option<bool>,
c: u32,
}

#[test]
fn issue_27_1() {
let x = TestBool {
a: 0x1234,
b: None,
c: 0x5678,
};

let expected = &[48, 8, 2, 2, 18, 52, 2, 2, 86, 120];

let (_, val) = TestBool::from_der(expected).unwrap();
assert_eq!(val, x);
}

#[test]
fn issue_27_2() {
let x = TestBool {
a: 0x1234,
b: Some(true),
c: 0x5678,
};

let expected = &[48, 11, 2, 2, 18, 52, 1, 1, 255, 2, 2, 86, 120];

let (_, val) = TestBool::from_der(expected).unwrap();
assert_eq!(val, x);
}

#[derive(DerSequence, Debug, PartialEq)]
struct TestInt {
a: u16,
b: Option<u32>,
c: bool,
}

#[test]
fn issue_27_3() {
let x = TestInt {
a: 0x1234,
b: None,
c: true,
};

let expected = &[48, 7, 2, 2, 18, 52, 1, 1, 255];

let (_, val) = TestInt::from_der(expected).unwrap();
assert_eq!(val, x);
}

#[test]
fn issue_27_4() {
let x = TestInt {
a: 0x1234,
b: Some(0x5678),
c: true,
};

let expected = &[48, 11, 2, 2, 18, 52, 2, 2, 86, 120, 1, 1, 255];

let (_, val) = TestInt::from_der(expected).unwrap();
assert_eq!(val, x);
}

0 comments on commit 1ca9981

Please sign in to comment.