tests and fixes

This commit is contained in:
Nikolay Volf 2016-02-17 11:48:12 +03:00
parent d63f13245f
commit 14b02ff26f
2 changed files with 69 additions and 2 deletions

View File

@ -546,6 +546,24 @@ impl KeyDirectory {
Ok(()) Ok(())
} }
/// Enumerates all keys in the directory
pub fn list(&self) -> Result<Vec<Uuid>, ::std::io::Error> {
let mut result = Vec::new();
for entry in try!(fs::read_dir(&self.path)) {
let entry = try!(entry);
if !try!(fs::metadata(entry.path())).is_dir() {
match entry.file_name().to_str() {
Some(ref name) => {
if let Ok(uuid) = uuid_from_string(name) { result.push(uuid); }
},
None => { continue; }
};
}
}
Ok(result)
}
fn key_path(&self, id: &Uuid) -> PathBuf { fn key_path(&self, id: &Uuid) -> PathBuf {
let mut path = PathBuf::new(); let mut path = PathBuf::new();
path.push(self.path.clone()); path.push(self.path.clone());
@ -1130,4 +1148,19 @@ mod specs {
assert_eq!(10, keys.len()) assert_eq!(10, keys.len())
} }
#[test]
fn can_list_keys() {
let temp_path = RandomTempPath::create_dir();
let mut directory = KeyDirectory::new(&temp_path.as_path());
let cipher_text: Bytes = FromHex::from_hex("a0f05555").unwrap();
let mut keys = Vec::new();
for _ in 0..33 {
let key = KeyFileContent::new(KeyFileCrypto::new_pbkdf2(cipher_text.clone(), H128::zero(), H256::random(), H256::random(), 32, 32));
keys.push(directory.save(key).unwrap());
}
assert_eq!(33, directory.list().unwrap().len());
}
} }

View File

@ -145,7 +145,7 @@ impl EncryptedHashMap<H128> for SecretStore {
// KECCAK(DK[16..31] ++ <ciphertext>), where DK[16..31] - derived_right_bits // KECCAK(DK[16..31] ++ <ciphertext>), where DK[16..31] - derived_right_bits
let mac = derive_mac(&derived_right_bits, &cipher_text.clone()).sha3(); let mac = derive_mac(&derived_right_bits, &cipher_text.clone()).sha3();
let key_file = KeyFileContent::new( let mut key_file = KeyFileContent::new(
KeyFileCrypto::new_pbkdf2( KeyFileCrypto::new_pbkdf2(
cipher_text, cipher_text,
iv, iv,
@ -153,6 +153,7 @@ impl EncryptedHashMap<H128> for SecretStore {
mac, mac,
KEY_ITERATIONS, KEY_ITERATIONS,
KEY_LENGTH)); KEY_LENGTH));
key_file.id = key;
if let Err(io_error) = self.directory.save(key_file) { if let Err(io_error) = self.directory.save(key_file) {
warn!("Error saving key file: {:?}", io_error); warn!("Error saving key file: {:?}", io_error);
} }
@ -210,7 +211,10 @@ mod tests {
let temp = RandomTempPath::create_dir(); let temp = RandomTempPath::create_dir();
let mut sstore = SecretStore::new_test(&temp); let mut sstore = SecretStore::new_test(&temp);
sstore.insert(H128::random(), "Cat".to_owned(), "pass"); let id = H128::random();
sstore.insert(id.clone(), "Cat".to_owned(), "pass");
assert!(sstore.get::<String>(&id, "pass").is_ok());
} }
#[test] #[test]
@ -236,6 +240,25 @@ mod tests {
} }
} }
fn pregenerate_keys(temp: &RandomTempPath, count: usize) -> Vec<H128> {
use keys::directory::{KeyFileContent, KeyFileCrypto};
let mut write_sstore = SecretStore::new_test(&temp);
let mut result = Vec::new();
for _ in 0..count {
result.push(write_sstore.directory.save(
KeyFileContent::new(
KeyFileCrypto::new_pbkdf2(
FromHex::from_hex("5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46").unwrap(),
H128::from_str("6087dab2f9fdbbfaddc31a909735c1e6").unwrap(),
H256::from_str("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd").unwrap(),
H256::from_str("517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2").unwrap(),
262144,
32)))
.unwrap());
}
result
}
#[test] #[test]
fn secret_store_get() { fn secret_store_get() {
let temp = RandomTempPath::create_dir(); let temp = RandomTempPath::create_dir();
@ -259,5 +282,16 @@ mod tests {
} }
} }
#[test]
fn secret_store_delete() {
let temp = RandomTempPath::create_dir();
let keys = pregenerate_keys(&temp, 5);
let mut sstore = SecretStore::new_test(&temp);
sstore.delete(&keys[2]);
assert_eq!(4, sstore.directory.list().unwrap().len())
}
} }