diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2018-05-21 21:59:25 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2018-05-22 00:01:54 +0200 |
commit | 9428ce3221209828b8cff0e34957ebb9b10b2d99 (patch) | |
tree | 94fb08204694966d6db1a487add706c7fedc85a9 /src/tests | |
parent | 3472841949caacadeec92dbf80eca13fd2171f5c (diff) | |
download | nitrokey-rs-9428ce3221209828b8cff0e34957ebb9b10b2d99.tar.gz nitrokey-rs-9428ce3221209828b8cff0e34957ebb9b10b2d99.tar.bz2 |
Correct invalid slot handling
While the Nitrokey device would generate a WrongSlot error, libnitrokey
catches these errors and raises an InvalidSlotException with error code
201. This patch matches this error code to CommandError::InvalidSlot,
corrects the documentation and adds test cases.
To be able to test a failing OTP generation command, we have to adapt
get_string_result to free the string only if successful. This is due to
the segfault issue in libnitrokey v3.3 (see todo list).
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/pro.rs | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/tests/pro.rs b/src/tests/pro.rs index 8394e47..a508801 100644 --- a/src/tests/pro.rs +++ b/src/tests/pro.rs @@ -117,8 +117,23 @@ fn hotp_slot_name() { let slot_data = OtpSlotData::new(1, "test-hotp", HOTP_SECRET, OtpMode::SixDigits); assert_eq!(CommandStatus::Success, admin.write_hotp_slot(slot_data, 0)); - let result = admin.device().get_hotp_slot_name(1); + let device = admin.device(); + let result = device.get_hotp_slot_name(1); assert_eq!("test-hotp", result.unwrap()); + let result = device.get_hotp_slot_name(4); + assert_eq!(CommandError::InvalidSlot, result.unwrap_err()); +} + +#[test] +#[cfg_attr(not(feature = "test-pro"), ignore)] +fn hotp_error() { + let admin = get_admin_test_device(); + let slot_data = OtpSlotData::new(1, "", HOTP_SECRET, OtpMode::SixDigits); + assert_eq!(CommandStatus::Error(CommandError::NoName), admin.write_hotp_slot(slot_data, 0)); + let slot_data = OtpSlotData::new(4, "test", HOTP_SECRET, OtpMode::SixDigits); + assert_eq!(CommandStatus::Error(CommandError::InvalidSlot), admin.write_hotp_slot(slot_data, 0)); + let code = admin.get_hotp_code(4); + assert_eq!(CommandError::InvalidSlot, code.unwrap_err()); } fn configure_totp(admin: &AdminAuthenticatedDevice) { @@ -181,9 +196,24 @@ fn totp_slot_name() { let slot_data = OtpSlotData::new(1, "test-totp", TOTP_SECRET, OtpMode::EightDigits); assert_eq!(CommandStatus::Success, admin.write_totp_slot(slot_data, 0)); - let result = admin.device().get_totp_slot_name(1); + let device = admin.device(); + let result = device.get_totp_slot_name(1); assert!(result.is_ok()); assert_eq!("test-totp", result.unwrap()); + let result = device.get_totp_slot_name(16); + assert_eq!(CommandError::InvalidSlot, result.unwrap_err()); +} + +#[test] +#[cfg_attr(not(feature = "test-pro"), ignore)] +fn totp_error() { + let admin = get_admin_test_device(); + let slot_data = OtpSlotData::new(1, "", HOTP_SECRET, OtpMode::SixDigits); + assert_eq!(CommandStatus::Error(CommandError::NoName), admin.write_hotp_slot(slot_data, 0)); + let slot_data = OtpSlotData::new(4, "test", HOTP_SECRET, OtpMode::SixDigits); + assert_eq!(CommandStatus::Error(CommandError::InvalidSlot), admin.write_hotp_slot(slot_data, 0)); + let code = admin.get_hotp_code(4); + assert_eq!(CommandError::InvalidSlot, code.unwrap_err()); } #[test] |