1 // Copyright 2024 Google LLC
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 //! Unit tests for top level credential iteration and adv deserialization
16
17 #![allow(clippy::unwrap_used)]
18
19 use crate::{
20 credential::book::CredentialBook,
21 deserialization_arena,
22 deserialization_arena::ArenaOutOfSpace,
23 extended::serialize::{AddSectionError, AdvBuilder, AdvertisementType},
24 header::NpVersionHeader,
25 tests::deser_v1_tests::{
26 add_mic_rand_salt_to_adv, add_sig_rand_salt_to_adv, SectionConfig, TestIdentity,
27 },
28 };
29 use crypto_provider_default::CryptoProviderImpl;
30 use rand::{prelude::StdRng, seq::IteratorRandom, SeedableRng};
31
32 #[test]
v1_arena_out_of_space_error_sig()33 fn v1_arena_out_of_space_error_sig() {
34 v1_arena_out_of_space_error_encrypted_adv(
35 // Need to use many DE's to be sure we go over the arena limit
36 add_sig_rand_salt_to_adv::<StdRng, CryptoProviderImpl, 5>,
37 )
38 }
39
40 #[test]
v1_arena_out_of_space_error_mic()41 fn v1_arena_out_of_space_error_mic() {
42 v1_arena_out_of_space_error_encrypted_adv(
43 // Need to use many DE's to be sure we go over the arena limit
44 add_mic_rand_salt_to_adv::<StdRng, CryptoProviderImpl, 5>,
45 )
46 }
47
v1_arena_out_of_space_error_encrypted_adv( add_to_adv: impl for<'a> Fn( &mut StdRng, &'a TestIdentity, &mut AdvBuilder, ) -> Result<SectionConfig<'a>, AddSectionError>, )48 fn v1_arena_out_of_space_error_encrypted_adv(
49 add_to_adv: impl for<'a> Fn(
50 &mut StdRng,
51 &'a TestIdentity,
52 &mut AdvBuilder,
53 ) -> Result<SectionConfig<'a>, AddSectionError>,
54 ) {
55 let mut rng = StdRng::from_entropy();
56 let mut builder = AdvBuilder::new(AdvertisementType::Encrypted);
57 let identities =
58 crate::tests::deser_v1_tests::TestIdentities::generate::<1, _, CryptoProviderImpl>(
59 &mut rng,
60 );
61 let _ = add_to_adv(&mut rng, &identities.0[0], &mut builder);
62 let adv = builder.into_advertisement().as_slice().to_vec();
63 let cred_book = identities.build_cred_book::<CryptoProviderImpl>();
64
65 let (remaining, header) = NpVersionHeader::parse(&adv).unwrap();
66
67 let h =
68 if let NpVersionHeader::V1(v1_header) = header { Some(v1_header) } else { None }.unwrap();
69
70 let mut sections_in_processing =
71 crate::extended::deserialize::SectionsInProcessing::<'_, _>::from_advertisement_contents::<
72 CryptoProviderImpl,
73 >(h, remaining)
74 .unwrap();
75
76 // fill up allocator so we will run out of space
77 let arena = deserialization_arena!();
78 let mut allocator = arena.into_allocator();
79 let _ = allocator.allocate(250).unwrap();
80
81 let (crypto_material, match_data) = cred_book.v1_iter().choose(&mut rng).unwrap();
82
83 let res = sections_in_processing.try_decrypt_with_credential::<_, CryptoProviderImpl>(
84 &mut allocator,
85 crypto_material,
86 match_data,
87 );
88 assert_eq!(Err(ArenaOutOfSpace), res);
89 }
90
91 mod coverage_gaming {
92 use crate::{
93 array_vec::ArrayVecOption,
94 credential::matched::EmptyMatchedCredential,
95 extended::{
96 deserialize::{
97 section::intermediate::PlaintextSection, DecryptedSection, SectionDeserializeError,
98 V1AdvertisementContents, V1DeserializedSection, VerificationMode,
99 },
100 salt::MultiSalt,
101 },
102 };
103 use alloc::format;
104
105 #[test]
decrypted_section_derives()106 fn decrypted_section_derives() {
107 let d = DecryptedSection::new(
108 VerificationMode::Mic,
109 MultiSalt::Extended([0u8; 16].into()),
110 [0u8; 16].into(),
111 &[],
112 );
113 let _ = format!("{:?}", d);
114 }
115
116 #[test]
section_deserialize_error_derives()117 fn section_deserialize_error_derives() {
118 let e = SectionDeserializeError::IncorrectCredential;
119 assert_eq!(e, e);
120 let _ = format!("{:?}", e);
121 }
122
123 #[test]
adv_contents_derives()124 fn adv_contents_derives() {
125 let c: V1AdvertisementContents<'_, EmptyMatchedCredential> =
126 V1AdvertisementContents::new(ArrayVecOption::default(), 0);
127 let _ = format!("{:?}", c);
128 assert_eq!(c, c);
129 }
130
131 #[test]
deserialized_section_derives()132 fn deserialized_section_derives() {
133 let d: V1DeserializedSection<'_, EmptyMatchedCredential> =
134 V1DeserializedSection::Plaintext(PlaintextSection::new(&[]));
135 assert_eq!(d, d);
136 let _ = format!("{:?}", d);
137 }
138
139 #[test]
verification_mode_derives()140 fn verification_mode_derives() {
141 let m = VerificationMode::Signature;
142 #[allow(clippy::clone_on_copy)]
143 let _ = format!("{:?}", m.clone());
144 }
145 }
146