Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
l1h3r committed Mar 3, 2021
1 parent 30f4f3c commit 3a8e545
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 88 deletions.
1 change: 1 addition & 0 deletions identity-account/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Error {
StrongholdPasswordNotSet,
StrongholdProcedureFailure,
StrongholdInvalidAddress,
MutexPoisoned,
}

impl From<std::io::Error> for Error {
Expand Down
180 changes: 92 additions & 88 deletions identity-account/src/stronghold/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ rusty_fork_test! {
);
})
}
}

rusty_fork_test! {
#[test]
fn test_password_persistence() {
block_on(async {
Expand Down Expand Up @@ -144,117 +142,123 @@ rusty_fork_test! {
);
})
}
}

#[tokio::test]
async fn test_store_basics() {
let password: EncryptionKey = derive_encryption_key("my-password:test_store_basics");
let snapshot: Snapshot = open_snapshot(&generate_filename(), password).await;

let store: Store = snapshot.store(b"store", &[]);
#[test]
fn test_store_basics() {
block_on(async {
let password: EncryptionKey = derive_encryption_key("my-password:test_store_basics");
let snapshot: Snapshot = open_snapshot(&generate_filename(), password).await;

assert!(store.get(location("A")).await.unwrap().is_empty());
assert!(store.get(location("B")).await.unwrap().is_empty());
assert!(store.get(location("C")).await.unwrap().is_empty());
let store: Store = snapshot.store(b"store", &[]);

store.set(location("A"), b"foo".to_vec(), None).await.unwrap();
store.set(location("B"), b"bar".to_vec(), None).await.unwrap();
store.set(location("C"), b"baz".to_vec(), None).await.unwrap();
assert!(store.get(location("A")).await.unwrap().is_empty());
assert!(store.get(location("B")).await.unwrap().is_empty());
assert!(store.get(location("C")).await.unwrap().is_empty());

assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());
store.set(location("A"), b"foo".to_vec(), None).await.unwrap();
store.set(location("B"), b"bar".to_vec(), None).await.unwrap();
store.set(location("C"), b"baz".to_vec(), None).await.unwrap();

store.del(location("A")).await.unwrap();
store.del(location("C")).await.unwrap();
assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());

assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
store.del(location("A")).await.unwrap();
store.del(location("C")).await.unwrap();

snapshot.unload(true).await.unwrap();
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());

fs::remove_file(store.path()).unwrap();
}
snapshot.unload(true).await.unwrap();

#[tokio::test]
async fn test_store_multiple_snapshots() {
let password: EncryptionKey = derive_encryption_key("my-password:test_store_multiple_snapshots");
let snapshot1: Snapshot = open_snapshot(&generate_filename(), password).await;
let snapshot2: Snapshot = open_snapshot(&generate_filename(), password).await;
let snapshot3: Snapshot = open_snapshot(&generate_filename(), password).await;

let store1: Store = snapshot1.store(b"store1", &[]);
let store2: Store = snapshot2.store(b"store2", &[]);
let store3: Store = snapshot3.store(b"store3", &[]);
let stores: &[_] = &[&store1, &store2, &store3];

for store in stores {
assert!(store.get(location("A")).await.unwrap().is_empty());
assert!(store.get(location("B")).await.unwrap().is_empty());
assert!(store.get(location("C")).await.unwrap().is_empty());
fs::remove_file(store.path()).unwrap();
})
}

