Debian Codegraph @

tests/derive_no_alloc.rs

raw symbols SCIP
1 //! Tests for custom derive support. Without alloc feature.
2 //!
3 //! # Debugging with `cargo expand`
4 //!
5 //! To expand the Rust code generated by the proc macro when debugging
6 //! issues related to these tests, run:
7 //!
8 //! ```text
9 //! $ cargo expand --test derive_no_alloc --all-features
10 //! ```
11
12 #![cfg(feature = "derive")]
13 // TODO: fix needless_question_mark in the derive crate
14 #![allow(clippy::needless_question_mark)]
15
16 /// Custom derive test cases for the `Sequence` macro, without alloc.
17 mod sequence {
18 use der::Decode;
19 use der::Encode;
20 use der::Sequence;
21
22 #[derive(Sequence, Default, Eq, PartialEq, Debug)]
23 #[asn1(tag_mode = "IMPLICIT")]
24 pub struct TypeCheckArraysSequenceFieldAttributeCombinations {
25 #[asn1(type = "OCTET STRING", deref = "true")]
26 pub array_bytes: [u8; 2],
27
28 #[asn1(type = "BIT STRING", deref = "true")]
29 pub array_bits: [u8; 2],
30
31 #[asn1(type = "OCTET STRING", context_specific = "0", deref = "true")]
32 pub array_implicit_bytes: [u8; 2],
33
34 #[asn1(type = "BIT STRING", context_specific = "1", deref = "true")]
35 pub array_implicit_bits: [u8; 2],
36
37 #[asn1(
38 type = "OCTET STRING",
39 context_specific = "2",
40 tag_mode = "EXPLICIT",
41 deref = "true"
42 )]
43 pub array_explicit_bytes: [u8; 2],
44
45 #[asn1(
46 type = "BIT STRING",
47 context_specific = "3",
48 tag_mode = "EXPLICIT",
49 deref = "true"
50 )]
51 pub array_explicit_bits: [u8; 2],
52
53 #[asn1(type = "BIT STRING", context_specific = "4", optional = "true")]
54 pub array_optional_implicit_bits: Option<[u8; 2]>,
55
56 #[asn1(type = "OCTET STRING", context_specific = "5", optional = "true")]
57 pub array_optional_implicit_bytes: Option<[u8; 2]>,
58
59 #[asn1(
60 type = "BIT STRING",
61 context_specific = "6",
62 optional = "true",
63 tag_mode = "EXPLICIT"
64 )]
65 pub array_optional_explicit_bits: Option<[u8; 2]>,
66
67 #[asn1(
68 type = "OCTET STRING",
69 context_specific = "7",
70 optional = "true",
71 tag_mode = "EXPLICIT"
72 )]
73 pub array_optional_explicit_bytes: Option<[u8; 2]>,
74 }
75
76 #[test]
77 fn type_combinations_arrays_instance() {
78 let mut buf = [0u8; 100];
79 let obj = TypeCheckArraysSequenceFieldAttributeCombinations {
80 array_bytes: [0xAA, 0xBB],
81 array_bits: [0xCC, 0xDD],
82
83 array_implicit_bytes: [0, 1],
84 array_implicit_bits: [2, 3],
85
86 array_explicit_bytes: [4, 5],
87 array_explicit_bits: [6, 7],
88
89 array_optional_implicit_bits: Some([8, 9]),
90 array_optional_implicit_bytes: Some([10, 11]),
91
92 array_optional_explicit_bits: Some([12, 13]),
93 array_optional_explicit_bytes: Some([14, 15]),
94 };
95
96 let der_encoded = obj.encode_to_slice(&mut buf).unwrap();
97 let obj_decoded =
98 TypeCheckArraysSequenceFieldAttributeCombinations::from_der(der_encoded).unwrap();
99 assert_eq!(obj, obj_decoded);
100 }
101 }
102 /// Custom derive test cases for the `Sequence` macro with heapless crate.
103 #[cfg(all(feature = "oid", feature = "heapless"))]
104 mod sequence_heapless_vec {
105 use der::Decode;
106 use der::Encode;
107 use der::Sequence;
108 use der::asn1::ObjectIdentifier;
109 use hex_literal::hex;
110
111 /// EU Tachograph Gen 2 certificate public key
112 #[derive(Sequence, Debug, Clone)]
113 #[asn1(tag_mode = "IMPLICIT")]
114 pub struct G2CertificatePublicKey {
115 pub domain_parameters: ObjectIdentifier,
116
117 /// Public point is '04 xx .. xx yy .. yy'
118 ///
119 /// Maximum: 1 + 2 * 66 bytes for Nist521
120 #[asn1(context_specific = "6", type = "OCTET STRING", deref = "true")]
121 pub public_point: heapless::Vec<u8, 133>,
122 }
123
124 static ROMANIA_PUBLIC_KEY: [u8; 80] = hex!(
125 "
126 30 4E 06 09 2B 24 03 03 02 08 01 01 07 86 41
127 04 86 23 90 68 B8 08 1F 5E 6B 49 EB 54 F7 8E 0D
128 FB E7 1F 85 26 15 60 73 7C 24 B0 10 24 F9 2A 02
129 03 67 80 3E 17 E9 F2 6E D5 A5 4E 25 F9 B5 46 B1
130 90 3C DD 76 47 DB 1D 8E E0 D9 86 D0 64 D8 3C DD
131 7F"
132 );
133
134 pub const BRAINPOOL_P_256_R_1: ObjectIdentifier =
135 ObjectIdentifier::new_unwrap("1.3.36.3.3.2.8.1.1.7");
136
137 #[test]
138 fn heapless_vec_decode_encode() {
139 let public_key = G2CertificatePublicKey::from_der(&ROMANIA_PUBLIC_KEY).unwrap();
140
141 assert_eq!(public_key.domain_parameters, BRAINPOOL_P_256_R_1);
142 assert_eq!(public_key.public_point.len(), 1 + 2 * (256 / 8));
143
144 let mut buf = [0u8; 80];
145 let pk_encoded = public_key.encode_to_slice(&mut buf).unwrap();
146
147 assert_eq!(pk_encoded, ROMANIA_PUBLIC_KEY);
148 }
149
150 #[derive(Sequence, Debug, Clone)]
151 #[allow(dead_code)]
152 pub struct HeaplessTypeCheck {
153 #[asn1(type = "OCTET STRING", deref = "true")]
154 pub octet_string_heapless: heapless::Vec<u8, 16>,
155
156 #[asn1(type = "OCTET STRING", deref = "true", tag_mode = "IMPLICIT")]
157 pub octet_string_heapless_implicit: heapless::Vec<u8, 16>,
158
159 #[asn1(context_specific = "0", type = "OCTET STRING", optional = "true")]
160 pub opt_octet_string_heapless: Option<heapless::Vec<u8, 16>>,
161
162 #[asn1(
163 context_specific = "1",
164 type = "OCTET STRING",
165 optional = "true",
166 tag_mode = "IMPLICIT"
167 )]
168 pub opt_octet_string_heapless_implicit: Option<heapless::Vec<u8, 16>>,
169 }
170 }