xref: /aosp_15_r20/system/secretkeeper/comm/tests/wire.rs (revision 3f8e9d82f4020c68ad19a99fc5fdc1fc90b79379)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! Unit tests for testing serialization and deserialization of internal types.
18 
19 use ciborium::value::Value;
20 use coset::{AsCborValue, CborSerializable};
21 use rdroidtest::rdroidtest;
22 use secretkeeper_comm::wire::{
23     AidlErrorCode, ApiError, OpCode, PerformOpReq, PerformOpResponse, PerformOpSuccessRsp,
24 };
25 
26 #[rdroidtest]
wire_req_roundtrip()27 fn wire_req_roundtrip() {
28     let tests = [
29         PerformOpReq::SecretManagement(vec![]),
30         PerformOpReq::SecretManagement(vec![1, 2, 3]),
31         PerformOpReq::DeleteIds(vec![]),
32         PerformOpReq::DeleteIds(vec![[1; 64], [2; 64]]),
33         PerformOpReq::DeleteAll,
34     ];
35 
36     for input in tests {
37         let data = input.clone().to_vec().unwrap();
38         let recovered = PerformOpReq::from_slice(&data).unwrap();
39         assert_eq!(input, recovered);
40     }
41 }
42 
43 #[rdroidtest]
wire_rsp_roundtrip()44 fn wire_rsp_roundtrip() {
45     let tests = [
46         PerformOpResponse::Success(PerformOpSuccessRsp::ProtectedResponse(vec![])),
47         PerformOpResponse::Success(PerformOpSuccessRsp::ProtectedResponse(vec![1, 2, 3])),
48         PerformOpResponse::Success(PerformOpSuccessRsp::Empty),
49         PerformOpResponse::Failure(ApiError {
50             err_code: AidlErrorCode::InternalError,
51             msg: "msg".to_string(),
52         }),
53     ];
54 
55     for input in tests {
56         let data = input.clone().to_vec().unwrap();
57         let recovered = PerformOpResponse::from_slice(&data).unwrap();
58         assert_eq!(input, recovered);
59     }
60 }
61 
62 #[rdroidtest]
wire_req_deserialize_fail()63 fn wire_req_deserialize_fail() {
64     let bogus_data = [0x99, 0x99];
65     let result = PerformOpReq::from_slice(&bogus_data);
66     assert!(result.is_err());
67 }
68 
69 #[rdroidtest]
wire_rsp_deserialize_fail()70 fn wire_rsp_deserialize_fail() {
71     let bogus_data = [0x99, 0x99];
72     let result = PerformOpReq::from_slice(&bogus_data);
73     assert!(result.is_err());
74 }
75 
76 #[rdroidtest]
wire_opcode_out_of_range()77 fn wire_opcode_out_of_range() {
78     let bignum = Value::Integer(999.into());
79     let result = OpCode::from_cbor_value(bignum);
80     assert!(result.is_err());
81 }
82 
83 #[rdroidtest]
wire_rsp_errcode_out_of_range()84 fn wire_rsp_errcode_out_of_range() {
85     let bogus_data = [
86         0x82, // 2-arr
87         0x19, 0x03, 0xe7, // int, value 999
88         0x60, // 0-tstr
89     ];
90     // Invalid error codes get mapped to `InternalError`.
91     let result = PerformOpResponse::from_slice(&bogus_data).unwrap();
92     assert_eq!(
93         result,
94         PerformOpResponse::Failure(ApiError {
95             err_code: AidlErrorCode::InternalError,
96             msg: "".to_string(),
97         })
98     );
99 }
100