use std::{fs::File, os::fd::AsRawFd as _, process::Stdio};
use super::configure_handle_inheritance;
#[test]
fn configuring_inheritance_does_not_change_parent_flags() {
let file = File::open("/").unwrap();
let fd = file.as_raw_fd();
let original_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
assert_ne!(original_flags, -1);
let mut command = tokio::process::Command::new("/bin/true");
configure_handle_inheritance(&mut command, &[&file]).unwrap();
let configured_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
assert_eq!(configured_flags, original_flags);
}
#[tokio::test]
async fn child_receives_descriptor_without_changing_parent_flags() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("payload");
std::fs::write(&path, b"inherited").unwrap();
let file = File::open(path).unwrap();
let fd = file.as_raw_fd();
let original_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
assert_ne!(original_flags & libc::FD_CLOEXEC, 0);
let mut command = tokio::process::Command::new("/bin/cat");
command
.arg(format!("/proc/self/fd/{fd}"))
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
configure_handle_inheritance(&mut command, &[&file]).unwrap();
let output = command.output().await.unwrap();
let final_flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
assert_eq!(final_flags, original_flags);
assert!(output.status.success(), "{:?}", output.stderr);
assert_eq!(output.stdout, b"inherited");
}