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