1 use der_parser::ber::*;
2 use der_parser::error::*;
3 use der_parser::oid::*;
4 use hex_literal::hex;
5 use nom::Err;
6 // use pretty_assertions::assert_eq;
7 use test_case::test_case;
8
9 #[cfg(feature = "bigint")]
10 use num_bigint::{BigInt, BigUint, Sign};
11
12 #[test_case(&hex!("01 01 00"), Some(false) ; "val true")]
13 #[test_case(&hex!("01 01 ff"), Some(true) ; "val false")]
14 #[test_case(&hex!("01 01 7f"), Some(true) ; "true not ff")]
15 #[test_case(&hex!("01 02 00 00"), None ; "invalid length")]
16 #[test_case(&hex!("01 01"), None ; "incomplete")]
tc_ber_bool(i: &[u8], out: Option<bool>)17 fn tc_ber_bool(i: &[u8], out: Option<bool>) {
18 let res = parse_ber_bool(i);
19 if let Some(b) = out {
20 let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
21 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
22 } else {
23 assert!(res.is_err());
24 }
25 }
26
27 #[test]
test_ber_bool()28 fn test_ber_bool() {
29 let empty = &b""[..];
30 let b_true = BerObject::from_obj(BerObjectContent::Boolean(true));
31 let b_false = BerObject::from_obj(BerObjectContent::Boolean(false));
32 assert_eq!(parse_ber_bool(&[0x01, 0x01, 0x00]), Ok((empty, b_false)));
33 assert_eq!(
34 parse_ber_bool(&[0x01, 0x01, 0xff]),
35 Ok((empty, b_true.clone()))
36 );
37 assert_eq!(parse_ber_bool(&[0x01, 0x01, 0x7f]), Ok((empty, b_true)));
38 assert_eq!(
39 parse_ber_bool(&[0x01, 0x02, 0x12, 0x34]),
40 Err(Err::Error(BerError::InvalidLength))
41 );
42 }
43
44 #[test]
test_seq_indefinite_length()45 fn test_seq_indefinite_length() {
46 let data = hex!("30 80 04 03 56 78 90 00 00 02 01 01");
47 let res = parse_ber(&data);
48 assert_eq!(
49 res,
50 Ok((
51 &data[9..],
52 BerObject::from_seq(vec![BerObject::from_obj(BerObjectContent::OctetString(
53 &data[4..=6]
54 )),])
55 ))
56 );
57 let res = parse_ber_sequence(&data);
58 assert_eq!(
59 res,
60 Ok((
61 &data[9..],
62 BerObject::from_seq(vec![BerObject::from_obj(BerObjectContent::OctetString(
63 &data[4..=6]
64 )),])
65 ))
66 );
67 }
68
69 #[test]
test_ber_set_of()70 fn test_ber_set_of() {
71 let empty = &b""[..];
72 let bytes = [
73 0x31, 0x0a, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x03, 0x01, 0x00, 0x00,
74 ];
75 let expected = BerObject::from_set(vec![
76 BerObject::from_int_slice(b"\x01\x00\x01"),
77 BerObject::from_int_slice(b"\x01\x00\x00"),
78 ]);
79 fn parser(i: &[u8]) -> BerResult {
80 parse_ber_set_of(parse_ber_integer)(i)
81 }
82 assert_eq!(parser(&bytes), Ok((empty, expected)));
83 // empty input should raise error (could not read set header)
84 assert!(parser(&[]).is_err());
85 // empty set is ok (returns empty vec)
86 assert!(parser(&[0x31, 0x00]).is_ok());
87 }
88
89 #[test]
test_ber_set_of_v()90 fn test_ber_set_of_v() {
91 let empty = &b""[..];
92 let bytes = [
93 0x31, 0x0a, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x03, 0x01, 0x00, 0x00,
94 ];
95 let expected = vec![
96 BerObject::from_int_slice(b"\x01\x00\x01"),
97 BerObject::from_int_slice(b"\x01\x00\x00"),
98 ];
99 fn parser(i: &[u8]) -> BerResult<Vec<BerObject>> {
100 parse_ber_set_of_v(parse_ber_integer)(i)
101 }
102 assert_eq!(parser(&bytes), Ok((empty, expected)));
103 // empty input should raise error (could not read set header)
104 assert!(parser(&[]).is_err());
105 // empty set is ok (returns empty vec)
106 assert_eq!(parser(&[0x31, 0x00]), Ok((empty, vec![])));
107 }
108
109 #[test]
test_set_indefinite_length()110 fn test_set_indefinite_length() {
111 let data = hex!("31 80 04 03 56 78 90 00 00");
112 let res = parse_ber(&data);
113 assert_eq!(
114 res,
115 Ok((
116 &data[9..],
117 BerObject::from_set(vec![BerObject::from_obj(BerObjectContent::OctetString(
118 &data[4..=6]
119 )),])
120 ))
121 );
122 let res = parse_ber_set(&data);
123 assert_eq!(
124 res,
125 Ok((
126 &data[9..],
127 BerObject::from_set(vec![BerObject::from_obj(BerObjectContent::OctetString(
128 &data[4..=6]
129 )),])
130 ))
131 );
132 }
133
134 #[test]
test_ber_int()135 fn test_ber_int() {
136 let empty = &b""[..];
137 let bytes = [0x02, 0x03, 0x01, 0x00, 0x01];
138 let expected = BerObject::from_obj(BerObjectContent::Integer(b"\x01\x00\x01"));
139 assert_eq!(parse_ber_integer(&bytes), Ok((empty, expected)));
140 }
141
142 #[test_case(&hex!("02 01 01"), Ok(1) ; "u32-1")]
143 #[test_case(&hex!("02 02 00 ff"), Ok(255) ; "u32-255")]
144 #[test_case(&hex!("02 02 01 23"), Ok(0x123) ; "u32-0x123")]
145 #[test_case(&hex!("02 04 01 23 45 67"), Ok(0x0123_4567) ; "u32-long-ok")]
146 #[test_case(&hex!("02 05 00 ff ff ff ff"), Ok(0xffff_ffff) ; "u32-long2-ok")]
147 #[test_case(&hex!("02 06 00 00 01 23 45 67"), Ok(0x0123_4567) ; "u32-long-leading-zeros-ok")]
148 #[test_case(&hex!("02 05 01 23 45 67 01"), Err(BerError::IntegerTooLarge) ; "u32 too large")]
149 #[test_case(&hex!("02 09 01 23 45 67 01 23 45 67 ab"), Err(BerError::IntegerTooLarge) ; "u32 too large 2")]
150 #[test_case(&hex!("03 03 01 00 01"), Err(BerError::InvalidTag) ; "invalid tag")]
tc_ber_u32(i: &[u8], out: Result<u32, BerError>)151 fn tc_ber_u32(i: &[u8], out: Result<u32, BerError>) {
152 let res = parse_ber_u32(i);
153 match out {
154 Ok(expected) => {
155 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
156 }
157 Err(e) => {
158 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
159 }
160 }
161 }
162
163 #[test_case(&hex!("02 01 01"), Ok(1) ; "u64-1")]
164 #[test_case(&hex!("02 02 00 ff"), Ok(255) ; "u64-255")]
165 #[test_case(&hex!("02 02 01 23"), Ok(0x123) ; "u64-0x123")]
166 #[test_case(&hex!("02 08 01 23 45 67 01 23 45 67"), Ok(0x0123_4567_0123_4567) ; "u64-long-ok")]
167 #[test_case(&hex!("02 09 00 ff ff ff ff ff ff ff ff"), Ok(0xffff_ffff_ffff_ffff) ; "u64-long2-ok")]
168 #[test_case(&hex!("02 09 01 23 45 67 01 23 45 67 ab"), Err(BerError::IntegerTooLarge) ; "u64 too large")]
169 #[test_case(&hex!("03 03 01 00 01"), Err(BerError::InvalidTag) ; "invalid tag")]
tc_ber_u64(i: &[u8], out: Result<u64, BerError>)170 fn tc_ber_u64(i: &[u8], out: Result<u64, BerError>) {
171 let res = parse_ber_u64(i);
172 match out {
173 Ok(expected) => {
174 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
175 }
176 Err(e) => {
177 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
178 }
179 }
180 }
181
182 #[test_case(&hex!("02 01 01"), Ok(1) ; "i64-1")]
183 #[test_case(&hex!("02 01 ff"), Ok(-1) ; "i64-neg1")]
184 #[test_case(&hex!("02 01 80"), Ok(-128) ; "i64-neg128")]
185 #[test_case(&hex!("02 02 ff 7f"), Ok(-129) ; "i64-neg129")]
186 #[test_case(&hex!("03 03 01 00 01"), Err(BerError::InvalidTag) ; "invalid tag")]
tc_ber_i64(i: &[u8], out: Result<i64, BerError>)187 fn tc_ber_i64(i: &[u8], out: Result<i64, BerError>) {
188 let res = parse_ber_i64(i);
189 match out {
190 Ok(expected) => {
191 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
192 }
193 Err(e) => {
194 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
195 }
196 }
197 }
198
199 #[cfg(feature = "bigint")]
200 #[test_case(&hex!("02 01 01"), Ok(BigInt::from(1)) ; "bigint-1")]
201 #[test_case(&hex!("02 02 00 ff"), Ok(BigInt::from(255)) ; "bigint-255")]
202 #[test_case(&hex!("02 01 ff"), Ok(BigInt::from(-1)) ; "bigint-neg1")]
203 #[test_case(&hex!("02 01 80"), Ok(BigInt::from(-128)) ; "bigint-neg128")]
204 #[test_case(&hex!("02 02 ff 7f"), Ok(BigInt::from(-129)) ; "bigint-neg129")]
205 #[test_case(&hex!("02 09 00 ff ff ff ff ff ff ff ff"), Ok(BigInt::from(0xffff_ffff_ffff_ffff_u64)) ; "bigint-long2-ok")]
206 #[test_case(&hex!("02 09 01 23 45 67 01 23 45 67 ab"), Ok(BigInt::from_bytes_be(Sign::Plus, &hex!("01 23 45 67 01 23 45 67 ab"))) ; "bigint-longer1")]
tc_ber_bigint(i: &[u8], out: Result<BigInt, BerError>)207 fn tc_ber_bigint(i: &[u8], out: Result<BigInt, BerError>) {
208 let res = parse_ber_integer(i);
209 match out {
210 Ok(expected) => {
211 let (rem, ber) = res.expect("parsing failed");
212 assert!(rem.is_empty());
213 let int = ber.as_bigint().expect("failed to convert to bigint");
214 pretty_assertions::assert_eq!(int, expected);
215 }
216 Err(e) => {
217 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
218 }
219 }
220 }
221
222 #[cfg(feature = "bigint")]
223 #[test_case(&hex!("02 01 01"), Ok(BigUint::from(1_u8)) ; "biguint-1")]
224 #[test_case(&hex!("02 02 00 ff"), Ok(BigUint::from(255_u8)) ; "biguint-255")]
225 #[test_case(&hex!("02 01 ff"), Err(BerError::IntegerNegative) ; "biguint-neg1")]
226 #[test_case(&hex!("02 01 80"), Err(BerError::IntegerNegative) ; "biguint-neg128")]
227 #[test_case(&hex!("02 02 ff 7f"), Err(BerError::IntegerNegative) ; "biguint-neg129")]
228 #[test_case(&hex!("02 09 00 ff ff ff ff ff ff ff ff"), Ok(BigUint::from(0xffff_ffff_ffff_ffff_u64)) ; "biguint-long2-ok")]
229 #[test_case(&hex!("02 09 01 23 45 67 01 23 45 67 ab"), Ok(BigUint::from_bytes_be(&hex!("01 23 45 67 01 23 45 67 ab"))) ; "biguint-longer1")]
230 #[test_case(&hex!("03 03 01 00 01"), Err(BerError::InvalidTag) ; "invalid tag")]
tc_ber_biguint(i: &[u8], out: Result<BigUint, BerError>)231 fn tc_ber_biguint(i: &[u8], out: Result<BigUint, BerError>) {
232 let res = parse_ber_integer(i).and_then(|(rem, ber)| Ok((rem, ber.as_biguint()?)));
233 match out {
234 Ok(expected) => {
235 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
236 }
237 Err(e) => {
238 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
239 }
240 }
241 }
242
243 #[test_case(&hex!("02 01 01"), Ok(&[1]) ; "slice 1")]
244 #[test_case(&hex!("02 01 ff"), Ok(&[255]) ; "slice 2")]
245 #[test_case(&hex!("02 09 01 23 45 67 01 23 45 67 ab"), Ok(&hex!("01 23 45 67 01 23 45 67 ab")) ; "slice 3")]
246 #[test_case(&hex!("22 80 02 01 01 00 00"), Ok(&[2, 1, 1]) ; "constructed slice")]
247 #[test_case(&hex!("03 03 01 00 01"), Err(BerError::InvalidTag) ; "invalid tag")]
tc_ber_slice(i: &[u8], out: Result<&[u8], BerError>)248 fn tc_ber_slice(i: &[u8], out: Result<&[u8], BerError>) {
249 let res = parse_ber_slice(i, 2);
250 match out {
251 Ok(expected) => {
252 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
253 }
254 Err(e) => {
255 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
256 }
257 }
258 }
259
260 #[test]
test_ber_bitstring_primitive()261 fn test_ber_bitstring_primitive() {
262 let empty = &b""[..];
263 let bytes = &[0x03, 0x07, 0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0];
264 let expected = BerObject::from_obj(BerObjectContent::BitString(
265 4,
266 BitStringObject { data: &bytes[3..] },
267 ));
268 assert_eq!(parse_ber_bitstring(bytes), Ok((empty, expected)));
269 //
270 // correct encoding, padding bits not all set to 0
271 //
272 let bytes = &[0x03, 0x04, 0x06, 0x6e, 0x5d, 0xe0];
273 let expected = BerObject::from_obj(BerObjectContent::BitString(
274 6,
275 BitStringObject { data: &bytes[3..] },
276 ));
277 assert_eq!(parse_ber_bitstring(bytes), Ok((empty, expected)));
278 //
279 // long form of length
280 //
281 let bytes = &[0x03, 0x81, 0x04, 0x06, 0x6e, 0x5d, 0xc0];
282 let expected = BerObject::from_obj(BerObjectContent::BitString(
283 6,
284 BitStringObject { data: &bytes[4..] },
285 ));
286 assert_eq!(parse_ber_bitstring(bytes), Ok((empty, expected)));
287 }
288
289 #[test]
test_ber_bitstring_constructed()290 fn test_ber_bitstring_constructed() {
291 let bytes = &[
292 0x23, 0x80, 0x03, 0x03, 0x00, 0x0a, 0x3b, 0x03, 0x05, 0x04, 0x5f, 0x29, 0x1c, 0xd0, 0x00,
293 0x00,
294 ];
295 assert_eq!(
296 parse_ber_bitstring(bytes),
297 Err(Err::Error(BerError::Unsupported))
298 ); // XXX valid encoding
299 }
300
301 #[test]
test_ber_octetstring_primitive()302 fn test_ber_octetstring_primitive() {
303 let empty = &b""[..];
304 let bytes = [0x04, 0x05, 0x41, 0x41, 0x41, 0x41, 0x41];
305 let expected = BerObject::from_obj(BerObjectContent::OctetString(b"AAAAA"));
306 assert_eq!(parse_ber_octetstring(&bytes), Ok((empty, expected)));
307 }
308
309 #[test]
test_ber_null()310 fn test_ber_null() {
311 let empty = &b""[..];
312 let expected = BerObject::from_obj(BerObjectContent::Null);
313 assert_eq!(parse_ber_null(&[0x05, 0x00]), Ok((empty, expected)));
314 }
315
316 #[test]
test_ber_oid()317 fn test_ber_oid() {
318 let empty = &b""[..];
319 let bytes = [
320 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
321 ];
322 let expected = BerObject::from_obj(BerObjectContent::OID(
323 Oid::from(&[1, 2, 840, 113_549, 1, 1, 5]).unwrap(),
324 ));
325 assert_eq!(parse_ber_oid(&bytes), Ok((empty, expected)));
326 }
327
328 #[test]
test_ber_enum()329 fn test_ber_enum() {
330 let empty = &b""[..];
331 let expected = BerObject::from_obj(BerObjectContent::Enum(2));
332 assert_eq!(parse_ber_enum(&[0x0a, 0x01, 0x02]), Ok((empty, expected)));
333 }
334
335 #[test_case(&hex!("0c 04 31 32 33 34"), Ok("1234") ; "utf8: numeric")]
336 #[test_case(&hex!("0c 05 68 65 6c 6c 6f"), Ok("hello") ; "utf8: string")]
337 #[test_case(&hex!("0c 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "utf8: string with spaces")]
338 #[test_case(&hex!("0c 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Ok("hello\\world") ; "utf8: string with backspace")]
339 #[test_case(&hex!("0c 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "utf8: string with plus")]
340 #[test_case(&hex!("0c 05 01 02 03 04 05"), Ok("\x01\x02\x03\x04\x05") ; "invalid chars")]
341 #[test_case(&hex!("0c 0e d0 bf d1 80 d0 b8 d0 b2 d0 b5 cc 81 d1 82"), Ok("приве́т") ; "utf8")]
342 #[test_case(&hex!("0c 04 00 9f 92 96"), Err(Err::Error(BerError::StringInvalidCharset)) ; "invalid utf8")]
tc_ber_utf8_string(i: &[u8], out: Result<&str, Err<BerError>>)343 fn tc_ber_utf8_string(i: &[u8], out: Result<&str, Err<BerError>>) {
344 let res = parse_ber_utf8string(i);
345 match out {
346 Ok(b) => {
347 let (rem, res) = res.expect("could not parse utf8string");
348 assert!(rem.is_empty());
349 let r = res.as_str().expect("could not convert to string");
350 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
351 pretty_assertions::assert_eq!(r, b);
352 }
353 Err(e) => {
354 pretty_assertions::assert_eq!(res, Err(e));
355 }
356 }
357 }
358
359 #[test_case(&hex!("12 04 31 32 33 34"), Ok("1234") ; "numeric string")]
360 #[test_case(&hex!("12 05 68 65 6c 6c 6f"), Err(Err::Error(BerError::StringInvalidCharset)) ; "invalid chars")]
361 #[test_case(&hex!("12 05 01 02 03 04 05"), Err(Err::Error(BerError::StringInvalidCharset)) ; "invalid chars2")]
tc_ber_numeric_string(i: &[u8], out: Result<&str, Err<BerError>>)362 fn tc_ber_numeric_string(i: &[u8], out: Result<&str, Err<BerError>>) {
363 let res = parse_ber_numericstring(i);
364 match out {
365 Ok(b) => {
366 let (rem, res) = res.expect("could not parse numericstring");
367 assert!(rem.is_empty());
368 let r = res.as_str().expect("could not convert to string");
369 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
370 pretty_assertions::assert_eq!(r, b);
371 }
372 Err(e) => {
373 pretty_assertions::assert_eq!(res, Err(e));
374 }
375 }
376 }
377
378 #[test_case(&hex!("13 04 31 32 33 34"), Ok("1234") ; "printable: numeric")]
379 #[test_case(&hex!("13 05 68 65 6c 6c 6f"), Ok("hello") ; "printable: string")]
380 #[test_case(&hex!("13 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "printable: string with spaces")]
381 #[test_case(&hex!("13 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Err(Err::Error(BerError::StringInvalidCharset)) ; "printable: string with backspace")]
382 #[test_case(&hex!("13 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "printable: string with plus")]
383 #[test_case(&hex!("13 05 01 02 03 04 05"), Err(Err::Error(BerError::StringInvalidCharset)) ; "invalid chars")]
tc_ber_printable_string(i: &[u8], out: Result<&str, Err<BerError>>)384 fn tc_ber_printable_string(i: &[u8], out: Result<&str, Err<BerError>>) {
385 let res = parse_ber_printablestring(i);
386 match out {
387 Ok(b) => {
388 let (rem, res) = res.expect("could not parse printablestring");
389 assert!(rem.is_empty());
390 let r = res.as_str().expect("could not convert to string");
391 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
392 pretty_assertions::assert_eq!(r, b);
393 }
394 Err(e) => {
395 pretty_assertions::assert_eq!(res, Err(e));
396 }
397 }
398 }
399
400 #[test_case(&hex!("16 04 31 32 33 34"), Ok("1234") ; "ia5: numeric")]
401 #[test_case(&hex!("16 05 68 65 6c 6c 6f"), Ok("hello") ; "ia5: string")]
402 #[test_case(&hex!("16 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "ia5: string with spaces")]
403 #[test_case(&hex!("16 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Ok("hello\\world") ; "ia5: string with backspace")]
404 #[test_case(&hex!("16 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "ia5: string with plus")]
405 #[test_case(&hex!("16 05 01 02 03 04 05"), Ok("\x01\x02\x03\x04\x05") ; "invalid chars")]
406 #[test_case(&hex!("16 0d d0 bf d1 80 d0 b8 d0 b2 d0 b5 cc 81 d1 82"), Err(Err::Error(BerError::StringInvalidCharset)) ; "utf8")]
tc_ber_ia5_string(i: &[u8], out: Result<&str, Err<BerError>>)407 fn tc_ber_ia5_string(i: &[u8], out: Result<&str, Err<BerError>>) {
408 let res = parse_ber_ia5string(i);
409 match out {
410 Ok(b) => {
411 let (rem, res) = res.expect("could not parse ia5string");
412 assert!(rem.is_empty());
413 let r = res.as_str().expect("could not convert to string");
414 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
415 pretty_assertions::assert_eq!(r, b);
416 }
417 Err(e) => {
418 pretty_assertions::assert_eq!(res, Err(e));
419 }
420 }
421 }
422
423 #[test_case(&hex!("1a 04 31 32 33 34"), Ok("1234") ; "visible: numeric")]
424 #[test_case(&hex!("1a 05 68 65 6c 6c 6f"), Ok("hello") ; "visible: string")]
425 #[test_case(&hex!("1a 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "visible: string with spaces")]
426 #[test_case(&hex!("1a 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Ok("hello\\world") ; "printable: string with backspace")]
427 #[test_case(&hex!("1a 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "printable: string with plus")]
428 #[test_case(&hex!("1a 05 01 02 03 04 05"), Err(Err::Error(BerError::StringInvalidCharset)) ; "invalid chars")]
tc_ber_visible_string(i: &[u8], out: Result<&str, Err<BerError>>)429 fn tc_ber_visible_string(i: &[u8], out: Result<&str, Err<BerError>>) {
430 let res = parse_ber_visiblestring(i);
431 match out {
432 Ok(b) => {
433 let (rem, res) = res.expect("could not parse visiblestring");
434 assert!(rem.is_empty());
435 let r = res.as_str().expect("could not convert to string");
436 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
437 pretty_assertions::assert_eq!(r, b);
438 }
439 Err(e) => {
440 pretty_assertions::assert_eq!(res, Err(e));
441 }
442 }
443 }
444
445 #[test]
test_ber_utf8string()446 fn test_ber_utf8string() {
447 let empty = &b""[..];
448 let bytes = [
449 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
450 ];
451 let expected = BerObject::from_obj(BerObjectContent::UTF8String("Some-State"));
452 assert_eq!(parse_ber_utf8string(&bytes), Ok((empty, expected)));
453 }
454
455 #[test]
test_ber_relativeoid()456 fn test_ber_relativeoid() {
457 let empty = &b""[..];
458 let bytes = hex!("0d 04 c2 7b 03 02");
459 let expected = BerObject::from_obj(BerObjectContent::RelativeOID(
460 Oid::from_relative(&[8571, 3, 2]).unwrap(),
461 ));
462 assert_eq!(parse_ber_relative_oid(&bytes), Ok((empty, expected)));
463 }
464
465 #[test]
test_ber_bmpstring()466 fn test_ber_bmpstring() {
467 let empty = &b""[..];
468 let bytes = hex!("1e 08 00 55 00 73 00 65 00 72");
469 let expected = BerObject::from_obj(BerObjectContent::BmpString(b"\x00U\x00s\x00e\x00r"));
470 assert_eq!(parse_ber_bmpstring(&bytes), Ok((empty, expected)));
471 }
472
473 #[test]
test_ber_customtags()474 fn test_ber_customtags() {
475 let bytes = hex!("8f 02 12 34");
476 let hdr = ber_read_element_header(&bytes)
477 .expect("ber_read_element_header")
478 .1;
479 // println!("{:?}", hdr);
480 let expected: &[u8] = &[0x8f];
481 assert_eq!(hdr.raw_tag, Some(expected));
482 let bytes = hex!("9f 0f 02 12 34");
483 let hdr = ber_read_element_header(&bytes)
484 .expect("ber_read_element_header")
485 .1;
486 // println!("{:?}", hdr);
487 let expected: &[u8] = &[0x9f, 0x0f];
488 assert_eq!(hdr.raw_tag, Some(expected));
489 }
490
491 #[test]
test_ber_indefinite()492 fn test_ber_indefinite() {
493 let bytes = hex!("30 80 02 03 01 00 01 00 00");
494 let (rem, val) = parse_ber_container::<_, _, BerError>(|i, _| {
495 assert!(!i.is_empty());
496 let (_, val) = parse_ber_u32(i)?;
497 Ok((i, val))
498 })(&bytes)
499 .unwrap();
500 assert!(rem.is_empty());
501 assert_eq!(val, 0x10001);
502 }
503
504 #[test]
test_ber_indefinite_recursion()505 fn test_ber_indefinite_recursion() {
506 let data = &hex!(
507 "
508 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
509 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
510 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
511 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
512 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
513 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
514 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
515 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
516 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80 00 00"
517 );
518 let _ = parse_ber_container::<_, _, BerError>(|i, _| Ok((i, ())))(data)
519 .expect_err("max parsing depth overflow");
520 }
521
522 #[test]
test_parse_ber_content()523 fn test_parse_ber_content() {
524 let bytes = &hex!("02 03 01 00 01");
525 let (i, header) = ber_read_element_header(bytes).expect("parsing failed");
526 let (rem, content) =
527 parse_ber_content(header.tag)(i, &header, MAX_RECURSION).expect("parsing failed");
528 assert!(rem.is_empty());
529 assert_eq!(header.tag, BerTag::Integer);
530 assert_eq!(content.as_u32(), Ok(0x10001));
531 }
532
533 #[test]
test_parse_ber_content2()534 fn test_parse_ber_content2() {
535 let bytes = &hex!("02 03 01 00 01");
536 let (i, header) = ber_read_element_header(bytes).expect("parsing failed");
537 let tag = header.tag;
538 let (rem, content) = parse_ber_content2(tag)(i, header, MAX_RECURSION).expect("parsing failed");
539 assert!(rem.is_empty());
540 assert_eq!(tag, BerTag::Integer);
541 assert_eq!(content.as_u32(), Ok(0x10001));
542 }
543
544 #[test]
parse_ber_private()545 fn parse_ber_private() {
546 let bytes = &hex!("c0 03 01 00 01");
547 let (rem, res) = parse_ber(bytes).expect("parsing failed");
548 assert!(rem.is_empty());
549 assert!(matches!(res.content, BerObjectContent::Private(_, _)));
550 }
551