From f035857d9a9dc14c85e6bdf22cbe72528235657d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 7 Jun 2018 03:03:29 +0200 Subject: Use Result<(), CommandError> instead of CommandStatus The Result enum is more idiomatic and easier to use than our custom CommandStatus enum with the same structure. This is especially true for the try operator ?. --- src/tests/otp.rs | 55 ++++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) (limited to 'src/tests/otp.rs') diff --git a/src/tests/otp.rs b/src/tests/otp.rs index 91f9f1b..cf71d9d 100644 --- a/src/tests/otp.rs +++ b/src/tests/otp.rs @@ -1,7 +1,6 @@ use std::ops::Deref; use tests::util::{Target, ADMIN_PASSWORD, USER_PASSWORD}; -use {Admin, Authenticate, CommandError, CommandStatus, Config, ConfigureOtp, GenerateOtp, OtpMode, - OtpSlotData}; +use {Admin, Authenticate, CommandError, Config, ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData}; // test suite according to RFC 4226, Appendix D static HOTP_SECRET: &str = "3132333435363738393031323334353637383930"; @@ -36,10 +35,7 @@ fn get_admin_test_device() -> Admin { fn configure_hotp(admin: &ConfigureOtp, counter: u8) { let slot_data = OtpSlotData::new(1, "test-hotp", HOTP_SECRET, OtpMode::SixDigits); - assert_eq!( - CommandStatus::Success, - admin.write_hotp_slot(slot_data, counter.into()) - ); + assert!(admin.write_hotp_slot(slot_data, counter.into()).is_ok()); } fn check_hotp_codes(device: &GenerateOtp, offset: u8) { @@ -56,7 +52,7 @@ fn check_hotp_codes(device: &GenerateOtp, offset: u8) { fn hotp_no_pin() { let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); configure_hotp(&admin, 0); check_hotp_codes(admin.deref(), 0); @@ -73,7 +69,7 @@ fn hotp_no_pin() { fn hotp_pin() { let admin = get_admin_test_device(); let config = Config::new(None, None, None, true); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); configure_hotp(&admin, 0); let user = admin.device().authenticate_user(USER_PASSWORD).unwrap(); @@ -87,7 +83,7 @@ fn hotp_pin() { fn hotp_slot_name() { 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)); + assert!(admin.write_hotp_slot(slot_data, 0).is_ok()); let device = admin.device(); let result = device.get_hotp_slot_name(1); @@ -102,12 +98,12 @@ 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), + Err(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), + Err(CommandError::InvalidSlot), admin.write_hotp_slot(slot_data, 0) ); let code = admin.get_hotp_code(4); @@ -119,13 +115,13 @@ fn hotp_error() { fn hotp_erase() { let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); let slot_data = OtpSlotData::new(1, "test1", HOTP_SECRET, OtpMode::SixDigits); - assert_eq!(CommandStatus::Success, admin.write_hotp_slot(slot_data, 0)); + assert!(admin.write_hotp_slot(slot_data, 0).is_ok()); let slot_data = OtpSlotData::new(2, "test2", HOTP_SECRET, OtpMode::SixDigits); - assert_eq!(CommandStatus::Success, admin.write_hotp_slot(slot_data, 0)); + assert!(admin.write_hotp_slot(slot_data, 0).is_ok()); - assert_eq!(CommandStatus::Success, admin.erase_hotp_slot(1)); + assert!(admin.erase_hotp_slot(1).is_ok()); let device = admin.device(); let result = device.get_hotp_slot_name(1); @@ -139,10 +135,7 @@ fn hotp_erase() { fn configure_totp(admin: &ConfigureOtp, factor: u64) { let slot_data = OtpSlotData::new(1, "test-totp", TOTP_SECRET, OtpMode::EightDigits); let time_window = 30u64.checked_mul(factor).unwrap(); - assert_eq!( - CommandStatus::Success, - admin.write_totp_slot(slot_data, time_window as u16) - ); + assert!(admin.write_totp_slot(slot_data, time_window as u16).is_ok()); } fn check_totp_codes(device: &GenerateOtp, factor: u64, timestamp_size: TotpTimestampSize) { @@ -153,7 +146,7 @@ fn check_totp_codes(device: &GenerateOtp, factor: u64, timestamp_size: TotpTimes continue; } - assert_eq!(CommandStatus::Success, device.set_time(time)); + assert!(device.set_time(time).is_ok()); let result = device.get_totp_code(1); assert!(result.is_ok()); let result_code = result.unwrap(); @@ -171,7 +164,7 @@ fn totp_no_pin() { // TODO: this test may fail due to bad timing --> find solution let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); configure_totp(&admin, 1); check_totp_codes(admin.deref(), 1, TotpTimestampSize::U32); @@ -191,7 +184,7 @@ fn totp_no_pin() { fn totp_no_pin_64() { let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); configure_totp(&admin, 1); check_totp_codes(admin.deref(), 1, TotpTimestampSize::U64); @@ -209,7 +202,7 @@ fn totp_pin() { // TODO: this test may fail due to bad timing --> find solution let admin = get_admin_test_device(); let config = Config::new(None, None, None, true); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); configure_totp(&admin, 1); let user = admin.device().authenticate_user(USER_PASSWORD).unwrap(); @@ -225,7 +218,7 @@ fn totp_pin() { fn totp_pin_64() { let admin = get_admin_test_device(); let config = Config::new(None, None, None, true); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); configure_totp(&admin, 1); let user = admin.device().authenticate_user(USER_PASSWORD).unwrap(); @@ -239,7 +232,7 @@ fn totp_pin_64() { fn totp_slot_name() { 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)); + assert!(admin.write_totp_slot(slot_data, 0).is_ok()); let device = admin.device(); let result = device.get_totp_slot_name(1); @@ -255,12 +248,12 @@ 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), + Err(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), + Err(CommandError::InvalidSlot), admin.write_hotp_slot(slot_data, 0) ); let code = admin.get_hotp_code(4); @@ -272,13 +265,13 @@ fn totp_error() { fn totp_erase() { let admin = get_admin_test_device(); let config = Config::new(None, None, None, false); - assert_eq!(CommandStatus::Success, admin.write_config(config)); + assert!(admin.write_config(config).is_ok()); let slot_data = OtpSlotData::new(1, "test1", TOTP_SECRET, OtpMode::SixDigits); - assert_eq!(CommandStatus::Success, admin.write_totp_slot(slot_data, 0)); + assert!(admin.write_totp_slot(slot_data, 0).is_ok()); let slot_data = OtpSlotData::new(2, "test2", TOTP_SECRET, OtpMode::SixDigits); - assert_eq!(CommandStatus::Success, admin.write_totp_slot(slot_data, 0)); + assert!(admin.write_totp_slot(slot_data, 0).is_ok()); - assert_eq!(CommandStatus::Success, admin.erase_totp_slot(1)); + assert!(admin.erase_totp_slot(1).is_ok()); let device = admin.device(); let result = device.get_totp_slot_name(1); -- cgit v1.2.1