| 1 |
use decoder::{decode, MAX_ARRAY_SIZE}; |
| 2 |
use std::collections::BTreeMap; |
| 3 |
use {CborError, CborType}; |
| 4 |
|
| 5 |
// First test all the basic types |
| 6 |
fn test_decoder(bytes: Vec<u8>, expected: CborType) { |
| 7 |
let result = decode(&bytes); |
| 8 |
assert!(result.is_ok()); |
| 9 |
assert_eq!(result.unwrap(), expected); |
| 10 |
} |
| 11 |
|
| 12 |
fn test_decoder_error(bytes: Vec<u8>, expected_error: CborError) { |
| 13 |
let result = decode(&bytes); |
| 14 |
assert!(result.is_err()); |
| 15 |
assert_eq!(result.unwrap_err(), expected_error); |
| 16 |
} |
| 17 |
|
| 18 |
fn test_integer(bytes: Vec<u8>, expected: u64) { |
| 19 |
let decoded = decode(&bytes).unwrap(); |
| 20 |
match decoded { |
| 21 |
CborType::Integer(val) => assert_eq!(val, expected), |
| 22 |
_ => assert_eq!(1, 0), |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
fn test_integer_all(bytes: Vec<u8>, expected_value: u64) { |
| 27 |
let expected = CborType::Integer(expected_value); |
| 28 |
test_decoder(bytes.clone(), expected); |
| 29 |
test_integer(bytes, expected_value); |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn test_integer_objects() { |
| 34 |
let bytes: Vec<u8> = vec![0x00]; |
| 35 |
test_integer_all(bytes, 0); |
| 36 |
|
| 37 |
let bytes = vec![0x01]; |
| 38 |
test_integer_all(bytes, 1); |
| 39 |
|
| 40 |
let bytes = vec![0x0A]; |
| 41 |
test_integer_all(bytes, 10); |
| 42 |
|
| 43 |
let bytes = vec![0x17]; |
| 44 |
test_integer_all(bytes, 23); |
| 45 |
|
| 46 |
let bytes = vec![0x18, 0x18]; |
| 47 |
test_integer_all(bytes, 24); |
| 48 |
|
| 49 |
let bytes = vec![0x18, 0x19]; |
| 50 |
test_integer_all(bytes, 25); |
| 51 |
|
| 52 |
let bytes = vec![0x18, 0x64]; |
| 53 |
test_integer_all(bytes, 100); |
| 54 |
|
| 55 |
let bytes = vec![0x19, 0x03, 0xe8]; |
| 56 |
test_integer_all(bytes, 1000); |
| 57 |
|
| 58 |
let bytes = vec![0x1a, 0x00, 0x0f, 0x42, 0x40]; |
| 59 |
test_integer_all(bytes, 1000000); |
| 60 |
|
| 61 |
let bytes = vec![0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00]; |
| 62 |
test_integer_all(bytes, 1000000000000); |
| 63 |
|
| 64 |
let bytes = vec![0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; |
| 65 |
test_integer_all(bytes, 18446744073709551615); |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(test)] |
| 69 |
fn test_tag(bytes: Vec<u8>, expected_tag: u64, expected_value: CborType) { |
| 70 |
let decoded = decode(&bytes).unwrap(); |
| 71 |
match decoded { |
| 72 |
CborType::Tag(tag, value) => { |
| 73 |
assert_eq!(expected_tag, tag); |
| 74 |
assert_eq!(expected_value, *value); |
| 75 |
} |
| 76 |
_ => assert_eq!(1, 0), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn test_tagged_objects() { |
| 82 |
let bytes: Vec<u8> = vec![0xD2, 0x02]; |
| 83 |
let expected_tag_value = 0x12; |
| 84 |
let expected_value = CborType::Integer(2); |
| 85 |
let expected = CborType::Tag(expected_tag_value, Box::new(expected_value.clone())); |
| 86 |
test_decoder(bytes.clone(), expected); |
| 87 |
test_tag(bytes, expected_tag_value, expected_value); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
#[cfg_attr(rustfmt, rustfmt_skip)] |
| 92 |
fn test_arrays() { |
| 93 |
// [] |
| 94 |
let bytes: Vec<u8> = vec![0x80]; |
| 95 |
let expected = CborType::Array(vec![]); |
| 96 |
test_decoder(bytes, expected); |
| 97 |
|
| 98 |
// [1, 2, 3] |
| 99 |
let bytes: Vec<u8> = vec![0x83, 0x01, 0x02, 0x03]; |
| 100 |
let tmp = vec![ |
| 101 |
CborType::Integer(1), |
| 102 |
CborType::Integer(2), |
| 103 |
CborType::Integer(3), |
| 104 |
]; |
| 105 |
let expected = CborType::Array(tmp); |
| 106 |
test_decoder(bytes, expected); |
| 107 |
|
| 108 |
// [1, [2, 3], [4, 5]] |
| 109 |
let bytes: Vec<u8> = vec![0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05]; |
| 110 |
let tmp1 = vec![CborType::Integer(2), CborType::Integer(3)]; |
| 111 |
let tmp2 = vec![CborType::Integer(4), CborType::Integer(5)]; |
| 112 |
let tmp = vec![ |
| 113 |
CborType::Integer(1), |
| 114 |
CborType::Array(tmp1), |
| 115 |
CborType::Array(tmp2), |
| 116 |
]; |
| 117 |
let expected = CborType::Array(tmp); |
| 118 |
test_decoder(bytes, expected); |
| 119 |
|
| 120 |
// [1, [[[[1]]]], [1]] |
| 121 |
let bytes: Vec<u8> = vec![0x83, 0x01, 0x81, 0x81, 0x81, 0x81, 0x01, 0x81, 0x02]; |
| 122 |
let tmp = vec![ |
| 123 |
CborType::Integer(1), |
| 124 |
CborType::Array(vec![ |
| 125 |
CborType::Array(vec![ |
| 126 |
CborType::Array(vec![ |
| 127 |
CborType::Array(vec![ |
| 128 |
CborType::Integer(1)])])])]), |
| 129 |
CborType::Array(vec![CborType::Integer(2)]), |
| 130 |
]; |
| 131 |
let expected = CborType::Array(tmp); |
| 132 |
test_decoder(bytes, expected); |
| 133 |
|
| 134 |
let bytes: Vec<u8> = vec![0x98, 0x1A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 135 |
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 136 |
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, |
| 137 |
0x17, 0x18, 0x18, 0x18, 0x19, 0x82, 0x81, 0x81, |
| 138 |
0x81, 0x05, 0x81, 0x1A, 0x49, 0x96, 0x02, 0xD2]; |
| 139 |
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 140 |
// 21, 22, 23, 24, 25, [[[[5]]], [1234567890]]] |
| 141 |
let tmp = vec![ |
| 142 |
CborType::Integer(1), |
| 143 |
CborType::Integer(2), |
| 144 |
CborType::Integer(3), |
| 145 |
CborType::Integer(4), |
| 146 |
CborType::Integer(5), |
| 147 |
CborType::Integer(6), |
| 148 |
CborType::Integer(7), |
| 149 |
CborType::Integer(8), |
| 150 |
CborType::Integer(9), |
| 151 |
CborType::Integer(10), |
| 152 |
CborType::Integer(11), |
| 153 |
CborType::Integer(12), |
| 154 |
CborType::Integer(13), |
| 155 |
CborType::Integer(14), |
| 156 |
CborType::Integer(15), |
| 157 |
CborType::Integer(16), |
| 158 |
CborType::Integer(17), |
| 159 |
CborType::Integer(18), |
| 160 |
CborType::Integer(19), |
| 161 |
CborType::Integer(20), |
| 162 |
CborType::Integer(21), |
| 163 |
CborType::Integer(22), |
| 164 |
CborType::Integer(23), |
| 165 |
CborType::Integer(24), |
| 166 |
CborType::Integer(25), |
| 167 |
CborType::Array(vec![ |
| 168 |
CborType::Array(vec![ |
| 169 |
CborType::Array(vec![ |
| 170 |
CborType::Array(vec![ |
| 171 |
CborType::Integer(5)])])]), |
| 172 |
CborType::Array(vec![CborType::Integer(1234567890)])]) |
| 173 |
]; |
| 174 |
let expected = CborType::Array(tmp); |
| 175 |
test_decoder(bytes, expected); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn test_signed_integer() { |
| 180 |
let bytes: Vec<u8> = vec![0x20]; |
| 181 |
let expected = CborType::SignedInteger(-1); |
| 182 |
test_decoder(bytes, expected); |
| 183 |
|
| 184 |
let bytes = vec![0x29]; |
| 185 |
let expected = CborType::SignedInteger(-10); |
| 186 |
test_decoder(bytes, expected); |
| 187 |
|
| 188 |
let bytes = vec![0x38, 0x63]; |
| 189 |
let expected = CborType::SignedInteger(-100); |
| 190 |
test_decoder(bytes, expected); |
| 191 |
|
| 192 |
let bytes = vec![0x39, 0x03, 0xe7]; |
| 193 |
let expected = CborType::SignedInteger(-1000); |
| 194 |
test_decoder(bytes, expected); |
| 195 |
|
| 196 |
let bytes = vec![0x39, 0x27, 0x0F]; |
| 197 |
let expected = CborType::SignedInteger(-10000); |
| 198 |
test_decoder(bytes, expected); |
| 199 |
|
| 200 |
let bytes = vec![0x3A, 0x00, 0x01, 0x86, 0x9F]; |
| 201 |
let expected = CborType::SignedInteger(-100000); |
| 202 |
test_decoder(bytes, expected); |
| 203 |
|
| 204 |
let bytes = vec![0x3B, 0x00, 0x00, 0x00, 0xE8, 0xD4, 0xA5, 0x0F, 0xFF]; |
| 205 |
let expected = CborType::SignedInteger(-1000000000000); |
| 206 |
test_decoder(bytes, expected); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn test_byte_strings() { |
| 211 |
let bytes: Vec<u8> = vec![0x40]; |
| 212 |
let expected = CborType::Bytes(vec![]); |
| 213 |
test_decoder(bytes, expected); |
| 214 |
|
| 215 |
// 01020304 |
| 216 |
let bytes: Vec<u8> = vec![0x44, 0x01, 0x02, 0x03, 0x04]; |
| 217 |
let expected = CborType::Bytes(vec![0x01, 0x02, 0x03, 0x04]); |
| 218 |
test_decoder(bytes, expected); |
| 219 |
|
| 220 |
// 0102030405060708090A0B0C0D0E0F10203040506070 |
| 221 |
let bytes: Vec<u8> = vec![ |
| 222 |
0x56, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 223 |
0x0f, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, |
| 224 |
]; |
| 225 |
let expected = CborType::Bytes(vec![ |
| 226 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 227 |
0x10, 0x20, 0x30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 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 266 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 267 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 268 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 269 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 270 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 271 |
]); |
| 272 |
test_decoder(bytes, expected); |
| 273 |
} |
| 274 |
|
| 275 |
#[test] |
| 276 |
fn test_maps() { |
| 277 |
// {} |
| 278 |
let bytes: Vec<u8> = vec![0xa0]; |
| 279 |
let expected: BTreeMap<CborType, CborType> = BTreeMap::new(); |
| 280 |
test_decoder(bytes, CborType::Map(expected)); |
| 281 |
|
| 282 |
// {1: 2, 3: 4} |
| 283 |
let bytes: Vec<u8> = vec![0xa2, 0x01, 0x02, 0x03, 0x04]; |
| 284 |
let mut expected: BTreeMap<CborType, CborType> = BTreeMap::new(); |
| 285 |
expected.insert(CborType::Integer(1), CborType::Integer(2)); |
| 286 |
expected.insert(CborType::Integer(3), CborType::Integer(4)); |
| 287 |
test_decoder(bytes, CborType::Map(expected)); |
| 288 |
|
| 289 |
// TODO: strings aren't properly supported as keys yet. |
| 290 |
// {"a": 1, "b": [2, 3]} |
| 291 |
// let bytes: Vec<u8> = vec![0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03]; |
| 292 |
// let expected = |
| 293 |
// CborType::Map(vec![ |
| 294 |
// CborMap{key: CborType::Integer(1), value: CborType::Integer(2)}, |
| 295 |
// CborMap{key: CborType::Integer(3), value: CborType::Integer(4)}]); |
| 296 |
// test_decoder(bytes, expected); |
| 297 |
|
| 298 |
// let bytes: Vec<u8> = vec![0x82, 0x61, 0x61, 0xa1, 0x61, 0x62, 0x61, 0x63]; |
| 299 |
// test_decoder(bytes, "[a, {b: c}]"); |
| 300 |
|
| 301 |
// let bytes: Vec<u8> = vec![0xa5, 0x61, 0x61, 0x61, 0x41, 0x61, 0x62, 0x61, |
| 302 |
// 0x42, 0x61, 0x63, 0x61, 0x43, 0x61, 0x64, 0x61, |
| 303 |
// 0x44, 0x61, 0x65, 0x61, 0x45]; |
| 304 |
// test_decoder(bytes, "{a: A, b: B, c: C, d: D, e: E}"); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_map_duplicate_keys() { |
| 309 |
let bytes: Vec<u8> = vec![0xa4, 0x01, 0x02, 0x02, 0x03, 0x01, 0x03, 0x04, 0x04]; |
| 310 |
test_decoder_error(bytes, CborError::DuplicateMapKey); |
| 311 |
} |
| 312 |
|
| 313 |
#[test] |
| 314 |
fn test_tag_with_no_value() { |
| 315 |
let bytes: Vec<u8> = vec![0xc0]; |
| 316 |
test_decoder_error(bytes, CborError::TruncatedInput); |
| 317 |
} |
| 318 |
|
| 319 |
#[test] |
| 320 |
fn test_truncated_int() { |
| 321 |
let bytes: Vec<u8> = vec![0x19, 0x03]; |
| 322 |
test_decoder_error(bytes, CborError::TruncatedInput); |
| 323 |
} |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn test_truncated_array() { |
| 327 |
let bytes: Vec<u8> = vec![0x83, 0x01, 0x02]; |
| 328 |
test_decoder_error(bytes, CborError::TruncatedInput); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn test_truncated_map() { |
| 333 |
let bytes: Vec<u8> = vec![0xa2, 0x01, 0x02, 0x00]; |
| 334 |
test_decoder_error(bytes, CborError::TruncatedInput); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn test_malformed_integer() { |
| 339 |
let bytes: Vec<u8> = vec![0x1c]; |
| 340 |
test_decoder_error(bytes, CborError::MalformedInput); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn test_signed_integer_too_large() { |
| 345 |
let bytes = vec![0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]; |
| 346 |
test_decoder_error(bytes, CborError::InputValueOutOfRange); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
fn test_null() { |
| 351 |
let bytes = vec![0xf6]; |
| 352 |
test_decoder(bytes, CborType::Null); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn test_null_in_array() { |
| 357 |
let bytes = vec![0x82, 0xf6, 0xf6]; |
| 358 |
test_decoder(bytes, CborType::Array(vec![CborType::Null, CborType::Null])); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn test_major_type_7() { |
| 363 |
for i in 0..0x20 { |
| 364 |
if i != 22 { |
| 365 |
let bytes = vec![0xe0 | i]; |
| 366 |
test_decoder_error(bytes, CborError::UnsupportedType); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
#[test] |
| 372 |
fn test_large_input() { |
| 373 |
let array = vec![0xFF; MAX_ARRAY_SIZE]; |
| 374 |
let expected = CborType::Bytes(array.clone()); |
| 375 |
let mut bytes = vec![0x5A, 0x08, 0x00, 0x00, 0x00]; |
| 376 |
bytes.extend_from_slice(&array); |
| 377 |
test_decoder(bytes, expected); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn test_too_large_input() { |
| 382 |
let array = vec![0xFF; MAX_ARRAY_SIZE + 1]; |
| 383 |
let mut bytes = vec![0x5A, 0x08, 0x00, 0x00, 0x01]; |
| 384 |
bytes.extend_from_slice(&array); |
| 385 |
test_decoder_error(bytes, CborError::InputTooLarge); |
| 386 |
} |
| 387 |
|
| 388 |
// We currently don't support CBOR strings (issue #39). |
| 389 |
#[test] |
| 390 |
fn test_invalid_input() { |
| 391 |
let bytes = vec![0x60]; |
| 392 |
test_decoder_error(bytes, CborError::UnsupportedType); |
| 393 |
} |
| 394 |
|
| 395 |
#[test] |
| 396 |
fn test_avoid_stack_exhaustion_with_arrays() { |
| 397 |
let mut bytes: Vec<u8> = Vec::new(); |
| 398 |
// Create a payload representing Array(Array(Array(Array(...(Array(0)))))) |
| 399 |
// If the implementation is not careful, this will exhaust the stack. |
| 400 |
for _ in 1..10000 { |
| 401 |
bytes.push(0b1000_0001); |
| 402 |
} |
| 403 |
bytes.push(0); |
| 404 |
test_decoder_error(bytes, CborError::MalformedInput); |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn test_avoid_stack_exhaustion_with_maps_1() { |
| 409 |
let mut bytes: Vec<u8> = Vec::new(); |
| 410 |
// Create a payload representing Map(0: Map(0: Map(0: Map(...Map())))) |
| 411 |
// If the implementation is not careful, this will exhaust the stack. |
| 412 |
for _ in 1..10000 { |
| 413 |
bytes.push(0b1010_0001); |
| 414 |
bytes.push(0); |
| 415 |
} |
| 416 |
bytes.push(0b1010_0000); |
| 417 |
test_decoder_error(bytes, CborError::MalformedInput); |
| 418 |
} |
| 419 |
|
| 420 |
#[test] |
| 421 |
fn test_avoid_stack_exhaustion_with_maps_2() { |
| 422 |
let mut bytes: Vec<u8> = Vec::new(); |
| 423 |
// Create a payload representing Map(Map(Map(...(Map(): 0): 0): 0): 0) |
| 424 |
// If the implementation is not careful, this will exhaust the stack. |
| 425 |
for _ in 1..10000 { |
| 426 |
bytes.push(0b1010_0001); |
| 427 |
} |
| 428 |
bytes.push(0b1010_0000); |
| 429 |
for _ in 1..9999 { |
| 430 |
bytes.push(0); |
| 431 |
} |
| 432 |
test_decoder_error(bytes, CborError::MalformedInput); |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn test_avoid_stack_exhaustion_with_tags() { |
| 437 |
let mut bytes: Vec<u8> = Vec::new(); |
| 438 |
// Create a payload representing Tag(6: Tag(6: Tag(6: Tag(...Tag(0))))) |
| 439 |
// If the implementation is not careful, this will exhaust the stack. |
| 440 |
for _ in 1..10000 { |
| 441 |
bytes.push(0b1100_0110); |
| 442 |
} |
| 443 |
bytes.push(0); |
| 444 |
test_decoder_error(bytes, CborError::MalformedInput); |
| 445 |
} |