for store in stores {
store.set(location("A"), b"foo".to_vec(), None).await.unwrap();
store.set(location("B"), b"bar".to_vec(), None).await.unwrap();
store.set(location("C"), b"baz".to_vec(), None).await.unwrap();
}
#[test]
fn test_store_multiple_snapshots() {
block_on(async {
let password: EncryptionKey = derive_encryption_key("my-password:test_store_multiple_snapshots");
let snapshot1: Snapshot = open_snapshot(&generate_filename(), password).await;
let snapshot2: Snapshot = open_snapshot(&generate_filename(), password).await;
let snapshot3: Snapshot = open_snapshot(&generate_filename(), password).await;

let store1: Store = snapshot1.store(b"store1", &[]);
let store2: Store = snapshot2.store(b"store2", &[]);
let store3: Store = snapshot3.store(b"store3", &[]);
let stores: &[_] = &[&store1, &store2, &store3];

for store in stores {
assert!(store.get(location("A")).await.unwrap().is_empty());
assert!(store.get(location("B")).await.unwrap().is_empty());
assert!(store.get(location("C")).await.unwrap().is_empty());
}

for store in stores {
assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());
}
for store in stores {
store.set(location("A"), b"foo".to_vec(), None).await.unwrap();
store.set(location("B"), b"bar".to_vec(), None).await.unwrap();
store.set(location("C"), b"baz".to_vec(), None).await.unwrap();
}

for store in stores {
store.del(location("A")).await.unwrap();
store.del(location("C")).await.unwrap();
}
for store in stores {
assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());
}

for store in stores {
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
}
for store in stores {
store.del(location("A")).await.unwrap();
store.del(location("C")).await.unwrap();
}

for store in stores {
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
}

snapshot1.unload(true).await.unwrap();
snapshot2.unload(true).await.unwrap();
snapshot3.unload(true).await.unwrap();
snapshot1.unload(true).await.unwrap();
snapshot2.unload(true).await.unwrap();
snapshot3.unload(true).await.unwrap();

for store in stores {
fs::remove_file(store.path()).unwrap();
for store in stores {
fs::remove_file(store.path()).unwrap();
}
})
}
}

#[tokio::test]
async fn test_store_persistence() {
let password: EncryptionKey = derive_encryption_key("my-password:test_store_persistence");
let filename: PathBuf = generate_filename();
#[test]
fn test_store_persistence() {
block_on(async {
let password: EncryptionKey = derive_encryption_key("my-password:test_store_persistence");
let filename: PathBuf = generate_filename();

{
let snapshot: Snapshot = open_snapshot(&filename, password).await;
let store: Store = snapshot.store(b"persistence", &[]);
{
let snapshot: Snapshot = open_snapshot(&filename, password).await;
let store: Store = snapshot.store(b"persistence", &[]);

assert!(store.get(location("A")).await.unwrap().is_empty());
assert!(store.get(location("B")).await.unwrap().is_empty());
assert!(store.get(location("C")).await.unwrap().is_empty());
assert!(store.get(location("A")).await.unwrap().is_empty());
assert!(store.get(location("B")).await.unwrap().is_empty());
assert!(store.get(location("C")).await.unwrap().is_empty());

store.set(location("A"), b"foo".to_vec(), None).await.unwrap();
store.set(location("B"), b"bar".to_vec(), None).await.unwrap();
store.set(location("C"), b"baz".to_vec(), None).await.unwrap();
store.set(location("A"), b"foo".to_vec(), None).await.unwrap();
store.set(location("B"), b"bar".to_vec(), None).await.unwrap();
store.set(location("C"), b"baz".to_vec(), None).await.unwrap();

assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());
assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());

snapshot.unload(true).await.unwrap();
}
snapshot.unload(true).await.unwrap();
}

{
let snapshot: Snapshot = load_snapshot(&filename, password).await;
let store: Store = snapshot.store(b"persistence", &[]);
{
let snapshot: Snapshot = load_snapshot(&filename, password).await;
let store: Store = snapshot.store(b"persistence", &[]);

assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());
assert_eq!(store.get(location("A")).await.unwrap(), b"foo".to_vec());
assert_eq!(store.get(location("B")).await.unwrap(), b"bar".to_vec());
assert_eq!(store.get(location("C")).await.unwrap(), b"baz".to_vec());

fs::remove_file(store.path()).unwrap();
fs::remove_file(store.path()).unwrap();
}
})
}
}

0 comments on commit 3a8e545

Please sign in to comment.