From ba506bfa085064b9be3e262806d2f5f4ca522aee Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 5 Jan 2019 17:46:42 -0800 Subject: 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. --- nitrocli/src/tests/mod.rs | 113 +++++++++++++++++++++++++++++++++++++++++++ nitrocli/src/tests/run.rs | 48 ++++++++++++++++++ nitrocli/src/tests/status.rs | 65 +++++++++++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 nitrocli/src/tests/mod.rs create mode 100644 nitrocli/src/tests/run.rs create mode 100644 nitrocli/src/tests/status.rs (limited to 'nitrocli/src/tests') 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 . * +// ************************************************************************* + +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` 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 = 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(device: Option, args: &[&'static str], f: F) -> (R, Vec, Vec) + where + F: FnOnce(&mut RunCtx<'_>, Vec) -> 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(device: Option, args: &[&'static str]) -> (i32, Vec, Vec) + where + I: IntoArg, + { + do_run(device, args, |c, a| crate::run(c, a)) + } + + /// Run `nitrocli`'s `handle_arguments` function. + pub fn handle(device: Option, args: &[&'static str]) -> Result + 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 . * +// ************************************************************************* + +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 . * +// ************************************************************************* + +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(()) +} -- cgit v1.2.1