From 3472841949caacadeec92dbf80eca13fd2171f5c Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 21 May 2018 20:59:51 +0000 Subject: Refactor test cases --- src/tests/pro.rs | 50 +++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/tests/pro.rs b/src/tests/pro.rs index 76d68a0..8394e47 100644 --- a/src/tests/pro.rs +++ b/src/tests/pro.rs @@ -24,6 +24,15 @@ static TOTP_CODES: &[(u64, &str)] = &[ (20000000000, "65353130"), ]; +fn get_test_device() -> UnauthenticatedDevice { + set_debug(false); + ::connect_model(Model::Pro).expect("Could not connect to the Nitrokey Pro.") +} + +fn get_admin_test_device() -> AdminAuthenticatedDevice { + get_test_device().authenticate_admin(ADMIN_PASSWORD).expect("Could not login as admin.") +} + #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn connect() { @@ -49,8 +58,7 @@ fn disconnect() { #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn get_serial_number() { - set_debug(false); - let device = ::connect().unwrap(); + let device = get_test_device(); let result = device.get_serial_number(); assert!(result.is_ok()); let serial_number = result.unwrap(); @@ -76,9 +84,7 @@ where #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn hotp() { - set_debug(false); - let device = ::connect().unwrap(); - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); + let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); assert_eq!(CommandStatus::Success, admin.write_config(config)); @@ -92,9 +98,7 @@ fn hotp() { #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn hotp_pin() { - set_debug(false); - let device = ::connect().unwrap(); - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); + let admin = get_admin_test_device(); let config = Config::new(None, None, None, true); assert_eq!(CommandStatus::Success, admin.write_config(config)); @@ -109,10 +113,7 @@ fn hotp_pin() { #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn hotp_slot_name() { - set_debug(false); - let device = ::connect().unwrap(); - - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); + let admin = get_admin_test_device(); let slot_data = OtpSlotData::new(1, "test-hotp", HOTP_SECRET, OtpMode::SixDigits); assert_eq!(CommandStatus::Success, admin.write_hotp_slot(slot_data, 0)); @@ -146,9 +147,7 @@ where #[cfg_attr(not(feature = "test-pro"), ignore)] fn totp() { // TODO: this test may fail due to bad timing --> find solution - set_debug(false); - let device = ::connect().unwrap(); - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); + let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); assert_eq!(CommandStatus::Success, admin.write_config(config)); @@ -163,9 +162,7 @@ fn totp() { #[cfg_attr(not(feature = "test-pro"), ignore)] fn totp_pin() { // TODO: this test may fail due to bad timing --> find solution - set_debug(false); - let device = ::connect().unwrap(); - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); + let admin = get_admin_test_device(); let config = Config::new(None, None, None, true); assert_eq!(CommandStatus::Success, admin.write_config(config)); @@ -180,10 +177,7 @@ fn totp_pin() { #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn totp_slot_name() { - set_debug(false); - let device = ::connect().unwrap(); - - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); + let admin = get_admin_test_device(); let slot_data = OtpSlotData::new(1, "test-totp", TOTP_SECRET, OtpMode::EightDigits); assert_eq!(CommandStatus::Success, admin.write_totp_slot(slot_data, 0)); @@ -195,8 +189,7 @@ fn totp_slot_name() { #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn get_firmware_version() { - set_debug(false); - let device = ::connect().unwrap(); + let device = get_test_device(); assert_eq!(0, device.get_major_firmware_version()); let minor = device.get_minor_firmware_version(); assert!(minor == 7 || minor == 8); @@ -225,8 +218,7 @@ fn user_retry(device: UnauthenticatedDevice, suffix: &str, count: u8) -> Unauthe #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn get_retry_count() { - set_debug(false); - let device = ::connect().unwrap(); + let device = get_test_device(); let device = admin_retry(device, "", 3); let device = admin_retry(device, "123", 2); @@ -242,11 +234,7 @@ fn get_retry_count() { #[test] #[cfg_attr(not(feature = "test-pro"), ignore)] fn read_write_config() { - set_debug(false); - let device = ::connect().unwrap(); - - let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); - + let admin = get_admin_test_device(); let config = Config::new(None, None, None, true); assert_eq!(CommandStatus::Success, admin.write_config(config)); let get_config = admin.get_config().unwrap(); -- cgit v1.2.1