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(Some(BigUint::from(1_u8))) ; "biguint-1")]
224 #[test_case(&hex!("02 02 00 ff"), Ok(Some(BigUint::from(255_u8))) ; "biguint-255")]
225 #[test_case(&hex!("02 01 ff"), Ok(None) ; "biguint-neg1")]
226 #[test_case(&hex!("02 01 80"), Ok(None) ; "biguint-neg128")]
227 #[test_case(&hex!("02 02 ff 7f"), Ok(None) ; "biguint-neg129")]
228 #[test_case(&hex!("02 09 00 ff ff ff ff ff ff ff ff"), Ok(Some(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(Some(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<Option<BigUint>, BerError>)231 fn tc_ber_biguint(i: &[u8], out: Result<Option<BigUint>, BerError>) {
232 let res = parse_ber_integer(i);
233 match out {
234 Ok(expected) => {
235 let (rem, ber) = res.expect("parsing failed");
236 assert!(rem.is_empty());
237 let uint = ber.as_biguint();
238 pretty_assertions::assert_eq!(uint, expected);
239 }
240 Err(e) => {
241 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
242 }
243 }
244 }
245
246 #[test_case(&hex!("02 01 01"), Ok(&[1]) ; "slice 1")]
247 #[test_case(&hex!("02 01 ff"), Ok(&[255]) ; "slice 2")]
248 #[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")]
249 #[test_case(&hex!("22 80 02 01 01 00 00"), Ok(&[2, 1, 1]) ; "constructed slice")]
250 #[test_case(&hex!("03 03 01 00 01"), Err(BerError::InvalidTag) ; "invalid tag")]
tc_ber_slice(i: &[u8], out: Result<&[u8], BerError>)251 fn tc_ber_slice(i: &[u8], out: Result<&[u8], BerError>) {
252 let res = parse_ber_slice(i, 2);
253 match out {
254 Ok(expected) => {
255 pretty_assertions::assert_eq!(res, Ok((&b""[..], expected)));
256 }
257 Err(e) => {
258 pretty_assertions::assert_eq!(res, Err(Err::Error(e)));
259 }
260 }
261 }
262
263 #[test]
test_ber_bitstring_primitive()264 fn test_ber_bitstring_primitive() {
265 let empty = &b""[..];
266 let bytes = &[0x03, 0x07, 0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0];
267 let expected = BerObject::from_obj(BerObjectContent::BitString(
268 4,
269 BitStringObject { data: &bytes[3..] },
270 ));
271 assert_eq!(parse_ber_bitstring(bytes), Ok((empty, expected)));
272 //
273 // correct encoding, padding bits not all set to 0
274 //
275 let bytes = &[0x03, 0x04, 0x06, 0x6e, 0x5d, 0xe0];
276 let expected = BerObject::from_obj(BerObjectContent::BitString(
277 6,
278 BitStringObject { data: &bytes[3..] },
279 ));
280 assert_eq!(parse_ber_bitstring(bytes), Ok((empty, expected)));
281 //
282 // long form of length
283 //
284 let bytes = &[0x03, 0x81, 0x04, 0x06, 0x6e, 0x5d, 0xc0];
285 let expected = BerObject::from_obj(BerObjectContent::BitString(
286 6,
287 BitStringObject { data: &bytes[4..] },
288 ));
289 assert_eq!(parse_ber_bitstring(bytes), Ok((empty, expected)));
290 }
291
292 #[test]
test_ber_bitstring_constructed()293 fn test_ber_bitstring_constructed() {
294 let bytes = &[
295 0x23, 0x80, 0x03, 0x03, 0x00, 0x0a, 0x3b, 0x03, 0x05, 0x04, 0x5f, 0x29, 0x1c, 0xd0, 0x00,
296 0x00,
297 ];
298 assert_eq!(
299 parse_ber_bitstring(bytes),
300 Err(Err::Error(BerError::Unsupported))
301 ); // XXX valid encoding
302 }
303
304 #[test]
test_ber_octetstring_primitive()305 fn test_ber_octetstring_primitive() {
306 let empty = &b""[..];
307 let bytes = [0x04, 0x05, 0x41, 0x41, 0x41, 0x41, 0x41];
308 let expected = BerObject::from_obj(BerObjectContent::OctetString(b"AAAAA"));
309 assert_eq!(parse_ber_octetstring(&bytes), Ok((empty, expected)));
310 }
311
312 #[test]
test_ber_null()313 fn test_ber_null() {
314 let empty = &b""[..];
315 let expected = BerObject::from_obj(BerObjectContent::Null);
316 assert_eq!(parse_ber_null(&[0x05, 0x00]), Ok((empty, expected)));
317 }
318
319 #[test]
test_ber_oid()320 fn test_ber_oid() {
321 let empty = &b""[..];
322 let bytes = [
323 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x05,
324 ];
325 let expected = BerObject::from_obj(BerObjectContent::OID(
326 Oid::from(&[1, 2, 840, 113_549, 1, 1, 5]).unwrap(),
327 ));
328 assert_eq!(parse_ber_oid(&bytes), Ok((empty, expected)));
329 }
330
331 #[test]
test_ber_enum()332 fn test_ber_enum() {
333 let empty = &b""[..];
334 let expected = BerObject::from_obj(BerObjectContent::Enum(2));
335 assert_eq!(parse_ber_enum(&[0x0a, 0x01, 0x02]), Ok((empty, expected)));
336 }
337
338 #[test_case(&hex!("0c 04 31 32 33 34"), Ok("1234") ; "utf8: numeric")]
339 #[test_case(&hex!("0c 05 68 65 6c 6c 6f"), Ok("hello") ; "utf8: string")]
340 #[test_case(&hex!("0c 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "utf8: string with spaces")]
341 #[test_case(&hex!("0c 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Ok("hello\\world") ; "utf8: string with backspace")]
342 #[test_case(&hex!("0c 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "utf8: string with plus")]
343 #[test_case(&hex!("0c 05 01 02 03 04 05"), Ok("\x01\x02\x03\x04\x05") ; "invalid chars")]
344 #[test_case(&hex!("0c 0e d0 bf d1 80 d0 b8 d0 b2 d0 b5 cc 81 d1 82"), Ok("приве́т") ; "utf8")]
345 #[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>>)346 fn tc_ber_utf8_string(i: &[u8], out: Result<&str, Err<BerError>>) {
347 let res = parse_ber_utf8string(i);
348 match out {
349 Ok(b) => {
350 let (rem, res) = res.expect("could not parse utf8string");
351 assert!(rem.is_empty());
352 let r = res.as_str().expect("could not convert to string");
353 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
354 pretty_assertions::assert_eq!(r, b);
355 }
356 Err(e) => {
357 pretty_assertions::assert_eq!(res, Err(e));
358 }
359 }
360 }
361
362 #[test_case(&hex!("12 04 31 32 33 34"), Ok("1234") ; "numeric string")]
363 #[test_case(&hex!("12 05 68 65 6c 6c 6f"), Err(Err::Error(BerError::StringInvalidCharset)) ; "invalid chars")]
364 #[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>>)365 fn tc_ber_numeric_string(i: &[u8], out: Result<&str, Err<BerError>>) {
366 let res = parse_ber_numericstring(i);
367 match out {
368 Ok(b) => {
369 let (rem, res) = res.expect("could not parse numericstring");
370 assert!(rem.is_empty());
371 let r = res.as_str().expect("could not convert to string");
372 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
373 pretty_assertions::assert_eq!(r, b);
374 }
375 Err(e) => {
376 pretty_assertions::assert_eq!(res, Err(e));
377 }
378 }
379 }
380
381 #[test_case(&hex!("13 04 31 32 33 34"), Ok("1234") ; "printable: numeric")]
382 #[test_case(&hex!("13 05 68 65 6c 6c 6f"), Ok("hello") ; "printable: string")]
383 #[test_case(&hex!("13 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "printable: string with spaces")]
384 #[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")]
385 #[test_case(&hex!("13 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "printable: string with plus")]
386 #[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>>)387 fn tc_ber_printable_string(i: &[u8], out: Result<&str, Err<BerError>>) {
388 let res = parse_ber_printablestring(i);
389 match out {
390 Ok(b) => {
391 let (rem, res) = res.expect("could not parse printablestring");
392 assert!(rem.is_empty());
393 let r = res.as_str().expect("could not convert to string");
394 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
395 pretty_assertions::assert_eq!(r, b);
396 }
397 Err(e) => {
398 pretty_assertions::assert_eq!(res, Err(e));
399 }
400 }
401 }
402
403 #[test_case(&hex!("16 04 31 32 33 34"), Ok("1234") ; "ia5: numeric")]
404 #[test_case(&hex!("16 05 68 65 6c 6c 6f"), Ok("hello") ; "ia5: string")]
405 #[test_case(&hex!("16 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "ia5: string with spaces")]
406 #[test_case(&hex!("16 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Ok("hello\\world") ; "ia5: string with backspace")]
407 #[test_case(&hex!("16 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "ia5: string with plus")]
408 #[test_case(&hex!("16 05 01 02 03 04 05"), Ok("\x01\x02\x03\x04\x05") ; "invalid chars")]
409 #[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>>)410 fn tc_ber_ia5_string(i: &[u8], out: Result<&str, Err<BerError>>) {
411 let res = parse_ber_ia5string(i);
412 match out {
413 Ok(b) => {
414 let (rem, res) = res.expect("could not parse ia5string");
415 assert!(rem.is_empty());
416 let r = res.as_str().expect("could not convert to string");
417 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
418 pretty_assertions::assert_eq!(r, b);
419 }
420 Err(e) => {
421 pretty_assertions::assert_eq!(res, Err(e));
422 }
423 }
424 }
425
426 #[test_case(&hex!("1a 04 31 32 33 34"), Ok("1234") ; "visible: numeric")]
427 #[test_case(&hex!("1a 05 68 65 6c 6c 6f"), Ok("hello") ; "visible: string")]
428 #[test_case(&hex!("1a 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64"), Ok("hello world") ; "visible: string with spaces")]
429 #[test_case(&hex!("1a 0b 68 65 6c 6c 6f 5c 77 6f 72 6c 64"), Ok("hello\\world") ; "printable: string with backspace")]
430 #[test_case(&hex!("1a 0b 68 65 6c 6c 6f 2b 77 6f 72 6c 64"), Ok("hello+world") ; "printable: string with plus")]
431 #[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>>)432 fn tc_ber_visible_string(i: &[u8], out: Result<&str, Err<BerError>>) {
433 let res = parse_ber_visiblestring(i);
434 match out {
435 Ok(b) => {
436 let (rem, res) = res.expect("could not parse visiblestring");
437 assert!(rem.is_empty());
438 let r = res.as_str().expect("could not convert to string");
439 // let expected = BerObject::from_obj(BerObjectContent::Boolean(b));
440 pretty_assertions::assert_eq!(r, b);
441 }
442 Err(e) => {
443 pretty_assertions::assert_eq!(res, Err(e));
444 }
445 }
446 }
447
448 #[test]
test_ber_utf8string()449 fn test_ber_utf8string() {
450 let empty = &b""[..];
451 let bytes = [
452 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65,
453 ];
454 let expected = BerObject::from_obj(BerObjectContent::UTF8String("Some-State"));
455 assert_eq!(parse_ber_utf8string(&bytes), Ok((empty, expected)));
456 }
457
458 #[test]
test_ber_relativeoid()459 fn test_ber_relativeoid() {
460 let empty = &b""[..];
461 let bytes = hex!("0d 04 c2 7b 03 02");
462 let expected = BerObject::from_obj(BerObjectContent::RelativeOID(
463 Oid::from_relative(&[8571, 3, 2]).unwrap(),
464 ));
465 assert_eq!(parse_ber_relative_oid(&bytes), Ok((empty, expected)));
466 }
467
468 #[test]
test_ber_bmpstring()469 fn test_ber_bmpstring() {
470 let empty = &b""[..];
471 let bytes = hex!("1e 08 00 55 00 73 00 65 00 72");
472 let expected = BerObject::from_obj(BerObjectContent::BmpString(b"\x00U\x00s\x00e\x00r"));
473 assert_eq!(parse_ber_bmpstring(&bytes), Ok((empty, expected)));
474 }
475
476 #[test]
test_ber_customtags()477 fn test_ber_customtags() {
478 let bytes = hex!("8f 02 12 34");
479 let hdr = ber_read_element_header(&bytes)
480 .expect("ber_read_element_header")
481 .1;
482 // println!("{:?}", hdr);
483 let expected: &[u8] = &[0x8f];
484 assert_eq!(hdr.raw_tag, Some(expected));
485 let bytes = hex!("9f 0f 02 12 34");
486 let hdr = ber_read_element_header(&bytes)
487 .expect("ber_read_element_header")
488 .1;
489 // println!("{:?}", hdr);
490 let expected: &[u8] = &[0x9f, 0x0f];
491 assert_eq!(hdr.raw_tag, Some(expected));
492 }
493
494 #[test]
test_ber_indefinite()495 fn test_ber_indefinite() {
496 let bytes = hex!("30 80 02 03 01 00 01 00 00");
497 let (rem, val) = parse_ber_container::<_, _, BerError>(|i, _| {
498 assert!(!i.is_empty());
499 let (_, val) = parse_ber_u32(i)?;
500 Ok((i, val))
501 })(&bytes)
502 .unwrap();
503 assert!(rem.is_empty());
504 assert_eq!(val, 0x10001);
505 }
506
507 #[test]
test_ber_indefinite_recursion()508 fn test_ber_indefinite_recursion() {
509 let data = &hex!(
510 "
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
517 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
518 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80
519 24 80 24 80 24 80 24 80 24 80 24 80 24 80 24 80 00 00"
520 );
521 let _ = parse_ber_container::<_, _, BerError>(|i, _| Ok((i, ())))(data)
522 .expect_err("max parsing depth overflow");
523 }
524
525 #[test]
test_parse_ber_content()526 fn test_parse_ber_content() {
527 let bytes = &hex!("02 03 01 00 01");
528 let (i, header) = ber_read_element_header(bytes).expect("parsing failed");
529 let (rem, content) =
530 parse_ber_content(header.tag)(i, &header, MAX_RECURSION).expect("parsing failed");
531 assert!(rem.is_empty());
532 assert_eq!(header.tag, BerTag::Integer);
533 assert_eq!(content.as_u32(), Ok(0x10001));
534 }
535
536 #[test]
test_parse_ber_content2()537 fn test_parse_ber_content2() {
538 let bytes = &hex!("02 03 01 00 01");
539 let (i, header) = ber_read_element_header(bytes).expect("parsing failed");
540 let tag = header.tag;
541 let (rem, content) = parse_ber_content2(tag)(i, header, MAX_RECURSION).expect("parsing failed");
542 assert!(rem.is_empty());
543 assert_eq!(tag, BerTag::Integer);
544 assert_eq!(content.as_u32(), Ok(0x10001));
545 }
546