From d4ce7ac09556715ce169958397688783d5653aab Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 9 Oct 2024 12:42:53 +0200 Subject: [PATCH] Add tests for `sq toolbox keyring filter`. --- tests/integration.rs | 1 + .../integration/sq_toolbox_keyring_filter.rs | 123 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 tests/integration/sq_toolbox_keyring_filter.rs diff --git a/tests/integration.rs b/tests/integration.rs index 9510b027..a083771c 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -23,6 +23,7 @@ mod integration { mod sq_pki_certify; mod sq_pki_link; mod sq_sign; + mod sq_toolbox_keyring_filter; mod sq_toolbox_packet_decrypt; mod sq_toolbox_packet_dump; } diff --git a/tests/integration/sq_toolbox_keyring_filter.rs b/tests/integration/sq_toolbox_keyring_filter.rs new file mode 100644 index 00000000..615bb5c2 --- /dev/null +++ b/tests/integration/sq_toolbox_keyring_filter.rs @@ -0,0 +1,123 @@ +use std::{ + fs::File, + io::{Cursor, Read}, +}; + +use sequoia_openpgp::{ + Result, + cert::{Cert, CertParser}, + parse::Parse, +}; + +use super::common::{Sq, artifact}; + +/// Creates a keyring. +fn build_keyring(sq: &Sq) { + let mut cmd = sq.command(); + cmd.arg("toolbox") + .arg("keyring") + .arg("merge") + .arg("--output=keys.pgp") + .arg(artifact("examples").join("alice-secret.pgp")) + .arg(artifact("examples").join("bob.pgp")) + .arg(artifact("keys").join("neal.pgp")); + sq.run(cmd, true); +} + +/// Reads all certs in. +fn read_certs(source: &mut (dyn Read + Sync + Send)) -> Vec { + CertParser::from_reader(source).unwrap() + .collect::>>().unwrap() +} + +/// Filters the keyring, then reads all certs in. +fn filter(sq: &Sq, args: &[&str]) -> Vec { + let mut cmd = sq.command(); + cmd.arg("toolbox") + .arg("keyring") + .arg("filter") + .arg("--output=-") + .args(args) + .arg("keys.pgp"); + let output = sq.run(cmd, true); + + read_certs(&mut Cursor::new(output.stdout)) +} + +#[test] +fn to_cert() -> Result<()> { + let sq = Sq::new(); + build_keyring(&sq); + + let keys = read_certs(&mut File::open(sq.working_dir().join("keys.pgp"))?); + assert!(keys.iter().any(|cert| cert.is_tsk())); + + let certs = filter(&sq, &["--to-cert"]); + assert!(! certs.iter().any(|cert| cert.is_tsk())); + + Ok(()) +} + +#[test] +fn userid() -> Result<()> { + let sq = Sq::new(); + build_keyring(&sq); + + let certs = filter(&sq, &["--userid", "Alice "]); + assert_eq!(certs.len(), 1); + assert_eq!(certs[0].fingerprint(), + "EB28F26E2739A4870ECC47726F0073F60FD0CBF0".parse()?); + + Ok(()) +} + +#[test] +fn userid_prune() -> Result<()> { + let sq = Sq::new(); + build_keyring(&sq); + + let neals_uid = "Neal H. Walfield "; + let certs = filter(&sq, &["--prune-certs", "--userid", neals_uid]); + assert_eq!(certs.len(), 1); + assert_eq!(certs[0].fingerprint(), + "8F17777118A33DDA9BA48E62AACB3243630052D9".parse()?); + assert_eq!(certs[0].userids().count(), 1); + assert_eq!(certs[0].userids().next().unwrap().value(), + neals_uid.as_bytes()); + + Ok(()) +} + +#[test] +fn domain() -> Result<()> { + let sq = Sq::new(); + build_keyring(&sq); + + let certs = filter(&sq, &["--domain", "example.org"]); + assert_eq!(certs.len(), 2); + let mut fiprs = certs.iter().map(Cert::fingerprint).collect::>(); + fiprs.sort(); + assert_eq!(&fiprs[..], + &[ + "511257EBBF077B7AEDAE5D093F68CB84CE537C9A".parse()?, + "EB28F26E2739A4870ECC47726F0073F60FD0CBF0".parse()?, + ]); + + Ok(()) +} + +#[test] +fn domain_prune() -> Result<()> { + let sq = Sq::new(); + build_keyring(&sq); + + let certs = filter(&sq, &["--prune-certs", "--domain", "sequoia-pgp.org"]); + assert_eq!(certs.len(), 1); + assert_eq!(certs[0].fingerprint(), + "8F17777118A33DDA9BA48E62AACB3243630052D9".parse()?); + assert_eq!(certs[0].userids().count(), 1); + assert_eq!(certs[0].userids().next().unwrap().value(), + b"Neal H. Walfield "); + + Ok(()) +}