1 //! `SetOf` tests. 2 3 #![cfg(feature = "alloc")] 4 5 use der::{asn1::SetOfVec, DerOrd}; 6 use proptest::{prelude::*, string::*}; 7 use std::collections::BTreeSet; 8 9 proptest! { 10 #[test] 11 fn sort_equiv(bytes in bytes_regex(".{0,64}").unwrap()) { 12 let mut uniq = BTreeSet::new(); 13 14 // Ensure there are no duplicates 15 if bytes.iter().copied().all(move |x| uniq.insert(x)) { 16 let mut expected = bytes.clone(); 17 expected.sort_by(|a, b| a.der_cmp(b).unwrap()); 18 19 let set = SetOfVec::try_from(bytes).unwrap(); 20 prop_assert_eq!(expected.as_slice(), set.as_slice()); 21 } 22 } 23 } 24 25 /// Set ordering tests. 26 #[cfg(all(feature = "derive", feature = "oid"))] 27 mod ordering { 28 use der::{ 29 asn1::{AnyRef, ObjectIdentifier, SetOf, SetOfVec}, 30 Decode, Sequence, ValueOrd, 31 }; 32 use hex_literal::hex; 33 34 /// X.501 `AttributeTypeAndValue` 35 #[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence, ValueOrd)] 36 pub struct AttributeTypeAndValue<'a> { 37 pub oid: ObjectIdentifier, 38 pub value: AnyRef<'a>, 39 } 40 41 const OUT_OF_ORDER_RDN_EXAMPLE: &[u8] = 42 &hex!("311F301106035504030C0A4A4F484E20534D495448300A060355040A0C03313233"); 43 44 /// For compatibility reasons, we allow non-canonical DER with out-of-order 45 /// sets in order to match the behavior of other implementations. 46 #[test] allow_out_of_order_setof()47 fn allow_out_of_order_setof() { 48 assert!(SetOf::<AttributeTypeAndValue<'_>, 2>::from_der(OUT_OF_ORDER_RDN_EXAMPLE).is_ok()); 49 } 50 51 /// Same as above, with `SetOfVec` instead of `SetOf`. 52 #[test] allow_out_of_order_setofvec()53 fn allow_out_of_order_setofvec() { 54 assert!(SetOfVec::<AttributeTypeAndValue<'_>>::from_der(OUT_OF_ORDER_RDN_EXAMPLE).is_ok()); 55 } 56 57 /// Test to ensure ordering is handled correctly. 58 #[test] ordering_regression()59 fn ordering_regression() { 60 let der_bytes = hex!("3139301906035504030C12546573742055736572393031353734333830301C060A0992268993F22C640101130E3437303031303030303134373333"); 61 let set = SetOf::<AttributeTypeAndValue<'_>, 3>::from_der(&der_bytes).unwrap(); 62 let attr1 = set.get(0).unwrap(); 63 assert_eq!(ObjectIdentifier::new("2.5.4.3").unwrap(), attr1.oid); 64 } 65 } 66