diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-14 17:34:34 +0000 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-14 18:37:07 +0100 | 
| commit | c34b56b2b4c317947fd8fd3ae6c1fa3a773ee775 (patch) | |
| tree | e152045fe0cb067af5724c6bb7ac23c721793700 | |
| parent | 0262ed2e614e9222b69970289a32ddb3683b3535 (diff) | |
| download | nitrokey-rs-c34b56b2b4c317947fd8fd3ae6c1fa3a773ee775.tar.gz nitrokey-rs-c34b56b2b4c317947fd8fd3ae6c1fa3a773ee775.tar.bz2 | |
Add the clear_new_sd_card_warning method to Storage
The clear_new_sd_card_warning method calls the libnitrokey
NK_clear_new_sd_card_warning function to reset the corresponding flag in
the Storage status.
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | TODO.md | 1 | ||||
| -rw-r--r-- | src/device.rs | 37 | ||||
| -rw-r--r-- | tests/device.rs | 16 | 
4 files changed, 54 insertions, 3 deletions
| diff --git a/CHANGELOG.md b/CHANGELOG.md index 7281f78..4969c00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@  # Unreleased -- Add the `get_production_info` method to the `Storage` struct. +- Add the `get_production_info` and `clear_new_sd_card_warning` methods to the +  `Storage` struct.  # v0.3.2 (2019-01-12)  - Make three additional error codes known: `CommandError::StringTooLong`, @@ -1,7 +1,6 @@  - Add support for the currently unsupported commands:      - `NK_is_AES_supported`      - `NK_send_startup` -    - `NK_clear_new_sd_card_warning`      - `NK_fill_SD_card_with_random_data`      - `NK_get_SD_usage_data_as_string`      - `NK_get_progress_bar_value` diff --git a/src/device.rs b/src/device.rs index 4032db6..1e347f4 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1240,6 +1240,41 @@ impl Storage {          result.and(Ok(StorageProductionInfo::from(raw_data)))      } +    /// Clears the warning for a new SD card. +    /// +    /// The Storage status contains a field for a new SD card warning.  After a factory reset, the +    /// field is set to true.  After filling the SD card with random data, it is set to false. +    /// This method can be used to set it to false without filling the SD card with random data. +    /// +    /// # Errors +    /// +    /// - [`InvalidString`][] if the provided password contains a null byte +    /// - [`WrongPassword`][] if the provided admin password is wrong +    /// +    /// # Example +    /// +    /// ```no_run +    /// # use nitrokey::CommandError; +    /// +    /// # fn try_main() -> Result<(), CommandError> { +    /// let device = nitrokey::Storage::connect()?; +    /// match device.clear_new_sd_card_warning("12345678") { +    ///     Ok(()) => println!("Cleared the new SD card warning."), +    ///     Err(err) => println!("Could not set the clear the new SD card warning: {}", err), +    /// }; +    /// #     Ok(()) +    /// # } +    /// ``` +    /// +    /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString +    /// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword +    pub fn clear_new_sd_card_warning(&self, admin_pin: &str) -> Result<(), CommandError> { +        let admin_pin = get_cstring(admin_pin)?; +        get_command_result(unsafe { +            nitrokey_sys::NK_clear_new_sd_card_warning(admin_pin.as_ptr()) +        }) +    } +      /// Blinks the red and green LED alternatively and infinitely until the device is reconnected.      pub fn wink(&self) -> Result<(), CommandError> {          get_command_result(unsafe { nitrokey_sys::NK_wink() }) @@ -1297,7 +1332,7 @@ impl From<nitrokey_sys::NK_storage_ProductionTest> for StorageProductionInfo {                  manufacturing_month: data.SD_Card_ManufacturingMonth_u8,                  oem: data.SD_Card_OEM_u16,                  manufacturer: data.SD_Card_Manufacturer_u8, -            } +            },          }      }  } diff --git a/tests/device.rs b/tests/device.rs index 915bd3a..9e2bba2 100644 --- a/tests/device.rs +++ b/tests/device.rs @@ -460,6 +460,22 @@ fn get_production_info(device: Storage) {  }  #[test_device] +fn clear_new_sd_card_warning(device: Storage) { +    assert_eq!(Ok(()), device.factory_reset(ADMIN_PASSWORD)); + +    // We have to perform an SD card operation to reset the new_sd_card_found field +    assert_eq!(Ok(()), device.lock()); + +    let status = device.get_status().unwrap(); +    assert!(status.new_sd_card_found); + +    assert_eq!(Ok(()), device.clear_new_sd_card_warning(ADMIN_PASSWORD)); + +    let status = device.get_status().unwrap(); +    assert!(!status.new_sd_card_found); +} + +#[test_device]  fn export_firmware(device: Storage) {      assert_eq!(          Err(CommandError::WrongPassword), | 
