summaryrefslogtreecommitdiffstats
path: root/vendor/ciborium/tests/error.rs
blob: 13bcd5ac5a163b382da13d9b9c77e7abcdeb5207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: Apache-2.0

use ciborium::{
    de::{from_reader, Error},
    ser::into_writer,
    value::Value,
};
use rstest::rstest;

#[rstest(bytes, error,
    // Invalid value
    case("1e", Error::Syntax(0)),

    // Indeterminate integers are invalid
    case("1f", Error::Syntax(0)),

    // Indeterminate integer in an array
    case("83011f03", Error::Syntax(2)),

    // Integer in a string continuation
    case("7F616101FF", Error::Syntax(3)),

    // Bytes in a string continuation
    case("7F61614101FF", Error::Syntax(3)),

    // Invalid UTF-8
    case("62C328", Error::Syntax(0)),

    // Invalid UTF-8 in a string continuation
    case("7F62C328FF", Error::Syntax(1)),
)]
fn test(bytes: &str, error: Error<std::io::Error>) {
    let bytes = hex::decode(bytes).unwrap();

    let correct = match error {
        Error::Io(..) => panic!(),
        Error::Syntax(x) => ("syntax", Some(x), None),
        Error::Semantic(x, y) => ("semantic", x, Some(y)),
        Error::RecursionLimitExceeded => panic!(),
    };

    let result: Result<Value, _> = from_reader(&bytes[..]);
    let actual = match result.unwrap_err() {
        Error::Io(..) => panic!(),
        Error::Syntax(x) => ("syntax", Some(x), None),
        Error::Semantic(x, y) => ("semantic", x, Some(y)),
        Error::RecursionLimitExceeded => panic!(),
    };

    assert_eq!(correct, actual);
}

#[test]
fn test_long_utf8_deserialization() {
    let s = (0..2000).map(|_| 'ボ').collect::<String>();
    let mut v = Vec::new();
    into_writer(&s, &mut v).unwrap();
    let _: String = from_reader(&*v).unwrap();
}