• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //! In-memory store for nonsecure Secretkeeper.
18 
19 use secretkeeper_comm::data_types::error::Error;
20 use secretkeeper_core::store::KeyValueStore;
21 use std::collections::HashMap;
22 
23 /// An in-memory implementation of [`KeyValueStore`]. Please note that this is entirely for testing
24 /// purposes. Refer to the documentation of `PolicyGatedStorage` and Secretkeeper HAL for
25 /// persistence requirements.
26 #[derive(Default)]
27 pub struct InMemoryStore(HashMap<Vec<u8>, Vec<u8>>);
28 impl KeyValueStore for InMemoryStore {
store(&mut self, key: &[u8], val: &[u8]) -> Result<(), Error>29     fn store(&mut self, key: &[u8], val: &[u8]) -> Result<(), Error> {
30         // This will overwrite the value if key is already present.
31         let _ = self.0.insert(key.to_vec(), val.to_vec());
32         Ok(())
33     }
34 
get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error>35     fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Error> {
36         let optional_val = self.0.get(key);
37         Ok(optional_val.cloned())
38     }
39 
delete(&mut self, key: &[u8]) -> Result<(), Error>40     fn delete(&mut self, key: &[u8]) -> Result<(), Error> {
41         self.0.remove(key);
42         Ok(())
43     }
44 
delete_all(&mut self) -> Result<(), Error>45     fn delete_all(&mut self) -> Result<(), Error> {
46         self.0.clear();
47         Ok(())
48     }
49 }
50