summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-06 23:27:06 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-07 00:29:52 +0100
commit3fab663891d42cfe317125650394c9560639b60c (patch)
tree36d72acd96d49cc1d8da85b920a30a42acc721cb
parentd44f8be7c038bc499b36aa502f4939f41ebebfbf (diff)
downloadnitrokey-rs-3fab663891d42cfe317125650394c9560639b60c.tar.gz
nitrokey-rs-3fab663891d42cfe317125650394c9560639b60c.tar.bz2
Add the connect_model function
This patch adds the global connect_model function that can be used to connect to a Nitrokey device of a given model. Contrary to Pro::connect and Storage::connect, the model does not have to be set at compile time.
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/device.rs29
-rw-r--r--src/lib.rs2
-rw-r--r--tests/device.rs10
4 files changed, 42 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7394cb0..4e046f2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,8 @@
- Add support for the hidden volumes on a Nitrokey Storage
(`enable_hidden_volume`, `disable_hidden_volume` and `create_hidden_volume`
methods for the `Storage` struct).
+- Add the `connect_model` function to connect to a specific model using an enum
+ variant.
# v0.3.0 (2019-01-04)
- Add a `force` argument to `ConfigureOtp::set_time`.
diff --git a/src/device.rs b/src/device.rs
index 88be5b5..2eee08e 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -613,6 +613,35 @@ pub fn connect() -> Result<DeviceWrapper, CommandError> {
}
}
+/// Connects to a Nitrokey device of the given model.
+///
+/// # Errors
+///
+/// - [`Undefined`][] if no Nitrokey device of the given model is connected
+///
+/// # Example
+///
+/// ```
+/// use nitrokey::DeviceWrapper;
+/// use nitrokey::Model;
+///
+/// fn do_something(device: DeviceWrapper) {}
+///
+/// match nitrokey::connect_model(Model::Pro) {
+/// Ok(device) => do_something(device),
+/// Err(err) => println!("Could not connect to a Nitrokey Pro: {}", err),
+/// }
+/// ```
+///
+/// [`Undefined`]: enum.CommandError.html#variant.Undefined
+pub fn connect_model(model: Model) -> Result<DeviceWrapper, CommandError> {
+ if connect_enum(model) {
+ Ok(create_device_wrapper(model))
+ } else {
+ Err(CommandError::Undefined)
+ }
+}
+
fn get_connected_model() -> Option<Model> {
unsafe {
match nitrokey_sys::NK_get_device_model() {
diff --git a/src/lib.rs b/src/lib.rs
index 9f21518..bb34870 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -98,7 +98,7 @@ use nitrokey_sys;
pub use crate::auth::{Admin, Authenticate, User};
pub use crate::config::Config;
pub use crate::device::{
- connect, Device, DeviceWrapper, Model, Pro, Storage, StorageStatus, VolumeStatus,
+ connect, connect_model, Device, DeviceWrapper, Model, Pro, Storage, StorageStatus, VolumeStatus,
};
pub use crate::otp::{ConfigureOtp, GenerateOtp, OtpMode, OtpSlotData};
pub use crate::pws::{GetPasswordSafe, PasswordSafe, SLOT_COUNT};
diff --git a/tests/device.rs b/tests/device.rs
index f7d8df3..db8194c 100644
--- a/tests/device.rs
+++ b/tests/device.rs
@@ -31,6 +31,8 @@ fn count_nitrokey_block_devices() -> usize {
#[test_device]
fn connect_no_device() {
assert!(nitrokey::connect().is_err());
+ assert!(nitrokey::connect_model(nitrokey::Model::Pro).is_err());
+ assert!(nitrokey::connect_model(nitrokey::Model::Storage).is_err());
assert!(nitrokey::Pro::connect().is_err());
assert!(nitrokey::Storage::connect().is_err());
}
@@ -41,7 +43,11 @@ fn connect_pro(device: Pro) {
drop(device);
assert!(nitrokey::connect().is_ok());
+ assert!(nitrokey::connect_model(nitrokey::Model::Pro).is_ok());
assert!(nitrokey::Pro::connect().is_ok());
+
+ assert!(nitrokey::connect_model(nitrokey::Model::Storage).is_err());
+ assert!(nitrokey::Storage::connect().is_err());
}
#[test_device]
@@ -50,7 +56,11 @@ fn connect_storage(device: Storage) {
drop(device);
assert!(nitrokey::connect().is_ok());
+ assert!(nitrokey::connect_model(nitrokey::Model::Storage).is_ok());
assert!(nitrokey::Storage::connect().is_ok());
+
+ assert!(nitrokey::connect_model(nitrokey::Model::Pro).is_err());
+ assert!(nitrokey::Pro::connect().is_err());
}
fn assert_empty_serial_number() {