#[test]
fn job_handle_debug_does_not_panic() {
use super::JobHandle;
let h = JobHandle::create_empty_for_test();
h.hard_kill().expect("hard_kill on a live job");
let s = format!("{h:?}");
assert!(s.contains("JobHandle"), "debug output: {s}");
}
#[test]
fn wait_drained_on_a_consumed_handle_is_unassessable() {
use super::JobHandle;
let h = JobHandle::create_empty_for_test();
h.hard_kill().expect("hard_kill on a live job");
let err = h
.wait_drained(Some(None), None)
.expect_err("a consumed job handle must not report a live drain verdict");
let crate::error::Error::Unassessable { source, .. } = err else {
panic!("expected Unassessable, got {err:?}");
};
assert!(
source.is_none(),
"nothing was asked of the OS on this path — no source is expected"
);
}
#[test]
fn query_job_pid_list_is_empty_for_an_unpopulated_job() {
use super::JobHandle;
let job = JobHandle::create_empty_for_test();
let job_handle = job.as_handle().expect("freshly created job handle must be live");
let pids = super::query_job_pid_list(job_handle).expect("query pid list");
assert!(pids.is_empty(), "an empty job must report no members: {pids:?}");
}
#[test]
fn wait_drained_raw_reports_drained_for_an_empty_job() {
use super::JobHandle;
let job = JobHandle::create_empty_for_test();
let job_handle = job.as_handle().expect("freshly created job handle must be live");
let verdict = super::wait_drained_raw(job_handle, Some(None), None).expect("wait_drained_raw");
assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
}
#[test]
fn wait_drained_raw_tracks_a_real_member_through_exit() {
use std::os::windows::io::AsRawHandle;
let mut child = std::process::Command::new("cmd")
.args(["/C", "more"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.expect("spawn cmd /C more");
let raw = child.as_raw_handle();
let job = super::assign_to_kill_on_close_job(raw).expect("assign to job");
let job_handle = job.as_handle().expect("freshly created job handle must be live");
let pids = super::query_job_pid_list(job_handle).expect("query pid list");
assert_eq!(
pids,
vec![child.id()],
"the job must report exactly the member just assigned"
);
let past = std::time::Instant::now() - std::time::Duration::from_secs(1);
let verdict = super::wait_drained_raw(job_handle, Some(Some(past)), None).expect("wait_drained_raw");
assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);
drop(child.stdin.take());
child.wait().expect("wait for cmd /C more to exit");
let verdict = super::wait_drained_raw(job_handle, Some(None), None).expect("wait_drained_raw");
assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
}
#[test]
fn caller_has_console_is_true_under_cargo_test() {
assert!(
matches!(super::caller_has_console(), Ok(true)),
"cargo test is expected to run with a console attached"
);
}
#[test]
fn console_probe_error_surfaces() {
super::fault::set_force_console_probe_error(true);
assert!(super::caller_has_console().is_err());
assert!(!super::fault::armed(), "the seam must be consumed by one call");
assert!(matches!(super::caller_has_console(), Ok(true)));
}
const E_INVALID_HANDLE: i32 = 0x8007_0006u32 as i32;
const E_ACCESS_DENIED: i32 = 0x8007_0005u32 as i32;
fn io_err(hresult: i32) -> std::io::Error {
std::io::Error::from_raw_os_error(hresult)
}
#[test]
fn invalid_handle_with_no_console_is_typed_no_console() {
let e = super::classify_ctrl_event_failure(4242, io_err(E_INVALID_HANDLE), Ok(false));
let crate::error::Error::NoConsole { detail } = e else {
panic!("expected NoConsole, got {e:?}");
};
assert!(detail.contains("4242"), "the detail must name the group: {detail}");
assert!(
detail.contains("kill_tree()"),
"the detail must name the way out: {detail}"
);
}
#[test]
fn invalid_handle_with_a_console_attached_stays_io() {
let e = super::classify_ctrl_event_failure(1, io_err(E_INVALID_HANDLE), Ok(true));
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
#[test]
fn invalid_handle_with_a_failed_probe_stays_io() {
let probe = Err(std::io::Error::from_raw_os_error(87)); let e = super::classify_ctrl_event_failure(1, io_err(E_INVALID_HANDLE), probe);
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
#[test]
fn a_different_failure_code_stays_io_whatever_the_probe_says() {
for console in [Ok(true), Ok(false)] {
let e = super::classify_ctrl_event_failure(1, io_err(E_ACCESS_DENIED), console);
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
let e = super::classify_ctrl_event_failure(1, io_err(E_ACCESS_DENIED), Err(io_err(87)));
assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}
#[test]
fn the_no_console_signature_matches_the_real_win32_mapping() {
use windows::Win32::Foundation::ERROR_INVALID_HANDLE;
let hr = windows::core::HRESULT::from_win32(ERROR_INVALID_HANDLE.0);
assert_eq!(hr.0, E_INVALID_HANDLE);
assert_eq!(
std::io::Error::from(windows::core::Error::from_hresult(hr)).raw_os_error(),
Some(E_INVALID_HANDLE)
);
}
mod mechanism_from_flags_tests {
use crate::containment::windows::{group_flags, mechanism_from_flags, root_flags};
use crate::graceful::GracefulMechanism;
use windows::Win32::System::Threading::{CREATE_NEW_CONSOLE, CREATE_NO_WINDOW, DETACHED_PROCESS};
#[test]
fn mechanism_from_flags_reports_none_for_no_flags() {
assert_eq!(mechanism_from_flags(0), GracefulMechanism::None);
}
#[test]
fn mechanism_from_flags_reports_console_group_for_group_flags() {
assert_eq!(mechanism_from_flags(group_flags()), GracefulMechanism::ConsoleGroup);
}
#[test]
fn mechanism_from_flags_reports_console_group_for_root_flags() {
assert_eq!(mechanism_from_flags(root_flags()), GracefulMechanism::ConsoleGroup);
}
#[test]
fn mechanism_from_flags_reports_other_console_group_for_group_plus_detached() {
assert_eq!(
mechanism_from_flags(group_flags() | DETACHED_PROCESS.0),
GracefulMechanism::OtherConsoleGroup
);
}
#[test]
fn mechanism_from_flags_reports_other_console_group_for_group_plus_new_console() {
assert_eq!(
mechanism_from_flags(group_flags() | CREATE_NEW_CONSOLE.0),
GracefulMechanism::OtherConsoleGroup
);
}
#[test]
fn mechanism_from_flags_reports_other_console_group_for_group_plus_no_window() {
assert_eq!(
mechanism_from_flags(group_flags() | CREATE_NO_WINDOW.0),
GracefulMechanism::OtherConsoleGroup
);
}
#[test]
fn mechanism_from_flags_reports_none_for_detached_without_a_group() {
assert_eq!(mechanism_from_flags(DETACHED_PROCESS.0), GracefulMechanism::None);
}
#[test]
fn mechanism_from_flags_reports_none_for_a_new_console_without_a_group() {
assert_eq!(mechanism_from_flags(CREATE_NEW_CONSOLE.0), GracefulMechanism::None);
}
#[test]
fn mechanism_from_flags_reports_none_for_no_window_without_a_group() {
assert_eq!(mechanism_from_flags(CREATE_NO_WINDOW.0), GracefulMechanism::None);
}
}
#[test]
fn probe_reports_unknown_when_the_query_fails() {
use crate::containment::windows::{fault, probe_job_breakaway, JobBreakaway};
assert_ne!(
probe_job_breakaway(),
JobBreakaway::Unknown,
"unarmed, the probe must reach a real verdict"
);
fault::set_force_job_probe_error(true);
assert_eq!(probe_job_breakaway(), JobBreakaway::Unknown);
assert_ne!(
probe_job_breakaway(),
JobBreakaway::Unknown,
"take semantics: the fault applies to exactly one call"
);
}
#[test]
fn probe_agrees_with_an_independent_is_process_in_job_measurement() {
use crate::containment::windows::{probe_job_breakaway, JobBreakaway};
use windows::Win32::System::JobObjects::IsProcessInJob;
use windows::Win32::System::Threading::GetCurrentProcess;
let mut in_job = windows::core::BOOL(0);
unsafe { IsProcessInJob(GetCurrentProcess(), None, &mut in_job) }.expect("IsProcessInJob");
let verdict = probe_job_breakaway();
if in_job.as_bool() {
assert_ne!(
verdict,
JobBreakaway::NotInJob,
"this process IS in a job, so the probe must not report otherwise"
);
} else {
assert_eq!(
verdict,
JobBreakaway::NotInJob,
"this process is in no job, so the probe must say so"
);
}
}