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 use diced_open_dice::DiceArtifacts;
18 use service_vm_comm::{Csr, CsrPayload};
19 
20 /// The following test data are generated with urandom
21 const DATA1: [u8; 32] = [
22     0x8b, 0x09, 0xc0, 0x7e, 0x20, 0x3c, 0xa2, 0x11, 0x7e, 0x7f, 0x0b, 0xdd, 0x2b, 0x68, 0x98, 0xb0,
23     0x2b, 0x34, 0xb5, 0x63, 0x39, 0x01, 0x90, 0x06, 0xaf, 0x5f, 0xdd, 0xb7, 0x81, 0xca, 0xc7, 0x46,
24 ];
25 const DATA2: [u8; 16] = [
26     0x6c, 0xb9, 0x39, 0x86, 0x9b, 0x2f, 0x12, 0xd8, 0x45, 0x92, 0x57, 0x44, 0x65, 0xce, 0x94, 0x63,
27 ];
28 
29 #[test]
csr_payload_cbor_serialization()30 fn csr_payload_cbor_serialization() {
31     let csr_payload = CsrPayload { public_key: DATA1.to_vec(), challenge: DATA2.to_vec() };
32     let expected_csr_payload = csr_payload.clone();
33     let cbor_vec = csr_payload.into_cbor_vec().unwrap();
34     let deserialized_csr_payload = CsrPayload::from_cbor_slice(&cbor_vec).unwrap();
35 
36     assert_eq!(expected_csr_payload, deserialized_csr_payload);
37 }
38 
39 #[test]
csr_cbor_serialization()40 fn csr_cbor_serialization() {
41     let dice_artifacts = diced_sample_inputs::make_sample_bcc_and_cdis().unwrap();
42     let dice_cert_chain = dice_artifacts.bcc().unwrap().to_vec();
43     let csr = Csr { signed_csr_payload: DATA1.to_vec(), dice_cert_chain };
44     let expected_csr = csr.clone();
45     let cbor_vec = csr.into_cbor_vec().unwrap();
46     let deserialized_csr = Csr::from_cbor_slice(&cbor_vec).unwrap();
47 
48     assert_eq!(expected_csr, deserialized_csr);
49 }
50