use std::iter::repeat_n;
use crate::{Error, Value};
fn nested_arrays(depth: usize) -> Vec<u8> {
let mut bytes = Vec::with_capacity(depth + 1);
bytes.extend(repeat_n(0x81, depth)); bytes.push(0xf6); bytes
}
fn nested_maps(depth: usize) -> Vec<u8> {
let mut bytes = Vec::with_capacity(depth * 2 + 1);
bytes.extend(repeat_n([0xa1, 0x00], depth).flatten()); bytes.push(0xf6); bytes
}
fn nested_tags(depth: usize) -> Vec<u8> {
let mut bytes = Vec::with_capacity(depth * 3 + 1);
bytes.extend(repeat_n([0xd9, 0xd9, 0xf7], depth).flatten()); bytes.push(0xf6); bytes
}
fn nested_mixed(depth: usize) -> Vec<u8> {
let mut bytes = Vec::new();
for i in 0..depth {
match i % 3 {
0 => bytes.push(0x81), 1 => bytes.extend([0xa1, 0x00]), _ => bytes.extend([0xd9, 0xd9, 0xf7]), }
}
bytes.push(0xf6); bytes
}
#[test]
fn nested_arrays_within_limit() {
let bytes = nested_arrays(199);
assert!(Value::decode(&bytes).is_ok());
}
#[test]
fn nested_arrays_at_limit() {
let bytes = nested_arrays(200);
assert!(Value::decode(&bytes).is_ok());
}
#[test]
fn nested_arrays_exceeds_limit() {
let bytes = nested_arrays(201);
assert_eq!(Value::decode(&bytes), Err(Error::LengthTooLarge));
}
#[test]
fn nested_maps_within_limit() {
let bytes = nested_maps(199);
assert!(Value::decode(&bytes).is_ok());
}
#[test]
fn nested_maps_at_limit() {
let bytes = nested_maps(200);
assert!(Value::decode(&bytes).is_ok());
}
#[test]
fn nested_maps_exceeds_limit() {
let bytes = nested_maps(201);
assert_eq!(Value::decode(&bytes), Err(Error::LengthTooLarge));
}
#[test]
fn nested_tags_within_limit() {
let bytes = nested_tags(199);
assert!(Value::decode(&bytes).is_ok());
}
#[test]
fn nested_tags_at_limit() {
let bytes = nested_tags(200);
assert!(Value::decode(&bytes).is_ok());
}
#[test]
fn nested_tags_exceeds_limit() {
let bytes = nested_tags(201);
assert_eq!(Value::decode(&bytes), Err(Error::LengthTooLarge));
}
#[test]
fn nested_mixed_exceeds_limit() {
let bytes = nested_mixed(201);
assert_eq!(Value::decode(&bytes), Err(Error::LengthTooLarge));
}
#[test]
fn array_declared_length_too_large() {
let bytes = [0x9a, 0x3b, 0x9a, 0xca, 0x01];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn map_declared_length_too_large() {
let bytes = [0xba, 0x3b, 0x9a, 0xca, 0x01];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn byte_string_declared_length_too_large() {
let bytes = [0x5a, 0x3b, 0x9a, 0xca, 0x01];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn text_string_declared_length_too_large() {
let bytes = [0x7a, 0x3b, 0x9a, 0xca, 0x01];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn array_max_u64_length() {
let bytes = [0x9b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn map_max_u64_length() {
let bytes = [0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn byte_string_max_u64_length() {
let bytes = [0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
assert_eq!(Value::decode(bytes), Err(Error::LengthTooLarge));
}
#[test]
fn array_declared_large_but_data_short() {
let bytes = [0x98, 0x64, 0x00];
assert_eq!(Value::decode(bytes), Err(Error::UnexpectedEof));
}
#[test]
fn map_declared_large_but_data_short() {
let bytes = [0xb8, 0x64, 0x00, 0x00];
assert_eq!(Value::decode(bytes), Err(Error::UnexpectedEof));
}
#[test]
fn byte_string_declared_large_but_data_short() {
let bytes = [0x58, 0x64, 0xaa, 0xbb, 0xcc];
assert_eq!(Value::decode(bytes), Err(Error::UnexpectedEof));
}