aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/tests/mod.rs
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/mod.rs
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/mod.rs')
-rw-r--r--nitrocli/src/tests/mod.rs113
1 files changed, 113 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())
+ }
+}