Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / third_party / betocore / np_adv / src / credential / tests.rs

tests.rs175 lines · 6.4 KiBRaw
1// Copyright 2023 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14//! Tests of functionality related to credentials, credential-views, and credential suppliers.
15
16extern crate alloc;
17
18use crate::credential::matched::{
19 EmptyMatchedCredential, KeySeedMatchedCredential, MatchedCredentialWithId,
20 ReferencedMatchedCredential,
21};
22use crate::credential::v1::MicSectionVerificationMaterial;
23use crate::credential::{
24 book::{
25 init_cache_from_source, CachedCredentialSource, CredentialBook, CredentialBookBuilder,
26 PossiblyCachedDiscoveryCryptoMaterialKind,
27 },
28 source::{CredentialSource, SliceCredentialSource},
29 v0::{V0DiscoveryCredential, V0},
30 v1::{V1BroadcastCredential, V1DiscoveryCredential, V1DiscoveryCryptoMaterial, V1},
31 MatchableCredential, MatchedCredential,
32};
33use crate::extended::{V1IdentityToken, V1_IDENTITY_TOKEN_LEN};
34use alloc::vec;
35use alloc::vec::Vec;
36use crypto_provider_default::CryptoProviderImpl;
37
38fn get_zeroed_v0_discovery_credential() -> V0DiscoveryCredential {
39 V0DiscoveryCredential::new([0u8; 32], [0u8; 32])
40}
41
42fn get_constant_packed_v1_discovery_credential(value: u8) -> V1DiscoveryCredential {
43 V1BroadcastCredential::new([value; 32], V1IdentityToken::from([value; V1_IDENTITY_TOKEN_LEN]))
44 .derive_discovery_credential::<CryptoProviderImpl>()
45}
46
47#[test]
48fn cached_credential_source_keeps_same_entries_as_original() {
49 let creds: [MatchableCredential<V1, KeySeedMatchedCredential>; 5] =
50 [0u8, 1, 2, 3, 4].map(|x| {
51 let match_data = KeySeedMatchedCredential::from([x; 32]);
52 MatchableCredential {
53 discovery_credential: get_constant_packed_v1_discovery_credential(x),
54 match_data,
55 }
56 });
57 let supplier = SliceCredentialSource::new(&creds);
58 let cache = init_cache_from_source::<_, _, 3, CryptoProviderImpl>(&supplier);
59 let cached = CachedCredentialSource::new(supplier, cache);
60 let cached_view = &cached;
61 assert_eq!(cached_view.iter().count(), 5);
62 // Now we're going to check that the pairings between the match-data
63 // and the MIC hmac key wind up being the same between the original
64 // creds list and what's provided by the cached source.
65 let expected: Vec<_> = creds
66 .iter()
67 .map(|cred| {
68 (
69 *cred
70 .discovery_credential
71 .mic_extended_salt_verification_material::<CryptoProviderImpl>()
72 .mic_hmac_key()
73 .as_bytes(),
74 ReferencedMatchedCredential::from(&cred.match_data),
75 )
76 })
77 .collect();
78 let actual: Vec<_> = cached_view
79 .iter()
80 .map(|(crypto_material, match_data)| {
81 (
82 *crypto_material
83 .mic_extended_salt_verification_material::<CryptoProviderImpl>()
84 .mic_hmac_key()
85 .as_bytes(),
86 match_data,
87 )
88 })
89 .collect();
90 assert_eq!(actual, expected);
91}
92
93#[test]
94fn cached_credential_source_has_requested_cache_size() {
95 let creds: [MatchableCredential<V0, EmptyMatchedCredential>; 10] =
96 [0u8; 10].map(|_| MatchableCredential {
97 discovery_credential: get_zeroed_v0_discovery_credential(),
98 match_data: EmptyMatchedCredential,
99 });
100 let supplier = SliceCredentialSource::new(&creds);
101 let cache = init_cache_from_source::<_, _, 5, CryptoProviderImpl>(&supplier);
102 let cached = CachedCredentialSource::new(supplier, cache);
103 let cached_view = &cached;
104 assert_eq!(cached_view.iter().count(), 10);
105 for (i, (cred, _)) in cached_view.iter().enumerate() {
106 if i < 5 {
107 // Should be cached
108 if let PossiblyCachedDiscoveryCryptoMaterialKind::Precalculated(_) = cred.wrapped {
109 } else {
110 panic!("Credential #{} was not cached", i);
111 }
112 } else {
113 // Should be discovery credentials
114 if let PossiblyCachedDiscoveryCryptoMaterialKind::Discovery(_) = cred.wrapped {
115 } else {
116 panic!("Credential #{} was not supposed to be cached", i);
117 }
118 }
119 }
120}
121
122#[test]
123fn precalculated_owned_book_with_id_iterates_correctly() {
124 let v0_creds: [MatchableCredential<V0, MatchedCredentialWithId<Vec<u8>>>; 2] =
125 [0, 1].map(|i| {
126 let match_data = MatchedCredentialWithId::new(vec![i], i as i64);
127 MatchableCredential {
128 discovery_credential: get_zeroed_v0_discovery_credential(),
129 match_data,
130 }
131 });
132 let v1_creds: [MatchableCredential<V1, MatchedCredentialWithId<Vec<u8>>>; 3] =
133 [2, 3, 4].map(|i| {
134 let match_data = MatchedCredentialWithId::new(vec![i], i as i64);
135 MatchableCredential {
136 discovery_credential: get_constant_packed_v1_discovery_credential(i),
137 match_data,
138 }
139 });
140
141 let v0_expected: Vec<_> = v0_creds
142 .iter()
143 .map(|c| (c.match_data.id(), c.match_data.fetch_encrypted_metadata().unwrap()))
144 .collect();
145
146 let v1_expected: Vec<_> = v1_creds
147 .iter()
148 .map(|c| (c.match_data.id(), c.match_data.fetch_encrypted_metadata().unwrap()))
149 .collect();
150
151 let book = CredentialBookBuilder::build_precalculated_owned_book_with_id::<CryptoProviderImpl>(
152 v0_creds, v1_creds,
153 );
154
155 let v0_from_book: Vec<_> =
156 book.v0_iter().map(|(_, m)| (m.id(), m.fetch_encrypted_metadata().unwrap())).collect();
157
158 assert_eq!(v0_from_book, v0_expected);
159
160 let v1_from_book: Vec<_> =
161 book.v1_iter().map(|(_, m)| (m.id(), m.fetch_encrypted_metadata().unwrap())).collect();
162
163 assert_eq!(v1_from_book, v1_expected);
164}
165
166mod coverage_gaming {
167 use crate::credential::MetadataDecryptionError;
168 use alloc::format;
169
170 #[test]
171 fn metadata_decryption_error_debug() {
172 let err = MetadataDecryptionError;
173 let _ = format!("{err:?}");
174 }
175}