aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/tests
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-05 17:46:42 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-05 17:46:42 -0800
commitba506bfa085064b9be3e262806d2f5f4ca522aee (patch)
tree65b2faf2fc9e5cb897928ba85aa3445974b74709 /nitrocli/src/tests
parentb4516220b95743b485b9bcd8226285255be9c9c4 (diff)
downloadnitrocli-ba506bfa085064b9be3e262806d2f5f4ca522aee.tar.gz
nitrocli-ba506bfa085064b9be3e262806d2f5f4ca522aee.tar.bz2
Add first set of integration tests
This change introduces the first set of integration-style test for the application. Those tests may or may not connect to an actual Nitrokey device (depending on what they test). We use the nitrokey-test crate's test attribute macro to automatically dispatch tests to connected devices or skip them if a required device is not present. It also provides the means for automatically serializing tests.
Diffstat (limited to 'nitrocli/src/tests')
-rw-r--r--nitrocli/src/tests/mod.rs113
-rw-r--r--nitrocli/src/tests/run.rs48
-rw-r--r--nitrocli/src/tests/status.rs65
3 files changed, 226 insertions, 0 deletions
diff --git a/nitrocli/src/tests/mod.rs b/nitrocli/src/tests/mod.rs
new file mode 100644
index 0000000..474b43c
--- /dev/null
+++ b/nitrocli/src/tests/mod.rs
@@ -0,0 +1,113 @@
+// mod.rs
+
+// *************************************************************************
+// * Copyright (C) 2019 Daniel Mueller (deso@posteo.net) *
+// * *
+// * This program is free software: you can redistribute it and/or modify *
+// * it under the terms of the GNU General Public License as published by *
+// * the Free Software Foundation, either version 3 of the License, or *
+// * (at your option) any later version. *
+// * *
+// * This program is distributed in the hope that it will be useful, *
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+// * GNU General Public License for more details. *
+// * *
+// * You should have received a copy of the GNU General Public License *
+// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+// *************************************************************************
+
+use nitrokey_test::test as test_device;
+
+// TODO: This is a hack to make the nitrokey-test crate work across
+// module boundaries. Upon first use of the nitrokey_test::test
+// macro a new function, __nitrokey_mutex, will be emitted, but it
+// is not visible in a different module. To work around that we
+// trigger the macro here first and then `use super::*` from all
+// of the submodules.
+#[test_device]
+fn dummy() {}
+
+mod run;
+mod status;
+
+/// An `Option<IntoArg>` that represents a non-present device. Rust can
+/// be notoriously bad at inferring type parameters and this constant
+/// alleviates the pain.
+const NO_DEV: Option<nitrokey::Pro> = None;
+
+/// A trait for conversion of a nitrokey::Device into an argument
+/// representing the device model that the program recognizes.
+pub trait IntoArg {
+ fn into_arg(self) -> &'static str;
+}
+
+impl IntoArg for nitrokey::Pro {
+ fn into_arg(self) -> &'static str {
+ "--model=pro"
+ }
+}
+
+impl IntoArg for nitrokey::Storage {
+ fn into_arg(self) -> &'static str {
+ "--model=storage"
+ }
+}
+
+impl IntoArg for nitrokey::DeviceWrapper {
+ fn into_arg(self) -> &'static str {
+ match self {
+ nitrokey::DeviceWrapper::Pro(x) => x.into_arg(),
+ nitrokey::DeviceWrapper::Storage(x) => x.into_arg(),
+ }
+ }
+}
+
+mod nitrocli {
+ use super::*;
+
+ use crate::args;
+ use crate::Result;
+ use crate::RunCtx;
+
+ fn do_run<F, R, I>(device: Option<I>, args: &[&'static str], f: F) -> (R, Vec<u8>, Vec<u8>)
+ where
+ F: FnOnce(&mut RunCtx<'_>, Vec<String>) -> R,
+ I: IntoArg,
+ {
+ let args = ["nitrocli"]
+ .into_iter()
+ .cloned()
+ .chain(device.into_iter().map(IntoArg::into_arg))
+ .chain(args.into_iter().cloned())
+ .map(ToOwned::to_owned)
+ .collect();
+
+ let mut stdout = Vec::new();
+ let mut stderr = Vec::new();
+
+ let ctx = &mut RunCtx {
+ stdout: &mut stdout,
+ stderr: &mut stderr,
+ };
+
+ (f(ctx, args), stdout, stderr)
+ }
+
+ /// Run `nitrocli`'s `run` function.
+ pub fn run<I>(device: Option<I>, args: &[&'static str]) -> (i32, Vec<u8>, Vec<u8>)
+ where
+ I: IntoArg,
+ {
+ do_run(device, args, |c, a| crate::run(c, a))
+ }
+
+ /// Run `nitrocli`'s `handle_arguments` function.
+ pub fn handle<I>(device: Option<I>, args: &[&'static str]) -> Result<String>
+ where
+ I: IntoArg,
+ {
+ let (res, out, _) = do_run(device, args, |c, a| args::handle_arguments(c, a));
+ res.map(|_| String::from_utf8_lossy(&out).into_owned())
+ }
+}
diff --git a/nitrocli/src/tests/run.rs b/nitrocli/src/tests/run.rs
new file mode 100644
index 0000000..51c2b87
--- /dev/null
+++ b/nitrocli/src/tests/run.rs
@@ -0,0 +1,48 @@
+// run.rs
+
+// *************************************************************************
+// * Copyright (C) 2019 Daniel Mueller (deso@posteo.net) *
+// * *
+// * This program is free software: you can redistribute it and/or modify *
+// * it under the terms of the GNU General Public License as published by *
+// * the Free Software Foundation, either version 3 of the License, or *
+// * (at your option) any later version. *
+// * *
+// * This program is distributed in the hope that it will be useful, *
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+// * GNU General Public License for more details. *
+// * *
+// * You should have received a copy of the GNU General Public License *
+// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+// *************************************************************************
+
+use super::*;
+use crate::tests::nitrocli;
+
+#[test]
+fn no_command_or_option() {
+ let (rc, out, err) = nitrocli::run(NO_DEV, &[]);
+
+ assert_ne!(rc, 0);
+ assert_eq!(out, b"");
+
+ let s = String::from_utf8_lossy(&err).into_owned();
+ assert!(s.starts_with("Usage:\n"), s);
+}
+
+#[test]
+fn help_option() {
+ fn test(opt: &'static str) {
+ let (rc, out, err) = nitrocli::run(NO_DEV, &[opt]);
+
+ assert_eq!(rc, 0);
+ assert_eq!(err, b"");
+
+ let s = String::from_utf8_lossy(&out).into_owned();
+ assert!(s.starts_with("Usage:\n"), s);
+ }
+
+ test("--help");
+ test("-h")
+}
diff --git a/nitrocli/src/tests/status.rs b/nitrocli/src/tests/status.rs
new file mode 100644
index 0000000..e8a4f51
--- /dev/null
+++ b/nitrocli/src/tests/status.rs
@@ -0,0 +1,65 @@
+// status.rs
+
+// *************************************************************************
+// * Copyright (C) 2019 Daniel Mueller (deso@posteo.net) *
+// * *
+// * This program is free software: you can redistribute it and/or modify *
+// * it under the terms of the GNU General Public License as published by *
+// * the Free Software Foundation, either version 3 of the License, or *
+// * (at your option) any later version. *
+// * *
+// * This program is distributed in the hope that it will be useful, *
+// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+// * GNU General Public License for more details. *
+// * *
+// * You should have received a copy of the GNU General Public License *
+// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
+// *************************************************************************
+
+use super::*;
+use crate::tests::nitrocli;
+
+// This test acts as verification that conversion of Error::Error
+// variants into the proper exit code works properly.
+#[test_device]
+fn not_found_raw() {
+ let (rc, out, err) = nitrocli::run(NO_DEV, &["status"]);
+
+ assert_ne!(rc, 0);
+ assert_eq!(out, b"");
+ assert_eq!(err, b"Nitrokey device not found\n");
+}
+
+#[test_device]
+fn not_found() {
+ match nitrocli::handle(NO_DEV, &["status"]) {
+ Ok(_) => assert!(false),
+ Err(err) => {
+ // Unfortunately we can't directly compare against the error
+ // because not all of the variants implement PartialEq.
+ match err {
+ crate::Error::Error(x) => assert_eq!(x, "Nitrokey device not found".to_string()),
+ _ => assert!(false, err),
+ }
+ }
+ }
+}
+
+#[test_device]
+fn output(device: nitrokey::DeviceWrapper) -> crate::Result<()> {
+ let re = regex::Regex::new(
+ r#"^Status:
+ model: (Pro|Storage)
+ serial number: 0x[[:xdigit:]]{8}
+ firmware version: \d+.\d+
+ user retry count: [0-3]
+ admin retry count: [0-3]
+$"#,
+ )
+ .unwrap();
+
+ let out = nitrocli::handle(Some(device), &["status"])?;
+ assert!(re.is_match(&out), out);
+ Ok(())
+}