Add tests for sq key approvals list.

- Test that the user ID designators behave correctly.
This commit is contained in:
Neal H. Walfield 2024-12-12 09:59:47 +01:00
parent 52fdea48e2
commit e227aecbc2
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
3 changed files with 84 additions and 0 deletions

View File

@ -8,6 +8,7 @@ mod integration {
mod sq_cli_version;
mod sq_decrypt;
mod sq_encrypt;
mod sq_key_approvals_list;
mod sq_key_approvals_update;
mod sq_key_delete;
mod sq_key_expire;

View File

@ -1146,6 +1146,58 @@ impl Sq {
.expect("sq key subkey bind succeeds")
}
pub fn try_key_approvals_list<'a, H, U>(&self,
args: &[&str],
cert: H,
userids: &[U])
-> Result<Vec<u8>>
where H: Into<FileOrKeyHandle>,
U: Into<UserIDArg<'a>> + Clone,
{
let cert = cert.into();
let userids: Vec<UserIDArg> = userids.iter()
.cloned()
.map(|u| u.into())
.collect();
let mut cmd = self.command();
cmd.arg("key").arg("approvals").arg("list");
cmd.args(args);
match &cert {
FileOrKeyHandle::FileOrStdin(file) => {
cmd.arg("--cert-file").arg(file);
}
FileOrKeyHandle::KeyHandle((_kh, s)) => {
cmd.arg("--cert").arg(s);
}
}
for userid in userids.iter() {
userid.as_arg(&mut cmd);
}
let output = self.run(cmd, None);
if output.status.success() {
Ok(output.stdout)
} else {
Err(anyhow::anyhow!("sq key approvals list returned an error"))
}
}
pub fn key_approvals_list<'a, H, U>(&self,
args: &[&str],
cert: H,
userids: &[U])
-> Vec<u8>
where H: Into<FileOrKeyHandle>,
U: Into<UserIDArg<'a>> + Clone,
{
self.try_key_approvals_list(args, cert, userids)
.expect("success")
}
pub fn key_approvals_update<'a, H, Q>(&self,
cert: H,
args: &[&str],

View File

@ -0,0 +1,31 @@
use super::common::Sq;
use super::common::UserIDArg;
#[test]
fn userid_designators() {
let self_signed_email = "alice@example.org";
let self_signed_userid
= &format!("Alice <{}>", self_signed_email);
let other_email = "alice@other.org";
let other_userid = &format!("Alice <{}>", other_email);
let sq = Sq::new();
let (cert, cert_path, _rev_path)
= sq.key_generate(&[], &[ self_signed_userid ]);
sq.key_import(cert_path);
// 1. --userid: use the specified self-signed user ID.
sq.key_approvals_list(
&[], cert.key_handle(), &[ UserIDArg::UserID(self_signed_userid) ]);
assert!(sq.try_key_approvals_list(
&[], cert.key_handle(), &[ UserIDArg::UserID(other_userid) ]).is_err());
// 2. --email: use the self-signed user ID with the specified
// email address.
sq.key_approvals_list(
&[], cert.key_handle(), &[ UserIDArg::Email(self_signed_email) ]);
assert!(sq.try_key_approvals_list(
&[], cert.key_handle(), &[ UserIDArg::Email(other_email) ]).is_err());
}