aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-08-31 10:34:34 -0700
committerDaniel Mueller <deso@posteo.net>2019-08-31 10:34:34 -0700
commit7e2adff71e293eef2570a6a8e43127bfab569cb3 (patch)
tree6e8d0ae565e480c5540b2975384882497130ed5b
parentfb55bcb2e1330086c3c39f5f9dcbd67aaf6fd1b3 (diff)
downloadnitrocli-7e2adff71e293eef2570a6a8e43127bfab569cb3.tar.gz
nitrocli-7e2adff71e293eef2570a6a8e43127bfab569cb3.tar.bz2
Introduce builder infrastructure for assembling 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 introduces a Builder struct that can be used to create such an instance in an idiomatic way.
-rw-r--r--nitrocli/src/tests/config.rs15
-rw-r--r--nitrocli/src/tests/encrypted.rs10
-rw-r--r--nitrocli/src/tests/hidden.rs2
-rw-r--r--nitrocli/src/tests/lock.rs4
-rw-r--r--nitrocli/src/tests/mod.rs38
-rw-r--r--nitrocli/src/tests/otp.rs18
-rw-r--r--nitrocli/src/tests/pin.rs7
-rw-r--r--nitrocli/src/tests/pws.rs13
-rw-r--r--nitrocli/src/tests/reset.rs2
-rw-r--r--nitrocli/src/tests/status.rs4
-rw-r--r--nitrocli/src/tests/unencrypted.rs2
11 files changed, 77 insertions, 38 deletions
diff --git a/nitrocli/src/tests/config.rs b/nitrocli/src/tests/config.rs
index ea3a0e8..4370b87 100644
--- a/nitrocli/src/tests/config.rs
+++ b/nitrocli/src/tests/config.rs
@@ -31,14 +31,23 @@ $"#,
)
.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"]);
+
assert_eq!(
res.unwrap_str_err(),
"--numlock and --no-numlock are mutually exclusive"
@@ -47,7 +56,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/nitrocli/src/tests/encrypted.rs b/nitrocli/src/tests/encrypted.rs
index 75b84c3..8b7d665 100644
--- a/nitrocli/src/tests/encrypted.rs
+++ b/nitrocli/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/nitrocli/src/tests/hidden.rs b/nitrocli/src/tests/hidden.rs
index 28a5d23..0193b45 100644
--- a/nitrocli/src/tests/hidden.rs
+++ b/nitrocli/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/nitrocli/src/tests/lock.rs b/nitrocli/src/tests/lock.rs
index 5140152..6c23e93 100644
--- a/nitrocli/src/tests/lock.rs
+++ b/nitrocli/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/nitrocli/src/tests/mod.rs b/nitrocli/src/tests/mod.rs
index 1e2fe26..bc76d6d 100644
--- a/nitrocli/src/tests/mod.rs
+++ b/nitrocli/src/tests/mod.rs
@@ -76,6 +76,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>,
@@ -97,18 +121,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/nitrocli/src/tests/otp.rs b/nitrocli/src/tests/otp.rs
index 4080e24..0c70d18 100644
--- a/nitrocli/src/tests/otp.rs
+++ b/nitrocli/src/tests/otp.rs
@@ -21,7 +21,10 @@ use super::*;
#[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"");
@@ -30,7 +33,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(),
@@ -49,7 +55,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"])?;
@@ -67,7 +73,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,
])?;
@@ -88,7 +94,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"])?;
@@ -98,7 +104,7 @@ fn set_get_totp(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/nitrocli/src/tests/pin.rs b/nitrocli/src/tests/pin.rs
index 958a36d..87c9064 100644
--- a/nitrocli/src/tests/pin.rs
+++ b/nitrocli/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/nitrocli/src/tests/pws.rs b/nitrocli/src/tests/pws.rs
index 651b2d5..fa2158f 100644
--- a/nitrocli/src/tests/pws.rs
+++ b/nitrocli/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/nitrocli/src/tests/reset.rs b/nitrocli/src/tests/reset.rs
index e197970..23137bf 100644
--- a/nitrocli/src/tests/reset.rs
+++ b/nitrocli/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/nitrocli/src/tests/status.rs b/nitrocli/src/tests/status.rs
index c9f4976..ea573e3 100644
--- a/nitrocli/src/tests/status.rs
+++ b/nitrocli/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/nitrocli/src/tests/unencrypted.rs b/nitrocli/src/tests/unencrypted.rs
index 547dcaf..2a7ae44 100644
--- a/nitrocli/src/tests/unencrypted.rs
+++ b/nitrocli/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());