use super::TempFile;
use crate::errors::ErrorKind;
use crate::{KeySource, SecretsManager};
#[test]
fn basic_store_get() {
let secrets_path = TempFile::new();
let mut sman = SecretsManager::new(KeySource::Password("mysecret")).unwrap();
sman.set("foo", "bar");
sman.set("foo", "bar".to_string());
sman.save_as(&secrets_path).unwrap();
let getd: String = sman.get("foo").unwrap();
assert_eq!("bar", getd);
let sman2 = SecretsManager::load(&secrets_path, KeySource::Password("mysecret")).unwrap();
let getd: String = sman2.get("foo").unwrap();
assert_eq!("bar", getd);
}
#[test]
fn wrong_password() {
let secrets_path = TempFile::new();
let mut sman = SecretsManager::new(KeySource::Password("mysecret")).unwrap();
sman.set("foo", "foo");
sman.save_as(&secrets_path).unwrap();
match SecretsManager::load(&secrets_path, KeySource::Password("notmysecret")) {
Ok(_) => panic!("Sentinel failed to detect wrong password on load"),
Err(e) => {
assert_eq!(ErrorKind::DecryptionFailure, e.kind());
}
}
}
#[test]
fn secret_not_found() {
let sman = SecretsManager::new(KeySource::Csprng).unwrap();
assert_eq!(Err(ErrorKind::SecretNotFound.into()), sman.get("foo"));
}
#[test]
fn csprng_export() {
let secrets_path = TempFile::new();
let key_path = TempFile::new();
{
let mut sman = SecretsManager::new(KeySource::Csprng).unwrap();
sman.export_key(&key_path).unwrap();
sman.set("foo", "bar");
sman.save_as(&secrets_path).unwrap();
}
let sman = SecretsManager::load(secrets_path, KeySource::File(key_path)).unwrap();
assert_eq!(Ok("bar".to_owned()), sman.get("foo"));
}
#[test]
fn password_export() {
let secrets_path = TempFile::new();
let key_path = TempFile::new();
{
let mut sman = SecretsManager::new(KeySource::Password("password123")).unwrap();
sman.export_keyfile(&key_path).unwrap();
sman.set("foo", "bar");
sman.save_as(&secrets_path).unwrap();
}
let sman = SecretsManager::load(secrets_path, KeySource::File(key_path)).unwrap();
assert_eq!(Ok("bar".to_owned()), sman.get("foo"));
}
#[test]
fn invalid_key_file() {
let key_path = TempFile::new();
match SecretsManager::new(KeySource::File(key_path)) {
Ok(_) => panic!("SecretsManager loaded with invalid key file!"),
Err(e) => assert_eq!(ErrorKind::InvalidKeyfile, e.kind()),
}
}
#[test]
fn binary_secret() {
let mut sman = SecretsManager::new(KeySource::Csprng).unwrap();
let (key, value) = ("secret", b"Hello, world!");
sman.set(key, &value[..]);
assert_eq!(&value[..], sman.get_as::<Vec<u8>>(key).unwrap().as_slice());
}
#[test]
fn legacy_generic_keysource() {
let _ = SecretsManager::load("secrets.json", KeySource::File("secrets.key"));
}
#[test]
fn str_as_generic_keysource() {
let _ = SecretsManager::load("secrets.json", "secrets.key");
}
#[test]
fn password_derive() {
let password = b"password123";
let salt = [0u8; 16];
let mut output = [0u8; 20];
crate::crypto::pbkdf2_hmac_sha1(
password,
&salt,
crate::shared::PBKDF2_ROUNDS as u32,
&mut output,
);
let expected = radix64::STD.decode("U5GGvWc15H96NUzIF1cDdy82mJU=").unwrap();
assert_eq!(output.as_slice(), &expected);
}