use std::io::Write;
use std::process::{Command, Stdio};
fn run_binary(binary: &str, input: &[u8]) -> Vec<u8> {
let build_status = Command::new("cargo")
.args(["build", "--workspace"])
.status()
.expect("Failed to build workspace binaries");
if !build_status.success() {
panic!("Failed to build workspace binaries");
}
let mut child = Command::new("cargo");
if binary == "test_binary" {
child.arg("run").arg("--bin").arg(binary);
} else if binary == "tokio-example-app" {
child.arg("run").arg("--package").arg(binary);
} else {
panic!("Unknown binary: {}", binary);
}
child.stdin(Stdio::piped()).stdout(Stdio::piped());
let mut child = child
.spawn()
.unwrap_or_else(|_| panic!("Failed to spawn process: {}", binary));
if let Some(mut stdin) = child.stdin.take() {
if !input.is_empty() {
stdin
.write_all(input)
.expect("Failed to write binary to stdin");
}
drop(stdin); }
let output = child.wait_with_output().expect("Failed to read stdout");
output.stdout }
#[test]
fn test_binary_input_handling() {
{
let binary_input: &[u8] = b"\xDE\xAD\xBE\xEF"; let output_bytes = run_binary("test_binary", binary_input);
assert_eq!(
output_bytes, binary_input,
"Expected output to match input, but got: {:?}",
output_bytes
);
}
{
let binary_input: &[u8] = b"\xDE\xAD\xBE\xEF"; let output_bytes = run_binary("tokio-example-app", binary_input);
assert_eq!(
output_bytes, binary_input,
"Expected output to match input, but got: {:?}",
output_bytes
);
}
}
#[test]
fn test_text_input_handling() {
{
let text_input = b"Hello, binary world!";
let output_bytes = run_binary("test_binary", text_input);
assert_eq!(
output_bytes, text_input,
"Expected output to match input, but got: {:?}",
output_bytes
);
}
{
let text_input = b"Hello, binary world!";
let output_bytes = run_binary("tokio-example-app", text_input);
assert_eq!(
output_bytes, text_input,
"Expected output to match input, but got: {:?}",
output_bytes
);
}
}
#[test]
fn test_empty_input() {
{
let output_bytes = run_binary("test_binary", b"");
assert_eq!(
output_bytes, b"fallback_value",
"Expected fallback value but got: {:?}",
output_bytes
);
}
{
let output_bytes = run_binary("tokio-example-app", b"");
assert_eq!(
output_bytes, b"fallback_value",
"Expected fallback value but got: {:?}",
output_bytes
);
}
}