diff options
| author | Daniel Mueller <deso@posteo.net> | 2019-01-03 11:02:39 -0800 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-05 10:28:37 +0100 | 
| commit | 0b3275f44306f96d41a306d8a753ec76be020d05 (patch) | |
| tree | 25efc175ee39d3b98fc836ec19a5b603e6d999e1 /tests | |
| parent | 65bff57e6139cc126191d4faabbcf74118932dd2 (diff) | |
| download | nitrokey-rs-0b3275f44306f96d41a306d8a753ec76be020d05.tar.gz nitrokey-rs-0b3275f44306f96d41a306d8a753ec76be020d05.tar.bz2 | |
Adjust OTP tests to use nitrokey-test
This change adjusts the OTP tests to use the nitrokey-test crate.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/otp.rs | 112 | 
1 files changed, 51 insertions, 61 deletions
| diff --git a/tests/otp.rs b/tests/otp.rs index c7d6e68..25c8cca 100644 --- a/tests/otp.rs +++ b/tests/otp.rs @@ -1,12 +1,15 @@  mod util; +use std::fmt::Debug;  use std::ops::Deref;  use nitrokey::{ -    Admin, Authenticate, CommandError, Config, ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData, +    Admin, Authenticate, CommandError, Config, ConfigureOtp, Device, GenerateOtp, OtpMode, +    OtpSlotData,  }; +use nitrokey_test::test as test_device; -use crate::util::{Target, ADMIN_PASSWORD, USER_PASSWORD}; +use crate::util::{ADMIN_PASSWORD, USER_PASSWORD};  // test suite according to RFC 4226, Appendix D  static HOTP_SECRET: &str = "3132333435363738393031323334353637383930"; @@ -32,9 +35,12 @@ enum TotpTimestampSize {      U64,  } -fn get_admin_test_device() -> Admin<Target> { -    Target::connect() -        .expect("Could not connect to the Nitrokey.") +fn make_admin_test_device<T>(device: T) -> Admin<T> +where +    T: Device, +    (T, nitrokey::CommandError): Debug, +{ +    device          .authenticate_admin(ADMIN_PASSWORD)          .expect("Could not login as admin.")  } @@ -53,20 +59,17 @@ fn check_hotp_codes(device: &GenerateOtp, offset: u8) {      });  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn set_time() { -    let device = Target::connect().expect("Could not connect to the Nitrokey."); +#[test_device] +fn set_time(device: DeviceWrapper) {      assert_eq!(Ok(()), device.set_time(1546385382, true));      assert_eq!(Ok(()), device.set_time(1546385392, false));      assert_eq!(Err(CommandError::Timestamp), device.set_time(1546385292, false));      assert_eq!(Ok(()), device.set_time(1546385382, true));  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn hotp_no_pin() { -    let admin = get_admin_test_device(); +#[test_device] +fn hotp_no_pin(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, false);      assert!(admin.write_config(config).is_ok()); @@ -80,10 +83,9 @@ fn hotp_no_pin() {      check_hotp_codes(&admin.device(), 0);  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn hotp_pin() { -    let admin = get_admin_test_device(); +#[test_device] +fn hotp_pin(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, true);      assert!(admin.write_config(config).is_ok()); @@ -94,10 +96,9 @@ fn hotp_pin() {      assert!(user.device().get_hotp_code(1).is_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn hotp_slot_name() { -    let admin = get_admin_test_device(); +#[test_device] +fn hotp_slot_name(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let slot_data = OtpSlotData::new(1, "test-hotp", HOTP_SECRET, OtpMode::SixDigits);      assert!(admin.write_hotp_slot(slot_data, 0).is_ok()); @@ -108,10 +109,9 @@ fn hotp_slot_name() {      assert_eq!(CommandError::InvalidSlot, result.unwrap_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn hotp_error() { -    let admin = get_admin_test_device(); +#[test_device] +fn hotp_error(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let slot_data = OtpSlotData::new(1, "", HOTP_SECRET, OtpMode::SixDigits);      assert_eq!(          Err(CommandError::NoName), @@ -126,10 +126,9 @@ fn hotp_error() {      assert_eq!(CommandError::InvalidSlot, code.unwrap_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn hotp_erase() { -    let admin = get_admin_test_device(); +#[test_device] +fn hotp_erase(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, false);      assert!(admin.write_config(config).is_ok());      let slot_data = OtpSlotData::new(1, "test1", HOTP_SECRET, OtpMode::SixDigits); @@ -174,11 +173,10 @@ fn check_totp_codes(device: &GenerateOtp, factor: u64, timestamp_size: TotpTimes      }  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn totp_no_pin() { +#[test_device] +fn totp_no_pin(device: DeviceWrapper) {      // TODO: this test may fail due to bad timing --> find solution -    let admin = get_admin_test_device(); +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, false);      assert!(admin.write_config(config).is_ok()); @@ -192,13 +190,11 @@ fn totp_no_pin() {      check_totp_codes(&admin.device(), 1, TotpTimestampSize::U32);  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -#[cfg_attr(feature = "test-storage", should_panic(expected = "assertion failed"))] -// Nitrokey Storage does only support timestamps that fit in a 32-bit unsigned integer.  Therefore -// the last RFC test case is expected to fail. -fn totp_no_pin_64() { -    let admin = get_admin_test_device(); +#[test_device] +// Nitrokey Storage does only support timestamps that fit in a 32-bit +// unsigned integer, so don't test with it. +fn totp_no_pin_64(device: Pro) { +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, false);      assert!(admin.write_config(config).is_ok()); @@ -212,11 +208,10 @@ fn totp_no_pin_64() {      check_totp_codes(&admin.device(), 1, TotpTimestampSize::U64);  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn totp_pin() { +#[test_device] +fn totp_pin(device: DeviceWrapper) {      // TODO: this test may fail due to bad timing --> find solution -    let admin = get_admin_test_device(); +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, true);      assert!(admin.write_config(config).is_ok()); @@ -227,12 +222,10 @@ fn totp_pin() {      assert!(user.device().get_totp_code(1).is_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -#[cfg_attr(feature = "test-storage", should_panic(expected = "assertion failed"))] +#[test_device]  // See comment for totp_no_pin_64. -fn totp_pin_64() { -    let admin = get_admin_test_device(); +fn totp_pin_64(device: Pro) { +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, true);      assert!(admin.write_config(config).is_ok()); @@ -243,10 +236,9 @@ fn totp_pin_64() {      assert!(user.device().get_totp_code(1).is_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn totp_slot_name() { -    let admin = get_admin_test_device(); +#[test_device] +fn totp_slot_name(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let slot_data = OtpSlotData::new(1, "test-totp", TOTP_SECRET, OtpMode::EightDigits);      assert!(admin.write_totp_slot(slot_data, 0).is_ok()); @@ -258,10 +250,9 @@ fn totp_slot_name() {      assert_eq!(CommandError::InvalidSlot, result.unwrap_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn totp_error() { -    let admin = get_admin_test_device(); +#[test_device] +fn totp_error(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let slot_data = OtpSlotData::new(1, "", HOTP_SECRET, OtpMode::SixDigits);      assert_eq!(          Err(CommandError::NoName), @@ -276,10 +267,9 @@ fn totp_error() {      assert_eq!(CommandError::InvalidSlot, code.unwrap_err());  } -#[test] -#[cfg_attr(not(any(feature = "test-pro", feature = "test-storage")), ignore)] -fn totp_erase() { -    let admin = get_admin_test_device(); +#[test_device] +fn totp_erase(device: DeviceWrapper) { +    let admin = make_admin_test_device(device);      let config = Config::new(None, None, None, false);      assert!(admin.write_config(config).is_ok());      let slot_data = OtpSlotData::new(1, "test1", TOTP_SECRET, OtpMode::SixDigits); | 
