summaryrefslogtreecommitdiffstats
path: root/ipc
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2022-06-16 12:22:21 +0200
committerJustus Winter <justus@sequoia-pgp.org>2022-06-20 12:18:41 +0200
commit0dc5553b9bee470cc88b436a949b596786b675e4 (patch)
tree285f181f53b93080c7c171276170488bbdf848f5 /ipc
parent2e16f584aca0ded8161202b96d4facc39825f20b (diff)
ipc: Improve error handling when importing keys.
Diffstat (limited to 'ipc')
-rw-r--r--ipc/tests/gpg-agent.rs34
1 files changed, 27 insertions, 7 deletions
diff --git a/ipc/tests/gpg-agent.rs b/ipc/tests/gpg-agent.rs
index 98b40255..898b86c8 100644
--- a/ipc/tests/gpg-agent.rs
+++ b/ipc/tests/gpg-agent.rs
@@ -2,6 +2,7 @@
use std::io::{self, Write};
+use anyhow::Context as _;
use futures::StreamExt;
use sequoia_openpgp as openpgp;
@@ -64,18 +65,37 @@ async fn help() -> openpgp::Result<()> {
const MESSAGE: &str = "дружба";
-fn gpg_import(ctx: &Context, what: &[u8]) {
+fn gpg_import(ctx: &Context, what: &[u8]) -> openpgp::Result<()> {
use std::process::{Command, Stdio};
let mut gpg = Command::new("gpg")
.stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
.arg("--homedir").arg(ctx.homedir().unwrap())
.arg("--import")
.spawn()
- .expect("failed to start gpg");
- gpg.stdin.as_mut().unwrap().write_all(what).unwrap();
- let status = gpg.wait().unwrap();
- assert!(status.success(), "gpg --import failed");
+ .context("failed to start gpg")?;
+ gpg.stdin.as_mut().unwrap().write_all(what)?;
+ let output = gpg.wait_with_output()?;
+
+ // We capture stdout and stderr, and use eprintln! so that the
+ // output will be captured by Rust's test harness. This way, the
+ // output will be at the right position, instead of out-of-order
+ // and garbled by the concurrent tests.
+ if ! output.stdout.is_empty() {
+ eprintln!("stdout:\n{}", String::from_utf8_lossy(&output.stdout));
+ }
+ if ! output.stderr.is_empty() {
+ eprintln!("stderr:\n{}", String::from_utf8_lossy(&output.stderr));
+ }
+
+ let status = output.status;
+ if status.success() {
+ Ok(())
+ } else {
+ Err(anyhow::anyhow!("gpg --import failed"))
+ }
}
#[test]
@@ -95,7 +115,7 @@ fn sign() -> openpgp::Result<()> {
let mut buf = Vec::new();
cert.as_tsk().serialize(&mut buf).unwrap();
- gpg_import(&ctx, &buf);
+ gpg_import(&ctx, &buf)?;
let keypair = KeyPair::new(
&ctx,
@@ -204,7 +224,7 @@ fn decrypt() -> openpgp::Result<()> {
let mut buf = Vec::new();
cert.as_tsk().serialize(&mut buf).unwrap();
- gpg_import(&ctx, &buf);
+ gpg_import(&ctx, &buf)?;
let mut message = Vec::new();
{