Add tests for sq key list.

- Add tests that check that `sq key list` returns an appropriate
    error code.
This commit is contained in:
Neal H. Walfield 2024-12-04 10:31:53 +01:00
parent fde96e5790
commit 0cc2aba0be
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
3 changed files with 51 additions and 1 deletions

View File

@ -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;

View File

@ -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<Vec<u8>> {
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<u8> {
self.try_key_list(args).expect("success")
}
/// Exports the specified key.
pub fn key_export(&self, kh: KeyHandle) -> Cert {
self.key_export_maybe(kh)

View File

@ -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());
}