Debian Codegraph @

tests/nesting.rs

raw symbols SCIP
1 //! Tests for handling of nested messages.
2
3 use der::{Decode, Error, ErrorKind, Header, Reader, SliceReader, Tag, asn1::AnyRef};
4
5 // This is the expected maximum depth.
6 // TODO(tarcieri): expose this as a constant in the public API?
7 const MAX_DEPTH: usize = 64;
8
9 fn walk<'a>(reader: &mut SliceReader<'a>) -> Result<AnyRef<'a>, Error> {
10 let header = Header::peek(reader)?;
11 if header.tag() == Tag::Sequence {
12 reader.sequence(|r| walk(r)) // der::read_nested recursion
13 } else {
14 AnyRef::decode(reader)
15 }
16 }
17
18 #[test]
19 #[allow(clippy::cast_possible_truncation, reason = "test")]
20 fn returns_nesting_depth_error_when_max_depth_encountered() {
21 let depth = MAX_DEPTH + 1;
22
23 let mut buf = vec![0x05, 0x00]; // innermost NULL
24 for _ in 0..depth {
25 let len = buf.len();
26 let mut next = Vec::with_capacity(len + 5);
27 next.push(0x30);
28 if len < 0x80 {
29 next.push(len as u8);
30 } else if len <= 0xFF {
31 next.push(0x81);
32 next.push(len as u8);
33 } else if len <= 0xFFFF {
34 next.push(0x82);
35 next.push((len >> 8) as u8);
36 next.push(len as u8);
37 } else {
38 next.push(0x83);
39 next.push((len >> 16) as u8);
40 next.push((len >> 8) as u8);
41 next.push(len as u8);
42 }
43 next.extend_from_slice(&buf);
44 buf = next;
45 }
46
47 let mut reader = SliceReader::new(&buf).unwrap();
48 let err = walk(&mut reader).expect_err("should return ErrorKind::NestingDepth");
49 assert_eq!(err.kind(), ErrorKind::NestingDepth);
50 }