use std::time::Duration;
use windows::Win32::Foundation::{ERROR_ACCESS_DENIED, ERROR_INVALID_HANDLE, HANDLE};
use windows::Win32::System::Threading::CreateEventW;
use crate::error::Error;
use crate::identity::{refusing_the_next_terminate, FakeKernel, ProcessId};
use crate::os_process::drive::exited_held;
use crate::os_process::{kill, open, open_with, terminate, Exit, OpenFailure, OsProcess as _, WaitFor};
use crate::signal::{Sent, Sig};
use crate::test_child::live_blocker as blocker;
#[skuld::test]
fn one_open_serves_look_signal_and_wait() {
let (child, _stdin, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
assert!(matches!(p.exited().unwrap(), Exit::Running(WaitFor::Signalled(_))));
assert!(!exited_held(&*p, Some(Duration::ZERO)).unwrap());
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Delivered);
assert!(exited_held(&*p, None).unwrap());
child.wait().unwrap();
}
#[skuld::test]
fn open_with_wrong_start_token_is_none_and_sends_nothing() {
let (child, _stdin, id) = blocker();
let other = id.with_other_value();
assert!(open(other).unwrap().is_none());
let p = open(id).unwrap().expect("the blocker is alive");
assert!(!exited_held(&*p, Some(Duration::ZERO)).unwrap());
child.kill().unwrap();
child.wait().unwrap();
}
#[skuld::test]
fn exited_before_first_look_returns_true() {
let (child, _stdin, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
child.wait().unwrap();
assert!(exited_held(&*p, Some(Duration::ZERO)).unwrap());
}
#[skuld::test]
fn kill_after_exit_is_ok() {
let (child, _stdin, id) = blocker();
child.kill().unwrap();
child.wait().unwrap();
let p = open(id)
.unwrap()
.expect("an exited process whose owner holds its handle opens");
kill(&*p).expect("killing an exited process is success");
}
#[skuld::test]
fn signal_after_exit_is_gone() {
let (child, _stdin, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
child.kill().unwrap();
child.wait().unwrap();
assert_eq!(p.signal(Sig::Kill).unwrap(), Sent::Gone);
}
#[skuld::test]
fn kill_ends_the_process() {
let (child, _stdin, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
kill(&*p).unwrap();
let status = child.wait().unwrap();
assert!(
!status.success(),
"a killed blocker does not exit by itself: {status:?}"
);
}
#[skuld::test]
fn terminate_of_a_windows_held_process_is_unsupported() {
let (child, _stdin, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
let err = terminate(&*p).expect_err("Windows has no graceful per-process signal");
assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");
assert!(!exited_held(&*p, Some(Duration::ZERO)).unwrap());
child.kill().unwrap();
child.wait().unwrap();
}
#[skuld::test]
fn the_wait_is_on_the_opened_process_handle() {
use std::os::windows::io::AsRawHandle;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::System::Threading::GetProcessId;
let (child, _stdin, id) = blocker();
let p = open(id).unwrap().expect("a live process opens");
match p.exited().unwrap() {
Exit::Running(WaitFor::Signalled(h)) => {
assert_eq!(unsafe { GetProcessId(HANDLE(h.as_raw_handle())) }, id.pid())
}
other => panic!("a live process reads Running(Signalled): {other:?}"),
}
child.kill().unwrap();
child.wait().unwrap();
}
#[skuld::test]
fn signal_failure_carries_the_win32_code() {
use windows::Win32::Foundation::ERROR_INVALID_HANDLE;
let p = super::WindowsProcess::for_child(
crate::identity::HostKernel,
super::test_event::new_test_event().expect("create an event handle"),
std::process::id(),
);
let err = p.signal(Sig::Kill).expect_err("an event cannot be terminated");
let Error::Io(io) = err else {
panic!("expected Io, got {err:?}");
};
assert_eq!(io.raw_os_error(), Some(ERROR_INVALID_HANDLE.0 as i32), "{io:?}");
}
#[skuld::test]
fn a_windows_child_owns_the_kernel_it_was_built_with() {
use std::sync::Arc;
use crate::identity::Kernel;
use crate::os_process::child::OsChild as _;
let kernel = Arc::new(FakeKernel::new());
let p = super::WindowsProcess::for_child(
Arc::clone(&kernel),
super::test_event::new_test_event().expect("create an event handle"),
std::process::id(),
);
assert_eq!(Arc::strong_count(&kernel), 2, "the box owns a handle on the kernel");
let _lent: &dyn Kernel = p.kernel();
drop(p);
assert_eq!(Arc::strong_count(&kernel), 1, "dropping the box releases the kernel");
}
#[skuld::test]
fn a_uac_handle_whose_access_cannot_be_read_is_taken_to_hold_synchronize_only() {
use windows::Win32::System::Threading::PROCESS_SYNCHRONIZE;
let granted = super::access_of_uac_read(Err::<u32, _>(std::io::Error::other("unreadable")));
assert_eq!(granted, PROCESS_SYNCHRONIZE);
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "holds SYNCHRONIZE")]
fn a_uac_handle_read_without_synchronize_breaks_the_contract() {
use windows::Win32::System::Threading::PROCESS_TERMINATE;
super::access_of_uac_read(Ok::<_, std::io::Error>(PROCESS_TERMINATE.0));
}
#[skuld::test]
fn a_uac_handle_keeps_the_access_it_was_read_as() {
use windows::Win32::System::Threading::{PROCESS_SYNCHRONIZE, PROCESS_TERMINATE};
let read = PROCESS_SYNCHRONIZE.0 | PROCESS_TERMINATE.0;
assert_eq!(super::access_of_uac_read(Ok::<_, std::io::Error>(read)).0, read);
}
#[skuld::test]
fn kill_of_a_process_that_is_exiting_but_not_signalled_is_ok() {
let (child, _stdin, id) = blocker();
let kernel = std::sync::Arc::new(refusing_the_next_terminate());
let p = open_with(std::sync::Arc::clone(&kernel), id)
.map_err(OpenFailure::into_open_error)
.unwrap()
.expect("a live process opens");
let result = kill(&*p);
child.kill().expect("end the fixture");
child.wait().expect("reap the fixture");
assert!(
kernel.all_rows_taken(),
"the kill reached TerminateProcess to be refused"
);
result.expect("a refusal on an exiting process is not a failed kill");
}
fn assert_access_denied<T: std::fmt::Debug>(result: Result<T, Error>, what: &str) {
let Err(Error::Io(io)) = result else {
panic!("{what} must be Io(ERROR_ACCESS_DENIED), got {result:?}");
};
assert_eq!(io.raw_os_error(), Some(ERROR_ACCESS_DENIED.0 as i32), "{what}: {io:?}");
}
#[skuld::test]
fn a_look_without_synchronize_reads_the_exit_code() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(a_look_without_synchronize_reads_the_exit_code),
crate::test_spawn::spawn,
) else {
return;
};
let child = crate::identity::windows_fixture::spawn_query_only(&done);
let id =
crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, child.handle(), child.pid())
.expect("the owned handle always yields an identity");
let p = open(id).unwrap().expect("a live process opens with QUERY alone");
assert_access_denied(p.exited(), "a look at a running process");
assert_access_denied(
exited_held(&*p, Some(Duration::ZERO)),
"a driven look at a running process",
);
child.terminate();
assert!(
matches!(p.exited().unwrap(), Exit::Exited),
"an exit code proves the exit"
);
}
#[skuld::test]
fn kill_of_a_live_process_without_terminate_is_io_access_denied() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(kill_of_a_live_process_without_terminate_is_io_access_denied),
crate::test_spawn::spawn,
) else {
return;
};
let child = crate::identity::windows_fixture::spawn_query_only(&done);
let id =
crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, child.handle(), child.pid())
.expect("the owned handle always yields an identity");
let p = open(id).unwrap().expect("a live process opens with QUERY alone");
assert_access_denied(p.signal(Sig::Kill), "a kill without PROCESS_TERMINATE");
assert!(child.is_running(), "nothing killed it");
}
fn event_handle() -> HANDLE {
unsafe { CreateEventW(None, true, false, None) }.expect("create an event handle")
}
#[skuld::test]
fn holds_process_of_an_unreadable_handle_carries_the_win32_code() {
let err = super::holds_process(event_handle(), ProcessId::current()).expect_err("an event has no creation time");
let crate::error::Error::Unassessable { source, .. } = err else {
panic!("expected Unassessable, got {err:?}");
};
let io = source.expect("the OS error is preserved");
assert_eq!(
crate::error::first_os_code(&io),
Some(ERROR_INVALID_HANDLE.0 as i32),
"{io:?}"
);
}
#[skuld::test]
fn open_verified_of_an_access_denied_live_process_carries_the_win32_code() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(open_verified_of_an_access_denied_live_process_carries_the_win32_code),
crate::test_spawn::spawn,
) else {
return;
};
let io = denied_open_source(&done, |id| super::open_verified(id).map(|_| ()));
assert_eq!(io.raw_os_error(), Some(ERROR_ACCESS_DENIED.0 as i32), "{io:?}");
}
fn denied_open_source(
done: &crate::test_own_process::Completion,
call: impl FnOnce(ProcessId) -> Result<(), crate::error::Error>,
) -> std::io::Error {
let child = crate::identity::windows_fixture::spawn_unkillable(done);
let id =
crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, child.handle(), child.pid())
.expect("the owned handle always yields an identity");
assert!(child.is_running(), "precondition: the subject must be live");
let err = call(id).expect_err("an unopenable live process is not 'gone'");
let crate::error::Error::Unassessable { source, .. } = err else {
panic!("expected Unassessable, got {err:?}");
};
let Some(crate::error::Error::Io(os)) = source.map(|source| std::sync::Arc::try_unwrap(source).expect("one owner"))
else {
panic!("the OS error is preserved");
};
os
}
#[skuld::test]
fn peek_of_a_running_child_is_running() {
use crate::os_process::child::{adopted_blocker, PeekOutcome, TryObserveOutcome};
let (os, _id, _stdin) = adopted_blocker();
assert_eq!(os.peek().unwrap(), PeekOutcome::Running);
os.signal(Sig::Kill).unwrap();
assert!(exited_held(&*os, None).unwrap());
assert!(matches!(os.try_observe().unwrap(), TryObserveOutcome::Exited(_)));
}
#[skuld::test]
fn try_observe_of_a_running_child_waits_on_the_handle() {
use crate::os_process::child::{adopted_blocker, TryObserveOutcome};
let (os, _id, _stdin) = adopted_blocker();
assert!(matches!(
os.try_observe().unwrap(),
TryObserveOutcome::Running(WaitFor::Signalled(_))
));
os.signal(Sig::Kill).unwrap();
assert!(exited_held(&*os, None).unwrap());
}
#[skuld::test]
fn try_observe_of_an_exited_child_reads_the_exit_code() {
use crate::os_process::child::{adopted_blocker, ExitRecord, TryObserveOutcome};
let (os, _id, _stdin) = adopted_blocker();
os.signal(Sig::Kill).unwrap();
assert!(exited_held(&*os, None).unwrap());
let TryObserveOutcome::Exited(ExitRecord::Status(status)) = os.try_observe().unwrap() else {
panic!("an exited child is seen to have exited");
};
assert_eq!(status.code(), Some(crate::identity::WINDOWS_TERMINATE_EXIT_CODE as i32));
}