Skip to content

Commit

Permalink
tests: make a ScopedKeyring for handling cleanup automatically
Browse files Browse the repository at this point in the history
Some tests handle it manually to test behavior once the keyring is
deallocated, so leave the old functionality there.
  • Loading branch information
mathstuf committed Oct 3, 2019
1 parent 0a4abab commit 736ae3d
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 258 deletions.
28 changes: 0 additions & 28 deletions src/tests/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,34 @@ fn empty_key_type() {
let mut keyring = utils::new_test_keyring();
let err = keyring.add_key::<EmptyKey, _, _>("", ()).unwrap_err();
assert_eq!(err, errno::Errno(libc::EINVAL));

keyring.invalidate().unwrap()
}

#[test]
fn unsupported_key_type() {
let mut keyring = utils::new_test_keyring();
let err = keyring.add_key::<UnsupportedKey, _, _>("", ()).unwrap_err();
assert_eq!(err, errno::Errno(libc::ENODEV));

keyring.invalidate().unwrap()
}

#[test]
fn invalid_key_type() {
let mut keyring = utils::new_test_keyring();
let err = keyring.add_key::<InvalidKey, _, _>("", ()).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}

#[test]
fn maxlen_key_type() {
let mut keyring = utils::new_test_keyring();
let err = keyring.add_key::<MaxLenKey, _, _>("", ()).unwrap_err();
assert_eq!(err, errno::Errno(libc::ENODEV));

keyring.invalidate().unwrap()
}

