use std::io::Write;
use std::os::unix::process::ExitStatusExt;
use birdcage::process::{Command, Stdio};
use birdcage::{Birdcage, Exception, Sandbox};
#[cfg(not(target_os = "linux"))]
fn main() {}
#[cfg(target_os = "linux")]
fn main() {
pipe_stdin_to_stdout();
exit_signal();
}
#[cfg(target_os = "linux")]
fn pipe_stdin_to_stdout() {
let mut cmd = Command::new("cat");
cmd.stdin(Stdio::piped());
cmd.stdout(Stdio::piped());
let mut sandbox = Birdcage::new();
sandbox.add_exception(Exception::ExecuteAndRead("/".into())).unwrap();
sandbox.add_exception(Exception::WriteAndRead("/".into())).unwrap();
let mut child = sandbox.spawn(cmd).unwrap();
let expected = b"test\n";
child.stdin.as_mut().unwrap().write_all(expected).unwrap();
let output = child.wait_with_output().unwrap();
assert_eq!(&output.stdout, expected);
}
#[cfg(target_os = "linux")]
fn exit_signal() {
let cmd = Command::new("cat");
let mut sandbox = Birdcage::new();
sandbox.add_exception(Exception::ExecuteAndRead("/".into())).unwrap();
sandbox.add_exception(Exception::WriteAndRead("/".into())).unwrap();
let mut child = sandbox.spawn(cmd).unwrap();
child.kill().unwrap();
let status = child.wait().unwrap();
assert_eq!(status.signal(), Some(9));
}