From 726dedc5534340907fb955c00da849470b41f408 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Thu, 26 Sep 2024 09:43:44 +0200 Subject: [PATCH] Make the test helper function Sq::key_adopt more consistent. - Make `Sq::key_adopt` more consistent with other functions by providing an `Sq::key_adopt_maybe` version, which panics on failure. --- tests/integration/common.rs | 57 +++++++++++++++++++++++++------ tests/integration/sq_key_adopt.rs | 41 ++++++---------------- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/tests/integration/common.rs b/tests/integration/common.rs index d68a03b6..9c3f9608 100644 --- a/tests/integration/common.rs +++ b/tests/integration/common.rs @@ -711,16 +711,23 @@ impl Sq { } } - /// Target is a certificate. + /// Calls `sq key adopt`. + /// + /// `keyrings` are a list of files to pass to `--keyring`. They + /// usually contain the key to adopt. + /// + /// `target` is the certificate that will adopt the key. /// /// `keys` is the set of keys to adopt. - pub fn key_adopt(&self, - extra_args: &[&str], - keyrings: Vec

, - target: T, - keys: Vec, - output_file: Q, - success: bool) + /// + /// The resulting certificate is NOT imported into the key store + /// or the cert store. + pub fn key_adopt_maybe(&self, + extra_args: &[&str], + keyrings: Vec

, + target: T, + keys: Vec, + output_file: Q) -> Result where P: AsRef, @@ -756,7 +763,7 @@ impl Sq { cmd.arg("--output").arg(&output_file); - let output = self.run(cmd, Some(success)); + let output = self.run(cmd, None); if output.status.success() { let cert = if output_file == PathBuf::from("-") { Cert::from_bytes(&output.stdout) @@ -769,11 +776,41 @@ impl Sq { Ok(cert) } else { Err(anyhow::anyhow!(format!( - "Failed (expected):\n{}", + "Failed:\n{}", String::from_utf8_lossy(&output.stderr)))) } } + /// Calls `sq key adopt`. + /// + /// `keyrings` are a list of files to pass to `--keyring`. They + /// usually contain the key to adopt. + /// + /// `target` is the certificate that will adopt the key. + /// + /// `keys` is the set of keys to adopt. + /// + /// The resulting certificate is NOT imported into the key store + /// or the cert store. + /// + /// This version panics if `sq key adopt` fails. + pub fn key_adopt(&self, + extra_args: &[&str], + keyrings: Vec

, + target: T, + keys: Vec, + output_file: Q) + -> Cert + where + P: AsRef, + T: Into, + K: Into, + Q: AsRef, + { + self.key_adopt_maybe(extra_args, keyrings, target, keys, output_file) + .expect("sq key adopt succeeds") + } + pub fn key_approvals_update<'a, H, Q>(&self, cert: H, args: &[&str], diff --git a/tests/integration/sq_key_adopt.rs b/tests/integration/sq_key_adopt.rs index 88a42575..9a99c70c 100644 --- a/tests/integration/sq_key_adopt.rs +++ b/tests/integration/sq_key_adopt.rs @@ -175,9 +175,7 @@ fn adopt_encryption() -> Result<()> { keyrings.to_vec(), handle, [ alice_encryption().0.clone() ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 2, (bob_primary(), &[alice_encryption()])).is_ok()); @@ -226,9 +224,7 @@ fn adopt_signing() -> Result<()> { keyrings.to_vec(), handle, [ alice_signing().0.clone() ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 2, (bob_primary(), &[alice_signing()])).is_ok()); @@ -278,9 +274,7 @@ fn adopt_certification() -> Result<()> { keyrings.to_vec(), handle, [ alice_primary().0.clone() ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!(check(&cert, 4, (carol_primary(), &[alice_primary()])).is_ok()); } @@ -330,9 +324,7 @@ fn adopt_encryption_and_signing() -> Result<()> { alice_signing().0.clone(), alice_encryption().0.clone(), ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 3, @@ -387,9 +379,7 @@ fn adopt_twice() -> Result<()> { alice_encryption().0.clone(), alice_encryption().0.clone(), ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 2, (bob_primary(), &[alice_encryption()])).is_ok()); @@ -410,9 +400,7 @@ fn adopt_key_appears_twice() -> Result<()> { [ alice_encryption().0.clone(), ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 2, (bob_primary(), &[alice_encryption()])).is_ok()); @@ -462,9 +450,7 @@ fn adopt_own_encryption() -> Result<()> { [ alice_encryption().0.clone(), ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 3, (alice_primary(), &[alice_encryption()])).is_ok()); @@ -515,9 +501,7 @@ fn adopt_own_primary() -> Result<()> { [ bob_primary().0.clone(), ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 2, (bob_primary(), &[bob_primary()])).is_ok()); @@ -569,7 +553,7 @@ fn adopt_missing() -> Result<()> { } // Adopt a key that is not present. - let r = sq.key_adopt( + let r = sq.key_adopt_maybe( &[], keyrings.to_vec(), handle, @@ -578,8 +562,7 @@ fn adopt_missing() -> Result<()> { .parse::() .expect("valid fingerprint") ].to_vec(), - "-", - false); + "-"); assert!(r.is_err()); } @@ -632,9 +615,7 @@ fn adopt_from_multiple() -> Result<()> { carol_signing().0.clone(), carol_encryption().0.clone(), ].to_vec(), - "-", - true) - .unwrap(); + "-"); assert!( check(&cert, 5,