1 // SPDX-License-Identifier: Apache-2.0
2 
3 use ciborium::{
4     de::{from_reader, Error},
5     ser::into_writer,
6     value::Value,
7 };
8 use rstest::rstest;
9 
10 #[rstest(bytes, error,
11     // Invalid value
12     case("1e", Error::Syntax(0)),
13 
14     // Indeterminate integers are invalid
15     case("1f", Error::Syntax(0)),
16 
17     // Indeterminate integer in an array
18     case("83011f03", Error::Syntax(2)),
19 
20     // Integer in a string continuation
21     case("7F616101FF", Error::Syntax(3)),
22 
23     // Bytes in a string continuation
24     case("7F61614101FF", Error::Syntax(3)),
25 
26     // Invalid UTF-8
27     case("62C328", Error::Syntax(0)),
28 
29     // Invalid UTF-8 in a string continuation
30     case("7F62C328FF", Error::Syntax(1)),
31 )]
test(bytes: &str, error: Error<std::io::Error>)32 fn test(bytes: &str, error: Error<std::io::Error>) {
33     let bytes = hex::decode(bytes).unwrap();
34 
35     let correct = match error {
36         Error::Io(..) => panic!(),
37         Error::Syntax(x) => ("syntax", Some(x), None),
38         Error::Semantic(x, y) => ("semantic", x, Some(y)),
39         Error::RecursionLimitExceeded => panic!(),
40     };
41 
42     let result: Result<Value, _> = from_reader(&bytes[..]);
43     let actual = match result.unwrap_err() {
44         Error::Io(..) => panic!(),
45         Error::Syntax(x) => ("syntax", Some(x), None),
46         Error::Semantic(x, y) => ("semantic", x, Some(y)),
47         Error::RecursionLimitExceeded => panic!(),
48     };
49 
50     assert_eq!(correct, actual);
51 }
52 
53 #[test]
test_long_utf8_deserialization()54 fn test_long_utf8_deserialization() {
55     let s = (0..2000).map(|_| 'ボ').collect::<String>();
56     let mut v = Vec::new();
57     into_writer(&s, &mut v).unwrap();
58     let _: String = from_reader(&*v).unwrap();
59 }
60