| 1 | #[macro_use] |
| 2 | extern crate serde; |
| 3 | |
| 4 | use serde_cbor_2::de; |
| 5 | |
| 6 | #[test] |
| 7 | fn test_str() { |
| 8 | let s: &str = |
| 9 | de::from_slice_with_scratch(&[0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72], &mut []).unwrap(); |
| 10 | assert_eq!(s, "foobar"); |
| 11 | } |
| 12 | |
| 13 | #[test] |
| 14 | fn test_bytes() { |
| 15 | let s: &[u8] = |
| 16 | de::from_slice_with_scratch(&[0x46, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72], &mut []).unwrap(); |
| 17 | assert_eq!(s, b"foobar"); |
| 18 | } |
| 19 | |
| 20 | #[test] |
| 21 | fn test_int() { |
| 22 | let num: i64 = de::from_slice_with_scratch(&[0x39, 0x07, 0xde], &mut []).unwrap(); |
| 23 | assert_eq!(num, -2015); |
| 24 | } |
| 25 | |
| 26 | #[test] |
| 27 | fn test_float() { |
| 28 | let float: f64 = de::from_slice_with_scratch(b"\xfa\x47\xc3\x50\x00", &mut []).unwrap(); |
| 29 | assert_eq!(float, 100000.0); |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn test_indefinite_object() { |
| 34 | #[derive(Debug, Deserialize, PartialEq)] |
| 35 | struct Foo { |
| 36 | a: u64, |
| 37 | b: [u64; 2], |
| 38 | } |
| 39 | let expected = Foo { a: 1, b: [2, 3] }; |
| 40 | let actual: Foo = |
| 41 | de::from_slice_with_scratch(b"\xbfaa\x01ab\x9f\x02\x03\xff\xff", &mut []).unwrap(); |
| 42 | assert_eq!(expected, actual); |
| 43 | } |
| 44 | |
| 45 | #[cfg(feature = "std")] |
| 46 | mod std_tests { |
| 47 | use std::collections::BTreeMap; |
| 48 | |
| 49 | use serde::de as serde_de; |
| 50 | use serde_cbor_2::value::Value; |
| 51 | use serde_cbor_2::{de, error, to_vec, Deserializer}; |
| 52 | |
| 53 | #[test] |
| 54 | fn test_string1() { |
| 55 | let value: error::Result<Value> = |
| 56 | de::from_slice(&[0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72]); |
| 57 | assert_eq!(value.unwrap(), Value::Text("foobar".to_owned())); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn test_string2() { |
| 62 | let value: error::Result<Value> = de::from_slice(&[ |
| 63 | 0x71, 0x49, 0x20, 0x6d, 0x65, 0x74, 0x20, 0x61, 0x20, 0x74, 0x72, 0x61, 0x76, 0x65, |
| 64 | 0x6c, 0x6c, 0x65, 0x72, |
| 65 | ]); |
| 66 | assert_eq!(value.unwrap(), Value::Text("I met a traveller".to_owned())); |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn test_string3() { |
| 71 | let slice = b"\x78\x2fI met a traveller from an antique land who said"; |
| 72 | let value: error::Result<Value> = de::from_slice(slice); |
| 73 | assert_eq!( |
| 74 | value.unwrap(), |
| 75 | Value::Text("I met a traveller from an antique land who said".to_owned()) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn test_byte_string() { |
| 81 | let value: error::Result<Value> = |
| 82 | de::from_slice(&[0x46, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72]); |
| 83 | assert_eq!(value.unwrap(), Value::Bytes(b"foobar".to_vec())); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn test_numbers1() { |
| 88 | let value: error::Result<Value> = de::from_slice(&[0x00]); |
| 89 | assert_eq!(value.unwrap(), Value::Integer(0)); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn test_numbers2() { |
| 94 | let value: error::Result<Value> = de::from_slice(&[0x1a, 0x00, 0xbc, 0x61, 0x4e]); |
| 95 | assert_eq!(value.unwrap(), Value::Integer(12345678)); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn test_numbers3() { |
| 100 | let value: error::Result<Value> = de::from_slice(&[0x39, 0x07, 0xde]); |
| 101 | assert_eq!(value.unwrap(), Value::Integer(-2015)); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn test_bool() { |
| 106 | let value: error::Result<Value> = de::from_slice(b"\xf4"); |
| 107 | assert_eq!(value.unwrap(), Value::Bool(false)); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn test_trailing_bytes() { |
| 112 | let value: error::Result<Value> = de::from_slice(b"\xf4trailing"); |
| 113 | assert!(value.is_err()); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn test_list1() { |
| 118 | let value: error::Result<Value> = de::from_slice(b"\x83\x01\x02\x03"); |
| 119 | assert_eq!( |
| 120 | value.unwrap(), |
| 121 | Value::Array(vec![ |
| 122 | Value::Integer(1), |
| 123 | Value::Integer(2), |
| 124 | Value::Integer(3) |
| 125 | ]) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn test_list2() { |
| 131 | let value: error::Result<Value> = de::from_slice(b"\x82\x01\x82\x02\x81\x03"); |
| 132 | assert_eq!( |
| 133 | value.unwrap(), |
| 134 | Value::Array(vec![ |
| 135 | Value::Integer(1), |
| 136 | Value::Array(vec![ |
| 137 | Value::Integer(2), |
| 138 | Value::Array(vec![Value::Integer(3)]) |
| 139 | ]) |
| 140 | ]) |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | #[test] |
| 145 | fn test_object() { |
| 146 | let value: error::Result<Value> = de::from_slice(b"\xa5aaaAabaBacaCadaDaeaE"); |
| 147 | let mut object = BTreeMap::new(); |
| 148 | object.insert(Value::Text("a".to_owned()), Value::Text("A".to_owned())); |
| 149 | object.insert(Value::Text("b".to_owned()), Value::Text("B".to_owned())); |
| 150 | object.insert(Value::Text("c".to_owned()), Value::Text("C".to_owned())); |
| 151 | object.insert(Value::Text("d".to_owned()), Value::Text("D".to_owned())); |
| 152 | object.insert(Value::Text("e".to_owned()), Value::Text("E".to_owned())); |
| 153 | assert_eq!(value.unwrap(), Value::Map(object)); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn test_indefinite_object() { |
| 158 | let value: error::Result<Value> = de::from_slice(b"\xbfaa\x01ab\x9f\x02\x03\xff\xff"); |
| 159 | let mut object = BTreeMap::new(); |
| 160 | object.insert(Value::Text("a".to_owned()), Value::Integer(1)); |
| 161 | object.insert( |
| 162 | Value::Text("b".to_owned()), |
| 163 | Value::Array(vec![Value::Integer(2), Value::Integer(3)]), |
| 164 | ); |
| 165 | assert_eq!(value.unwrap(), Value::Map(object)); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn test_indefinite_list() { |
| 170 | let value: error::Result<Value> = de::from_slice(b"\x9f\x01\x02\x03\xff"); |
| 171 | assert_eq!( |
| 172 | value.unwrap(), |
| 173 | Value::Array(vec![ |
| 174 | Value::Integer(1), |
| 175 | Value::Integer(2), |
| 176 | Value::Integer(3) |
| 177 | ]) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn test_indefinite_string() { |
| 183 | let value: error::Result<Value> = |
| 184 | de::from_slice(b"\x7f\x65Mary \x64Had \x62a \x67Little \x60\x64Lamb\xff"); |
| 185 | assert_eq!( |
| 186 | value.unwrap(), |
| 187 | Value::Text("Mary Had a Little Lamb".to_owned()) |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn test_indefinite_byte_string() { |
| 193 | let value: error::Result<Value> = de::from_slice(b"\x5f\x42\x01\x23\x42\x45\x67\xff"); |
| 194 | assert_eq!(value.unwrap(), Value::Bytes(b"\x01#Eg".to_vec())); |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn test_multiple_indefinite_strings() { |
| 199 | let input = b"\x82\x7f\x65Mary \x64Had \x62a \x67Little \x60\x64Lamb\xff\x5f\x42\x01\x23\x42\x45\x67\xff"; |
| 200 | _test_multiple_indefinite_strings(de::from_slice(input)); |
| 201 | _test_multiple_indefinite_strings(de::from_mut_slice(input.to_vec().as_mut())); |
| 202 | let mut buf = [0u8; 64]; |
| 203 | _test_multiple_indefinite_strings(de::from_slice_with_scratch(input, &mut buf)); |
| 204 | } |
| 205 | fn _test_multiple_indefinite_strings(value: error::Result<Value>) { |
| 206 | // This assures that buffer rewinding in infinite buffers works as intended. |
| 207 | assert_eq!( |
| 208 | value.unwrap(), |
| 209 | Value::Array(vec![ |
| 210 | Value::Text("Mary Had a Little Lamb".to_owned()), |
| 211 | Value::Bytes(b"\x01#Eg".to_vec()) |
| 212 | ]) |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn test_float() { |
| 218 | let value: error::Result<Value> = de::from_slice(b"\xfa\x47\xc3\x50\x00"); |
| 219 | assert_eq!(value.unwrap(), Value::Float(100000.0)); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn test_self_describing() { |
| 224 | let value: error::Result<Value> = |
| 225 | de::from_slice(&[0xd9, 0xd9, 0xf7, 0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72]); |
| 226 | let expected = Value::Text("foobar".to_owned()); |
| 227 | let strip_tags = |x: Value| { |
| 228 | if let Value::Tag(_, inner) = x { |
| 229 | *inner |
| 230 | } else { |
| 231 | x |
| 232 | } |
| 233 | }; |
| 234 | assert_eq!(strip_tags(value.unwrap()), expected); |
| 235 | } |
| 236 | |
| 237 | #[test] |
| 238 | fn test_f16() { |
| 239 | let mut x: Value = de::from_slice(&[0xf9, 0x41, 0x00]).unwrap(); |
| 240 | assert_eq!(x, Value::Float(2.5)); |
| 241 | x = de::from_slice(&[0xf9, 0x41, 0x90]).unwrap(); |
| 242 | assert_eq!(x, Value::Float(2.78125)); |
| 243 | x = de::from_slice(&[0xf9, 0x50, 0x90]).unwrap(); |
| 244 | assert_eq!(x, Value::Float(36.5)); |
| 245 | x = de::from_slice(&[0xf9, 0xd0, 0x90]).unwrap(); |
| 246 | assert_eq!(x, Value::Float(-36.5)); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn test_crazy_list() { |
| 251 | let slice = b"\x88\x1b\x00\x00\x00\x1c\xbe\x99\x1d\xc7\x3b\x00\x7a\xcf\x51\xdc\x51\x70\xdb\x3a\x1b\x3a\x06\xdd\xf5\xf6\xf7\xfb\x41\x76\x5e\xb1\xf8\x00\x00\x00\xf9\x7c\x00"; |
| 252 | let value: Vec<Value> = de::from_slice(slice).unwrap(); |
| 253 | assert_eq!( |
| 254 | value, |
| 255 | vec![ |
| 256 | Value::Integer(123456789959), |
| 257 | Value::Integer(-34567897654325468), |
| 258 | Value::Integer(-456787678), |
| 259 | Value::Bool(true), |
| 260 | Value::Null, |
| 261 | Value::Null, |
| 262 | Value::Float(23456543.5), |
| 263 | Value::Float(f64::INFINITY) |
| 264 | ] |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn test_nan() { |
| 270 | let value: f64 = de::from_slice(b"\xf9\x7e\x00").unwrap(); |
| 271 | assert!(value.is_nan()); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn test_32f16() { |
| 276 | let value: f32 = de::from_slice(b"\xf9\x50\x00").unwrap(); |
| 277 | assert_eq!(value, 32.0f32); |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | // The file was reported as not working by user kie0tauB |
| 282 | // but it parses to a cbor value. |
| 283 | fn test_kietaub_file() { |
| 284 | let file = include_bytes!("kietaub.cbor"); |
| 285 | let value_result: error::Result<Value> = de::from_slice(file); |
| 286 | value_result.unwrap(); |
| 287 | } |
| 288 | |
| 289 | #[test] |
| 290 | fn test_option_roundtrip() { |
| 291 | let obj1 = Some(10u32); |
| 292 | |
| 293 | let v = to_vec(&obj1).unwrap(); |
| 294 | let obj2: Result<Option<u32>, _> = serde_cbor_2::de::from_reader(&v[..]); |
| 295 | println!("{obj2:?}"); |
| 296 | |
| 297 | assert_eq!(obj1, obj2.unwrap()); |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn test_option_none_roundtrip() { |
| 302 | let obj1 = None; |
| 303 | |
| 304 | let v = to_vec(&obj1).unwrap(); |
| 305 | println!("{v:?}"); |
| 306 | let obj2: Result<Option<u32>, _> = serde_cbor_2::de::from_reader(&v[..]); |
| 307 | |
| 308 | assert_eq!(obj1, obj2.unwrap()); |
| 309 | } |
| 310 | |
| 311 | #[test] |
| 312 | fn test_variable_length_map() { |
| 313 | let slice = b"\xbf\x67\x6d\x65\x73\x73\x61\x67\x65\x64\x70\x6f\x6e\x67\xff"; |
| 314 | let value: Value = de::from_slice(slice).unwrap(); |
| 315 | let mut map = BTreeMap::new(); |
| 316 | map.insert( |
| 317 | Value::Text("message".to_string()), |
| 318 | Value::Text("pong".to_string()), |
| 319 | ); |
| 320 | assert_eq!(value, Value::Map(map)) |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn test_object_determinism_roundtrip() { |
| 325 | let expected = b"\xa2aa\x01ab\x82\x02\x03"; |
| 326 | |
| 327 | // 0.1% chance of not catching failure |
| 328 | for _ in 0..10 { |
| 329 | assert_eq!( |
| 330 | &to_vec(&de::from_slice::<Value>(expected).unwrap()).unwrap(), |
| 331 | expected |
| 332 | ); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn stream_deserializer() { |
| 338 | let slice = b"\x01\x66foobar"; |
| 339 | let mut it = Deserializer::from_slice(slice).into_iter::<Value>(); |
| 340 | assert_eq!(Value::Integer(1), it.next().unwrap().unwrap()); |
| 341 | assert_eq!( |
| 342 | Value::Text("foobar".to_string()), |
| 343 | it.next().unwrap().unwrap() |
| 344 | ); |
| 345 | assert!(it.next().is_none()); |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn stream_deserializer_eof() { |
| 350 | let slice = b"\x01\x66foob"; |
| 351 | let mut it = Deserializer::from_slice(slice).into_iter::<Value>(); |
| 352 | assert_eq!(Value::Integer(1), it.next().unwrap().unwrap()); |
| 353 | assert!(it.next().unwrap().unwrap_err().is_eof()); |
| 354 | } |
| 355 | |
| 356 | #[test] |
| 357 | fn stream_deserializer_eof_in_indefinite() { |
| 358 | let slice = b"\x7f\x65Mary \x64Had \x62a \x60\x67Little \x60\x64Lamb\xff"; |
| 359 | let indices: &[usize] = &[ |
| 360 | 2, // announcement but no data |
| 361 | 10, // mid-buffer EOF |
| 362 | 12, // neither new element nor end marker |
| 363 | ]; |
| 364 | for end_of_slice in indices { |
| 365 | let mut it = Deserializer::from_slice(&slice[..*end_of_slice]).into_iter::<Value>(); |
| 366 | assert!(it.next().unwrap().unwrap_err().is_eof()); |
| 367 | |
| 368 | let mut mutcopy = slice[..*end_of_slice].to_vec(); |
| 369 | let mut it = Deserializer::from_mut_slice(mutcopy.as_mut()).into_iter::<Value>(); |
| 370 | assert!(it.next().unwrap().unwrap_err().is_eof()); |
| 371 | |
| 372 | let mut buf = [0u8; 64]; |
| 373 | let mut it = Deserializer::from_slice_with_scratch(&slice[..*end_of_slice], &mut buf) |
| 374 | .into_iter::<Value>(); |
| 375 | assert!(it.next().unwrap().unwrap_err().is_eof()); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | #[test] |
| 380 | fn crash() { |
| 381 | let file = include_bytes!("crash.cbor"); |
| 382 | let value_result: error::Result<Value> = de::from_slice(file); |
| 383 | assert_eq!( |
| 384 | value_result.unwrap_err().classify(), |
| 385 | serde_cbor_2::error::Category::Syntax |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | fn from_slice_stream<'a, T>(slice: &'a [u8]) -> error::Result<(&'a [u8], T)> |
| 390 | where |
| 391 | T: serde_de::Deserialize<'a>, |
| 392 | { |
| 393 | let mut deserializer = Deserializer::from_slice(slice); |
| 394 | let value = serde_de::Deserialize::deserialize(&mut deserializer)?; |
| 395 | let rest = &slice[deserializer.byte_offset()..]; |
| 396 | |
| 397 | Ok((rest, value)) |
| 398 | } |
| 399 | |
| 400 | #[test] |
| 401 | fn test_slice_offset() { |
| 402 | let v: Vec<u8> = vec![ |
| 403 | 0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, 0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, |
| 404 | ]; |
| 405 | let (rest, value): (&[u8], String) = from_slice_stream(&v[..]).unwrap(); |
| 406 | assert_eq!(value, "foobar"); |
| 407 | assert_eq!(rest, &[0x66, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72]); |
| 408 | let (rest, value): (&[u8], String) = from_slice_stream(rest).unwrap(); |
| 409 | assert_eq!(value, "foobar"); |
| 410 | assert_eq!(rest, &[]); |
| 411 | } |
| 412 | |
| 413 | #[derive(Debug, Copy, Clone)] |
| 414 | struct Options { |
| 415 | standard: bool, |
| 416 | legacy: bool, |
| 417 | packed: bool, |
| 418 | named: bool, |
| 419 | } |
| 420 | |
| 421 | impl Default for Options { |
| 422 | fn default() -> Self { |
| 423 | Options { |
| 424 | standard: true, |
| 425 | legacy: true, |
| 426 | packed: true, |
| 427 | named: true, |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | impl Options { |
| 433 | fn no_standard(self) -> Self { |
| 434 | Options { |
| 435 | standard: false, |
| 436 | ..self |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | fn no_legacy(self) -> Self { |
| 441 | Options { |
| 442 | legacy: false, |
| 443 | ..self |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | fn no_packed(self) -> Self { |
| 448 | Options { |
| 449 | packed: false, |
| 450 | ..self |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | fn no_named(self) -> Self { |
| 455 | Options { |
| 456 | named: false, |
| 457 | ..self |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | fn from_slice_stream_options<'a, T>( |
| 463 | slice: &'a [u8], |
| 464 | options: Options, |
| 465 | ) -> error::Result<(&'a [u8], T)> |
| 466 | where |
| 467 | T: serde_de::Deserialize<'a>, |
| 468 | { |
| 469 | let deserializer = Deserializer::from_slice(slice); |
| 470 | let deserializer = if !options.packed { |
| 471 | deserializer.disable_packed_format() |
| 472 | } else { |
| 473 | deserializer |
| 474 | }; |
| 475 | let deserializer = if !options.named { |
| 476 | deserializer.disable_named_format() |
| 477 | } else { |
| 478 | deserializer |
| 479 | }; |
| 480 | let deserializer = if !options.standard { |
| 481 | deserializer.disable_standard_enums() |
| 482 | } else { |
| 483 | deserializer |
| 484 | }; |
| 485 | let mut deserializer = if !options.legacy { |
| 486 | deserializer.disable_legacy_enums() |
| 487 | } else { |
| 488 | deserializer |
| 489 | }; |
| 490 | let value = serde_de::Deserialize::deserialize(&mut deserializer)?; |
| 491 | let rest = &slice[deserializer.byte_offset()..]; |
| 492 | |
| 493 | Ok((rest, value)) |
| 494 | } |
| 495 | |
| 496 | #[test] |
| 497 | fn test_deserializer_enums() { |
| 498 | #[derive(Debug, PartialEq, Deserialize)] |
| 499 | enum Enum { |
| 500 | Unit, |
| 501 | NewType(i32), |
| 502 | Tuple(String, bool), |
| 503 | Struct { x: i32, y: i32 }, |
| 504 | } |
| 505 | |
| 506 | // This is the format used in serde >= 0.10 |
| 507 | // |
| 508 | // Serialization of Enum::NewType(10) |
| 509 | let v: Vec<u8> = vec![ |
| 510 | 0xa1, // map 1pair |
| 511 | 0x67, 0x4e, 0x65, 0x77, 0x54, 0x79, 0x70, 0x65, // utf8 string: NewType |
| 512 | 0x1a, // u32 |
| 513 | 0x00, 0x00, 0x00, 0x0a, // 10 (dec) |
| 514 | ]; |
| 515 | let (_rest, value): (&[u8], Enum) = from_slice_stream(&v[..]).unwrap(); |
| 516 | assert_eq!(value, Enum::NewType(10)); |
| 517 | let (_rest, value): (&[u8], Enum) = |
| 518 | from_slice_stream_options(&v[..], Options::default().no_legacy()).unwrap(); |
| 519 | assert_eq!(value, Enum::NewType(10)); |
| 520 | let value: error::Result<(&[u8], Enum)> = |
| 521 | from_slice_stream_options(&v[..], Options::default().no_standard()); |
| 522 | assert_eq!( |
| 523 | value.unwrap_err().classify(), |
| 524 | serde_cbor_2::error::Category::Syntax |
| 525 | ); |
| 526 | let value: error::Result<(&[u8], Enum)> = |
| 527 | from_slice_stream_options(&v[..], Options::default().no_standard().no_legacy()); |
| 528 | assert_eq!( |
| 529 | value.unwrap_err().classify(), |
| 530 | serde_cbor_2::error::Category::Syntax |
| 531 | ); |
| 532 | // Serialization of Enum::Unit |
| 533 | let v: Vec<u8> = vec![ |
| 534 | 0x64, 0x55, 0x6e, 0x69, 0x74, // utf8 string: Unit |
| 535 | ]; |
| 536 | let (_rest, value): (&[u8], Enum) = from_slice_stream(&v[..]).unwrap(); |
| 537 | assert_eq!(value, Enum::Unit); |
| 538 | let (_rest, value): (&[u8], Enum) = |
| 539 | from_slice_stream_options(&v[..], Options::default().no_legacy()).unwrap(); |
| 540 | assert_eq!(value, Enum::Unit); |
| 541 | let (_rest, value): (&[u8], Enum) = |
| 542 | from_slice_stream_options(&v[..], Options::default().no_standard()).unwrap(); |
| 543 | assert_eq!(value, Enum::Unit); |
| 544 | let value: error::Result<(&[u8], Enum)> = |
| 545 | from_slice_stream_options(&v[..], Options::default().no_legacy().no_standard()); |
| 546 | assert_eq!( |
| 547 | value.unwrap_err().classify(), |
| 548 | serde_cbor_2::error::Category::Syntax |
| 549 | ); |
| 550 | |
| 551 | // This is the format used in serde <= 0.9 |
| 552 | let v: Vec<u8> = vec![ |
| 553 | 0x82, // array 2 items |
| 554 | 0x67, 0x4e, 0x65, 0x77, 0x54, 0x79, 0x70, 0x65, // utf8 string: NewType |
| 555 | 0x1a, // u32 |
| 556 | 0x00, 0x00, 0x00, 0x0a, // 10 (dec) |
| 557 | ]; |
| 558 | let (_rest, value): (&[u8], Enum) = from_slice_stream(&v[..]).unwrap(); |
| 559 | assert_eq!(value, Enum::NewType(10)); |
| 560 | let value: error::Result<(&[u8], Enum)> = |
| 561 | from_slice_stream_options(&v[..], Options::default().no_legacy()); |
| 562 | assert_eq!( |
| 563 | value.unwrap_err().classify(), |
| 564 | serde_cbor_2::error::Category::Syntax |
| 565 | ); |
| 566 | let value: error::Result<(&[u8], Enum)> = |
| 567 | from_slice_stream_options(&v[..], Options::default().no_standard()); |
| 568 | assert_eq!(value.unwrap().1, Enum::NewType(10)); |
| 569 | let value: error::Result<(&[u8], Enum)> = |
| 570 | from_slice_stream_options(&v[..], Options::default().no_standard().no_legacy()); |
| 571 | assert_eq!( |
| 572 | value.unwrap_err().classify(), |
| 573 | serde_cbor_2::error::Category::Syntax |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | #[test] |
| 578 | fn test_packed_deserialization() { |
| 579 | #[derive(Debug, PartialEq, Deserialize)] |
| 580 | struct User { |
| 581 | user_id: u32, |
| 582 | password_hash: [u8; 4], |
| 583 | } |
| 584 | |
| 585 | // unpacked |
| 586 | let v: Vec<u8> = vec![ |
| 587 | 0xa2, // map 2pair |
| 588 | 0x67, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, // utf8 string: user_id |
| 589 | 0x0a, // integer: 10 |
| 590 | // utf8 string: password_hash |
| 591 | 0x6d, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x68, 0x61, 0x73, 0x68, |
| 592 | 0x84, 0x01, 0x02, 0x03, 0x04, // 4 byte array [1, 2, 3, 4] |
| 593 | ]; |
| 594 | |
| 595 | let (_rest, value): (&[u8], User) = from_slice_stream(&v[..]).unwrap(); |
| 596 | assert_eq!( |
| 597 | value, |
| 598 | User { |
| 599 | user_id: 10, |
| 600 | password_hash: [1, 2, 3, 4], |
| 601 | } |
| 602 | ); |
| 603 | let (_rest, value): (&[u8], User) = |
| 604 | from_slice_stream_options(&v[..], Options::default().no_packed()).unwrap(); |
| 605 | assert_eq!( |
| 606 | value, |
| 607 | User { |
| 608 | user_id: 10, |
| 609 | password_hash: [1, 2, 3, 4], |
| 610 | } |
| 611 | ); |
| 612 | let value: error::Result<(&[u8], User)> = |
| 613 | from_slice_stream_options(&v[..], Options::default().no_named()); |
| 614 | assert_eq!( |
| 615 | value.unwrap_err().classify(), |
| 616 | serde_cbor_2::error::Category::Syntax |
| 617 | ); |
| 618 | |
| 619 | // unpacked - indefinite length |
| 620 | let v: Vec<u8> = vec![ |
| 621 | 0xbf, // map to be followed by a break |
| 622 | 0x67, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, // utf8 string: user_id |
| 623 | 0x0a, // integer: 10 |
| 624 | // utf8 string: password_hash |
| 625 | 0x6d, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x68, 0x61, 0x73, 0x68, |
| 626 | 0x84, 0x01, 0x02, 0x03, 0x04, // 4 byte array [1, 2, 3, 4] |
| 627 | 0xff, // break |
| 628 | ]; |
| 629 | |
| 630 | let (_rest, value): (&[u8], User) = from_slice_stream(&v[..]).unwrap(); |
| 631 | assert_eq!( |
| 632 | value, |
| 633 | User { |
| 634 | user_id: 10, |
| 635 | password_hash: [1, 2, 3, 4], |
| 636 | } |
| 637 | ); |
| 638 | let (_rest, value): (&[u8], User) = |
| 639 | from_slice_stream_options(&v[..], Options::default().no_packed()).unwrap(); |
| 640 | assert_eq!( |
| 641 | value, |
| 642 | User { |
| 643 | user_id: 10, |
| 644 | password_hash: [1, 2, 3, 4], |
| 645 | } |
| 646 | ); |
| 647 | let value: error::Result<(&[u8], User)> = |
| 648 | from_slice_stream_options(&v[..], Options::default().no_named()); |
| 649 | assert_eq!( |
| 650 | value.unwrap_err().classify(), |
| 651 | serde_cbor_2::error::Category::Syntax |
| 652 | ); |
| 653 | |
| 654 | // packed |
| 655 | let v: Vec<u8> = vec![ |
| 656 | 0xa2, // map 2pair |
| 657 | 0x00, // index 0 |
| 658 | 0x0a, // integer: 10 |
| 659 | 0x01, // index 1 |
| 660 | 0x84, 0x01, 0x02, 0x03, 0x04, // 4 byte array [1, 2, 3, 4] |
| 661 | ]; |
| 662 | |
| 663 | let (_rest, value): (&[u8], User) = from_slice_stream(&v[..]).unwrap(); |
| 664 | assert_eq!( |
| 665 | value, |
| 666 | User { |
| 667 | user_id: 10, |
| 668 | password_hash: [1, 2, 3, 4], |
| 669 | } |
| 670 | ); |
| 671 | let (_rest, value): (&[u8], User) = |
| 672 | from_slice_stream_options(&v[..], Options::default().no_named()).unwrap(); |
| 673 | assert_eq!( |
| 674 | value, |
| 675 | User { |
| 676 | user_id: 10, |
| 677 | password_hash: [1, 2, 3, 4], |
| 678 | } |
| 679 | ); |
| 680 | let value: error::Result<(&[u8], User)> = |
| 681 | from_slice_stream_options(&v[..], Options::default().no_packed()); |
| 682 | assert_eq!( |
| 683 | value.unwrap_err().classify(), |
| 684 | serde_cbor_2::error::Category::Syntax |
| 685 | ); |
| 686 | |
| 687 | // packed - indefinite length |
| 688 | let v: Vec<u8> = vec![ |
| 689 | 0xbf, // map, to be followed by a break |
| 690 | 0x00, // index 0 |
| 691 | 0x0a, // integer: 10 |
| 692 | 0x01, // index 1 |
| 693 | 0x84, 0x01, 0x02, 0x03, 0x04, // 4 byte array [1, 2, 3, 4] |
| 694 | 0xff, // break |
| 695 | ]; |
| 696 | |
| 697 | let (_rest, value): (&[u8], User) = from_slice_stream(&v[..]).unwrap(); |
| 698 | assert_eq!( |
| 699 | value, |
| 700 | User { |
| 701 | user_id: 10, |
| 702 | password_hash: [1, 2, 3, 4], |
| 703 | } |
| 704 | ); |
| 705 | let (_rest, value): (&[u8], User) = |
| 706 | from_slice_stream_options(&v[..], Options::default().no_named()).unwrap(); |
| 707 | assert_eq!( |
| 708 | value, |
| 709 | User { |
| 710 | user_id: 10, |
| 711 | password_hash: [1, 2, 3, 4], |
| 712 | } |
| 713 | ); |
| 714 | let value: error::Result<(&[u8], User)> = |
| 715 | from_slice_stream_options(&v[..], Options::default().no_packed()); |
| 716 | assert_eq!( |
| 717 | value.unwrap_err().classify(), |
| 718 | serde_cbor_2::error::Category::Syntax |
| 719 | ); |
| 720 | } |
| 721 | |
| 722 | use serde_cbor_2::{de::from_slice, ser::to_vec_packed}; |
| 723 | use std::net::{IpAddr, Ipv4Addr}; |
| 724 | #[test] |
| 725 | fn test_ipaddr_deserialization() { |
| 726 | let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); |
| 727 | let buf = to_vec_packed(&ip).unwrap(); |
| 728 | let deserialized_ip = from_slice::<IpAddr>(&buf).unwrap(); |
| 729 | assert_eq!(ip, deserialized_ip); |
| 730 | |
| 731 | let buf = to_vec(&ip).unwrap(); |
| 732 | let deserialized_ip = from_slice::<IpAddr>(&buf).unwrap(); |
| 733 | assert_eq!(ip, deserialized_ip); |
| 734 | } |
| 735 | |
| 736 | #[test] |
| 737 | fn attempt_stack_overflow() { |
| 738 | // Create a tag 17, followed by 999 more tag 17: |
| 739 | // 17(17(17(17(17(17(17(17(17(17(17(17(17(17(17(17(17(17(... |
| 740 | // This causes deep recursion in the decoder and may |
| 741 | // exhaust the stack and therfore result in a stack overflow. |
| 742 | let input = vec![0xd1; 1000]; |
| 743 | let err = |
| 744 | serde_cbor_2::from_slice::<serde_cbor_2::Value>(&input).expect_err("recursion limit"); |
| 745 | assert!(err.is_syntax()); |
| 746 | assert!(err.offset() > 0); |
| 747 | |
| 748 | assert!(!err.is_io()); |
| 749 | assert!(!err.is_eof()); |
| 750 | assert!(!err.is_data()); |
| 751 | assert!(!err.is_scratch_too_small()); |
| 752 | assert!(err.is_syntax()); |
| 753 | } |
| 754 | } |