use super::fault;
use crate::command::Command;
use crate::error::Error;
fn blocker() -> Command {
let mut cmd = Command::new();
#[cfg(unix)]
cmd.args(["sleep", "30"]);
#[cfg(windows)]
cmd.args(["ping", "-n", "30", "127.0.0.1"]);
cmd
}
#[test]
fn identity_failure_reaps_the_spawned_child() {
fault::set_force_identity_vanished(true);
let mut cmd = blocker();
let err = cmd.spawn().err();
fault::set_force_identity_vanished(false);
let err = err.expect("forced identity-vanish must make spawn return Err");
assert!(
matches!(err, Error::Io(_)),
"identity-vanish surfaces as an Io error, got {err:?}"
);
fault::assert_child_reaped(fault::take_captured().expect("seam captured the child's identity"));
}
#[test]
fn attach_failure_reaps_the_spawned_child() {
fault::set_force_attach_failure(true);
let mut cmd = blocker();
let err = cmd.spawn().err();
fault::set_force_attach_failure(false);
let err = err.expect("forced attach failure must make spawn return Err");
assert!(
matches!(err, Error::Containment { .. }),
"a real attach failure surfaces as Error::Containment, got {err:?}"
);
fault::assert_child_reaped(fault::take_captured().expect("seam captured the child's identity"));
}
#[test]
fn spawn_unelevated_runs_a_plain_child() {
let mut c = crate::command::Command::new();
#[cfg(unix)]
c.args(["true"]);
#[cfg(windows)]
c.args(["cmd", "/C", "exit 0"]);
let kill_on_drop = c.kill_on_drop_flag();
let child = super::spawn_unelevated(&mut c, kill_on_drop).expect("spawn");
assert!(child.wait().expect("wait").success());
}
#[test]
fn non_elevated_spawn_skips_the_elevation_branch() {
let mut c = crate::command::Command::new();
#[cfg(unix)]
c.args(["true"]);
#[cfg(windows)]
c.args(["cmd", "/C", "exit 0"]);
let child = super::spawn(&mut c).expect("non-elevated spawn");
assert!(child.wait().expect("wait").success());
}
#[cfg(windows)]
#[test]
fn elevated_pipe_is_rejected_deterministically_regardless_of_privilege() {
let mut c = crate::command::Command::new();
c.args(["whoami"]).elevate();
c.stdout(crate::stdio::Stdio::pipe()).unwrap();
assert!(matches!(
super::spawn(&mut c),
Err(crate::error::Error::Unsupported { .. })
));
}
#[cfg(windows)]
#[test]
fn routes_to_raw_backend_answers_for_executables_and_high_descriptors() {
use crate::stdio::Stdio;
let mut argv_only = Command::new();
argv_only.args(["cmd", "/C", "exit 0"]);
assert!(
!super::routes_to_raw_backend(&argv_only),
"an argv-only command stays on the std path"
);
let mut exe_only = Command::new();
exe_only.executable("cmd").args(["cmd", "/C", "exit 0"]);
assert!(super::routes_to_raw_backend(&exe_only), "an executable() routes to raw");
let mut high_fd_only = Command::new();
high_fd_only.args(["cmd", "/C", "exit 0"]);
high_fd_only.fd(3, Stdio::pipe_out()).unwrap();
assert!(
super::routes_to_raw_backend(&high_fd_only),
"a descriptor >= 3 routes to raw even with no executable(): std cannot carry it, and the \
std path's fd >= 3 collection is unix-only, so it would be dropped in silence"
);
let mut both = Command::new();
both.executable("cmd").args(["cmd", "/C", "exit 0"]);
both.fd(3, Stdio::pipe_out()).unwrap();
assert!(super::routes_to_raw_backend(&both));
}
#[cfg(windows)]
#[test]
fn a_refused_raw_spawn_does_not_clear_our_handle_inheritance() {
use crate::containment::windows::observe;
let mut refused = Command::new();
refused
.executable("cmd")
.args(["cmd", "/C", "exit 0"])
.contain()
.creation_flags(windows::Win32::System::Threading::CREATE_SUSPENDED.0);
observe::take_inheritance_cleared();
let err = refused.spawn().expect_err("a reserved bit must be refused");
assert!(matches!(err, Error::Unsupported { .. }), "got {err:?}");
assert!(
!observe::take_inheritance_cleared(),
"the refusal ran after the mutation it was supposed to precede"
);
let mut allowed = Command::new();
allowed.executable("cmd").args(["cmd", "/C", "exit 0"]).contain();
let child = allowed
.spawn()
.expect("the same command without the reserved bit spawns");
assert!(
observe::take_inheritance_cleared(),
"the seam must record a real call, else the negative leg above proves nothing"
);
child.wait().expect("reap");
}
#[cfg(windows)]
#[test]
fn a_refused_std_spawn_does_not_clear_our_handle_inheritance() {
use crate::containment::windows::observe;
let mut refused = Command::new();
refused
.args(["cmd", "/C", "exit 0"])
.contain()
.creation_flags(windows::Win32::System::Threading::CREATE_SUSPENDED.0);
assert!(
!super::routes_to_raw_backend(&refused),
"this leg is only a std-path proof while the command stays off the raw backend"
);
observe::take_inheritance_cleared();
let err = refused.spawn().expect_err("a reserved bit must be refused");
assert!(matches!(err, Error::Unsupported { .. }), "got {err:?}");
assert!(
!observe::take_inheritance_cleared(),
"the refusal ran after the mutation it was supposed to precede"
);
let mut allowed = Command::new();
allowed.args(["cmd", "/C", "exit 0"]).contain();
let child = allowed
.spawn()
.expect("the same command without the reserved bit spawns");
assert!(
observe::take_inheritance_cleared(),
"the seam must record a real call, else the negative leg above proves nothing"
);
child.wait().expect("reap");
}