From 95d18c5b06399fa4109de808f70b1048d4494d39 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 5 Jun 2018 22:19:53 +0200 Subject: Use a Device reference in PasswordSafe Instead of wrapping an owned Device instance, PasswordSafe now only requires a reference to a Device. The lifetime parameter makes sure that the device lives at least as long as the password safe. Using a reference instead of an owned device allows us to implement Drop on PasswordSafe to make sure that the password safe is disabled once it is destructed. --- src/tests/pws.rs | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) (limited to 'src/tests/pws.rs') diff --git a/src/tests/pws.rs b/src/tests/pws.rs index f45a81a..30d6853 100644 --- a/src/tests/pws.rs +++ b/src/tests/pws.rs @@ -2,46 +2,29 @@ use pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT}; use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD}; use util::{CommandError, CommandStatus}; -fn get_pws() -> PasswordSafe { - Target::connect() - .unwrap() - .get_password_safe(USER_PASSWORD) - .unwrap() +fn get_pws(device: &Target) -> PasswordSafe { + device.get_password_safe(USER_PASSWORD).unwrap() } #[test] #[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] fn enable() { + let device = Target::connect().unwrap(); assert!( - Target::connect() - .unwrap() + device .get_password_safe(&(USER_PASSWORD.to_owned() + "123")) .is_err() ); - assert!( - Target::connect() - .unwrap() - .get_password_safe(USER_PASSWORD) - .is_ok() - ); - assert!( - Target::connect() - .unwrap() - .get_password_safe(ADMIN_PASSWORD) - .is_err() - ); - assert!( - Target::connect() - .unwrap() - .get_password_safe(USER_PASSWORD) - .is_ok() - ); + assert!(device.get_password_safe(USER_PASSWORD).is_ok()); + assert!(device.get_password_safe(ADMIN_PASSWORD).is_err()); + assert!(device.get_password_safe(USER_PASSWORD).is_ok()); } #[test] #[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] fn get_status() { - let pws = get_pws(); + let device = Target::connect().unwrap(); + let pws = get_pws(&device); for i in 0..SLOT_COUNT { assert_eq!( CommandStatus::Success, @@ -75,7 +58,8 @@ fn get_status() { #[test] #[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] fn get_data() { - let pws = get_pws(); + let device = Target::connect().unwrap(); + let pws = get_pws(&device); assert_eq!( CommandStatus::Success, pws.write_slot(1, "name", "login", "password") @@ -118,7 +102,8 @@ fn get_data() { #[test] #[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] fn write() { - let pws = get_pws(); + let device = Target::connect().unwrap(); + let pws = get_pws(&device); assert_eq!( CommandStatus::Error(CommandError::InvalidSlot), @@ -153,7 +138,8 @@ fn write() { #[test] #[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] fn erase() { - let pws = get_pws(); + let device = Target::connect().unwrap(); + let pws = get_pws(&device); assert_eq!( CommandStatus::Error(CommandError::InvalidSlot), pws.erase_slot(SLOT_COUNT) -- cgit v1.2.1