Debian Codegraph @

tests/set_of.rs

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