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