Skip to content

Commit

Permalink
feat(prop-strategies): implement strategy for ExplicitTag
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBestTvarynka committed Nov 24, 2023
1 parent 2215278 commit 10c169f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion crates/asn1-parser/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Asn1Type<'_> {
Asn1Type::BitString(b) => Asn1Type::BitString(b.to_owned()),
Asn1Type::Bool(b) => Asn1Type::Bool(b.clone()),
Asn1Type::Null(n) => Asn1Type::Null(n.clone()),
// Asn1Type::ExplicitTag(_) => todo!(),
Asn1Type::ExplicitTag(e) => Asn1Type::ExplicitTag(e.to_owned()),
_ => unimplemented!(),
}
}
Expand Down
16 changes: 16 additions & 0 deletions crates/asn1-parser/src/tags/explicit_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ pub struct ExplicitTag<'data> {

pub type OwnedExplicitTag = ExplicitTag<'static>;

impl<'data> ExplicitTag<'data> {
pub fn new(tag: u8, inner: Asn1<'data>) -> Self {
Self {
tag,
inner,
}
}

pub fn to_owned(&self) -> OwnedExplicitTag {
OwnedExplicitTag {
tag: self.tag,
inner: self.inner.to_owned(),
}
}
}

impl Asn1Entity for ExplicitTag<'_> {
fn tag(&self) -> Tag {
Tag(self.tag)
Expand Down
32 changes: 21 additions & 11 deletions crates/prop-strategies/src/constructors.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
use asn1_parser::{Asn1Type, OwnedAsn1, OwnedAsn1Type, OwnedSequence};
use asn1_parser::{Asn1Type, OwnedAsn1, OwnedAsn1Type, OwnedExplicitTag, OwnedSequence};
use proptest::collection::vec;
use proptest::prop_oneof;
use proptest::strategy::Strategy;
use proptest::strategy::{Just, Strategy};

use crate::{any_bit_string, any_bool, any_octet_string, any_utf8_string};
use crate::{any_bit_string, any_bmp_string, any_bool, any_null, any_octet_string, any_utf8_string};

fn any_leaf_asn1_type() -> impl Strategy<Value = OwnedAsn1Type> {
prop_oneof![
any_octet_string().prop_map(Asn1Type::OctetString),
any_utf8_string().prop_map(Asn1Type::Utf8String),
any_bit_string().prop_map(Asn1Type::BitString),
any_bmp_string().prop_map(Asn1Type::BmpString),
any_bool().prop_map(Asn1Type::Bool),
any_null().prop_map(Asn1Type::Null),
]
.no_shrink()
}

pub fn recursive_empty_asn1_type() -> impl Strategy<Value = OwnedAsn1Type> {
any_leaf_asn1_type().prop_recursive(16, 64, 32, |inner| {
vec(inner, 1..16).prop_map(|fields| {
Asn1Type::Sequence(OwnedSequence::from(
fields
.into_iter()
.map(|asn1_type| OwnedAsn1::new(Default::default(), Box::new(asn1_type)))
.collect::<Vec<_>>(),
))
})
prop_oneof![
vec(inner.clone(), 1..16).prop_map(|fields| {
Asn1Type::Sequence(OwnedSequence::from(
fields
.into_iter()
.map(|asn1_type| OwnedAsn1::new(Default::default(), Box::new(asn1_type)))
.collect::<Vec<_>>(),
))
}),
(0_u8..31)
.prop_flat_map(move |tag| (Just(tag), inner.clone()))
.prop_map(|(tag, inner)| Asn1Type::ExplicitTag(OwnedExplicitTag::new(
tag,
OwnedAsn1::new(Default::default(), Box::new(inner))
))),
]
})
}
3 changes: 1 addition & 2 deletions crates/prop-strategies/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod generic_types;
mod string;

use asn1_parser::{Asn1Type, OwnedAsn1Type};
use constructors::recursive_empty_asn1_type;
pub use constructors::*;
pub use generic_types::*;
use proptest::collection::vec;
Expand All @@ -11,8 +12,6 @@ use proptest::prop_oneof;
use proptest::strategy::Strategy;
pub use string::*;

use crate::constructors::recursive_empty_asn1_type;

pub fn bytes(size: usize) -> impl Strategy<Value = Vec<u8>> {
vec(any::<u8>(), 0..size).no_shrink()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/prop-strategies/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use asn1_parser::{BitString, OwnedBitString, OwnedOctetString, OwnedUtf8String, OwnedBmpString};
use asn1_parser::{BitString, OwnedBitString, OwnedBmpString, OwnedOctetString, OwnedUtf8String};
use proptest::prop_compose;

use crate::{bytes, string};
Expand Down

0 comments on commit 10c169f

Please sign in to comment.