summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-09-07 20:47:41 +0200
committerDaniel Mueller <deso@posteo.net>2021-01-10 17:37:56 -0800
commit9975f8810399d5e535f2e2fd62b4dc56b313f955 (patch)
treef54fd799be96b939dba1ce908c04ffbf336405b3
parentddc6f47970011e2092b3e068198072c18a4a5fb6 (diff)
downloadnitrocli-9975f8810399d5e535f2e2fd62b4dc56b313f955.tar.gz
nitrocli-9975f8810399d5e535f2e2fd62b4dc56b313f955.tar.bz2
Add test cases for new connection logic
This patch adds test cases for the new connection logic: - connect_multiple checks that nitrocli aborts if more than one Nitrokey device is connected and no filter is set. - connect_serial_number checks that we can use the --serial-number option to connect to all supported Nitrokey devices. - connect_wrong_serial_number checks that nitrocli aborts if it can’t find a device with the requested serial number. - connect_model checks that we can use the --model option to select the Nitrokey device to connect to, and that nitrocli aborts if it finds no matching device or multiple matching devices.
-rw-r--r--src/tests/run.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/tests/run.rs b/src/tests/run.rs
index 4d470f6..4f53608 100644
--- a/src/tests/run.rs
+++ b/src/tests/run.rs
@@ -3,6 +3,7 @@
// Copyright (C) 2019-2020 The Nitrocli Developers
// SPDX-License-Identifier: GPL-3.0-or-later
+use std::collections;
use std::path;
use super::*;
@@ -106,3 +107,75 @@ fn config_file() {
assert_eq!(true, config.no_cache);
assert_eq!(2, config.verbosity);
}
+
+#[test_device]
+fn connect_multiple(_model: nitrokey::Model) -> anyhow::Result<()> {
+ let devices = nitrokey::list_devices()?;
+ if devices.len() > 1 {
+ let res = Nitrocli::new().handle(&["status"]);
+ let err = res.unwrap_err().to_string();
+ assert_eq!(
+ err,
+ "Multiple Nitrokey devices found. Use the --model and --serial-number options to select one"
+ );
+ }
+ Ok(())
+}
+
+#[test_device]
+fn connect_serial_number(_model: nitrokey::Model) -> anyhow::Result<()> {
+ let devices = nitrokey::list_devices()?;
+ for serial_number in devices.iter().filter_map(|d| d.serial_number) {
+ let res = Nitrocli::new().handle(&["status", &format!("--serial-number={}", serial_number)])?;
+ assert!(res.contains(&format!("serial number: {}\n", serial_number)));
+ }
+ Ok(())
+}
+
+#[test_device]
+fn connect_wrong_serial_number(_model: nitrokey::Model) {
+ let res = Nitrocli::new().handle(&["status", "--serial-number=0xdeadbeef"]);
+ let err = res.unwrap_err().to_string();
+ assert_eq!(
+ err,
+ "Nitrokey device not found (filter: serial number in [0xdeadbeef])"
+ );
+}
+
+#[test_device]
+fn connect_model(_model: nitrokey::Model) -> anyhow::Result<()> {
+ let devices = nitrokey::list_devices()?;
+ let mut model_counts = collections::BTreeMap::new();
+ let _ = model_counts.insert(nitrokey::Model::Pro.to_string(), 0);
+ let _ = model_counts.insert(nitrokey::Model::Storage.to_string(), 0);
+ for model in devices.iter().filter_map(|d| d.model) {
+ *model_counts.entry(model.to_string()).or_default() += 1;
+ }
+
+ for (model, count) in model_counts {
+ let res = Nitrocli::new().handle(&["status", &format!("--model={}", model.to_lowercase())]);
+ if count == 0 {
+ let err = res.unwrap_err().to_string();
+ assert_eq!(
+ err,
+ format!(
+ "Nitrokey device not found (filter: model={})",
+ model.to_lowercase()
+ )
+ );
+ } else if count == 1 {
+ assert!(res?.contains(&format!("model: {}\n", model)));
+ } else {
+ let err = res.unwrap_err().to_string();
+ assert_eq!(
+ err,
+ format!(
+ "Multiple Nitrokey devices found (filter: model={}). ",
+ model.to_lowercase()
+ ) + "Use the --model and --serial-number options to select one"
+ );
+ }
+ }
+
+ Ok(())
+}