summaryrefslogtreecommitdiff
path: root/nitrokey/tests/pws.rs
diff options
context:
space:
mode:
Diffstat (limited to 'nitrokey/tests/pws.rs')
-rw-r--r--nitrokey/tests/pws.rs54
1 files changed, 29 insertions, 25 deletions
diff --git a/nitrokey/tests/pws.rs b/nitrokey/tests/pws.rs
index df99e1c..b0e5abe 100644
--- a/nitrokey/tests/pws.rs
+++ b/nitrokey/tests/pws.rs
@@ -7,13 +7,12 @@ use std::ffi::CStr;
use libc::{c_int, c_void, free};
use nitrokey::{
- CommandError, Device, Error, GetPasswordSafe, LibraryError, PasswordSafe, SLOT_COUNT,
+ CommandError, Device, Error, GetPasswordSafe, LibraryError, PasswordSafe, DEFAULT_ADMIN_PIN,
+ DEFAULT_USER_PIN, SLOT_COUNT,
};
use nitrokey_sys;
use nitrokey_test::test as test_device;
-use crate::util::{ADMIN_PASSWORD, USER_PASSWORD};
-
fn get_slot_name_direct(slot: u8) -> Result<String, Error> {
let ptr = unsafe { nitrokey_sys::NK_get_password_safe_slot_name(slot) };
if ptr.is_null() {
@@ -33,33 +32,35 @@ fn get_slot_name_direct(slot: u8) -> Result<String, Error> {
}
}
-fn get_pws<T>(device: &T) -> PasswordSafe
+fn get_pws<T>(device: &mut T) -> PasswordSafe
where
T: Device,
{
- device.get_password_safe(USER_PASSWORD).unwrap()
+ unwrap_ok!(device.get_password_safe(DEFAULT_USER_PIN))
}
#[test_device]
fn enable(device: DeviceWrapper) {
+ let mut device = device;
assert_cmd_err!(
CommandError::WrongPassword,
- device.get_password_safe(&(USER_PASSWORD.to_owned() + "123"))
+ device.get_password_safe(&(DEFAULT_USER_PIN.to_owned() + "123"))
);
- assert!(device.get_password_safe(USER_PASSWORD).is_ok());
+ assert_any_ok!(device.get_password_safe(DEFAULT_USER_PIN));
assert_cmd_err!(
CommandError::WrongPassword,
- device.get_password_safe(ADMIN_PASSWORD)
+ device.get_password_safe(DEFAULT_ADMIN_PIN)
);
- assert!(device.get_password_safe(USER_PASSWORD).is_ok());
+ assert_any_ok!(device.get_password_safe(DEFAULT_USER_PIN));
}
#[test_device]
fn drop(device: DeviceWrapper) {
+ let mut device = device;
{
- let pws = get_pws(&device);
+ let mut pws = get_pws(&mut device);
assert_ok!((), pws.write_slot(1, "name", "login", "password"));
- assert_eq!("name", pws.get_slot_name(1).unwrap());
+ assert_ok!("name".to_string(), pws.get_slot_name(1));
let result = get_slot_name_direct(1);
assert_ok!(String::from("name"), result);
}
@@ -72,15 +73,16 @@ fn drop(device: DeviceWrapper) {
#[test_device]
fn get_status(device: DeviceWrapper) {
- let pws = get_pws(&device);
+ let mut device = device;
+ let mut pws = get_pws(&mut device);
for i in 0..SLOT_COUNT {
assert_ok!((), pws.erase_slot(i));
}
- let status = pws.get_slot_status().unwrap();
+ let status = unwrap_ok!(pws.get_slot_status());
assert_eq!(status, [false; SLOT_COUNT as usize]);
assert_ok!((), pws.write_slot(1, "name", "login", "password"));
- let status = pws.get_slot_status().unwrap();
+ let status = unwrap_ok!(pws.get_slot_status());
for i in 0..SLOT_COUNT {
assert_eq!(i == 1, status[i as usize]);
}
@@ -88,17 +90,17 @@ fn get_status(device: DeviceWrapper) {
for i in 0..SLOT_COUNT {
assert_ok!((), pws.write_slot(i, "name", "login", "password"));
}
- let status = pws.get_slot_status().unwrap();
- assert_eq!(status, [true; SLOT_COUNT as usize]);
+ assert_ok!([true; SLOT_COUNT as usize], pws.get_slot_status());
}
#[test_device]
fn get_data(device: DeviceWrapper) {
- let pws = get_pws(&device);
+ let mut device = device;
+ let mut pws = get_pws(&mut device);
assert_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_ok!("name".to_string(), pws.get_slot_name(1));
+ assert_ok!("login".to_string(), pws.get_slot_login(1));
+ assert_ok!("password".to_string(), pws.get_slot_password(1));
assert_ok!((), pws.erase_slot(1));
assert_cmd_err!(CommandError::SlotNotProgrammed, pws.get_slot_name(1));
@@ -109,9 +111,9 @@ fn get_data(device: DeviceWrapper) {
let login = "pär@test.com";
let password = "'i3lJc[09?I:,[u7dWz9";
assert_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_ok!(name.to_string(), pws.get_slot_name(1));
+ assert_ok!(login.to_string(), pws.get_slot_login(1));
+ assert_ok!(password.to_string(), pws.get_slot_password(1));
assert_lib_err!(LibraryError::InvalidSlot, pws.get_slot_name(SLOT_COUNT));
assert_lib_err!(LibraryError::InvalidSlot, pws.get_slot_login(SLOT_COUNT));
@@ -120,7 +122,8 @@ fn get_data(device: DeviceWrapper) {
#[test_device]
fn write(device: DeviceWrapper) {
- let pws = get_pws(&device);
+ let mut device = device;
+ let mut pws = get_pws(&mut device);
assert_lib_err!(
LibraryError::InvalidSlot,
@@ -145,7 +148,8 @@ fn write(device: DeviceWrapper) {
#[test_device]
fn erase(device: DeviceWrapper) {
- let pws = get_pws(&device);
+ let mut device = device;
+ let mut pws = get_pws(&mut device);
assert_lib_err!(LibraryError::InvalidSlot, pws.erase_slot(SLOT_COUNT));
assert_ok!((), pws.write_slot(0, "name", "login", "password"));