Skip to content

Commit

Permalink
30DaysOfTests: password addition with v2 store
Browse files Browse the repository at this point in the history
  • Loading branch information
conradkleinespel committed Aug 16, 2017
1 parent 4f60fa0 commit 918fe7c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/password/v2.rs
Expand Up @@ -204,11 +204,8 @@ impl PasswordStore {
pub fn new(master_password: SafeString) -> IoResult<PasswordStore> {
// TODO: move this elsewhere so "get_default_scrypt_params"
let salt = generate_random_salt()?;
let key = generate_encryption_key(
get_default_scrypt_params(),
master_password.deref(),
salt,
);
let key =
generate_encryption_key(get_default_scrypt_params(), master_password.deref(), salt);

Ok(PasswordStore {
key: key,
Expand Down Expand Up @@ -505,7 +502,8 @@ impl PasswordStore {
#[cfg(test)]
mod test {
use super::get_default_scrypt_params;
use password::v2::{generate_encryption_key, generate_random_iv, generate_random_salt};
use password::v2::{PasswordStore, Password, generate_encryption_key, generate_random_iv,
generate_random_salt};

#[test]
fn test_generate_random_iv_has_right_length() {
Expand All @@ -523,9 +521,30 @@ mod test {
generate_encryption_key(
get_default_scrypt_params(),
"hello world",
generate_random_salt().unwrap()
generate_random_salt().unwrap(),
).len(),
32
);
}

#[test]
fn test_create_password_store() {
let mut store = PasswordStore::new("****".into()).unwrap();

assert!(store.add_password(Password::new("name".into(), "username".into(), "password".into())).is_ok());

// need a wrap around the immutable borrow so the borrow checker is happy
{
// only the 1 password is here
let passwords = store.get_all_passwords();
assert_eq!(passwords.len(), 1);

// is had the right information
let p = passwords[0];
assert_eq!(p.name, "name");
assert_eq!(p.username, "username");
assert_eq!(p.password, "password".into());
assert_eq!(p.updated_at, p.created_at);
}
}
}
13 changes: 13 additions & 0 deletions src/safe_string.rs
Expand Up @@ -17,6 +17,7 @@ use std::ops::Drop;
use std::ops::Deref;
use serde::ser::{Serialize, Serializer};
use serde::de::{Deserialize, Deserializer, Visitor, Error};
use std::convert::Into;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SafeString {
Expand Down Expand Up @@ -61,6 +62,18 @@ impl Deref for SafeString {
}
}

impl Into<SafeString> for String {
fn into(self) -> SafeString {
SafeString::new(self)
}
}

impl<'a> Into<SafeString> for &'a str {
fn into(self) -> SafeString {
self.to_string().into()
}
}

impl Serialize for SafeString {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down

0 comments on commit 918fe7c

Please sign in to comment.