diff options
Diffstat (limited to 'nitrocli/src/tests')
-rw-r--r-- | nitrocli/src/tests/config.rs | 66 | ||||
-rw-r--r-- | nitrocli/src/tests/lock.rs | 43 | ||||
-rw-r--r-- | nitrocli/src/tests/mod.rs | 180 | ||||
-rw-r--r-- | nitrocli/src/tests/otp.rs | 114 | ||||
-rw-r--r-- | nitrocli/src/tests/pin.rs | 64 | ||||
-rw-r--r-- | nitrocli/src/tests/pws.rs | 123 | ||||
-rw-r--r-- | nitrocli/src/tests/reset.rs | 54 | ||||
-rw-r--r-- | nitrocli/src/tests/run.rs | 66 | ||||
-rw-r--r-- | nitrocli/src/tests/status.rs | 55 | ||||
-rw-r--r-- | nitrocli/src/tests/storage.rs | 117 |
10 files changed, 0 insertions, 882 deletions
diff --git a/nitrocli/src/tests/config.rs b/nitrocli/src/tests/config.rs deleted file mode 100644 index 8983cb8..0000000 --- a/nitrocli/src/tests/config.rs +++ /dev/null @@ -1,66 +0,0 @@ -// config.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::*; - -#[test_device] -fn get(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let re = regex::Regex::new( - r#"^Config: - numlock binding: (not set|\d+) - capslock binding: (not set|\d+) - scrollock binding: (not set|\d+) - require user PIN for OTP: (true|false) -$"#, - ) - .unwrap(); - - let out = Nitrocli::with_dev(device).handle(&["config", "get"])?; - assert!(re.is_match(&out), out); - Ok(()) -} - -#[test_device] -fn set_wrong_usage(device: nitrokey::DeviceWrapper) { - let res = Nitrocli::with_dev(device).handle(&["config", "set", "--numlock", "2", "-N"]); - assert_eq!( - res.unwrap_str_err(), - "--numlock and --no-numlock are mutually exclusive" - ); -} - -#[test_device] -fn set_get(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["config", "set", "-s", "1", "-c", "0", "-N"])?; - - let re = regex::Regex::new( - r#"^Config: - numlock binding: not set - capslock binding: 0 - scrollock binding: 1 - require user PIN for OTP: (true|false) -$"#, - ) - .unwrap(); - - let out = ncli.handle(&["config", "get"])?; - assert!(re.is_match(&out), out); - Ok(()) -} diff --git a/nitrocli/src/tests/lock.rs b/nitrocli/src/tests/lock.rs deleted file mode 100644 index 1993350..0000000 --- a/nitrocli/src/tests/lock.rs +++ /dev/null @@ -1,43 +0,0 @@ -// lock.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::*; - -#[test_device] -fn lock_pro(device: nitrokey::Pro) -> crate::Result<()> { - // We can't really test much more here than just success of the command. - let out = Nitrocli::with_dev(device).handle(&["lock"])?; - assert!(out.is_empty()); - - Ok(()) -} - -#[test_device] -fn lock_storage(device: nitrokey::Storage) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["storage", "open"])?; - - let out = ncli.handle(&["lock"])?; - assert!(out.is_empty()); - - let device = nitrokey::Storage::connect()?; - assert!(!device.get_status()?.encrypted_volume.active); - - Ok(()) -} diff --git a/nitrocli/src/tests/mod.rs b/nitrocli/src/tests/mod.rs deleted file mode 100644 index b1e1618..0000000 --- a/nitrocli/src/tests/mod.rs +++ /dev/null @@ -1,180 +0,0 @@ -// 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 std::ffi; -use std::fmt; - -use nitrokey_test::test as test_device; - -// TODO: Those defines should potentially be taken from the `nitrokey` -// crate, once exported. -const NITROKEY_DEFAULT_ADMIN_PIN: &str = "12345678"; -const NITROKEY_DEFAULT_USER_PIN: &str = "123456"; - -// 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 config; -mod lock; -mod otp; -mod pin; -mod pws; -mod reset; -mod run; -mod status; -mod storage; - -/// A trait simplifying checking for expected errors. -pub trait UnwrapError { - /// Unwrap an Error::Error variant. - fn unwrap_str_err(self) -> String; - /// Unwrap a Error::CommandError variant. - fn unwrap_cmd_err(self) -> (Option<&'static str>, nitrokey::CommandError); -} - -impl<T> UnwrapError for crate::Result<T> -where - T: fmt::Debug, -{ - fn unwrap_str_err(self) -> String { - match self.unwrap_err() { - crate::Error::Error(err) => err, - err => panic!("Unexpected error variant found: {:?}", err), - } - } - - fn unwrap_cmd_err(self) -> (Option<&'static str>, nitrokey::CommandError) { - match self.unwrap_err() { - crate::Error::CommandError(ctx, err) => (ctx, err), - err => panic!("Unexpected error variant found: {:?}", err), - } - } -} - -struct Nitrocli { - model: Option<nitrokey::Model>, - admin_pin: Option<ffi::OsString>, - user_pin: Option<ffi::OsString>, - new_admin_pin: Option<ffi::OsString>, - new_user_pin: Option<ffi::OsString>, - password: Option<ffi::OsString>, -} - -impl Nitrocli { - pub fn new() -> Self { - Self { - model: None, - admin_pin: Some(NITROKEY_DEFAULT_ADMIN_PIN.into()), - user_pin: Some(NITROKEY_DEFAULT_USER_PIN.into()), - new_admin_pin: None, - new_user_pin: None, - password: None, - } - } - - pub fn with_dev<D>(device: D) -> Self - where - D: nitrokey::Device, - { - let result = Self { - model: Some(device.get_model()), - admin_pin: Some(NITROKEY_DEFAULT_ADMIN_PIN.into()), - user_pin: Some(NITROKEY_DEFAULT_USER_PIN.into()), - new_admin_pin: None, - new_user_pin: None, - password: Some("1234567".into()), - }; - - drop(device); - result - } - - pub fn admin_pin(&mut self, pin: impl Into<ffi::OsString>) { - self.admin_pin = Some(pin.into()) - } - - pub fn new_admin_pin(&mut self, pin: impl Into<ffi::OsString>) { - self.new_admin_pin = Some(pin.into()) - } - - pub fn user_pin(&mut self, pin: impl Into<ffi::OsString>) { - self.user_pin = Some(pin.into()) - } - - pub fn new_user_pin(&mut self, pin: impl Into<ffi::OsString>) { - self.new_user_pin = Some(pin.into()) - } - - fn model_to_arg(model: nitrokey::Model) -> &'static str { - match model { - nitrokey::Model::Pro => "--model=pro", - nitrokey::Model::Storage => "--model=storage", - } - } - - fn do_run<F, R>(&mut self, args: &[&'static str], f: F) -> (R, Vec<u8>, Vec<u8>) - where - F: FnOnce(&mut crate::RunCtx<'_>, Vec<String>) -> R, - { - let args = ["nitrocli"] - .into_iter() - .cloned() - .chain(self.model.map(Self::model_to_arg)) - .chain(args.into_iter().cloned()) - .map(ToOwned::to_owned) - .collect(); - - let mut stdout = Vec::new(); - let mut stderr = Vec::new(); - - let ctx = &mut crate::RunCtx { - stdout: &mut stdout, - stderr: &mut stderr, - admin_pin: self.admin_pin.clone(), - user_pin: self.user_pin.clone(), - new_admin_pin: self.new_admin_pin.clone(), - new_user_pin: self.new_user_pin.clone(), - password: self.password.clone(), - no_cache: true, - }; - - (f(ctx, args), stdout, stderr) - } - - /// Run `nitrocli`'s `run` function. - pub fn run(&mut self, args: &[&'static str]) -> (i32, Vec<u8>, Vec<u8>) { - self.do_run(args, |c, a| crate::run(c, a)) - } - - /// Run `nitrocli`'s `handle_arguments` function. - pub fn handle(&mut self, args: &[&'static str]) -> crate::Result<String> { - let (res, out, _) = self.do_run(args, |c, a| crate::args::handle_arguments(c, a)); - res.map(|_| String::from_utf8_lossy(&out).into_owned()) - } - - pub fn model(&self) -> Option<nitrokey::Model> { - self.model - } -} diff --git a/nitrocli/src/tests/otp.rs b/nitrocli/src/tests/otp.rs deleted file mode 100644 index 39ddf29..0000000 --- a/nitrocli/src/tests/otp.rs +++ /dev/null @@ -1,114 +0,0 @@ -// otp.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::*; - -#[test_device] -fn set_invalid_slot_raw(device: nitrokey::DeviceWrapper) { - let (rc, out, err) = Nitrocli::with_dev(device).run(&["otp", "set", "100", "name", "1234"]); - - assert_ne!(rc, 0); - assert_eq!(out, b""); - assert_eq!(&err[..24], b"Could not write OTP slot"); -} - -#[test_device] -fn set_invalid_slot(device: nitrokey::DeviceWrapper) { - let res = Nitrocli::with_dev(device).handle(&["otp", "set", "100", "name", "1234"]); - - assert_eq!( - res.unwrap_cmd_err(), - ( - Some("Could not write OTP slot"), - nitrokey::CommandError::InvalidSlot - ) - ); -} - -#[test_device] -fn status(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let re = regex::Regex::new( - r#"^alg\tslot\tname -((totp|hotp)\t\d+\t.+\n)+$"#, - ) - .unwrap(); - - let mut ncli = Nitrocli::with_dev(device); - // Make sure that we have at least something to display by ensuring - // that there is one slot programmed. - let _ = ncli.handle(&["otp", "set", "0", "the-name", "123456"])?; - - let out = ncli.handle(&["otp", "status"])?; - assert!(re.is_match(&out), out); - Ok(()) -} - -#[test_device] -fn set_get_hotp(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - // Secret and expected HOTP values as per RFC 4226: Appendix D -- HOTP - // Algorithm: Test Values. - const SECRET: &str = "12345678901234567890"; - const OTP1: &str = concat!(755224, "\n"); - const OTP2: &str = concat!(287082, "\n"); - - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&[ - "otp", "set", "-a", "hotp", "-f", "ascii", "1", "name", &SECRET, - ])?; - - let out = ncli.handle(&["otp", "get", "-a", "hotp", "1"])?; - assert_eq!(out, OTP1); - - let out = ncli.handle(&["otp", "get", "-a", "hotp", "1"])?; - assert_eq!(out, OTP2); - Ok(()) -} - -#[test_device] -fn set_get_totp(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - // Secret and expected TOTP values as per RFC 6238: Appendix B -- - // Test Vectors. - const SECRET: &str = "12345678901234567890"; - const TIME: &str = stringify!(1111111111); - const OTP: &str = concat!(14050471, "\n"); - - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["otp", "set", "-d", "8", "-f", "ascii", "2", "name", &SECRET])?; - - let out = ncli.handle(&["otp", "get", "-t", TIME, "2"])?; - assert_eq!(out, OTP); - Ok(()) -} - -#[test_device] -fn clear(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["otp", "set", "3", "hotp-test", "abcdef"])?; - let _ = ncli.handle(&["otp", "clear", "3"])?; - let res = ncli.handle(&["otp", "get", "3"]); - - assert_eq!( - res.unwrap_cmd_err(), - ( - Some("Could not generate OTP"), - nitrokey::CommandError::SlotNotProgrammed - ) - ); - Ok(()) -} diff --git a/nitrocli/src/tests/pin.rs b/nitrocli/src/tests/pin.rs deleted file mode 100644 index fe61b2d..0000000 --- a/nitrocli/src/tests/pin.rs +++ /dev/null @@ -1,64 +0,0 @@ -// pin.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::Authenticate; -use nitrokey::Device; - -use super::*; - -#[test_device] -fn unblock(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let (device, err) = device.authenticate_user("wrong-pin").unwrap_err(); - assert_eq!(err, nitrokey::CommandError::WrongPassword); - assert!(device.get_user_retry_count() < 3); - - let model = device.get_model(); - let _ = Nitrocli::with_dev(device).handle(&["pin", "unblock"])?; - let device = nitrokey::connect_model(model)?; - assert_eq!(device.get_user_retry_count(), 3); - Ok(()) -} - -#[test_device] -fn set_user(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - - // Set a new user PIN. - ncli.new_user_pin("new-pin"); - let out = ncli.handle(&["pin", "set", "user"])?; - assert!(out.is_empty()); - - let device = nitrokey::connect_model(ncli.model().unwrap())?; - let (device, err) = device - .authenticate_user(NITROKEY_DEFAULT_USER_PIN) - .unwrap_err(); - assert_eq!(err, nitrokey::CommandError::WrongPassword); - drop(device); - - // Revert to the default user PIN. - ncli.user_pin("new-pin"); - ncli.new_user_pin(NITROKEY_DEFAULT_USER_PIN); - - let out = ncli.handle(&["pin", "set", "user"])?; - assert!(out.is_empty()); - - let device = nitrokey::connect_model(ncli.model().unwrap())?; - let _ = device.authenticate_user(NITROKEY_DEFAULT_USER_PIN).unwrap(); - Ok(()) -} diff --git a/nitrocli/src/tests/pws.rs b/nitrocli/src/tests/pws.rs deleted file mode 100644 index 5d0f245..0000000 --- a/nitrocli/src/tests/pws.rs +++ /dev/null @@ -1,123 +0,0 @@ -// pws.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::*; - -#[test_device] -fn set_invalid_slot(device: nitrokey::DeviceWrapper) { - let res = Nitrocli::with_dev(device).handle(&["pws", "set", "100", "name", "login", "1234"]); - - assert_eq!( - res.unwrap_cmd_err(), - ( - Some("Could not write PWS slot"), - nitrokey::CommandError::InvalidSlot - ) - ); -} - -#[test_device] -fn status(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let re = regex::Regex::new( - r#"^slot\tname -(\d+\t.+\n)+$"#, - ) - .unwrap(); - - let mut ncli = Nitrocli::with_dev(device); - // Make sure that we have at least something to display by ensuring - // that there are there is one slot programmed. - let _ = ncli.handle(&["pws", "set", "0", "the-name", "the-login", "123456"])?; - - let out = ncli.handle(&["pws", "status"])?; - assert!(re.is_match(&out), out); - Ok(()) -} - -#[test_device] -fn set_get(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - const NAME: &str = "dropbox"; - const LOGIN: &str = "d-e-s-o"; - const PASSWORD: &str = "my-secret-password"; - - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["pws", "set", "1", &NAME, &LOGIN, &PASSWORD])?; - - let out = ncli.handle(&["pws", "get", "1", "--quiet", "--name"])?; - assert_eq!(out, format!("{}\n", NAME)); - - let out = ncli.handle(&["pws", "get", "1", "--quiet", "--login"])?; - assert_eq!(out, format!("{}\n", LOGIN)); - - let out = ncli.handle(&["pws", "get", "1", "--quiet", "--password"])?; - assert_eq!(out, format!("{}\n", PASSWORD)); - - let out = ncli.handle(&["pws", "get", "1", "--quiet"])?; - assert_eq!(out, format!("{}\n{}\n{}\n", NAME, LOGIN, PASSWORD)); - - let out = ncli.handle(&["pws", "get", "1"])?; - assert_eq!( - out, - format!( - "name: {}\nlogin: {}\npassword: {}\n", - NAME, LOGIN, PASSWORD - ), - ); - Ok(()) -} - -#[test_device] -fn set_reset_get(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - const NAME: &str = "some/svc"; - const LOGIN: &str = "a\\user"; - const PASSWORD: &str = "!@&-)*(&+%^@"; - - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["pws", "set", "2", &NAME, &LOGIN, &PASSWORD])?; - - let out = ncli.handle(&["reset"])?; - assert_eq!(out, ""); - - let res = ncli.handle(&["pws", "get", "2"]); - assert_eq!( - res.unwrap_cmd_err(), - ( - Some("Could not access PWS slot"), - nitrokey::CommandError::SlotNotProgrammed - ) - ); - Ok(()) -} - -#[test_device] -fn clear(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - let _ = ncli.handle(&["pws", "set", "10", "clear-test", "some-login", "abcdef"])?; - let _ = ncli.handle(&["pws", "clear", "10"])?; - let res = ncli.handle(&["pws", "get", "10"]); - - assert_eq!( - res.unwrap_cmd_err(), - ( - Some("Could not access PWS slot"), - nitrokey::CommandError::SlotNotProgrammed - ) - ); - Ok(()) -} diff --git a/nitrocli/src/tests/reset.rs b/nitrocli/src/tests/reset.rs deleted file mode 100644 index 2e567fa..0000000 --- a/nitrocli/src/tests/reset.rs +++ /dev/null @@ -1,54 +0,0 @@ -// reset.rs - -// ************************************************************************* -// * Copyright (C) 2019 Robin Krahl (robin.krahl@ireas.org) * -// * * -// * 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::Authenticate; -use nitrokey::GetPasswordSafe; - -use super::*; - -#[test_device] -fn reset(device: nitrokey::DeviceWrapper) -> crate::Result<()> { - let new_admin_pin = "87654321"; - let mut ncli = Nitrocli::with_dev(device); - - // Change the admin PIN. - ncli.new_admin_pin(new_admin_pin); - let _ = ncli.handle(&["pin", "set", "admin"])?; - - // Check that the admin PIN has been changed. - let device = nitrokey::connect_model(ncli.model().unwrap())?; - let _ = device.authenticate_admin(new_admin_pin).unwrap(); - - // Perform factory reset - ncli.admin_pin(new_admin_pin); - let out = ncli.handle(&["reset"])?; - assert!(out.is_empty()); - - // Check that the admin PIN has been reset. - let device = nitrokey::connect_model(ncli.model().unwrap())?; - let device = device - .authenticate_admin(NITROKEY_DEFAULT_ADMIN_PIN) - .unwrap(); - - // Check that the password store works, i.e., the AES key has been - // built. - let _ = device.get_password_safe(NITROKEY_DEFAULT_USER_PIN)?; - - Ok(()) -} diff --git a/nitrocli/src/tests/run.rs b/nitrocli/src/tests/run.rs deleted file mode 100644 index dda7473..0000000 --- a/nitrocli/src/tests/run.rs +++ /dev/null @@ -1,66 +0,0 @@ -// 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::*; - -#[test] -fn no_command_or_option() { - let (rc, out, err) = Nitrocli::new().run(&[]); - - 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::new().run(&[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") -} - -#[test] -fn version_option() { - fn test(re: ®ex::Regex, opt: &'static str) { - let (rc, out, err) = Nitrocli::new().run(&[opt]); - - assert_eq!(rc, 0); - assert_eq!(err, b""); - - let s = String::from_utf8_lossy(&out).into_owned(); - let _ = re; - assert!(re.is_match(&s), out); - } - - let re = regex::Regex::new(r"^nitrocli \d+.\d+.\d+(-[^-]+)*\n$").unwrap(); - - test(&re, "--version"); - test(&re, "-V"); -} diff --git a/nitrocli/src/tests/status.rs b/nitrocli/src/tests/status.rs deleted file mode 100644 index 83ce61f..0000000 --- a/nitrocli/src/tests/status.rs +++ /dev/null @@ -1,55 +0,0 @@ -// 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::*; - -// 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::new().run(&["status"]); - - assert_ne!(rc, 0); - assert_eq!(out, b""); - assert_eq!(err, b"Nitrokey device not found\n"); -} - -#[test_device] -fn not_found() { - let res = Nitrocli::new().handle(&["status"]); - assert_eq!(res.unwrap_str_err(), "Nitrokey device not found"); -} - -#[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::with_dev(device).handle(&["status"])?; - assert!(re.is_match(&out), out); - Ok(()) -} diff --git a/nitrocli/src/tests/storage.rs b/nitrocli/src/tests/storage.rs deleted file mode 100644 index be933ca..0000000 --- a/nitrocli/src/tests/storage.rs +++ /dev/null @@ -1,117 +0,0 @@ -// storage.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::*; - -#[test_device] -fn status_on_pro(device: nitrokey::Pro) { - let res = Nitrocli::with_dev(device).handle(&["storage", "status"]); - assert_eq!( - res.unwrap_str_err(), - "This command is only available on the Nitrokey Storage", - ); -} - -#[test_device] -fn status_open_close(device: nitrokey::Storage) -> crate::Result<()> { - fn make_re(open: Option<bool>) -> regex::Regex { - let encrypted = match open { - Some(open) => { - if open { - "active" - } else { - "(read-only|inactive)" - } - } - None => "(read-only|active|inactive)", - }; - let re = format!( - r#"^Status: - SD card ID: 0x[[:xdigit:]]{{8}} - firmware: (un)?locked - storage keys: (not )?created - volumes: - unencrypted: (read-only|active|inactive) - encrypted: {} - hidden: (read-only|active|inactive) -$"#, - encrypted - ); - regex::Regex::new(&re).unwrap() - } - - let mut ncli = Nitrocli::with_dev(device); - let out = ncli.handle(&["storage", "status"])?; - assert!(make_re(None).is_match(&out), out); - - let _ = ncli.handle(&["storage", "open"])?; - let out = ncli.handle(&["storage", "status"])?; - assert!(make_re(Some(true)).is_match(&out), out); - - let _ = ncli.handle(&["storage", "close"])?; - let out = ncli.handle(&["storage", "status"])?; - assert!(make_re(Some(false)).is_match(&out), out); - - Ok(()) -} - -#[test_device] -fn encrypted_open_close(device: nitrokey::Storage) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - let out = ncli.handle(&["storage", "open"])?; - assert!(out.is_empty()); - - let device = nitrokey::Storage::connect()?; - assert!(device.get_status()?.encrypted_volume.active); - assert!(!device.get_status()?.hidden_volume.active); - drop(device); - - let out = ncli.handle(&["storage", "close"])?; - assert!(out.is_empty()); - - let device = nitrokey::Storage::connect()?; - assert!(!device.get_status()?.encrypted_volume.active); - assert!(!device.get_status()?.hidden_volume.active); - - Ok(()) -} - -#[test_device] -fn hidden_create_open_close(device: nitrokey::Storage) -> crate::Result<()> { - let mut ncli = Nitrocli::with_dev(device); - let out = ncli.handle(&["storage", "hidden", "create", "0", "50", "100"])?; - assert!(out.is_empty()); - - let out = ncli.handle(&["storage", "hidden", "open"])?; - assert!(out.is_empty()); - - let device = nitrokey::Storage::connect()?; - assert!(!device.get_status()?.encrypted_volume.active); - assert!(device.get_status()?.hidden_volume.active); - drop(device); - - let out = ncli.handle(&["storage", "hidden", "close"])?; - assert!(out.is_empty()); - - let device = nitrokey::Storage::connect()?; - assert!(!device.get_status()?.encrypted_volume.active); - assert!(!device.get_status()?.hidden_volume.active); - - Ok(()) -} |