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 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 nitrocli/src/tests/mod.rs (limited to 'nitrocli/src/tests/mod.rs') 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()) + } +} -- cgit v1.2.1