#[test]
fn overlong_key_type() {
let mut keyring = utils::new_test_keyring();
let err = keyring.add_key::<OverlongKey, _, _>("", ()).unwrap_err();
assert_eq!(err, errno::Errno(libc::EINVAL));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -84,8 +74,6 @@ fn keyring_with_payload() {
.add_key::<KeyringShadow, _, _>("", "payload")
.unwrap_err();
assert_eq!(err, errno::Errno(libc::EINVAL));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -102,8 +90,6 @@ fn max_user_description() {
assert_eq!(key.description().unwrap().description, maxdesc);
key.invalidate().unwrap();
}

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -116,8 +102,6 @@ fn overlong_user_description() {
.add_key::<User, _, _>(toolarge.as_ref(), "payload".as_bytes())
.unwrap_err();
assert_eq!(err, errno::Errno(libc::EINVAL));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -142,8 +126,6 @@ fn add_key_to_non_keyring() {
.add_key::<User, _, _>("add_key_to_non_keyring", expected)
.unwrap_err();
assert_eq!(err, errno::Errno(libc::ENOTDIR));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -159,8 +141,6 @@ fn add_keyring_to_non_keyring() {
.add_keyring("add_keyring_to_non_keyring")
.unwrap_err();
assert_eq!(err, errno::Errno(libc::ENOTDIR));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -170,8 +150,6 @@ fn add_key() {
let payload = "payload".as_bytes();
let key = keyring.add_key::<User, _, _>("add_key", payload).unwrap();
assert_eq!(key.read().unwrap(), payload);

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -182,8 +160,6 @@ fn add_keyring() {
let (keys, keyrings) = new_keyring.read().unwrap();
assert!(keys.is_empty());
assert!(keyrings.is_empty());

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -201,8 +177,6 @@ fn add_key_replace() {
assert_eq!(key, key_updated);
assert_eq!(key.read().unwrap(), payload);
assert_eq!(key_updated.read().unwrap(), payload);

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -222,6 +196,4 @@ fn add_keyring_replace() {
let (keys, keyrings) = updated_keyring.read().unwrap();
assert!(keys.is_empty());
assert!(keyrings.is_empty());

keyring.invalidate().unwrap()
}
15 changes: 0 additions & 15 deletions src/tests/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ fn clear_non_keyring() {
assert_eq!(err, errno::Errno(libc::ENOTDIR));

keyring.unlink_key(&key).unwrap();
keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -68,8 +67,6 @@ fn clear_deleted_keyring() {
assert_eq!(err, errno::Errno(libc::ENOKEY));
break;
}

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -86,9 +83,6 @@ fn clear_empty_keyring() {
let (keys, keyrings) = keyring.read().unwrap();
assert_eq!(keys.len(), 0);
assert_eq!(keyrings.len(), 0);

// Clean up.
keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -117,9 +111,6 @@ fn clear_keyring_one_key() {
let (keys, keyrings) = keyring.read().unwrap();
assert_eq!(keys.len(), 0);
assert_eq!(keyrings.len(), 0);

// Clean up.
keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -160,9 +151,6 @@ fn clear_keyring_many_keys() {
let (keys, keyrings) = keyring.read().unwrap();
assert_eq!(keys.len(), 0);
assert_eq!(keyrings.len(), 0);

// Clean up.
keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -190,7 +178,4 @@ fn clear_keyring_keyring() {
let (keys, keyrings) = keyring.read().unwrap();
assert_eq!(keys.len(), 0);
assert_eq!(keyrings.len(), 0);

// Clean up.
keyring.invalidate().unwrap()
}
6 changes: 0 additions & 6 deletions src/tests/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ fn non_existent_key() {

keyring.unlink_key(&key).unwrap();
utils::wait_for_key_gc(&key);

keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -112,8 +110,6 @@ fn describe_key_no_perm() {

let err = key.description().unwrap_err();
assert_eq!(err, errno::Errno(libc::EACCES));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -128,6 +124,4 @@ fn describe_revoked_key() {

let err = key_mirror.description().unwrap_err();
assert_eq!(err, errno::Errno(libc::EKEYREVOKED));

keyring.invalidate().unwrap()
}
12 changes: 0 additions & 12 deletions src/tests/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ fn instantiate_already_instantiated() {

let err = manager.instantiate(None, payload).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -143,8 +141,6 @@ fn reject_already_instantiated() {
let errno = errno::Errno(libc::EKEYREJECTED);
let err = manager.reject(None, duration, errno).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -159,8 +155,6 @@ fn negate_already_instantiated() {
let duration = Duration::from_secs(1);
let err = manager.negate(None, duration).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -178,8 +172,6 @@ fn instantiate_unlinked_key() {

let err = manager.instantiate(None, payload).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -199,8 +191,6 @@ fn reject_unlinked_key() {
let errno = errno::Errno(libc::EKEYREJECTED);
let err = manager.reject(None, duration, errno).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -219,6 +209,4 @@ fn negate_unlinked_key() {
let duration = Duration::from_secs(1);
let err = manager.negate(None, duration).unwrap_err();
assert_eq!(err, errno::Errno(libc::EPERM));

keyring.invalidate().unwrap()
}
6 changes: 1 addition & 5 deletions src/tests/invalidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ fn unlinked_key() {

let err = key.invalidate().unwrap_err();
assert_eq!(err, errno::Errno(libc::ENOKEY));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -95,13 +93,11 @@ fn invalidate_key() {
assert!(keys.is_empty());
assert!(keyrings.is_empty());
}

keyring.invalidate().unwrap()
}

#[test]
fn invalidate_keyring() {
let mut keyring = utils::new_test_keyring();
let mut keyring = utils::new_test_keyring_manual();

{
let (keys, keyrings) = keyring.read().unwrap();
Expand Down
24 changes: 0 additions & 24 deletions src/tests/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ fn invalid_target() {

let err = invalid_keyring.link_keyring(&keyring).unwrap_err();
assert_eq!(err, errno::Errno(libc::EINVAL));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -47,8 +45,6 @@ fn invalid_source() {

let err = keyring.link_keyring(&invalid_keyring).unwrap_err();
assert_eq!(err, errno::Errno(libc::EINVAL));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -65,8 +61,6 @@ fn link_to_non_keyring() {

let err = not_a_keyring.link_key(&linked_key).unwrap_err();
assert_eq!(err, errno::Errno(libc::ENOTDIR));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -83,8 +77,6 @@ fn link_unlinked_key() {

let err = target_keyring.link_key(&key).unwrap_err();
assert_eq!(err, errno::Errno(libc::ENOKEY));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -103,8 +95,6 @@ fn link_into_unlinked_keyring() {

let err = target_keyring.link_key(&key).unwrap_err();
assert_eq!(err, errno::Errno(libc::ENOKEY));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -114,8 +104,6 @@ fn link_self() {

let err = keyring.link_keyring(&keyring_observer).unwrap_err();
assert_eq!(err, errno::Errno(libc::EDEADLK));

keyring.invalidate().unwrap()
}

#[test]
Expand All @@ -125,8 +113,6 @@ fn link_self_via_child() {

let err = target_keyring.link_keyring(&keyring).unwrap_err();
assert_eq!(err, errno::Errno(libc::EDEADLK));

keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -161,8 +147,6 @@ fn link_self_via_child_chains() {

let err = target_keyring.link_keyring(&keyring).unwrap_err();
assert_eq!(err, errno::Errno(libc::ELOOP));

keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -198,8 +182,6 @@ fn link_self_via_keyring_stacks() {

let err = keyring_b.link_keyring(&keyring_a_root).unwrap_err();
assert_eq!(err, errno::Errno(libc::EDEADLK));

keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -235,8 +217,6 @@ fn link_self_via_keyring_deep_stacks() {

let err = keyring_b.link_keyring(&keyring_a_root).unwrap_err();
assert_eq!(err, errno::Errno(libc::ELOOP));

keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -277,8 +257,6 @@ fn multiply_link_key_into_keyring() {
assert_eq!(keys[0], key);
assert_eq!(keyrings.len(), 1);
assert_eq!(keyrings[0], new_keyring);

keyring.invalidate().unwrap()
}

#[test]
Expand Down Expand Up @@ -318,6 +296,4 @@ fn multiply_link_keyring_into_keyring() {
assert_eq!(keyrings.len(), 2);
assert_eq!(keyrings[0], new_keyring);
assert_eq!(keyrings[1], inner_keyring);

keyring.invalidate().unwrap()
}
Loading

0 comments on commit 736ae3d

Please sign in to comment.