From 54f77851093932d82076f9ef393753e9d6692179 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Fri, 4 Jan 2019 13:00:23 -0800 Subject: Prefer assert_eq over is_ok() checks We experienced various problems running the tests and while they may or may not be caused by local setup issues, it is helpful to have more information than just an indication that an assertion (true/false) was violated. To that end, this change adjusts some of the assert!(().is_ok()) to compare against Ok(()) instead. This way, if the result is not the Ok variant, the error code will get printed. --- tests/device.rs | 18 +++++++++--------- tests/otp.rs | 38 +++++++++++++++++++------------------- tests/pws.rs | 28 ++++++++++++++-------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/tests/device.rs b/tests/device.rs index 0c8bb26..d6ed0c4 100644 --- a/tests/device.rs +++ b/tests/device.rs @@ -120,7 +120,7 @@ fn get_retry_count(device: DeviceWrapper) { fn config(device: DeviceWrapper) { let admin = device.authenticate_admin(ADMIN_PASSWORD).unwrap(); let config = Config::new(None, None, None, true); - assert!(admin.write_config(config).is_ok()); + assert_eq!(Ok(()), admin.write_config(config)); let get_config = admin.get_config().unwrap(); assert_eq!(config, get_config); @@ -128,12 +128,12 @@ fn config(device: DeviceWrapper) { assert_eq!(Err(CommandError::InvalidSlot), admin.write_config(config)); let config = Config::new(Some(1), None, Some(0), false); - assert!(admin.write_config(config).is_ok()); + assert_eq!(Ok(()), admin.write_config(config)); let get_config = admin.get_config().unwrap(); assert_eq!(config, get_config); let config = Config::new(None, None, None, false); - assert!(admin.write_config(config).is_ok()); + assert_eq!(Ok(()), admin.write_config(config)); let get_config = admin.get_config().unwrap(); assert_eq!(config, get_config); } @@ -332,26 +332,26 @@ fn change_update_pin(device: Storage) { #[test_device] fn encrypted_volume(device: Storage) { - assert!(device.lock().is_ok()); + assert_eq!(Ok(()), device.lock()); assert_eq!(1, count_nitrokey_block_devices()); - assert!(device.disable_encrypted_volume().is_ok()); + assert_eq!(Ok(()), device.disable_encrypted_volume()); assert_eq!(1, count_nitrokey_block_devices()); assert_eq!( Err(CommandError::WrongPassword), device.enable_encrypted_volume("123") ); assert_eq!(1, count_nitrokey_block_devices()); - assert!(device.enable_encrypted_volume(USER_PASSWORD).is_ok()); + assert_eq!(Ok(()), device.enable_encrypted_volume(USER_PASSWORD)); assert_eq!(2, count_nitrokey_block_devices()); - assert!(device.disable_encrypted_volume().is_ok()); + assert_eq!(Ok(()), device.disable_encrypted_volume()); assert_eq!(1, count_nitrokey_block_devices()); } #[test_device] fn lock(device: Storage) { - assert!(device.enable_encrypted_volume(USER_PASSWORD).is_ok()); - assert!(device.lock().is_ok()); + assert_eq!(Ok(()), device.enable_encrypted_volume(USER_PASSWORD)); + assert_eq!(Ok(()), device.lock()); assert_eq!(1, count_nitrokey_block_devices()); } diff --git a/tests/otp.rs b/tests/otp.rs index 25c8cca..8124c76 100644 --- a/tests/otp.rs +++ b/tests/otp.rs @@ -47,7 +47,7 @@ where fn configure_hotp(admin: &ConfigureOtp, counter: u8) { let slot_data = OtpSlotData::new(1, "test-hotp", HOTP_SECRET, OtpMode::SixDigits); - assert!(admin.write_hotp_slot(slot_data, counter.into()).is_ok()); + assert_eq!(Ok(()), admin.write_hotp_slot(slot_data, counter.into())); } fn check_hotp_codes(device: &GenerateOtp, offset: u8) { @@ -71,7 +71,7 @@ fn set_time(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_config(config)); configure_hotp(&admin, 0); check_hotp_codes(admin.deref(), 0); @@ -87,7 +87,7 @@ fn hotp_no_pin(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_config(config)); configure_hotp(&admin, 0); let user = admin.device().authenticate_user(USER_PASSWORD).unwrap(); @@ -100,7 +100,7 @@ fn hotp_pin(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_hotp_slot(slot_data, 0)); let device = admin.device(); let result = device.get_hotp_slot_name(1); @@ -130,13 +130,13 @@ fn hotp_error(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_config(config)); let slot_data = OtpSlotData::new(1, "test1", HOTP_SECRET, OtpMode::SixDigits); - assert!(admin.write_hotp_slot(slot_data, 0).is_ok()); + assert_eq!(Ok(()), admin.write_hotp_slot(slot_data, 0)); let slot_data = OtpSlotData::new(2, "test2", HOTP_SECRET, OtpMode::SixDigits); - assert!(admin.write_hotp_slot(slot_data, 0).is_ok()); + assert_eq!(Ok(()), admin.write_hotp_slot(slot_data, 0)); - assert!(admin.erase_hotp_slot(1).is_ok()); + assert_eq!(Ok(()), admin.erase_hotp_slot(1)); let device = admin.device(); let result = device.get_hotp_slot_name(1); @@ -150,7 +150,7 @@ fn hotp_erase(device: DeviceWrapper) { 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!(admin.write_totp_slot(slot_data, time_window as u16).is_ok()); + assert_eq!(Ok(()), admin.write_totp_slot(slot_data, time_window as u16)); } fn check_totp_codes(device: &GenerateOtp, factor: u64, timestamp_size: TotpTimestampSize) { @@ -161,7 +161,7 @@ fn check_totp_codes(device: &GenerateOtp, factor: u64, timestamp_size: TotpTimes continue; } - assert!(device.set_time(time, true).is_ok()); + assert_eq!(Ok(()), device.set_time(time, true)); let result = device.get_totp_code(1); assert!(result.is_ok()); let result_code = result.unwrap(); @@ -178,7 +178,7 @@ fn totp_no_pin(device: DeviceWrapper) { // TODO: this test may fail due to bad timing --> find solution let admin = make_admin_test_device(device); let config = Config::new(None, None, None, false); - assert!(admin.write_config(config).is_ok()); + assert_eq!(Ok(()), admin.write_config(config)); configure_totp(&admin, 1); check_totp_codes(admin.deref(), 1, TotpTimestampSize::U32); @@ -196,7 +196,7 @@ fn totp_no_pin(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_config(config)); configure_totp(&admin, 1); check_totp_codes(admin.deref(), 1, TotpTimestampSize::U64); @@ -213,7 +213,7 @@ fn totp_pin(device: DeviceWrapper) { // TODO: this test may fail due to bad timing --> find solution let admin = make_admin_test_device(device); let config = Config::new(None, None, None, true); - assert!(admin.write_config(config).is_ok()); + assert_eq!(Ok(()), admin.write_config(config)); configure_totp(&admin, 1); let user = admin.device().authenticate_user(USER_PASSWORD).unwrap(); @@ -227,7 +227,7 @@ fn totp_pin(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_config(config)); configure_totp(&admin, 1); let user = admin.device().authenticate_user(USER_PASSWORD).unwrap(); @@ -240,7 +240,7 @@ fn totp_pin_64(device: Pro) { 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()); + assert_eq!(Ok(()), admin.write_totp_slot(slot_data, 0)); let device = admin.device(); let result = device.get_totp_slot_name(1); @@ -271,13 +271,13 @@ fn totp_error(device: DeviceWrapper) { 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()); + assert_eq!(Ok(()), admin.write_config(config)); let slot_data = OtpSlotData::new(1, "test1", TOTP_SECRET, OtpMode::SixDigits); - assert!(admin.write_totp_slot(slot_data, 0).is_ok()); + assert_eq!(Ok(()), admin.write_totp_slot(slot_data, 0)); let slot_data = OtpSlotData::new(2, "test2", TOTP_SECRET, OtpMode::SixDigits); - assert!(admin.write_totp_slot(slot_data, 0).is_ok()); + assert_eq!(Ok(()), admin.write_totp_slot(slot_data, 0)); - assert!(admin.erase_totp_slot(1).is_ok()); + assert_eq!(Ok(()), admin.erase_totp_slot(1)); let device = admin.device(); let result = device.get_totp_slot_name(1); diff --git a/tests/pws.rs b/tests/pws.rs index 4a2ca4f..b349558 100644 --- a/tests/pws.rs +++ b/tests/pws.rs @@ -49,14 +49,14 @@ fn enable(device: DeviceWrapper) { fn drop(device: DeviceWrapper) { { let pws = get_pws(&device); - assert!(pws.write_slot(1, "name", "login", "password").is_ok()); + assert_eq!(Ok(()), pws.write_slot(1, "name", "login", "password")); assert_eq!("name", pws.get_slot_name(1).unwrap()); let result = get_slot_name_direct(1); assert_eq!(Ok(String::from("name")), result); } let result = get_slot_name_direct(1); assert_eq!(Ok(String::from("name")), result); - assert!(device.lock().is_ok()); + assert_eq!(Ok(()), device.lock()); let result = get_slot_name_direct(1); assert_eq!(Err(CommandError::NotAuthorized), result); } @@ -65,19 +65,19 @@ fn drop(device: DeviceWrapper) { fn get_status(device: DeviceWrapper) { let pws = get_pws(&device); for i in 0..SLOT_COUNT { - assert!(pws.erase_slot(i).is_ok(), "Could not erase slot {}", i); + assert_eq!(Ok(()), pws.erase_slot(i), "Could not erase slot {}", i); } let status = pws.get_slot_status().unwrap(); assert_eq!(status, [false; SLOT_COUNT as usize]); - assert!(pws.write_slot(1, "name", "login", "password").is_ok()); + assert_eq!(Ok(()), pws.write_slot(1, "name", "login", "password")); let status = pws.get_slot_status().unwrap(); for i in 0..SLOT_COUNT { assert_eq!(i == 1, status[i as usize]); } for i in 0..SLOT_COUNT { - assert!(pws.write_slot(i, "name", "login", "password").is_ok()); + assert_eq!(Ok(()), pws.write_slot(i, "name", "login", "password")); } let status = pws.get_slot_status().unwrap(); assert_eq!(status, [true; SLOT_COUNT as usize]); @@ -86,12 +86,12 @@ fn get_status(device: DeviceWrapper) { #[test_device] fn get_data(device: DeviceWrapper) { let pws = get_pws(&device); - assert!(pws.write_slot(1, "name", "login", "password").is_ok()); + assert_eq!(Ok(()), pws.write_slot(1, "name", "login", "password")); assert_eq!("name", pws.get_slot_name(1).unwrap()); assert_eq!("login", pws.get_slot_login(1).unwrap()); assert_eq!("password", pws.get_slot_password(1).unwrap()); - assert!(pws.erase_slot(1).is_ok()); + assert_eq!(Ok(()), pws.erase_slot(1)); // TODO: check error codes assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(1)); assert_eq!(Err(CommandError::Undefined), pws.get_slot_login(1)); @@ -100,7 +100,7 @@ fn get_data(device: DeviceWrapper) { let name = "with å"; let login = "pär@test.com"; let password = "'i3lJc[09?I:,[u7dWz9"; - assert!(pws.write_slot(1, name, login, password).is_ok()); + assert_eq!(Ok(()), pws.write_slot(1, name, login, password)); assert_eq!(name, pws.get_slot_name(1).unwrap()); assert_eq!(login, pws.get_slot_login(1).unwrap()); assert_eq!(password, pws.get_slot_password(1).unwrap()); @@ -128,17 +128,17 @@ fn write(device: DeviceWrapper) { pws.write_slot(SLOT_COUNT, "name", "login", "password") ); - assert!(pws.write_slot(0, "", "login", "password").is_ok()); + assert_eq!(Ok(()), pws.write_slot(0, "", "login", "password")); assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(0)); assert_eq!(Ok(String::from("login")), pws.get_slot_login(0)); assert_eq!(Ok(String::from("password")), pws.get_slot_password(0)); - assert!(pws.write_slot(0, "name", "", "password").is_ok()); + assert_eq!(Ok(()), pws.write_slot(0, "name", "", "password")); assert_eq!(Ok(String::from("name")), pws.get_slot_name(0)); assert_eq!(Err(CommandError::Undefined), pws.get_slot_login(0)); assert_eq!(Ok(String::from("password")), pws.get_slot_password(0)); - assert!(pws.write_slot(0, "name", "login", "").is_ok()); + assert_eq!(Ok(()), pws.write_slot(0, "name", "login", "")); assert_eq!(Ok(String::from("name")), pws.get_slot_name(0)); assert_eq!(Ok(String::from("login")), pws.get_slot_login(0)); assert_eq!(Err(CommandError::Undefined), pws.get_slot_password(0)); @@ -149,8 +149,8 @@ fn erase(device: DeviceWrapper) { let pws = get_pws(&device); assert_eq!(Err(CommandError::InvalidSlot), pws.erase_slot(SLOT_COUNT)); - assert!(pws.write_slot(0, "name", "login", "password").is_ok()); - assert!(pws.erase_slot(0).is_ok()); - assert!(pws.erase_slot(0).is_ok()); + assert_eq!(Ok(()), pws.write_slot(0, "name", "login", "password")); + assert_eq!(Ok(()), pws.erase_slot(0)); + assert_eq!(Ok(()), pws.erase_slot(0)); assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(0)); } -- cgit v1.2.1