summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/device.rs48
-rw-r--r--tests/lib.rs16
-rw-r--r--tests/otp.rs4
-rw-r--r--tests/pws.rs4
4 files changed, 47 insertions, 25 deletions
diff --git a/tests/device.rs b/tests/device.rs
index 5c52024..e367558 100644
--- a/tests/device.rs
+++ b/tests/device.rs
@@ -33,40 +33,39 @@ fn count_nitrokey_block_devices() -> usize {
#[test_device]
fn connect_no_device() {
- assert_cmu_err!(CommunicationError::NotConnected, nitrokey::connect());
- assert_cmu_err!(
- CommunicationError::NotConnected,
- nitrokey::connect_model(nitrokey::Model::Pro)
- );
+ let mut manager = unwrap_ok!(nitrokey::take());
+
+ assert_cmu_err!(CommunicationError::NotConnected, manager.connect());
assert_cmu_err!(
CommunicationError::NotConnected,
- nitrokey::connect_model(nitrokey::Model::Storage)
+ manager.connect_model(nitrokey::Model::Pro)
);
- assert_cmu_err!(CommunicationError::NotConnected, nitrokey::Pro::connect());
assert_cmu_err!(
CommunicationError::NotConnected,
- nitrokey::Storage::connect()
+ manager.connect_model(nitrokey::Model::Storage)
);
+ assert_cmu_err!(CommunicationError::NotConnected, manager.connect_pro());
+ assert_cmu_err!(CommunicationError::NotConnected, manager.connect_storage());
}
#[test_device]
fn connect_pro(device: Pro) {
assert_eq!(device.get_model(), nitrokey::Model::Pro);
- drop(device);
- assert_any_ok!(nitrokey::connect());
- assert_any_ok!(nitrokey::connect_model(nitrokey::Model::Pro));
- assert_any_ok!(nitrokey::Pro::connect());
+ let manager = device.into_manager();
+ assert_any_ok!(manager.connect());
+ assert_any_ok!(manager.connect_model(nitrokey::Model::Pro));
+ assert_any_ok!(manager.connect_pro());
}
#[test_device]
fn connect_storage(device: Storage) {
assert_eq!(device.get_model(), nitrokey::Model::Storage);
- drop(device);
- assert_any_ok!(nitrokey::connect());
- assert_any_ok!(nitrokey::connect_model(nitrokey::Model::Storage));
- assert_any_ok!(nitrokey::Storage::connect());
+ let manager = device.into_manager();
+ assert_any_ok!(manager.connect());
+ assert_any_ok!(manager.connect_model(nitrokey::Model::Storage));
+ assert_any_ok!(manager.connect_storage());
}
fn assert_empty_serial_number() {
@@ -97,7 +96,10 @@ fn get_firmware_version(device: Pro) {
assert!(version.minor > 0);
}
-fn admin_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T {
+fn admin_retry<'a, T>(device: T, suffix: &str, count: u8) -> T
+where
+ T: Authenticate<'a> + Device<'a> + 'a,
+{
let result = device.authenticate_admin(&(DEFAULT_ADMIN_PIN.to_owned() + suffix));
let device = match result {
Ok(admin) => admin.device(),
@@ -107,7 +109,10 @@ fn admin_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) ->
return device;
}
-fn user_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T {
+fn user_retry<'a, T>(device: T, suffix: &str, count: u8) -> T
+where
+ T: Authenticate<'a> + Device<'a> + 'a,
+{
let result = device.authenticate_user(&(DEFAULT_USER_PIN.to_owned() + suffix));
let device = match result {
Ok(admin) => admin.device(),
@@ -216,10 +221,10 @@ fn change_admin_pin(device: DeviceWrapper) {
device.authenticate_admin(ADMIN_NEW_PASSWORD).unwrap_err();
}
-fn require_failed_user_login<D>(device: D, password: &str, error: CommandError) -> D
+fn require_failed_user_login<'a, D>(device: D, password: &str, error: CommandError) -> D
where
- D: Device + Authenticate,
- nitrokey::User<D>: std::fmt::Debug,
+ D: Device<'a> + Authenticate<'a> + 'a,
+ nitrokey::User<'a, D>: std::fmt::Debug,
{
let result = device.authenticate_user(password);
assert!(result.is_err());
@@ -339,6 +344,7 @@ fn factory_reset(device: DeviceWrapper) {
assert_utf8_err_or_ne("testpw", pws.get_slot_password(0));
drop(pws);
+ assert_ok!(3, device.get_user_retry_count());
assert_ok!((), device.build_aes_key(DEFAULT_ADMIN_PIN));
}
diff --git a/tests/lib.rs b/tests/lib.rs
index 8ab75f6..25aae0f 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -10,3 +10,19 @@ fn get_library_version() {
assert!(version.git.is_empty() || version.git.starts_with("v"));
assert!(version.major > 0);
}
+
+#[test]
+fn take_manager() {
+ assert!(nitrokey::take().is_ok());
+
+ let result = nitrokey::take();
+ assert!(result.is_ok());
+ let result2 = nitrokey::take();
+ match result2 {
+ Ok(_) => panic!("Expected error, got Ok(_)!"),
+ Err(nitrokey::Error::ConcurrentAccessError) => {}
+ Err(err) => panic!("Expected ConcurrentAccessError, got {}", err),
+ }
+ drop(result);
+ assert!(nitrokey::take().is_ok());
+}
diff --git a/tests/otp.rs b/tests/otp.rs
index c0bbecf..aafda59 100644
--- a/tests/otp.rs
+++ b/tests/otp.rs
@@ -36,9 +36,9 @@ enum TotpTimestampSize {
U64,
}
-fn make_admin_test_device<T>(device: T) -> Admin<T>
+fn make_admin_test_device<'a, T>(device: T) -> Admin<'a, T>
where
- T: Device,
+ T: Device<'a>,
(T, nitrokey::Error): Debug,
{
unwrap_ok!(device.authenticate_admin(DEFAULT_ADMIN_PIN))
diff --git a/tests/pws.rs b/tests/pws.rs
index b0e5abe..7169695 100644
--- a/tests/pws.rs
+++ b/tests/pws.rs
@@ -32,9 +32,9 @@ fn get_slot_name_direct(slot: u8) -> Result<String, Error> {
}
}
-fn get_pws<T>(device: &mut T) -> PasswordSafe
+fn get_pws<'a, T>(device: &mut T) -> PasswordSafe<'_, 'a>
where
- T: Device,
+ T: Device<'a>,
{
unwrap_ok!(device.get_password_safe(DEFAULT_USER_PIN))
}