From 0cc2aba0befe85ae00640f61e293157904482287 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Wed, 4 Dec 2024 10:31:53 +0100 Subject: [PATCH] Add tests for sq key list. - Add tests that check that `sq key list` returns an appropriate error code. --- tests/integration.rs | 3 ++- tests/integration/common.rs | 20 ++++++++++++++++++++ tests/integration/sq_key_list.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/integration/sq_key_list.rs diff --git a/tests/integration.rs b/tests/integration.rs index 16b05512..124dc31c 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -8,15 +8,16 @@ mod integration { mod sq_cli_version; mod sq_decrypt; mod sq_encrypt; - mod sq_key_subkey_bind; mod sq_key_approvals_update; mod sq_key_delete; mod sq_key_expire; mod sq_key_generate; mod sq_key_import_export; + mod sq_key_list; mod sq_key_password; mod sq_key_revoke; mod sq_key_subkey; + mod sq_key_subkey_bind; mod sq_key_subkey_delete; mod sq_key_subkey_expire; mod sq_key_subkey_export; diff --git a/tests/integration/common.rs b/tests/integration/common.rs index d24e979a..478f9081 100644 --- a/tests/integration/common.rs +++ b/tests/integration/common.rs @@ -933,6 +933,26 @@ impl Sq { self.run(cmd, Some(true)); } + /// Runs `sq key list` with the supplied arguments. + pub fn try_key_list(&self, args: &[&str]) -> Result> { + let mut cmd = self.command(); + cmd.arg("key").arg("list"); + for arg in args { + cmd.arg(arg); + } + let output = self.run(cmd, None); + if output.status.success() { + Ok(output.stdout) + } else { + Err(anyhow::anyhow!("sq cert list returned an error")) + } + } + + /// Runs `sq key list` with the supplied arguments. + pub fn key_list(&self, args: &[&str]) -> Vec { + self.try_key_list(args).expect("success") + } + /// Exports the specified key. pub fn key_export(&self, kh: KeyHandle) -> Cert { self.key_export_maybe(kh) diff --git a/tests/integration/sq_key_list.rs b/tests/integration/sq_key_list.rs new file mode 100644 index 00000000..2c6d672e --- /dev/null +++ b/tests/integration/sq_key_list.rs @@ -0,0 +1,29 @@ +use crate::integration::common::Sq; + +#[test] +fn list_empty() { + let sq = Sq::new(); + + // Listing an empty key store should not be an error. + sq.key_list(&[]); + + // Listing an empty key store with a pattern (that doesn't + // match anything) should be. + assert!(sq.try_key_list(&["not found"]).is_err()); + + let (cert, cert_path, _rev_path) + = sq.key_generate(&[], &[ "alice" ]); + sq.key_import(cert_path); + + // Not linked => ok. + sq.key_list(&["alice"]); + // Not found => error. + assert!(sq.try_key_list(&["not found"]).is_err()); + + // Linked and found => ok. + sq.pki_link_add(&[], cert.key_handle(), &["alice"]); + sq.key_list(&["alice"]); + + // Not found => error. + assert!(sq.try_key_list(&["not found"]).is_err()); +}