aboutsummaryrefslogtreecommitdiff
path: root/nitrokey/tests/pws.rs
diff options
context:
space:
mode:
Diffstat (limited to 'nitrokey/tests/pws.rs')
-rw-r--r--nitrokey/tests/pws.rs72
1 files changed, 32 insertions, 40 deletions
diff --git a/nitrokey/tests/pws.rs b/nitrokey/tests/pws.rs
index 5061298..b349558 100644
--- a/nitrokey/tests/pws.rs
+++ b/nitrokey/tests/pws.rs
@@ -5,8 +5,9 @@ use std::ffi::CStr;
use libc::{c_int, c_void, free};
use nitrokey::{CommandError, Device, GetPasswordSafe, PasswordSafe, SLOT_COUNT};
use nitrokey_sys;
+use nitrokey_test::test as test_device;
-use crate::util::{Target, ADMIN_PASSWORD, USER_PASSWORD};
+use crate::util::{ADMIN_PASSWORD, USER_PASSWORD};
fn get_slot_name_direct(slot: u8) -> Result<String, CommandError> {
let ptr = unsafe { nitrokey_sys::NK_get_password_safe_slot_name(slot) };
@@ -27,14 +28,15 @@ fn get_slot_name_direct(slot: u8) -> Result<String, CommandError> {
}
}
-fn get_pws(device: &Target) -> PasswordSafe {
+fn get_pws<T>(device: &T) -> PasswordSafe
+where
+ T: Device,
+{
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();
+#[test_device]
+fn enable(device: DeviceWrapper) {
assert!(device
.get_password_safe(&(USER_PASSWORD.to_owned() + "123"))
.is_err());
@@ -43,59 +45,53 @@ fn enable() {
assert!(device.get_password_safe(USER_PASSWORD).is_ok());
}
-#[test]
-#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
-fn drop() {
- let device = Target::connect().unwrap();
+#[test_device]
+fn drop(device: DeviceWrapper) {
{
let pws = get_pws(&device);
- assert!(pws.write_slot(1, "name", "login", "password").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(1, "name", "login", "password"));
assert_eq!("name", pws.get_slot_name(1).unwrap());
let result = get_slot_name_direct(1);
assert_eq!(Ok(String::from("name")), result);
}
let result = get_slot_name_direct(1);
assert_eq!(Ok(String::from("name")), result);
- assert!(device.lock().is_ok());
+ assert_eq!(Ok(()), device.lock());
let result = get_slot_name_direct(1);
assert_eq!(Err(CommandError::NotAuthorized), result);
}
-#[test]
-#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
-fn get_status() {
- let device = Target::connect().unwrap();
+#[test_device]
+fn get_status(device: DeviceWrapper) {
let pws = get_pws(&device);
for i in 0..SLOT_COUNT {
- assert!(pws.erase_slot(i).is_ok(), "Could not erase slot {}", i);
+ assert_eq!(Ok(()), pws.erase_slot(i), "Could not erase slot {}", i);
}
let status = pws.get_slot_status().unwrap();
assert_eq!(status, [false; SLOT_COUNT as usize]);
- assert!(pws.write_slot(1, "name", "login", "password").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(1, "name", "login", "password"));
let status = pws.get_slot_status().unwrap();
for i in 0..SLOT_COUNT {
assert_eq!(i == 1, status[i as usize]);
}
for i in 0..SLOT_COUNT {
- assert!(pws.write_slot(i, "name", "login", "password").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(i, "name", "login", "password"));
}
let status = pws.get_slot_status().unwrap();
assert_eq!(status, [true; SLOT_COUNT as usize]);
}
-#[test]
-#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
-fn get_data() {
- let device = Target::connect().unwrap();
+#[test_device]
+fn get_data(device: DeviceWrapper) {
let pws = get_pws(&device);
- assert!(pws.write_slot(1, "name", "login", "password").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(1, "name", "login", "password"));
assert_eq!("name", pws.get_slot_name(1).unwrap());
assert_eq!("login", pws.get_slot_login(1).unwrap());
assert_eq!("password", pws.get_slot_password(1).unwrap());
- assert!(pws.erase_slot(1).is_ok());
+ assert_eq!(Ok(()), pws.erase_slot(1));
// TODO: check error codes
assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(1));
assert_eq!(Err(CommandError::Undefined), pws.get_slot_login(1));
@@ -104,7 +100,7 @@ fn get_data() {
let name = "with å";
let login = "pär@test.com";
let password = "'i3lJc[09?I:,[u7dWz9";
- assert!(pws.write_slot(1, name, login, password).is_ok());
+ assert_eq!(Ok(()), pws.write_slot(1, name, login, password));
assert_eq!(name, pws.get_slot_name(1).unwrap());
assert_eq!(login, pws.get_slot_login(1).unwrap());
assert_eq!(password, pws.get_slot_password(1).unwrap());
@@ -123,10 +119,8 @@ fn get_data() {
);
}
-#[test]
-#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
-fn write() {
- let device = Target::connect().unwrap();
+#[test_device]
+fn write(device: DeviceWrapper) {
let pws = get_pws(&device);
assert_eq!(
@@ -134,31 +128,29 @@ fn write() {
pws.write_slot(SLOT_COUNT, "name", "login", "password")
);
- assert!(pws.write_slot(0, "", "login", "password").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(0, "", "login", "password"));
assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(0));
assert_eq!(Ok(String::from("login")), pws.get_slot_login(0));
assert_eq!(Ok(String::from("password")), pws.get_slot_password(0));
- assert!(pws.write_slot(0, "name", "", "password").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(0, "name", "", "password"));
assert_eq!(Ok(String::from("name")), pws.get_slot_name(0));
assert_eq!(Err(CommandError::Undefined), pws.get_slot_login(0));
assert_eq!(Ok(String::from("password")), pws.get_slot_password(0));
- assert!(pws.write_slot(0, "name", "login", "").is_ok());
+ assert_eq!(Ok(()), pws.write_slot(0, "name", "login", ""));
assert_eq!(Ok(String::from("name")), pws.get_slot_name(0));
assert_eq!(Ok(String::from("login")), pws.get_slot_login(0));
assert_eq!(Err(CommandError::Undefined), pws.get_slot_password(0));
}
-#[test]
-#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)]
-fn erase() {
- let device = Target::connect().unwrap();
+#[test_device]
+fn erase(device: DeviceWrapper) {
let pws = get_pws(&device);
assert_eq!(Err(CommandError::InvalidSlot), pws.erase_slot(SLOT_COUNT));
- assert!(pws.write_slot(0, "name", "login", "password").is_ok());
- assert!(pws.erase_slot(0).is_ok());
- assert!(pws.erase_slot(0).is_ok());
+ assert_eq!(Ok(()), pws.write_slot(0, "name", "login", "password"));
+ assert_eq!(Ok(()), pws.erase_slot(0));
+ assert_eq!(Ok(()), pws.erase_slot(0));
assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(0));
}