xref: /aosp_15_r20/system/keymint/derive/tests/integration_test.rs (revision 9860b7637a5f185913c70aa0caabe3ecb78441e4)
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 kmr_derive::AsCborValue;
16 use kmr_wire::{cbor_type_error, AsCborValue, CborError};
17 
18 #[derive(Clone, Debug, PartialEq, Eq, AsCborValue)]
19 struct NamedFields {
20     i: i32,
21     s: String,
22 }
23 
24 #[test]
test_derive_named_struct_roundtrip()25 fn test_derive_named_struct_roundtrip() {
26     let want = NamedFields { i: 42, s: "a string".to_string() };
27     let want_value = want.clone().to_cbor_value().unwrap();
28     let got = NamedFields::from_cbor_value(want_value).unwrap();
29     assert_eq!(want, got);
30     assert_eq!(NamedFields::cddl_typename().unwrap(), "NamedFields");
31     assert_eq!(NamedFields::cddl_schema().unwrap(), "[\n    i: int,\n    s: tstr,\n]");
32 }
33 
34 #[derive(Clone, Debug, PartialEq, Eq, AsCborValue)]
35 struct UnnamedFields(i32, String);
36 
37 #[test]
test_derive_unnamed_struct_roundtrip()38 fn test_derive_unnamed_struct_roundtrip() {
39     let want = UnnamedFields(42, "a string".to_string());
40     let want_value = want.clone().to_cbor_value().unwrap();
41     let got = UnnamedFields::from_cbor_value(want_value).unwrap();
42     assert_eq!(want, got);
43     assert_eq!(UnnamedFields::cddl_typename().unwrap(), "UnnamedFields");
44     assert_eq!(UnnamedFields::cddl_schema().unwrap(), "[\n    int,\n    tstr,\n]");
45 }
46 
47 #[derive(Clone, Debug, PartialEq, Eq, AsCborValue)]
48 enum NumericEnum {
49     One = 1,
50     Two = 2,
51     Three = 3,
52 }
53 
54 #[test]
test_derive_numeric_enum_roundtrip()55 fn test_derive_numeric_enum_roundtrip() {
56     let want = NumericEnum::Two;
57     let want_value = want.clone().to_cbor_value().unwrap();
58     let got = NumericEnum::from_cbor_value(want_value).unwrap();
59     assert_eq!(want, got);
60     assert_eq!(NumericEnum::cddl_typename().unwrap(), "NumericEnum");
61     assert_eq!(
62         NumericEnum::cddl_schema().unwrap(),
63         "&(\n    NumericEnum_One: 1,\n    NumericEnum_Two: 2,\n    NumericEnum_Three: 3,\n)"
64     );
65 }
66