aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-09-08 18:23:30 +0200
committerDaniel Mueller <deso@posteo.net>2020-09-09 08:55:50 -0700
commit4a8c01adb5100fd0397aad239edc5e80d13aca13 (patch)
tree8bd215f32d2fee5b185efbcd36888c6fe44ffaea /src/tests
parent16f6b3ba0c3535efd1b9288ea1980cdd281b6565 (diff)
downloadnitrocli-4a8c01adb5100fd0397aad239edc5e80d13aca13.tar.gz
nitrocli-4a8c01adb5100fd0397aad239edc5e80d13aca13.tar.bz2
Add --usb-path option to select device
This patch adds the --usb-path option as an additional way to filter the Nitrokey device to connect to. While the serial number is a better identifier in theory, the Nitrokey Storage devices do not send their serial number in the USB device descriptor. Having the --usb-path options allows users to select one of multiple Nitrokey Storage devices. While we could directly call the nitrokey::Manager::connect_path function with the specified path, we integrate the --usb-path option into the existing find_device function for consistent error messages and to avoid having to duplicate the --model and --serial-number checks.
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/run.rs92
1 files changed, 90 insertions, 2 deletions
diff --git a/src/tests/run.rs b/src/tests/run.rs
index 4f53608..b39b1da 100644
--- a/src/tests/run.rs
+++ b/src/tests/run.rs
@@ -4,6 +4,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
use std::collections;
+use std::ops;
use std::path;
use super::*;
@@ -116,7 +117,7 @@ fn connect_multiple(_model: nitrokey::Model) -> anyhow::Result<()> {
let err = res.unwrap_err().to_string();
assert_eq!(
err,
- "Multiple Nitrokey devices found. Use the --model and --serial-number options to select one"
+ "Multiple Nitrokey devices found. Use the --model, --serial-number, and --usb-path options to select one"
);
}
Ok(())
@@ -143,6 +144,32 @@ fn connect_wrong_serial_number(_model: nitrokey::Model) {
}
#[test_device]
+fn connect_usb_path(_model: nitrokey::Model) -> anyhow::Result<()> {
+ for device in nitrokey::list_devices()? {
+ let res = Nitrocli::new().handle(&["status", &format!("--usb-path={}", device.path)]);
+ assert!(res.is_ok());
+ let res = res?;
+ if let Some(model) = device.model {
+ assert!(res.contains(&format!("model: {}\n", model)));
+ }
+ if let Some(sn) = device.serial_number {
+ assert!(res.contains(&format!("serial number: {}\n", sn)));
+ }
+ }
+ Ok(())
+}
+
+#[test_device]
+fn connect_wrong_usb_path(_model: nitrokey::Model) {
+ let res = Nitrocli::new().handle(&["status", "--usb-path=not-a-path"]);
+ let err = res.unwrap_err().to_string();
+ assert_eq!(
+ err,
+ "Nitrokey device not found (filter: usb path=not-a-path)"
+ );
+}
+
+#[test_device]
fn connect_model(_model: nitrokey::Model) -> anyhow::Result<()> {
let devices = nitrokey::list_devices()?;
let mut model_counts = collections::BTreeMap::new();
@@ -172,10 +199,71 @@ fn connect_model(_model: nitrokey::Model) -> anyhow::Result<()> {
format!(
"Multiple Nitrokey devices found (filter: model={}). ",
model.to_lowercase()
- ) + "Use the --model and --serial-number options to select one"
+ ) + "Use the --model, --serial-number, and --usb-path options to select one"
);
}
}
Ok(())
}
+
+#[test_device]
+fn connect_usb_path_model_serial(_model: nitrokey::Model) -> anyhow::Result<()> {
+ let devices = nitrokey::list_devices()?;
+ for device in devices {
+ let mut args = Vec::new();
+ args.push("status".to_owned());
+ args.push(format!("--usb-path={}", device.path));
+ if let Some(model) = device.model {
+ args.push(format!("--model={}", model.to_string().to_lowercase()));
+ }
+ if let Some(sn) = device.serial_number {
+ args.push(format!("--serial-number={}", sn));
+ }
+
+ let res = Nitrocli::new().handle(&args.iter().map(ops::Deref::deref).collect::<Vec<_>>())?;
+ if let Some(model) = device.model {
+ assert!(res.contains(&format!("model: {}\n", model)));
+ }
+ if let Some(sn) = device.serial_number {
+ assert!(res.contains(&format!("serial number: {}\n", sn)));
+ }
+ }
+ Ok(())
+}
+
+#[test_device]
+fn connect_usb_path_model_wrong_serial(_model: nitrokey::Model) -> anyhow::Result<()> {
+ let devices = nitrokey::list_devices()?;
+ for device in devices {
+ let mut args = Vec::new();
+ args.push("status".to_owned());
+ args.push(format!("--usb-path={}", device.path));
+ if let Some(model) = device.model {
+ args.push(format!("--model={}", model.to_string().to_lowercase()));
+ }
+ args.push("--serial-number=0xdeadbeef".to_owned());
+
+ let res = Nitrocli::new().handle(&args.iter().map(ops::Deref::deref).collect::<Vec<_>>());
+ let err = res.unwrap_err().to_string();
+ if let Some(model) = device.model {
+ assert_eq!(
+ err,
+ format!(
+ "Nitrokey device not found (filter: model={}, serial number in [0xdeadbeef], usb path={})",
+ model.to_string().to_lowercase(),
+ device.path
+ )
+ );
+ } else {
+ assert_eq!(
+ err,
+ format!(
+ "Nitrokey device not found (filter: serial number in [0xdeadbeef], usb path={})",
+ device.path
+ )
+ );
+ }
+ }
+ Ok(())
+}