diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-16 03:16:01 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-16 20:17:16 +0100 |
commit | 79a3298ab5973a47cb68ef48e554ec88fb7e99a6 (patch) | |
tree | 1b513b99f19660c74d2b6f20b2ecce9268438ad0 | |
parent | 3c8092cd937d6f449b1959eab9e7e15549970d85 (diff) | |
download | nitrokey-rs-79a3298ab5973a47cb68ef48e554ec88fb7e99a6.tar.gz nitrokey-rs-79a3298ab5973a47cb68ef48e554ec88fb7e99a6.tar.bz2 |
pws: Interpret empty strings as unprogrammed slots
Until the last commit, all empty strings returned by the library were
interepreted as errors. As the PWS functions return empty strings for
unprogrammed slots, the methods to access the PWS data returned an error
when querying a slot that is not programmed. Since the last commit,
they return an empty string instead.
This patch restores the old behavior by returning an error instead of an
empty string. Yet we change the error variant: SlotNotProgrammed
instead of Undefined.
-rw-r--r-- | src/pws.rs | 29 | ||||
-rw-r--r-- | tests/pws.rs | 23 |
2 files changed, 37 insertions, 15 deletions
@@ -129,6 +129,14 @@ fn get_password_safe<'a>( result.map(|()| PasswordSafe { _device: device }) } +fn get_pws_result(s: String) -> Result<String, CommandError> { + if s.is_empty() { + Err(CommandError::SlotNotProgrammed) + } else { + Ok(s) + } +} + impl<'a> PasswordSafe<'a> { /// Returns the status of all password slots. /// @@ -172,10 +180,12 @@ impl<'a> PasswordSafe<'a> { /// Returns the name of the given slot (if it is programmed). /// + /// This method also returns a `SlotNotProgrammed` error if the name is empty. + /// /// # Errors /// /// - [`InvalidSlot`][] if the given slot is out of range - /// - [`Undefined`][] if the slot is not programmed + /// - [`SlotNotProgrammed`][] if the slot is not programmed /// /// # Example /// @@ -199,17 +209,20 @@ impl<'a> PasswordSafe<'a> { /// ``` /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot - /// [`Undefined`]: enum.CommandError.html#variant.Undefined + /// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed pub fn get_slot_name(&self, slot: u8) -> Result<String, CommandError> { unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_name(slot)) } + .and_then(get_pws_result) } /// Returns the login for the given slot (if it is programmed). /// + /// This method also returns a `SlotNotProgrammed` error if the login is empty. + /// /// # Errors /// /// - [`InvalidSlot`][] if the given slot is out of range - /// - [`Undefined`][] if the slot is not programmed + /// - [`SlotNotProgrammed`][] if the slot is not programmed /// /// # Example /// @@ -229,17 +242,20 @@ impl<'a> PasswordSafe<'a> { /// ``` /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot - /// [`Undefined`]: enum.CommandError.html#variant.Undefined + /// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed pub fn get_slot_login(&self, slot: u8) -> Result<String, CommandError> { unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_login(slot)) } + .and_then(get_pws_result) } /// Returns the password for the given slot (if it is programmed). /// + /// This method also returns a `SlotNotProgrammed` error if the password is empty. + /// /// # Errors /// /// - [`InvalidSlot`][] if the given slot is out of range - /// - [`Undefined`][] if the slot is not programmed + /// - [`SlotNotProgrammed`][] if the slot is not programmed /// /// # Example /// @@ -259,9 +275,10 @@ impl<'a> PasswordSafe<'a> { /// ``` /// /// [`InvalidSlot`]: enum.CommandError.html#variant.InvalidSlot - /// [`Undefined`]: enum.CommandError.html#variant.Undefined + /// [`SlotNotProgrammed`]: enum.CommandError.html#variant.SlotNotProgrammed pub fn get_slot_password(&self, slot: u8) -> Result<String, CommandError> { unsafe { result_from_string(nitrokey_sys::NK_get_password_safe_slot_password(slot)) } + .and_then(get_pws_result) } /// Writes the given slot with the given name, login and password. diff --git a/tests/pws.rs b/tests/pws.rs index b349558..fbcc0c1 100644 --- a/tests/pws.rs +++ b/tests/pws.rs @@ -20,7 +20,7 @@ fn get_slot_name_direct(slot: u8) -> Result<String, CommandError> { true => { let error = unsafe { nitrokey_sys::NK_get_last_command_status() } as c_int; match error { - 0 => Err(CommandError::Undefined), + 0 => Ok(s), other => Err(CommandError::from(other)), } } @@ -92,10 +92,12 @@ fn get_data(device: DeviceWrapper) { assert_eq!("password", pws.get_slot_password(1).unwrap()); 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)); - assert_eq!(Err(CommandError::Undefined), pws.get_slot_password(1)); + assert_eq!(Err(CommandError::SlotNotProgrammed), pws.get_slot_name(1)); + assert_eq!(Err(CommandError::SlotNotProgrammed), pws.get_slot_login(1)); + assert_eq!( + Err(CommandError::SlotNotProgrammed), + pws.get_slot_password(1) + ); let name = "with å"; let login = "pär@test.com"; @@ -129,19 +131,22 @@ fn write(device: DeviceWrapper) { ); assert_eq!(Ok(()), pws.write_slot(0, "", "login", "password")); - assert_eq!(Err(CommandError::Undefined), pws.get_slot_name(0)); + assert_eq!(Err(CommandError::SlotNotProgrammed), 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_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!(Err(CommandError::SlotNotProgrammed), pws.get_slot_login(0)); assert_eq!(Ok(String::from("password")), pws.get_slot_password(0)); 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)); + assert_eq!( + Err(CommandError::SlotNotProgrammed), + pws.get_slot_password(0) + ); } #[test_device] @@ -152,5 +157,5 @@ fn erase(device: DeviceWrapper) { 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)); + assert_eq!(Err(CommandError::SlotNotProgrammed), pws.get_slot_name(0)); } |