use std::time::Duration;
use super::{
assert_no_break_sent, assert_proxy_answered, connect_reporter, kill_release_reap, report_done, spawn_gui_root,
};
use crate::child::graceful::ctrl_break_tests::CALLS;
use crate::error::Error;
use crate::graceful_hooks::{release_at, HookPoint};
use crate::identity::Liveness;
use crate::stop::proxy::protocol::{CtrlEvent, ProxyAnswer};
use crate::stop::proxy::proxy_tests::Fixture;
use crate::{Containment, StopTarget};
pub(crate) const REPORTER_TEST: &str = "test_child::ctrl_break::console_less::fixture_console_less_reporter";
pub(crate) const ADDR_ENV: &str = "COSCA_FIXTURE_CONSOLE_LESS_ADDR";
pub(crate) fn assert_no_console() {
use windows::Win32::Foundation::{SetLastError, ERROR_INVALID_HANDLE, WIN32_ERROR};
use windows::Win32::System::Console::GetConsoleProcessList;
let mut list = [0u32; 1];
let n = unsafe {
SetLastError(WIN32_ERROR(0));
GetConsoleProcessList(&mut list)
};
assert_eq!(n, 0, "the reporter must have no console, else the checks are vacuous");
assert_eq!(
std::io::Error::last_os_error().raw_os_error(),
Some(ERROR_INVALID_HANDLE.0 as i32),
"GetConsoleProcessList must fail for want of a console, not for another reason"
);
}
fn spawn_group_leader() -> crate::test_child::StdChild {
use windows::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP;
crate::test_child::blocker_with_flags(CREATE_NEW_PROCESS_GROUP.0, crate::test_child::Console::Hidden, |_| {})
}
fn assert_refused_sends_are_not_sent() {
use crate::containment::{Contains as _, TerminateOutcome, TreeStop};
let leader = spawn_group_leader();
let job = crate::containment::windows::assign_to_kill_on_close_job(
std::os::windows::io::AsRawHandle::as_raw_handle(&*leader),
)
.expect("assign the group leader to a job");
let broken = TreeStop::of_event(
crate::stop::proxy::protocol::CtrlEvent::CtrlBreak,
crate::containment::SendOutcome::direct(crate::containment::windows::terminate(leader.id())),
);
let (outcome, _) = job.send_terminate(leader.id(), None, broken);
assert!(
matches!(outcome, TerminateOutcome::NotSent(_)),
"a job's refused break must be NotSent, got {outcome:?}"
);
assert_no_break_sent();
job.hard_kill().expect("end the group leader");
let leader = spawn_group_leader();
let id = crate::identity::ProcessId::of(leader.id())
.found()
.expect("the live group leader resolves");
let broken = TreeStop::of_event(
crate::stop::proxy::protocol::CtrlEvent::CtrlBreak,
crate::containment::SendOutcome::direct(crate::containment::windows::terminate(leader.id())),
);
let (outcome, _) = crate::containment::treewalk::terminate_tree(&crate::identity::HostKernel, id, None, broken);
assert!(
matches!(outcome, TerminateOutcome::NotSent(_)),
"a walker's refused break must be NotSent, got {outcome:?}"
);
assert_no_break_sent();
}
fn direct(target: &StopTarget<'_>) -> Result<(), Error> {
crate::stop::send_ctrl_break_to_group(target)
}
fn assert_the_direct_send_has_no_console() {
let (child, sock) = spawn_gui_root(|cmd| _ = cmd.graceful_stop(direct));
assert_eq!(child.containment(), Containment::JobObject);
assert!(
matches!(child.terminate_tree(), Err(Error::NoConsole { .. })),
"terminate_tree must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"a fail-fast terminate leaves the tree alone"
);
assert!(
matches!(
child.graceful_shutdown_tree(Duration::ZERO),
Err(Error::NoConsole { .. })
),
"graceful_shutdown_tree must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"graceful_shutdown_tree must fail before signalling, not after hard-killing"
);
kill_release_reap(&child, sock, true);
let (child, sock) = spawn_gui_root(|cmd| _ = cmd.graceful_stop(direct));
assert!(
matches!(child.terminate(), Err(Error::NoConsole { .. })),
"terminate must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"a fail-fast terminate leaves the child alone"
);
assert!(
matches!(child.graceful_shutdown(Duration::ZERO), Err(Error::NoConsole { .. })),
"graceful_shutdown must report NoConsole"
);
assert_no_break_sent();
assert_eq!(
child.is_alive(),
Liveness::Alive,
"graceful_shutdown must fail before signalling, not after hard-killing"
);
kill_release_reap(&child, sock, false);
}
fn assert_the_default_asks_the_proxy() {
for call in CALLS {
let fixture = Fixture::answering(ProxyAnswer::Sent.code());
let (child, sock) = spawn_gui_root(|cmd| _ = cmd.ctrl_proxy(fixture.setting()));
assert!(
!StopTarget::from(&child).leads_group_on_our_console(),
"the caller had no console at the spawn, so there is no route from it"
);
if call.escalates() {
let _release = release_at(HookPoint::BeforeReap, sock);
let status = call
.run(&child, Duration::ZERO)
.unwrap_or_else(|e| panic!("{call:?}: {e:?}"));
assert_the_proxy_was_asked(call, call.escalates(), child.pid(), status);
} else {
let status = call
.run(&child, Duration::ZERO)
.unwrap_or_else(|e| panic!("{call:?}: {e:?}"));
assert_the_proxy_was_asked(call, call.escalates(), child.pid(), status);
kill_release_reap(&child, sock, false);
}
}
}
pub(crate) fn assert_the_proxy_was_asked(
call: impl std::fmt::Debug,
escalates: bool,
pid: u32,
status: Option<std::process::ExitStatus>,
) {
assert_proxy_answered(pid, CtrlEvent::CtrlBreak, ProxyAnswer::Sent);
if escalates {
let status = status.expect("a graceful call answers a status");
assert_eq!(status.code(), Some(1), "{call:?}: the escalation's kill: {status:?}");
}
}
#[skuld::test]
fn fixture_console_less_reporter() {
let Some(mut report) = connect_reporter(ADDR_ENV) else {
return;
};
assert_no_console();
assert_refused_sends_are_not_sent();
assert_the_direct_send_has_no_console();
assert_the_default_asks_the_proxy();
report_done(&mut report);
}