aboutsummaryrefslogtreecommitdiff
path: root/tests/device.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-02-02 11:27:52 +0100
committerRobin Krahl <robin.krahl@ireas.org>2019-02-02 11:27:52 +0100
commita31f73db289b00f81c5f70e60807ef7348f1a499 (patch)
tree6d51c0223778d4957ef1660f845fe5bd404d8538 /tests/device.rs
parentad76653b3be57c0cfd31c8056a8d68537034324e (diff)
parent13006c00dcbd570cf8347d89557834e320427377 (diff)
downloadnitrokey-rs-a31f73db289b00f81c5f70e60807ef7348f1a499.tar.gz
nitrokey-rs-a31f73db289b00f81c5f70e60807ef7348f1a499.tar.bz2
Merge branch 'mutable-references' into next
RFC: https://lists.sr.ht/~ireas/nitrokey-rs-dev/%3C20190126174327.tbuyk2s535kfiqm4%40localhost%3E https://lists.sr.ht/~ireas/nitrokey-rs-dev/%3C43cc304d-23e8-7f95-2167-ce3f0530b81e%40posteo.net%3E * mutable-references: Store mutable reference to Device in PasswordSafe Refactor User and Admin to use a mutable reference Require mutable reference if method changes device state Add device_mut method to DeviceWrapper Implement DerefMut for User and Admin
Diffstat (limited to 'tests/device.rs')
-rw-r--r--tests/device.rs163
1 files changed, 86 insertions, 77 deletions
diff --git a/tests/device.rs b/tests/device.rs
index 969a7df..bffd767 100644
--- a/tests/device.rs
+++ b/tests/device.rs
@@ -98,42 +98,35 @@ fn get_firmware_version(device: Pro) {
assert!(version.minor > 0);
}
-fn admin_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T {
- let result = device.authenticate_admin(&(ADMIN_PASSWORD.to_owned() + suffix));
- let device = match result {
- Ok(admin) => admin.device(),
- Err((device, _)) => device,
- };
+fn admin_retry<T: Authenticate + Device>(device: &mut T, suffix: &str, count: u8) {
+ assert_any_ok!(device.authenticate_admin(&(ADMIN_PASSWORD.to_owned() + suffix)));
assert_ok!(count, device.get_admin_retry_count());
- return device;
}
-fn user_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T {
- let result = device.authenticate_user(&(USER_PASSWORD.to_owned() + suffix));
- let device = match result {
- Ok(admin) => admin.device(),
- Err((device, _)) => device,
- };
+fn user_retry<T: Authenticate + Device>(device: &mut T, suffix: &str, count: u8) {
+ assert_any_ok!(device.authenticate_user(&(USER_PASSWORD.to_owned() + suffix)));
assert_ok!(count, device.get_user_retry_count());
- return device;
}
#[test_device]
fn get_retry_count(device: DeviceWrapper) {
- let device = admin_retry(device, "", 3);
- let device = admin_retry(device, "123", 2);
- let device = admin_retry(device, "456", 1);
- let device = admin_retry(device, "", 3);
-
- let device = user_retry(device, "", 3);
- let device = user_retry(device, "123", 2);
- let device = user_retry(device, "456", 1);
- user_retry(device, "", 3);
+ let mut device = device;
+
+ admin_retry(&mut device, "", 3);
+ admin_retry(&mut device, "123", 2);
+ admin_retry(&mut device, "456", 1);
+ admin_retry(&mut device, "", 3);
+
+ user_retry(&mut device, "", 3);
+ user_retry(&mut device, "123", 2);
+ user_retry(&mut device, "456", 1);
+ user_retry(&mut device, "", 3);
}
#[test_device]
fn config(device: DeviceWrapper) {
- let admin = unwrap_ok!(device.authenticate_admin(ADMIN_PASSWORD));
+ let mut device = device;
+ let mut admin = unwrap_ok!(device.authenticate_admin(ADMIN_PASSWORD));
let config = Config::new(None, None, None, true);
assert_ok!((), admin.write_config(config));
@@ -153,41 +146,52 @@ fn config(device: DeviceWrapper) {
#[test_device]
fn change_user_pin(device: DeviceWrapper) {
- let device = device.authenticate_user(USER_PASSWORD).unwrap().device();
- let device = device.authenticate_user(USER_NEW_PASSWORD).unwrap_err().0;
+ let mut device = device;
+ assert_any_ok!(device.authenticate_user(USER_PASSWORD));
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_user(USER_NEW_PASSWORD)
+ );
assert_ok!((), device.change_user_pin(USER_PASSWORD, USER_NEW_PASSWORD));
- let device = device.authenticate_user(USER_PASSWORD).unwrap_err().0;
- let device = device
- .authenticate_user(USER_NEW_PASSWORD)
- .unwrap()
- .device();
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_user(USER_PASSWORD)
+ );
+ assert_any_ok!(device.authenticate_user(USER_NEW_PASSWORD));
let result = device.change_user_pin(USER_PASSWORD, USER_PASSWORD);
assert_cmd_err!(CommandError::WrongPassword, result);
assert_ok!((), device.change_user_pin(USER_NEW_PASSWORD, USER_PASSWORD));
- let device = device.authenticate_user(USER_PASSWORD).unwrap().device();
- assert!(device.authenticate_user(USER_NEW_PASSWORD).is_err());
+ assert_any_ok!(device.authenticate_user(USER_PASSWORD));
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_user(USER_NEW_PASSWORD)
+ );
}
#[test_device]
fn change_admin_pin(device: DeviceWrapper) {
- let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap().device();
- let device = device.authenticate_admin(ADMIN_NEW_PASSWORD).unwrap_err().0;
+ let mut device = device;
+ assert_any_ok!(device.authenticate_admin(ADMIN_PASSWORD));
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_admin(ADMIN_NEW_PASSWORD)
+ );
assert_ok!(
(),
device.change_admin_pin(ADMIN_PASSWORD, ADMIN_NEW_PASSWORD)
);
- let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap_err().0;
- let device = device
- .authenticate_admin(ADMIN_NEW_PASSWORD)
- .unwrap()
- .device();
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_admin(ADMIN_PASSWORD)
+ );
+ assert_any_ok!(device.authenticate_admin(ADMIN_NEW_PASSWORD));
assert_cmd_err!(
CommandError::WrongPassword,
@@ -199,28 +203,24 @@ fn change_admin_pin(device: DeviceWrapper) {
device.change_admin_pin(ADMIN_NEW_PASSWORD, ADMIN_PASSWORD)
);
- let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap().device();
- device.authenticate_admin(ADMIN_NEW_PASSWORD).unwrap_err();
+ assert_any_ok!(device.authenticate_admin(ADMIN_PASSWORD));
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_admin(ADMIN_NEW_PASSWORD)
+ );
}
-fn require_failed_user_login<D>(device: D, password: &str, error: CommandError) -> D
-where
- D: Device + Authenticate,
- nitrokey::User<D>: std::fmt::Debug,
-{
- let result = device.authenticate_user(password);
- assert!(result.is_err());
- let err = result.unwrap_err();
- match err.1 {
- Error::CommandError(err) => assert_eq!(error, err),
- _ => assert!(false),
- };
- err.0
+fn require_failed_user_login<D: Authenticate + Device>(device: &mut D, password: &str) {
+ assert_cmd_err!(
+ CommandError::WrongPassword,
+ device.authenticate_user(password)
+ );
}
#[test_device]
fn unlock_user_pin(device: DeviceWrapper) {
- let device = device.authenticate_user(USER_PASSWORD).unwrap().device();
+ let mut device = device;
+ assert_any_ok!(device.authenticate_user(USER_PASSWORD));
assert_ok!((), device.unlock_user_pin(ADMIN_PASSWORD, USER_PASSWORD));
assert_cmd_err!(
CommandError::WrongPassword,
@@ -229,10 +229,10 @@ fn unlock_user_pin(device: DeviceWrapper) {
// block user PIN
let wrong_password = USER_PASSWORD.to_owned() + "foo";
- let device = require_failed_user_login(device, &wrong_password, CommandError::WrongPassword);
- let device = require_failed_user_login(device, &wrong_password, CommandError::WrongPassword);
- let device = require_failed_user_login(device, &wrong_password, CommandError::WrongPassword);
- let device = require_failed_user_login(device, USER_PASSWORD, CommandError::WrongPassword);
+ require_failed_user_login(&mut device, &wrong_password);
+ require_failed_user_login(&mut device, &wrong_password);
+ require_failed_user_login(&mut device, &wrong_password);
+ require_failed_user_login(&mut device, USER_PASSWORD);
// unblock with current PIN
assert_cmd_err!(
@@ -240,13 +240,13 @@ fn unlock_user_pin(device: DeviceWrapper) {
device.unlock_user_pin(USER_PASSWORD, USER_PASSWORD)
);
assert_ok!((), device.unlock_user_pin(ADMIN_PASSWORD, USER_PASSWORD));
- let device = device.authenticate_user(USER_PASSWORD).unwrap().device();
+ assert_any_ok!(device.authenticate_user(USER_PASSWORD));
// block user PIN
- let device = require_failed_user_login(device, &wrong_password, CommandError::WrongPassword);
- let device = require_failed_user_login(device, &wrong_password, CommandError::WrongPassword);
- let device = require_failed_user_login(device, &wrong_password, CommandError::WrongPassword);
- let device = require_failed_user_login(device, USER_PASSWORD, CommandError::WrongPassword);
+ require_failed_user_login(&mut device, &wrong_password);
+ require_failed_user_login(&mut device, &wrong_password);
+ require_failed_user_login(&mut device, &wrong_password);
+ require_failed_user_login(&mut device, USER_PASSWORD);
// unblock with new PIN
assert_cmd_err!(
@@ -272,12 +272,12 @@ fn assert_utf8_err_or_ne(left: &str, right: Result<String, Error>) {
#[test_device]
fn factory_reset(device: DeviceWrapper) {
- let admin = unwrap_ok!(device.authenticate_admin(ADMIN_PASSWORD));
+ let mut device = device;
+ let mut admin = unwrap_ok!(device.authenticate_admin(ADMIN_PASSWORD));
let otp_data = OtpSlotData::new(1, "test", "0123468790", OtpMode::SixDigits);
assert_ok!((), admin.write_totp_slot(otp_data, 30));
- let device = admin.device();
- let pws = unwrap_ok!(device.get_password_safe(USER_PASSWORD));
+ let mut pws = unwrap_ok!(device.get_password_safe(USER_PASSWORD));
assert_ok!((), pws.write_slot(0, "test", "testlogin", "testpw"));
drop(pws);
@@ -297,23 +297,24 @@ fn factory_reset(device: DeviceWrapper) {
);
assert_ok!((), device.factory_reset(ADMIN_NEW_PASSWORD));
- let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap().device();
+ assert_any_ok!(device.authenticate_admin(ADMIN_PASSWORD));
let user = unwrap_ok!(device.authenticate_user(USER_PASSWORD));
assert_cmd_err!(CommandError::SlotNotProgrammed, user.get_totp_slot_name(1));
- let device = user.device();
let pws = unwrap_ok!(device.get_password_safe(USER_PASSWORD));
assert_utf8_err_or_ne("test", pws.get_slot_name(0));
assert_utf8_err_or_ne("testlogin", pws.get_slot_login(0));
assert_utf8_err_or_ne("testpw", pws.get_slot_password(0));
+ drop(pws);
assert_ok!((), device.build_aes_key(ADMIN_PASSWORD));
}
#[test_device]
fn build_aes_key(device: DeviceWrapper) {
- let pws = unwrap_ok!(device.get_password_safe(USER_PASSWORD));
+ let mut device = device;
+ let mut pws = unwrap_ok!(device.get_password_safe(USER_PASSWORD));
assert_ok!((), pws.write_slot(0, "test", "testlogin", "testpw"));
drop(pws);
@@ -323,7 +324,7 @@ fn build_aes_key(device: DeviceWrapper) {
);
assert_ok!((), device.build_aes_key(ADMIN_PASSWORD));
- let device = device.authenticate_admin(ADMIN_PASSWORD).unwrap().device();
+ assert_any_ok!(device.authenticate_admin(ADMIN_PASSWORD));
let pws = unwrap_ok!(device.get_password_safe(USER_PASSWORD));
assert_utf8_err_or_ne("test", pws.get_slot_name(0));
@@ -333,6 +334,7 @@ fn build_aes_key(device: DeviceWrapper) {
#[test_device]
fn change_update_pin(device: Storage) {
+ let mut device = device;
assert_cmd_err!(
CommandError::WrongPassword,
device.change_update_pin(UPDATE_NEW_PIN, UPDATE_PIN)
@@ -343,6 +345,7 @@ fn change_update_pin(device: Storage) {
#[test_device]
fn encrypted_volume(device: Storage) {
+ let mut device = device;
assert_ok!((), device.lock());
assert_eq!(1, count_nitrokey_block_devices());
@@ -361,6 +364,7 @@ fn encrypted_volume(device: Storage) {
#[test_device]
fn hidden_volume(device: Storage) {
+ let mut device = device;
assert_ok!((), device.lock());
assert_eq!(1, count_nitrokey_block_devices());
@@ -396,6 +400,7 @@ fn hidden_volume(device: Storage) {
#[test_device]
fn lock(device: Storage) {
+ let mut device = device;
assert_ok!((), device.enable_encrypted_volume(USER_PASSWORD));
assert_ok!((), device.lock());
assert_eq!(1, count_nitrokey_block_devices());
@@ -405,6 +410,7 @@ fn lock(device: Storage) {
fn set_encrypted_volume_mode(device: Storage) {
// This test case does not check the device status as the command only works with firmware
// version 0.49. For later versions, it does not do anything and always returns Ok(()).
+ let mut device = device;
assert_ok!(
(),
@@ -441,12 +447,13 @@ fn set_unencrypted_volume_mode(device: Storage) {
);
}
- fn assert_success(device: &Storage, mode: VolumeMode) {
+ fn assert_success(device: &mut Storage, mode: VolumeMode) {
assert_ok!((), device.set_unencrypted_volume_mode(ADMIN_PASSWORD, mode));
assert_mode(&device, mode);
}
- assert_success(&device, VolumeMode::ReadOnly);
+ let mut device = device;
+ assert_success(&mut device, VolumeMode::ReadOnly);
assert_cmd_err!(
CommandError::WrongPassword,
@@ -454,9 +461,9 @@ fn set_unencrypted_volume_mode(device: Storage) {
);
assert_mode(&device, VolumeMode::ReadOnly);
- assert_success(&device, VolumeMode::ReadWrite);
- assert_success(&device, VolumeMode::ReadWrite);
- assert_success(&device, VolumeMode::ReadOnly);
+ assert_success(&mut device, VolumeMode::ReadWrite);
+ assert_success(&mut device, VolumeMode::ReadWrite);
+ assert_success(&mut device, VolumeMode::ReadOnly);
}
#[test_device]
@@ -488,6 +495,7 @@ fn get_production_info(device: Storage) {
#[test_device]
fn clear_new_sd_card_warning(device: Storage) {
+ let mut device = device;
assert_ok!((), device.factory_reset(ADMIN_PASSWORD));
thread::sleep(time::Duration::from_secs(3));
assert_ok!((), device.build_aes_key(ADMIN_PASSWORD));
@@ -506,6 +514,7 @@ fn clear_new_sd_card_warning(device: Storage) {
#[test_device]
fn export_firmware(device: Storage) {
+ let mut device = device;
assert_cmd_err!(
CommandError::WrongPassword,
device.export_firmware("someadminpn")