1 //! `const-oid` crate tests
2
3 // TODO(tarcieri): test full set of OID encoding constraints specified here:
4 // <https://misc.daniel-marschall.de/asn.1/oid_facts.html>
5
6 use const_oid::{Error, ObjectIdentifier};
7 use hex_literal::hex;
8 use std::string::ToString;
9
10 /// Example OID value with a root arc of `0` (and large arc).
11 const EXAMPLE_OID_0_STR: &str = "0.9.2342.19200300.100.1.1";
12 const EXAMPLE_OID_0_BER: &[u8] = &hex!("0992268993F22C640101");
13 const EXAMPLE_OID_0: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_0_STR);
14
15 /// Example OID value with a root arc of `1`.
16 const EXAMPLE_OID_1_STR: &str = "1.2.840.10045.2.1";
17 const EXAMPLE_OID_1_BER: &[u8] = &hex!("2A8648CE3D0201");
18 const EXAMPLE_OID_1: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_1_STR);
19
20 /// Example OID value with a root arc of `2`.
21 const EXAMPLE_OID_2_STR: &str = "2.16.840.1.101.3.4.1.42";
22 const EXAMPLE_OID_2_BER: &[u8] = &hex!("60864801650304012A");
23 const EXAMPLE_OID_2: ObjectIdentifier = ObjectIdentifier::new_unwrap(EXAMPLE_OID_2_STR);
24
25 /// Example OID value with a large arc
26 const EXAMPLE_OID_LARGE_ARC_STR: &str = "0.9.2342.19200300.100.1.1";
27 const EXAMPLE_OID_LARGE_ARC_BER: &[u8] = &hex!("0992268993F22C640101");
28 const EXAMPLE_OID_LARGE_ARC: ObjectIdentifier =
29 ObjectIdentifier::new_unwrap("0.9.2342.19200300.100.1.1");
30
31 #[test]
from_bytes()32 fn from_bytes() {
33 let oid0 = ObjectIdentifier::from_bytes(EXAMPLE_OID_0_BER).unwrap();
34 assert_eq!(oid0.arc(0).unwrap(), 0);
35 assert_eq!(oid0.arc(1).unwrap(), 9);
36 assert_eq!(oid0, EXAMPLE_OID_0);
37
38 let oid1 = ObjectIdentifier::from_bytes(EXAMPLE_OID_1_BER).unwrap();
39 assert_eq!(oid1.arc(0).unwrap(), 1);
40 assert_eq!(oid1.arc(1).unwrap(), 2);
41 assert_eq!(oid1, EXAMPLE_OID_1);
42
43 let oid2 = ObjectIdentifier::from_bytes(EXAMPLE_OID_2_BER).unwrap();
44 assert_eq!(oid2.arc(0).unwrap(), 2);
45 assert_eq!(oid2.arc(1).unwrap(), 16);
46 assert_eq!(oid2, EXAMPLE_OID_2);
47
48 let oid3 = ObjectIdentifier::from_bytes(EXAMPLE_OID_LARGE_ARC_BER).unwrap();
49 assert_eq!(oid3.arc(0).unwrap(), 0);
50 assert_eq!(oid3.arc(1).unwrap(), 9);
51 assert_eq!(oid3.arc(2).unwrap(), 2342);
52 assert_eq!(oid3.arc(3).unwrap(), 19200300);
53 assert_eq!(oid3.arc(4).unwrap(), 100);
54 assert_eq!(oid3.arc(5).unwrap(), 1);
55 assert_eq!(oid3.arc(6).unwrap(), 1);
56 assert_eq!(oid3, EXAMPLE_OID_LARGE_ARC);
57
58 // Empty
59 assert_eq!(ObjectIdentifier::from_bytes(&[]), Err(Error::Empty));
60
61 // Truncated
62 assert_eq!(
63 ObjectIdentifier::from_bytes(&[42]),
64 Err(Error::NotEnoughArcs)
65 );
66 assert_eq!(
67 ObjectIdentifier::from_bytes(&[42, 134]),
68 Err(Error::NotEnoughArcs)
69 );
70 }
71
72 #[test]
from_str()73 fn from_str() {
74 let oid0 = EXAMPLE_OID_0_STR.parse::<ObjectIdentifier>().unwrap();
75 assert_eq!(oid0.arc(0).unwrap(), 0);
76 assert_eq!(oid0.arc(1).unwrap(), 9);
77 assert_eq!(oid0, EXAMPLE_OID_0);
78
79 let oid1 = EXAMPLE_OID_1_STR.parse::<ObjectIdentifier>().unwrap();
80 assert_eq!(oid1.arc(0).unwrap(), 1);
81 assert_eq!(oid1.arc(1).unwrap(), 2);
82 assert_eq!(oid1, EXAMPLE_OID_1);
83
84 let oid2 = EXAMPLE_OID_2_STR.parse::<ObjectIdentifier>().unwrap();
85 assert_eq!(oid2.arc(0).unwrap(), 2);
86 assert_eq!(oid2.arc(1).unwrap(), 16);
87 assert_eq!(oid2, EXAMPLE_OID_2);
88
89 let oid3 = EXAMPLE_OID_LARGE_ARC_STR
90 .parse::<ObjectIdentifier>()
91 .unwrap();
92 assert_eq!(oid3.arc(0).unwrap(), 0);
93 assert_eq!(oid3.arc(1).unwrap(), 9);
94 assert_eq!(oid3.arc(2).unwrap(), 2342);
95 assert_eq!(oid3.arc(3).unwrap(), 19200300);
96 assert_eq!(oid3.arc(4).unwrap(), 100);
97 assert_eq!(oid3.arc(5).unwrap(), 1);
98 assert_eq!(oid3.arc(6).unwrap(), 1);
99 assert_eq!(oid3, EXAMPLE_OID_LARGE_ARC);
100
101 // Too short
102 assert_eq!("1.2".parse::<ObjectIdentifier>(), Err(Error::NotEnoughArcs));
103
104 // Truncated
105 assert_eq!(
106 "1.2.840.10045.2.".parse::<ObjectIdentifier>(),
107 Err(Error::TrailingDot)
108 );
109
110 // Invalid first arc
111 assert_eq!(
112 "3.2.840.10045.2.1".parse::<ObjectIdentifier>(),
113 Err(Error::ArcInvalid { arc: 3 })
114 );
115
116 // Invalid second arc
117 assert_eq!(
118 "1.40.840.10045.2.1".parse::<ObjectIdentifier>(),
119 Err(Error::ArcInvalid { arc: 40 })
120 );
121 }
122
123 #[test]
display()124 fn display() {
125 assert_eq!(EXAMPLE_OID_0.to_string(), EXAMPLE_OID_0_STR);
126 assert_eq!(EXAMPLE_OID_1.to_string(), EXAMPLE_OID_1_STR);
127 assert_eq!(EXAMPLE_OID_2.to_string(), EXAMPLE_OID_2_STR);
128 assert_eq!(EXAMPLE_OID_LARGE_ARC.to_string(), EXAMPLE_OID_LARGE_ARC_STR);
129 }
130
131 #[test]
try_from_u32_slice()132 fn try_from_u32_slice() {
133 let oid1 = ObjectIdentifier::from_arcs([1, 2, 840, 10045, 2, 1]).unwrap();
134 assert_eq!(oid1.arc(0).unwrap(), 1);
135 assert_eq!(oid1.arc(1).unwrap(), 2);
136 assert_eq!(EXAMPLE_OID_1, oid1);
137
138 let oid2 = ObjectIdentifier::from_arcs([2, 16, 840, 1, 101, 3, 4, 1, 42]).unwrap();
139 assert_eq!(oid2.arc(0).unwrap(), 2);
140 assert_eq!(oid2.arc(1).unwrap(), 16);
141 assert_eq!(EXAMPLE_OID_2, oid2);
142
143 // Too short
144 assert_eq!(
145 ObjectIdentifier::from_arcs([1, 2]),
146 Err(Error::NotEnoughArcs)
147 );
148
149 // Invalid first arc
150 assert_eq!(
151 ObjectIdentifier::from_arcs([3, 2, 840, 10045, 3, 1, 7]),
152 Err(Error::ArcInvalid { arc: 3 })
153 );
154
155 // Invalid second arc
156 assert_eq!(
157 ObjectIdentifier::from_arcs([1, 40, 840, 10045, 3, 1, 7]),
158 Err(Error::ArcInvalid { arc: 40 })
159 );
160 }
161
162 #[test]
as_bytes()163 fn as_bytes() {
164 assert_eq!(EXAMPLE_OID_1.as_bytes(), EXAMPLE_OID_1_BER);
165 assert_eq!(EXAMPLE_OID_2.as_bytes(), EXAMPLE_OID_2_BER);
166 }
167
168 #[test]
parse_empty()169 fn parse_empty() {
170 assert_eq!(ObjectIdentifier::new(""), Err(Error::Empty));
171 }
172
173 #[test]
parse_not_enough_arcs()174 fn parse_not_enough_arcs() {
175 assert_eq!(ObjectIdentifier::new("1.2"), Err(Error::NotEnoughArcs));
176 }
177
178 #[test]
parse_invalid_first_arc()179 fn parse_invalid_first_arc() {
180 assert_eq!(
181 ObjectIdentifier::new("3.2.840.10045.3.1.7"),
182 Err(Error::ArcInvalid { arc: 3 })
183 );
184 }
185
186 #[test]
parse_invalid_second_arc()187 fn parse_invalid_second_arc() {
188 assert_eq!(
189 ObjectIdentifier::new("1.40.840.10045.3.1.7"),
190 Err(Error::ArcInvalid { arc: 40 })
191 );
192 }
193
194 #[test]
parent()195 fn parent() {
196 let oid = ObjectIdentifier::new("1.2.3.4").unwrap();
197 let parent = oid.parent().unwrap();
198 assert_eq!(parent, ObjectIdentifier::new("1.2.3").unwrap());
199 assert_eq!(parent.parent(), None);
200 }
201
202 #[test]
push_arc()203 fn push_arc() {
204 let oid = ObjectIdentifier::new("1.2.3").unwrap();
205 assert_eq!(
206 oid.push_arc(4).unwrap(),
207 ObjectIdentifier::new("1.2.3.4").unwrap()
208 );
209 }
210