1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use super::*;
16 use crate::cbor::value::Value;
17 use alloc::vec;
18
19 #[test]
test_read_to_value_ok()20 fn test_read_to_value_ok() {
21 let tests = vec![
22 ("01", Value::Integer(1.into())),
23 ("40", Value::Bytes(vec![])),
24 ("60", Value::Text(String::new())),
25 ];
26 for (hexdata, want) in tests {
27 let data = hex::decode(hexdata).unwrap();
28 let got = read_to_value(&data).unwrap();
29 assert_eq!(got, want, "failed for {}", hexdata);
30 }
31 }
32
33 #[test]
test_read_to_value_fail()34 fn test_read_to_value_fail() {
35 let tests = vec![
36 ("0101", CborError::ExtraneousData),
37 ("43", CborError::DecodeFailed(cbor::de::Error::Io(EndOfFile))),
38 ("8001", CborError::ExtraneousData),
39 ];
40 for (hexdata, want_err) in tests {
41 let data = hex::decode(hexdata).unwrap();
42 let got_err = read_to_value(&data).expect_err("decoding expected to fail");
43 assert_eq!(format!("{:?}", got_err), format!("{:?}", want_err), "failed for {}", hexdata);
44 }
45 }
46