From 03d04439323f60bc2f4371585ae21404dbcb7eeb Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Mon, 24 Aug 2020 18:04:20 -0700 Subject: Introduce builder-inspired way for configuring Nitrocli instance In the future we would like to provide more ways for tests to create a Nitrocli instance. In order to prevent explosion of with_XXX methods for each possible combination of arguments, this change allows for an easier configuration of an existing object with builder-pattern-inspired modifier methods. --- src/tests/config.rs | 9 ++++++--- src/tests/encrypted.rs | 8 +++++--- src/tests/hidden.rs | 2 +- src/tests/list.rs | 2 +- src/tests/lock.rs | 4 ++-- src/tests/mod.rs | 42 ++++++++++++++++++++++-------------------- src/tests/otp.rs | 18 ++++++++++-------- src/tests/pin.rs | 11 ++++++----- src/tests/pws.rs | 11 ++++++----- src/tests/reset.rs | 5 ++--- src/tests/status.rs | 4 ++-- src/tests/unencrypted.rs | 2 +- 12 files changed, 64 insertions(+), 54 deletions(-) diff --git a/src/tests/config.rs b/src/tests/config.rs index 75ec983..babd32c 100644 --- a/src/tests/config.rs +++ b/src/tests/config.rs @@ -34,17 +34,20 @@ $"#, ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["config", "get"])?; + let out = Nitrocli::new().model(model).handle(&["config", "get"])?; + assert!(re.is_match(&out), out); Ok(()) } #[test_device] fn set_wrong_usage(model: nitrokey::Model) { - let err = Nitrocli::with_model(model) + let err = Nitrocli::new() + .model(model) .handle(&["config", "set", "--numlock", "2", "-N"]) .unwrap_err() .to_string(); + assert!( err.contains("The argument '--numlock ' cannot be used with '--no-numlock'"), err, @@ -53,7 +56,7 @@ fn set_wrong_usage(model: nitrokey::Model) { #[test_device] fn set_get(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); 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 485bb6e..81a151a 100644 --- a/src/tests/encrypted.rs +++ b/src/tests/encrypted.rs @@ -30,7 +30,7 @@ $"#, regex::Regex::new(&re).unwrap() } - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let out = ncli.handle(&["status"])?; assert!(make_re(None).is_match(&out), out); @@ -47,10 +47,12 @@ $"#, #[test_device(pro)] fn encrypted_open_on_pro(model: nitrokey::Model) { - let err = Nitrocli::with_model(model) + let err = Nitrocli::new() + .model(model) .handle(&["encrypted", "open"]) .unwrap_err() .to_string(); + assert_eq!( err, "This command is only available on the Nitrokey Storage", @@ -59,7 +61,7 @@ fn encrypted_open_on_pro(model: nitrokey::Model) { #[test_device(storage)] fn encrypted_open_close(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let out = ncli.handle(&["encrypted", "open"])?; assert!(out.is_empty()); diff --git a/src/tests/hidden.rs b/src/tests/hidden.rs index 82a4630..1b072e1 100644 --- a/src/tests/hidden.rs +++ b/src/tests/hidden.rs @@ -7,7 +7,7 @@ use super::*; #[test_device(storage)] fn hidden_create_open_close(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model).password("1234567"); 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 7ff88a7..4c28995 100644 --- a/src/tests/list.rs +++ b/src/tests/list.rs @@ -22,7 +22,7 @@ fn connected(model: nitrokey::Model) -> anyhow::Result<()> { ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["list"])?; + let out = Nitrocli::new().model(model).handle(&["list"])?; assert!(re.is_match(&out), out); Ok(()) } diff --git a/src/tests/lock.rs b/src/tests/lock.rs index e04e8eb..5180486 100644 --- a/src/tests/lock.rs +++ b/src/tests/lock.rs @@ -8,7 +8,7 @@ use super::*; #[test_device(pro)] fn lock_pro(model: nitrokey::Model) -> anyhow::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::new().model(model).handle(&["lock"])?; assert!(out.is_empty()); Ok(()) @@ -16,7 +16,7 @@ fn lock_pro(model: nitrokey::Model) -> anyhow::Result<()> { #[test_device(storage)] fn lock_storage(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let _ = ncli.handle(&["encrypted", "open"])?; let out = ncli.handle(&["lock"])?; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index d31348b..631ffb0 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -41,34 +41,36 @@ impl Nitrocli { } } - pub fn with_model(model: M) -> Self - where - M: Into, - { - 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()), - } + /// Set the model to use. + fn model(mut self, model: nitrokey::Model) -> Self { + self.model = Some(model); + self + } + + /// Set the password to use for certain operations. + fn password(mut self, password: impl Into) -> Self { + self.password = Some(password.into()); + self } - pub fn admin_pin(&mut self, pin: impl Into) { - self.admin_pin = Some(pin.into()) + pub fn admin_pin(mut self, pin: impl Into) -> Self { + self.admin_pin = Some(pin.into()); + self } - pub fn new_admin_pin(&mut self, pin: impl Into) { - self.new_admin_pin = Some(pin.into()) + pub fn new_admin_pin(mut self, pin: impl Into) -> Self { + self.new_admin_pin = Some(pin.into()); + self } - pub fn user_pin(&mut self, pin: impl Into) { - self.user_pin = Some(pin.into()) + pub fn user_pin(mut self, pin: impl Into) -> Self { + self.user_pin = Some(pin.into()); + self } - pub fn new_user_pin(&mut self, pin: impl Into) { - self.new_user_pin = Some(pin.into()) + pub fn new_user_pin(mut self, pin: impl Into) -> Self { + self.new_user_pin = Some(pin.into()); + self } fn model_to_arg(model: nitrokey::Model) -> &'static str { diff --git a/src/tests/otp.rs b/src/tests/otp.rs index d907f18..3431e8b 100644 --- a/src/tests/otp.rs +++ b/src/tests/otp.rs @@ -9,8 +9,9 @@ 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", "-f", "hex"]); + let (rc, out, err) = Nitrocli::new() + .model(model) + .run(&["otp", "set", "100", "name", "1234", "-f", "hex"]); assert_ne!(rc, 0); assert_eq!(out, b""); @@ -19,7 +20,8 @@ fn set_invalid_slot_raw(model: nitrokey::Model) { #[test_device] fn set_invalid_slot(model: nitrokey::Model) { - let err = Nitrocli::with_model(model) + let err = Nitrocli::new() + .model(model) .handle(&["otp", "set", "100", "name", "1234", "-f", "hex"]) .unwrap_err() .to_string(); @@ -35,7 +37,7 @@ fn status(model: nitrokey::Model) -> anyhow::Result<()> { ) .unwrap(); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); // 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", "-f", "hex"])?; @@ -53,7 +55,7 @@ fn set_get_hotp(model: nitrokey::Model) -> anyhow::Result<()> { const OTP1: &str = concat!(755224, "\n"); const OTP2: &str = concat!(287082, "\n"); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let _ = ncli.handle(&[ "otp", "set", "-a", "hotp", "-f", "ascii", "1", "name", &SECRET, ])?; @@ -74,7 +76,7 @@ fn set_get_totp(model: nitrokey::Model) -> anyhow::Result<()> { const TIME: &str = stringify!(1111111111); const OTP: &str = concat!(14050471, "\n"); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let _ = ncli.handle(&["otp", "set", "-d", "8", "-f", "ascii", "2", "name", &SECRET])?; let out = ncli.handle(&["otp", "get", "-t", TIME, "2"])?; @@ -90,7 +92,7 @@ fn set_totp_uneven_chars(model: nitrokey::Model) -> anyhow::Result<()> { ]; for (format, secret) in &secrets { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let _ = ncli.handle(&["otp", "set", "-f", format.as_ref(), "3", "foobar", &secret])?; } Ok(()) @@ -98,7 +100,7 @@ fn set_totp_uneven_chars(model: nitrokey::Model) -> anyhow::Result<()> { #[test_device] fn clear(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); 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 7770fba..484e256 100644 --- a/src/tests/pin.rs +++ b/src/tests/pin.rs @@ -21,7 +21,7 @@ fn unblock(model: nitrokey::Model) -> anyhow::Result<()> { assert!(device.get_user_retry_count()? < 3); } - let _ = Nitrocli::with_model(model).handle(&["pin", "unblock"])?; + let _ = Nitrocli::new().model(model).handle(&["pin", "unblock"])?; { let mut manager = nitrokey::force_take()?; @@ -33,9 +33,9 @@ fn unblock(model: nitrokey::Model) -> anyhow::Result<()> { #[test_device] fn set_user(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let ncli = Nitrocli::new().model(model); // Set a new user PIN. - ncli.new_user_pin("new-pin"); + let mut ncli = ncli.new_user_pin("new-pin"); let out = ncli.handle(&["pin", "set", "user"])?; assert!(out.is_empty()); @@ -53,8 +53,9 @@ fn set_user(model: nitrokey::Model) -> anyhow::Result<()> { } // Revert to the default user PIN. - ncli.user_pin("new-pin"); - ncli.new_user_pin(nitrokey::DEFAULT_USER_PIN); + let mut ncli = ncli + .user_pin("new-pin") + .new_user_pin(nitrokey::DEFAULT_USER_PIN); let out = ncli.handle(&["pin", "set", "user"])?; assert!(out.is_empty()); diff --git a/src/tests/pws.rs b/src/tests/pws.rs index 45c4400..cdead84 100644 --- a/src/tests/pws.rs +++ b/src/tests/pws.rs @@ -7,7 +7,8 @@ use super::*; #[test_device] fn set_invalid_slot(model: nitrokey::Model) { - let err = Nitrocli::with_model(model) + let err = Nitrocli::new() + .model(model) .handle(&["pws", "set", "100", "name", "login", "1234"]) .unwrap_err() .to_string(); @@ -23,7 +24,7 @@ fn status(model: nitrokey::Model) -> anyhow::Result<()> { ) .unwrap(); - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); // 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"])?; @@ -39,7 +40,7 @@ fn set_get(model: nitrokey::Model) -> anyhow::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::new().model(model); let _ = ncli.handle(&["pws", "set", "1", &NAME, &LOGIN, &PASSWORD])?; let out = ncli.handle(&["pws", "get", "1", "--quiet", "--name"])?; @@ -71,7 +72,7 @@ fn set_reset_get(model: nitrokey::Model) -> anyhow::Result<()> { const LOGIN: &str = "a\\user"; const PASSWORD: &str = "!@&-)*(&+%^@"; - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let _ = ncli.handle(&["pws", "set", "2", &NAME, &LOGIN, &PASSWORD])?; let out = ncli.handle(&["reset"])?; @@ -85,7 +86,7 @@ fn set_reset_get(model: nitrokey::Model) -> anyhow::Result<()> { #[test_device] fn clear(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); 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 bf1e75b..9934284 100644 --- a/src/tests/reset.rs +++ b/src/tests/reset.rs @@ -11,10 +11,9 @@ use super::*; #[test_device] fn reset(model: nitrokey::Model) -> anyhow::Result<()> { let new_admin_pin = "87654321"; - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model).new_admin_pin(new_admin_pin); // Change the admin PIN. - ncli.new_admin_pin(new_admin_pin); let _ = ncli.handle(&["pin", "set", "admin"])?; { @@ -25,7 +24,7 @@ fn reset(model: nitrokey::Model) -> anyhow::Result<()> { } // Perform factory reset - ncli.admin_pin(new_admin_pin); + let mut ncli = ncli.admin_pin(new_admin_pin); let out = ncli.handle(&["reset"])?; assert!(out.is_empty()); diff --git a/src/tests/status.rs b/src/tests/status.rs index 0fd5b5b..d158103 100644 --- a/src/tests/status.rs +++ b/src/tests/status.rs @@ -39,7 +39,7 @@ $"#, ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["status"])?; + let out = Nitrocli::new().model(model).handle(&["status"])?; assert!(re.is_match(&out), out); Ok(()) } @@ -65,7 +65,7 @@ $"#, ) .unwrap(); - let out = Nitrocli::with_model(model).handle(&["status"])?; + let out = Nitrocli::new().model(model).handle(&["status"])?; assert!(re.is_match(&out), out); Ok(()) } diff --git a/src/tests/unencrypted.rs b/src/tests/unencrypted.rs index 26c6604..89049de 100644 --- a/src/tests/unencrypted.rs +++ b/src/tests/unencrypted.rs @@ -7,7 +7,7 @@ use super::*; #[test_device(storage)] fn unencrypted_set_read_write(model: nitrokey::Model) -> anyhow::Result<()> { - let mut ncli = Nitrocli::with_model(model); + let mut ncli = Nitrocli::new().model(model); let out = ncli.handle(&["unencrypted", "set", "read-write"])?; assert!(out.is_empty()); -- cgit v1.2.3