fix(API): Be polite and kill child processes in api integration tests

This commit is contained in:
Karolin Varner
2024-08-19 00:24:07 +02:00
parent 77760d71df
commit 9bbf9433e6
2 changed files with 69 additions and 43 deletions

View File

@@ -19,7 +19,7 @@ use rosenpass_util::{
file::LoadValueB64, file::LoadValueB64,
io::IoErrorKind, io::IoErrorKind,
length_prefix_encoding::{decoder::LengthPrefixDecoder, encoder::LengthPrefixEncoder}, length_prefix_encoding::{decoder::LengthPrefixDecoder, encoder::LengthPrefixEncoder},
mem::MoveExt, mem::{DiscardResultExt, MoveExt},
mio::WriteWithFileDescriptors, mio::WriteWithFileDescriptors,
zerocopy::ZerocopySliceExt, zerocopy::ZerocopySliceExt,
}; };
@@ -29,6 +29,15 @@ use zerocopy::AsBytes;
use rosenpass::protocol::SymKey; use rosenpass::protocol::SymKey;
struct KillChild(std::process::Child);
impl Drop for KillChild {
fn drop(&mut self) {
self.0.kill().discard_result();
self.0.wait().discard_result()
}
}
#[test] #[test]
fn api_integration_api_setup() -> anyhow::Result<()> { fn api_integration_api_setup() -> anyhow::Result<()> {
rosenpass_secret_memory::policy::secret_policy_use_only_malloc_secrets(); rosenpass_secret_memory::policy::secret_policy_use_only_malloc_secrets();
@@ -120,7 +129,8 @@ fn api_integration_api_setup() -> anyhow::Result<()> {
let deliberate_fail_child_fd = 3; let deliberate_fail_child_fd = 3;
// Start peer a // Start peer a
let _proc_a = std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass")) let _proc_a = KillChild(
std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass"))
.args(["--api-stream-fd", &deliberate_fail_child_fd.to_string()]) .args(["--api-stream-fd", &deliberate_fail_child_fd.to_string()])
.fd_mappings(vec![FdMapping { .fd_mappings(vec![FdMapping {
parent_fd: deliberate_fail_api_server.move_here().as_raw_fd(), parent_fd: deliberate_fail_api_server.move_here().as_raw_fd(),
@@ -132,10 +142,12 @@ fn api_integration_api_setup() -> anyhow::Result<()> {
]) ])
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::null()) .stdout(Stdio::null())
.spawn()?; .spawn()?,
);
// Start peer b // Start peer b
let proc_b = std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass")) let mut proc_b = KillChild(
std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass"))
.args([ .args([
"exchange-config", "exchange-config",
peer_b.config_file_path.to_str().context("")?, peer_b.config_file_path.to_str().context("")?,
@@ -143,10 +155,11 @@ fn api_integration_api_setup() -> anyhow::Result<()> {
.stdin(Stdio::null()) .stdin(Stdio::null())
.stderr(Stdio::null()) .stderr(Stdio::null())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.spawn()?; .spawn()?,
);
// Acquire stdout // Acquire stdout
let mut out_b = BufReader::new(proc_b.stdout.context("")?).lines(); let mut out_b = BufReader::new(proc_b.0.stdout.take().context("")?).lines();
// Now connect to the peers // Now connect to the peers
let api_path = peer_a.api.listen_path[0].as_path(); let api_path = peer_a.api.listen_path[0].as_path();

View File

@@ -8,16 +8,25 @@ use std::{
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use rosenpass::api; use rosenpass::api;
use rosenpass_to::{ops::copy_slice_least_src, To}; use rosenpass_to::{ops::copy_slice_least_src, To};
use rosenpass_util::zerocopy::ZerocopySliceExt;
use rosenpass_util::{ use rosenpass_util::{
file::LoadValueB64, file::LoadValueB64,
length_prefix_encoding::{decoder::LengthPrefixDecoder, encoder::LengthPrefixEncoder}, length_prefix_encoding::{decoder::LengthPrefixDecoder, encoder::LengthPrefixEncoder},
}; };
use rosenpass_util::{mem::DiscardResultExt, zerocopy::ZerocopySliceExt};
use tempfile::TempDir; use tempfile::TempDir;
use zerocopy::AsBytes; use zerocopy::AsBytes;
use rosenpass::protocol::SymKey; use rosenpass::protocol::SymKey;
struct KillChild(std::process::Child);
impl Drop for KillChild {
fn drop(&mut self) {
self.0.kill().discard_result();
self.0.wait().discard_result()
}
}
#[test] #[test]
fn api_integration_test() -> anyhow::Result<()> { fn api_integration_test() -> anyhow::Result<()> {
rosenpass_secret_memory::policy::secret_policy_use_only_malloc_secrets(); rosenpass_secret_memory::policy::secret_policy_use_only_malloc_secrets();
@@ -93,28 +102,32 @@ fn api_integration_test() -> anyhow::Result<()> {
peer_b.commit()?; peer_b.commit()?;
// Start peer a // Start peer a
let proc_a = std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass")) let mut proc_a = KillChild(
std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass"))
.args([ .args([
"exchange-config", "exchange-config",
peer_a.config_file_path.to_str().context("")?, peer_a.config_file_path.to_str().context("")?,
]) ])
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.spawn()?; .spawn()?,
);
// Start peer b // Start peer b
let proc_b = std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass")) let mut proc_b = KillChild(
std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass"))
.args([ .args([
"exchange-config", "exchange-config",
peer_b.config_file_path.to_str().context("")?, peer_b.config_file_path.to_str().context("")?,
]) ])
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.spawn()?; .spawn()?,
);
// Acquire stdout // Acquire stdout
let mut out_a = BufReader::new(proc_a.stdout.context("")?).lines(); let mut out_a = BufReader::new(proc_a.0.stdout.take().context("")?).lines();
let mut out_b = BufReader::new(proc_b.stdout.context("")?).lines(); let mut out_b = BufReader::new(proc_b.0.stdout.take().context("")?).lines();
// Wait for the keys to successfully exchange a key // Wait for the keys to successfully exchange a key
let mut attempt = 0; let mut attempt = 0;