diff options
-rw-r--r-- | src/tests/config.rs | 14 | ||||
-rw-r--r-- | src/tests/encrypted.rs | 10 | ||||
-rw-r--r-- | src/tests/hidden.rs | 2 | ||||
-rw-r--r-- | src/tests/list.rs | 2 | ||||
-rw-r--r-- | src/tests/lock.rs | 4 | ||||
-rw-r--r-- | src/tests/mod.rs | 38 | ||||
-rw-r--r-- | src/tests/otp.rs | 20 | ||||
-rw-r--r-- | src/tests/pin.rs | 7 | ||||
-rw-r--r-- | src/tests/pws.rs | 13 | ||||
-rw-r--r-- | src/tests/reset.rs | 2 | ||||
-rw-r--r-- | src/tests/status.rs | 4 | ||||
-rw-r--r-- | src/tests/unencrypted.rs | 2 |
12 files changed, 78 insertions, 40 deletions
diff --git a/src/tests/config.rs b/src/tests/config.rs index fa311d5..3fec240 100644 --- a/src/tests/config.rs +++ b/src/tests/config.rs @@ -48,14 +48,22 @@ $"#, ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["config", "get"])?; + let out = Nitrocli::make() + .model(model) + .build() + .handle(&["config", "get"])?; + assert!(re.is_match(&out), out); Ok(()) } #[test_device] fn set_wrong_usage(model: nitrokey::Model) { - let res = Nitrocli::with_model(model).handle(&["config", "set", "--numlock", "2", "-N"]); + let res = + Nitrocli::make() + .model(model) + .build() + .handle(&["config", "set", "--numlock", "2", "-N"]); let err = res.unwrap_str_err(); assert!( err.contains("The argument '--numlock <numlock>' cannot be used with '--no-numlock'"), @@ -65,7 +73,7 @@ fn set_wrong_usage(model: nitrokey::Model) { #[test_device] fn set_get(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["config", "set", "-s", "1", "-c", "0", "-N"])?; let re = regex::Regex::new( diff --git a/src/tests/encrypted.rs b/src/tests/encrypted.rs index aed2662..4705df6 100644 --- a/src/tests/encrypted.rs +++ b/src/tests/encrypted.rs @@ -44,7 +44,7 @@ $"#, regex::Regex::new(&re).unwrap() } - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let out = ncli.handle(&["status"])?; assert!(make_re(None).is_match(&out), out); @@ -61,7 +61,11 @@ $"#, #[test_device(pro)] fn encrypted_open_on_pro(model: nitrokey::Model) { - let res = Nitrocli::with_model(model).handle(&["encrypted", "open"]); + let res = Nitrocli::make() + .model(model) + .build() + .handle(&["encrypted", "open"]); + assert_eq!( res.unwrap_str_err(), "This command is only available on the Nitrokey Storage", @@ -70,7 +74,7 @@ fn encrypted_open_on_pro(model: nitrokey::Model) { #[test_device(storage)] fn encrypted_open_close(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let out = ncli.handle(&["encrypted", "open"])?; assert!(out.is_empty()); diff --git a/src/tests/hidden.rs b/src/tests/hidden.rs index eabcfd4..9e9d60b 100644 --- a/src/tests/hidden.rs +++ b/src/tests/hidden.rs @@ -21,7 +21,7 @@ use super::*; #[test_device(storage)] fn hidden_create_open_close(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).password("1234567").build(); let out = ncli.handle(&["hidden", "create", "0", "50", "100"])?; assert!(out.is_empty()); diff --git a/src/tests/list.rs b/src/tests/list.rs index 4f80afe..3bdc7aa 100644 --- a/src/tests/list.rs +++ b/src/tests/list.rs @@ -36,7 +36,7 @@ fn connected(model: nitrokey::Model) -> crate::Result<()> { ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["list"])?; + let out = Nitrocli::make().model(model).build().handle(&["list"])?; assert!(re.is_match(&out), out); Ok(()) } diff --git a/src/tests/lock.rs b/src/tests/lock.rs index b29f394..c5ca671 100644 --- a/src/tests/lock.rs +++ b/src/tests/lock.rs @@ -22,7 +22,7 @@ use super::*; #[test_device(pro)] fn lock_pro(model: nitrokey::Model) -> crate::Result<()> { // We can't really test much more here than just success of the command. - let out = Nitrocli::with_model(model).handle(&["lock"])?; + let out = Nitrocli::make().model(model).build().handle(&["lock"])?; assert!(out.is_empty()); Ok(()) @@ -30,7 +30,7 @@ fn lock_pro(model: nitrokey::Model) -> crate::Result<()> { #[test_device(storage)] fn lock_storage(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["encrypted", "open"])?; let out = ncli.handle(&["lock"])?; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index abf63e3..e86f42f 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -84,6 +84,30 @@ where } } +struct Builder(Nitrocli); + +impl Builder { + /// Set the model to use. + fn model(mut self, model: nitrokey::Model) -> Self { + self.0.model = Some(model); + self + } + + /// Set the password to use for certain operations. + fn password<P>(mut self, password: P) -> Self + where + P: Into<ffi::OsString>, + { + self.0.password = Some(password.into()); + self + } + + /// Build the final `Nitrocli` object. + fn build(self) -> Nitrocli { + self.0 + } +} + struct Nitrocli { model: Option<nitrokey::Model>, admin_pin: Option<ffi::OsString>, @@ -105,18 +129,8 @@ impl Nitrocli { } } - pub fn with_model<M>(model: M) -> Self - where - M: Into<nitrokey::Model>, - { - Self { - model: Some(model.into()), - 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()), - } + pub fn make() -> Builder { + Builder(Self::new()) } pub fn admin_pin(&mut self, pin: impl Into<ffi::OsString>) { diff --git a/src/tests/otp.rs b/src/tests/otp.rs index f923170..1b59669 100644 --- a/src/tests/otp.rs +++ b/src/tests/otp.rs @@ -23,7 +23,10 @@ use crate::args; #[test_device] fn set_invalid_slot_raw(model: nitrokey::Model) { - let (rc, out, err) = Nitrocli::with_model(model).run(&["otp", "set", "100", "name", "1234"]); + let (rc, out, err) = Nitrocli::make() + .model(model) + .build() + .run(&["otp", "set", "100", "name", "1234"]); assert_ne!(rc, 0); assert_eq!(out, b""); @@ -32,7 +35,10 @@ fn set_invalid_slot_raw(model: nitrokey::Model) { #[test_device] fn set_invalid_slot(model: nitrokey::Model) { - let res = Nitrocli::with_model(model).handle(&["otp", "set", "100", "name", "1234"]); + let res = Nitrocli::make() + .model(model) + .build() + .handle(&["otp", "set", "100", "name", "1234"]); assert_eq!( res.unwrap_lib_err(), @@ -51,7 +57,7 @@ fn status(model: nitrokey::Model) -> crate::Result<()> { ) .unwrap(); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); // 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"])?; @@ -69,7 +75,7 @@ fn set_get_hotp(model: nitrokey::Model) -> crate::Result<()> { const OTP1: &str = concat!(755224, "\n"); const OTP2: &str = concat!(287082, "\n"); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&[ "otp", "set", "-a", "hotp", "-f", "ascii", "1", "name", &SECRET, ])?; @@ -90,7 +96,7 @@ fn set_get_totp(model: nitrokey::Model) -> crate::Result<()> { const TIME: &str = stringify!(1111111111); const OTP: &str = concat!(14050471, "\n"); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["otp", "set", "-d", "8", "-f", "ascii", "2", "name", &SECRET])?; let out = ncli.handle(&["otp", "get", "-t", TIME, "2"])?; @@ -106,7 +112,7 @@ fn set_totp_uneven_chars(model: nitrokey::Model) -> crate::Result<()> { ]; for (format, secret) in &secrets { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["otp", "set", "-f", format.as_ref(), "3", "foobar", &secret])?; } Ok(()) @@ -114,7 +120,7 @@ fn set_totp_uneven_chars(model: nitrokey::Model) -> crate::Result<()> { #[test_device] fn clear(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["otp", "set", "3", "hotp-test", "abcdef"])?; let _ = ncli.handle(&["otp", "clear", "3"])?; let res = ncli.handle(&["otp", "get", "3"]); diff --git a/src/tests/pin.rs b/src/tests/pin.rs index 958a36d..87c9064 100644 --- a/src/tests/pin.rs +++ b/src/tests/pin.rs @@ -35,7 +35,10 @@ fn unblock(model: nitrokey::Model) -> crate::Result<()> { assert!(device.get_user_retry_count()? < 3); } - let _ = Nitrocli::with_model(model).handle(&["pin", "unblock"])?; + let _ = Nitrocli::make() + .model(model) + .build() + .handle(&["pin", "unblock"])?; { let mut manager = nitrokey::force_take()?; @@ -47,7 +50,7 @@ fn unblock(model: nitrokey::Model) -> crate::Result<()> { #[test_device] fn set_user(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); // Set a new user PIN. ncli.new_user_pin("new-pin"); let out = ncli.handle(&["pin", "set", "user"])?; diff --git a/src/tests/pws.rs b/src/tests/pws.rs index 651b2d5..fa2158f 100644 --- a/src/tests/pws.rs +++ b/src/tests/pws.rs @@ -21,7 +21,10 @@ use super::*; #[test_device] fn set_invalid_slot(model: nitrokey::Model) { - let res = Nitrocli::with_model(model).handle(&["pws", "set", "100", "name", "login", "1234"]); + let res = Nitrocli::make() + .model(model) + .build() + .handle(&["pws", "set", "100", "name", "login", "1234"]); assert_eq!( res.unwrap_lib_err(), @@ -40,7 +43,7 @@ fn status(model: nitrokey::Model) -> crate::Result<()> { ) .unwrap(); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); // 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"])?; @@ -56,7 +59,7 @@ fn set_get(model: nitrokey::Model) -> crate::Result<()> { const LOGIN: &str = "d-e-s-o"; const PASSWORD: &str = "my-secret-password"; - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["pws", "set", "1", &NAME, &LOGIN, &PASSWORD])?; let out = ncli.handle(&["pws", "get", "1", "--quiet", "--name"])?; @@ -88,7 +91,7 @@ fn set_reset_get(model: nitrokey::Model) -> crate::Result<()> { const LOGIN: &str = "a\\user"; const PASSWORD: &str = "!@&-)*(&+%^@"; - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["pws", "set", "2", &NAME, &LOGIN, &PASSWORD])?; let out = ncli.handle(&["reset"])?; @@ -107,7 +110,7 @@ fn set_reset_get(model: nitrokey::Model) -> crate::Result<()> { #[test_device] fn clear(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let _ = ncli.handle(&["pws", "set", "10", "clear-test", "some-login", "abcdef"])?; let _ = ncli.handle(&["pws", "clear", "10"])?; let res = ncli.handle(&["pws", "get", "10"]); diff --git a/src/tests/reset.rs b/src/tests/reset.rs index e197970..23137bf 100644 --- a/src/tests/reset.rs +++ b/src/tests/reset.rs @@ -25,7 +25,7 @@ use super::*; #[test_device] fn reset(model: nitrokey::Model) -> crate::Result<()> { let new_admin_pin = "87654321"; - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); // Change the admin PIN. ncli.new_admin_pin(new_admin_pin); diff --git a/src/tests/status.rs b/src/tests/status.rs index c9f4976..ea573e3 100644 --- a/src/tests/status.rs +++ b/src/tests/status.rs @@ -49,7 +49,7 @@ $"#, ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["status"])?; + let out = Nitrocli::make().model(model).build().handle(&["status"])?; assert!(re.is_match(&out), out); Ok(()) } @@ -75,7 +75,7 @@ $"#, ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["status"])?; + let out = Nitrocli::make().model(model).build().handle(&["status"])?; assert!(re.is_match(&out), out); Ok(()) } diff --git a/src/tests/unencrypted.rs b/src/tests/unencrypted.rs index ceeb17c..0704a6c 100644 --- a/src/tests/unencrypted.rs +++ b/src/tests/unencrypted.rs @@ -21,7 +21,7 @@ use super::*; #[test_device(storage)] fn unencrypted_set_read_write(model: nitrokey::Model) -> crate::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::make().model(model).build(); let out = ncli.handle(&["unencrypted", "set", "read-write"])?; assert!(out.is_empty()); |