• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0
2 
3 use ciborium::{de::from_reader, de::Error, value::Value};
4 use rstest::rstest;
5 
6 #[rstest(bytes, error,
7     // Invalid value
8     case("1e", Error::Syntax(0)),
9 
10     // Indeterminate integers are invalid
11     case("1f", Error::Syntax(0)),
12 
13     // Indeterminate integer in an array
14     case("83011f03", Error::Syntax(2)),
15 
16     // Integer in a string continuation
17     case("7F616101FF", Error::Syntax(3)),
18 
19     // Bytes in a string continuation
20     case("7F61614101FF", Error::Syntax(3)),
21 
22     // Invalid UTF-8
23     case("62C328", Error::Syntax(0)),
24 
25     // Invalid UTF-8 in a string continuation
26     case("7F62C328FF", Error::Syntax(1)),
27 )]
test(bytes: &str, error: Error<std::io::Error>)28 fn test(bytes: &str, error: Error<std::io::Error>) {
29     let bytes = hex::decode(bytes).unwrap();
30 
31     let correct = match error {
32         Error::Io(..) => panic!(),
33         Error::Syntax(x) => ("syntax", Some(x), None),
34         Error::Semantic(x, y) => ("semantic", x, Some(y)),
35         Error::RecursionLimitExceeded => panic!(),
36     };
37 
38     let result: Result<Value, _> = from_reader(&bytes[..]);
39     let actual = match result.unwrap_err() {
40         Error::Io(..) => panic!(),
41         Error::Syntax(x) => ("syntax", Some(x), None),
42         Error::Semantic(x, y) => ("semantic", x, Some(y)),
43         Error::RecursionLimitExceeded => panic!(),
44     };
45 
46     assert_eq!(correct, actual);
47 }
48