add testing and docs to key_store.
@@ -12,14 +12,17 @@ pub struct KeyStore<P: BoxProvider + Clone + Send + Sync + 'static> {
}
impl<P: BoxProvider + Clone + Send + Sync + 'static> KeyStore<P> {
+ /// Creates a new `KeyStore`.
pub fn new() -> Self {
Self { store: HashMap::new() }
}
+ /// gets the key from the `KeyStore` by removing it. Returns an `Option<Key<P>>`
pub fn get_key(&mut self, id: VaultId) -> Option<Key<P>> {
self.store.remove(&id)
}
+ /// Creates a new key for the `VaultId` and returns it.
pub fn create_key(&mut self, id: VaultId) -> Key<P> {
let key = self
.store
@@ -29,10 +32,12 @@ impl<P: BoxProvider + Clone + Send + Sync + 'static> KeyStore<P> {
key.clone()
}
- pub fn insert_key(&mut self, id: VaultId, key: Key<P>) {
- self.store.entry(id).or_insert(key);
+ /// Inserts a key into the `KeyStore` by `VaultId`. If the `VaultId` already exists, it just returns the existing `&Key<P>`
+ pub fn insert_key(&mut self, id: VaultId, key: Key<P>) -> &Key<P> {
+ self.store.entry(id).or_insert(key)
}
+ /// Rebuilds the `KeyStore` while throwing out any existing `VauldId`, `Key<P>` pairs. Accepts a `Vec<Key<P>>` and returns the a `Vec<VaultId>` containing all of the new `VaultId`s
pub fn rebuild_keystore(&mut self, keys: Vec<Key<P>>) -> Vec<VaultId> {
let mut store: HashMap<VaultId, Key<P>> = HashMap::new();
let mut id_buffer: Vec<VaultId> = Vec::new();
@@ -49,3 +54,51 @@ impl<P: BoxProvider + Clone + Send + Sync + 'static> KeyStore<P> {
id_buffer
}
}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+ use crate::provider::Provider;
+
+ #[test]
+ fn test_keystore() {
+ let vid0 = VaultId::random::<Provider>().expect(line_error!());
+ let vid1 = VaultId::random::<Provider>().expect(line_error!());
+ let key0 = Key::<Provider>::random().expect(line_error!());
+
+ let mut key_store = KeyStore::<Provider>::new();
+
+ let key = key_store.create_key(vid0);
+ let inner_key = key_store.get_key(vid0).expect(line_error!());
+
+ assert_eq!(key, inner_key);
+ assert!(!key_store.store.contains_key(&vid0));
+
+ key_store.insert_key(vid0, key);
+
+ assert!(key_store.store.contains_key(&vid0));
+
+ key_store.insert_key(vid1, key0.clone());
+
+ assert!(key_store.store.contains_key(&vid1));
+
+ let inserted_key = key_store.insert_key(vid0, key0.clone());
+
+ assert_ne!(inserted_key, &key0);
+
+ let mut key_vec: Vec<Key<Provider>> = Vec::new();
+ for _ in 0..10 {
+ key_vec.push(Key::<Provider>::random().expect(line_error!()));
+ }
+
+ let vault_vec = key_store.rebuild_keystore(key_vec);
+
+ assert_eq!(vault_vec.len(), 10);
+ assert!(!vault_vec.contains(&vid0));
+ assert!(!vault_vec.contains(&vid1));
+
+ for v in vault_vec.iter() {
+ assert!(key_store.store.contains_key(v));
+ }
+ }
+}
