diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-27 23:34:04 +0000 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-28 00:36:53 +0100 | 
| commit | 1d68e24db4078ad1a004afd7bec90a81e7d31ec8 (patch) | |
| tree | fafbdf83ccf00838adf224bec0813320a23d75cc | |
| parent | c30cbd35ba187cd6e5055d3beb8420b11fb030ec (diff) | |
| download | nitrokey-rs-1d68e24db4078ad1a004afd7bec90a81e7d31ec8.tar.gz nitrokey-rs-1d68e24db4078ad1a004afd7bec90a81e7d31ec8.tar.bz2 | |
Add get_firmware_version method
This patch combines the get_{major,minor}_firmware_version methods into
the new get_firmware_version method that returns a FirmwareVersion
struct.  Currently, this requires casting from i32 to u8.  But this will
be fixed with the next libnitrokey version as we change the return types
for the firmware getters.
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | src/device.rs | 45 | ||||
| -rw-r--r-- | tests/device.rs | 7 | 
3 files changed, 20 insertions, 33 deletions
| diff --git a/CHANGELOG.md b/CHANGELOG.md index 25a8c31..c51727e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ SPDX-License-Identifier: MIT  - Implement `From<(T: Device, Error)>` for `Error`.  - Fix timing issues with the `totp_no_pin` and `totp_pin` test cases.  - Always return a `Result` in functions that communicate with a device. +- Combine `get_{major,minor}_firmware_version` into `get_firmware_version`.  # v0.3.4 (2019-01-20)  - Fix authentication methods that assumed that `char` is signed. diff --git a/src/device.rs b/src/device.rs index 4178922..2fac4f2 100644 --- a/src/device.rs +++ b/src/device.rs @@ -379,7 +379,7 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp + fmt::Debug {          result_or_error(unsafe { nitrokey_sys::NK_get_admin_retry_count() })      } -    /// Returns the major part of the firmware version (should be zero). +    /// Returns the firmware version.      ///      /// # Example      /// @@ -389,37 +389,24 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp + fmt::Debug {      ///      /// # fn try_main() -> Result<(), Error> {      /// let device = nitrokey::connect()?; -    /// println!( -    ///     "Firmware version: {}.{}", -    ///     device.get_major_firmware_version().unwrap(), -    ///     device.get_minor_firmware_version().unwrap(), -    /// ); +    /// match device.get_firmware_version() { +    ///     Ok(version) => println!("Firmware version: {}", version), +    ///     Err(err) => println!("Could not access firmware version: {}", err), +    /// };      /// #     Ok(())      /// # }      /// ``` -    fn get_major_firmware_version(&self) -> Result<i32, Error> { -        result_or_error(unsafe { nitrokey_sys::NK_get_major_firmware_version() }) -    } - -    /// Returns the minor part of the firmware version (for example 8 for version 0.8). -    /// -    /// # Example -    /// -    /// ```no_run -    /// use nitrokey::Device; -    /// # use nitrokey::Error; -    /// -    /// # fn try_main() -> Result<(), Error> { -    /// let device = nitrokey::connect()?; -    /// println!( -    ///     "Firmware version: {}.{}", -    ///     device.get_major_firmware_version().unwrap(), -    ///     device.get_minor_firmware_version().unwrap(), -    /// ); -    /// #     Ok(()) -    /// # } -    fn get_minor_firmware_version(&self) -> Result<i32, Error> { -        result_or_error(unsafe { nitrokey_sys::NK_get_minor_firmware_version() }) +    fn get_firmware_version(&self) -> Result<FirmwareVersion, Error> { +        let major = result_or_error(unsafe { nitrokey_sys::NK_get_major_firmware_version() })?; +        let minor = result_or_error(unsafe { nitrokey_sys::NK_get_minor_firmware_version() })?; +        let max = i32::from(u8::max_value()); +        if major < 0 || minor < 0 || major > max || minor > max { +            return Err(Error::UnexpectedError); +        } +        Ok(FirmwareVersion { +            major: major as u8, +            minor: minor as u8, +        })      }      /// Returns the current configuration of the Nitrokey device. diff --git a/tests/device.rs b/tests/device.rs index 7ab4d66..d80f011 100644 --- a/tests/device.rs +++ b/tests/device.rs @@ -95,10 +95,9 @@ fn get_serial_number(device: DeviceWrapper) {  }  #[test_device]  fn get_firmware_version(device: Pro) { -    assert_ok!(0, device.get_major_firmware_version()); -    let minor = device.get_minor_firmware_version(); -    assert!(minor.is_ok()); -    assert!(minor.unwrap() > 0); +    let version = device.get_firmware_version().unwrap(); +    assert_eq!(0, version.major); +    assert!(version.minor > 0);  }  fn admin_retry<T: Authenticate + Device>(device: T, suffix: &str, count: u8) -> T { | 
