Port sq key import / export test to the common test framework.
- Port the `sq key import` / `sq key export` test to the common test framework.
This commit is contained in:
parent
8849a8c627
commit
916c110626
@ -713,6 +713,39 @@ impl Sq {
|
||||
.expect("can parse certificate")
|
||||
}
|
||||
|
||||
/// Exports the specified keys.
|
||||
pub fn key_subkey_export<H>(&self, khs: Vec<H>) -> Vec<Cert>
|
||||
where H: Into<KeyHandle>
|
||||
{
|
||||
self.key_subkey_export_maybe(khs)
|
||||
.expect("can export key")
|
||||
}
|
||||
|
||||
/// Exports the specified keys from the key store.
|
||||
///
|
||||
/// Returns an error if `sq key subkey export` fails. This
|
||||
/// happens if the key is known, but the key store doesn't manage
|
||||
/// any of its secret key material.
|
||||
pub fn key_subkey_export_maybe<H>(&self, khs: Vec<H>) -> Result<Vec<Cert>>
|
||||
where H: Into<KeyHandle>
|
||||
{
|
||||
let mut cmd = self.command();
|
||||
cmd.args([ "key", "export" ]);
|
||||
for kh in khs.into_iter() {
|
||||
let kh: KeyHandle = kh.into();
|
||||
cmd.arg("--key").arg(kh.to_string());
|
||||
}
|
||||
let output = self.run(cmd, None);
|
||||
|
||||
if output.status.success() {
|
||||
let parser = CertParser::from_bytes(&output.stdout)
|
||||
.expect("can parse certificate");
|
||||
Ok(parser.collect::<Result<Vec<Cert>>>()?)
|
||||
} else {
|
||||
Err(anyhow::anyhow!("sq key export returned an error"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete the specified key.
|
||||
pub fn key_subkey_delete<'a, H, Q>(&self,
|
||||
cert_handle: H,
|
||||
|
@ -1,16 +1,11 @@
|
||||
use assert_cmd::Command;
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
use sequoia_openpgp as openpgp;
|
||||
use openpgp::KeyID;
|
||||
use openpgp::KeyHandle;
|
||||
use openpgp::Result;
|
||||
use openpgp::cert::prelude::*;
|
||||
use openpgp::packet::Key;
|
||||
use openpgp::parse::Parse;
|
||||
|
||||
mod common;
|
||||
use common::power_set;
|
||||
use common::Sq;
|
||||
|
||||
mod integration {
|
||||
use super::*;
|
||||
@ -18,56 +13,27 @@ mod integration {
|
||||
#[test]
|
||||
fn sq_key_import_export() -> Result<()>
|
||||
{
|
||||
let dir = TempDir::new()?;
|
||||
|
||||
let rev_pgp = dir.path().join("rev.pgp");
|
||||
let rev_pgp_str = &*rev_pgp.to_string_lossy();
|
||||
|
||||
let key_pgp = dir.path().join("key.pgp");
|
||||
let key_pgp_str = &*key_pgp.to_string_lossy();
|
||||
let sq = Sq::new();
|
||||
|
||||
// Generate a few keys as red herrings.
|
||||
for _ in 0..10 {
|
||||
let mut cmd = Command::cargo_bin("sq")?;
|
||||
cmd.env("SEQUOIA_HOME", dir.path());
|
||||
cmd.args(["--force", "key", "generate",
|
||||
"--no-userids",
|
||||
"--rev-cert", &rev_pgp_str]);
|
||||
cmd.assert().success();
|
||||
for i in 0..10 {
|
||||
let (_, key_pgp, _) = sq.key_generate(&[], &[&format!("Key {}", i)]);
|
||||
sq.key_import(key_pgp);
|
||||
}
|
||||
|
||||
// Generate a key in a file.
|
||||
let mut cmd = Command::cargo_bin("sq")?;
|
||||
cmd.env("SEQUOIA_HOME", dir.path());
|
||||
cmd.args(["key", "generate",
|
||||
"--no-userids",
|
||||
"--output", &key_pgp_str]);
|
||||
cmd.assert().success();
|
||||
|
||||
let cert = Cert::from_file(&key_pgp)?;
|
||||
assert!(cert.is_tsk());
|
||||
|
||||
// Import it into the key store.
|
||||
let mut cmd = Command::cargo_bin("sq")?;
|
||||
cmd.env("SEQUOIA_HOME", dir.path());
|
||||
cmd.args(["key", "import",
|
||||
&*key_pgp.to_string_lossy()]);
|
||||
cmd.assert().success();
|
||||
// Generate and import a key.
|
||||
let (cert, key_pgp, _) = sq.key_generate(&[], &["Alice"]);
|
||||
sq.key_import(key_pgp);
|
||||
|
||||
// Export the whole certificate.
|
||||
for by_fpr in [true, false] {
|
||||
let mut cmd = Command::cargo_bin("sq")?;
|
||||
cmd.env("SEQUOIA_HOME", dir.path());
|
||||
cmd.args(["key", "export", "--cert",
|
||||
&if by_fpr {
|
||||
cert.fingerprint().to_string()
|
||||
let kh: KeyHandle = if by_fpr {
|
||||
cert.fingerprint().into()
|
||||
} else {
|
||||
cert.keyid().to_string()
|
||||
}]);
|
||||
let result = cmd.assert().success();
|
||||
let stdout = &result.get_output().stdout;
|
||||
cert.keyid().into()
|
||||
};
|
||||
|
||||
let got = Cert::from_bytes(stdout).expect("cert");
|
||||
let got = sq.key_export(kh);
|
||||
assert_eq!(cert, got);
|
||||
}
|
||||
|
||||
@ -97,21 +63,9 @@ mod integration {
|
||||
}
|
||||
|
||||
// Export the selection.
|
||||
let mut cmd = Command::cargo_bin("sq")?;
|
||||
cmd.env("SEQUOIA_HOME", dir.path());
|
||||
cmd.args(["key", "export"]);
|
||||
for id in selection.iter() {
|
||||
if by_fpr {
|
||||
cmd.args(["--key", &id.to_string()]);
|
||||
} else {
|
||||
cmd.args(["--key", &KeyID::from(id).to_string()]);
|
||||
}
|
||||
}
|
||||
eprintln!(" Running: {:?}", cmd);
|
||||
let result = cmd.assert().success();
|
||||
let stdout = &result.get_output().stdout;
|
||||
|
||||
let got = Cert::from_bytes(stdout).expect("cert");
|
||||
let got = sq.key_subkey_export(selection.clone());
|
||||
assert_eq!(got.len(), 1);
|
||||
let got = got.into_iter().next().unwrap();
|
||||
|
||||
// Make sure we got exactly what we asked for; no
|
||||
// more, no less.
|
||||
|
Loading…
x
Reference in New Issue
Block a user