use windows::Win32::Foundation::ERROR_ACCESS_DENIED;
use windows::Win32::System::Threading::{
PROCESS_ACCESS_RIGHTS, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE, PROCESS_TERMINATE,
};
use super::{creation_token, current_token, exit_look, is_running, open_dropping_refused, start_token, Look, Opened};
use crate::identity::windows_fixture::{spawn_restricted, spawn_restricted_shell};
use crate::identity::{refusing_the_next_terminate, HostKernel, Key, Liveness, Resolved};
use crate::os_process::windows::CREATE_PROCESS_ACCESS;
fn exited_now(id: crate::identity::ProcessId) -> Result<bool, crate::error::Error> {
crate::test_child::exited_through_open(id, Some(std::time::Duration::ZERO))
}
#[skuld::test]
fn start_token_of_current_process_is_stable() {
let pid = std::process::id();
let a = start_token(pid);
assert!(matches!(a, Resolved::Found(_)), "our own token must resolve: {a:?}");
assert_eq!(a, start_token(pid), "the same pid must yield the same token");
}
#[skuld::test]
fn current_token_agrees_with_the_by_pid_read() {
assert_eq!(current_token(), start_token(std::process::id()));
}
#[skuld::test]
fn is_running_alive_for_self_dead_for_wrong_token() {
let pid = std::process::id();
let Resolved::Found(tok) = start_token(pid) else {
panic!("our own token must resolve");
};
assert_eq!(
is_running(&HostKernel, pid, tok),
Liveness::Alive,
"we are obviously running"
);
let wrong = Key::from_raw(tok.raw().wrapping_add(1));
assert_eq!(
is_running(&HostKernel, pid, wrong),
Liveness::Dead,
"a wrong token is a different process"
);
}
#[skuld::test]
fn is_running_reads_the_creation_time_through_its_kernel() {
use windows::core::HRESULT;
use crate::identity::FakeKernel;
let pid = std::process::id();
let stated = Key::from_raw(1);
let rows: [(&str, Result<Key, HRESULT>, Liveness); 3] = [
("the stated start", Ok(stated), Liveness::Alive),
("another start", Ok(Key::from_raw(2)), Liveness::Dead),
(
"a refused read",
Err(HRESULT::from_win32(ERROR_ACCESS_DENIED.0)),
Liveness::Unknown,
),
];
for (name, row, expected) in rows {
let kernel = FakeKernel::new().on_creation_token(row);
assert_eq!(is_running(&kernel, pid, stated), expected, "{name}");
assert!(kernel.all_rows_taken(), "{name}: the row was not read");
}
}
#[skuld::test]
fn denied_query_limited_reads_unknown_not_gone() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(denied_query_limited_reads_unknown_not_gone),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted(&done, PROCESS_SYNCHRONIZE.0);
let token = creation_token(child.handle()).expect("the owned handle can always read the token");
assert!(child.is_running(), "precondition: the subject must be live");
assert_eq!(
start_token(child.pid()),
Resolved::Unknown,
"an access-denied live process must not resolve as Gone"
);
assert_eq!(
is_running(&HostKernel, child.pid(), token),
Liveness::Unknown,
"an access-denied live process must not read as Dead"
);
assert!(child.is_running(), "and it must still have been live throughout");
}
#[skuld::test]
fn denied_synchronize_resolves_identity_but_liveness_is_unknown_while_running() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(denied_synchronize_resolves_identity_but_liveness_is_unknown_while_running),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let token = creation_token(child.handle()).expect("owned handle reads the token");
assert!(child.is_running(), "precondition: the subject must be live");
assert_eq!(start_token(child.pid()), Resolved::Found(token));
assert_eq!(
is_running(&HostKernel, child.pid(), token),
Liveness::Unknown,
"SYNCHRONIZE is denied and STILL_ACTIVE is ambiguous — never Alive, never Dead"
);
assert!(child.is_running());
}
#[skuld::test]
fn exited_process_denying_synchronize_reads_dead() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(exited_process_denying_synchronize_reads_dead),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let token = creation_token(child.handle()).expect("owned handle reads the token");
child.terminate(); assert!(!child.is_running(), "precondition: the subject must have exited");
assert_eq!(is_running(&HostKernel, child.pid(), token), Liveness::Dead);
}
#[skuld::test]
fn nonexistent_pid_is_gone_not_unknown() {
let ghost = 0xFFFF_FFF0u32;
assert_eq!(start_token(ghost), Resolved::Gone);
assert_eq!(is_running(&HostKernel, ghost, Key::from_raw(1)), Liveness::Dead);
}
#[skuld::test]
fn kill_of_an_access_denied_live_process_is_an_error() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(kill_of_an_access_denied_live_process_is_an_error),
crate::test_spawn::spawn,
) else {
return;
};
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 = crate::os_process::open(id).expect_err("a process we may not open must not read as gone");
let crate::error::Error::Unassessable { source, .. } = err else {
panic!("a denial must not read as an I/O failure: {err:?}");
};
let io = source.expect("the OS error is preserved");
assert_eq!(
crate::error::first_os_code(&io),
Some(ERROR_ACCESS_DENIED.0 as i32),
"{io}"
);
assert!(child.is_running(), "and it must still be running — nothing killed it");
}
#[skuld::test]
fn waiting_on_an_access_denied_live_process_is_an_error() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(waiting_on_an_access_denied_live_process_is_an_error),
crate::test_spawn::spawn,
) else {
return;
};
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 = exited_now(id).expect_err("an unopenable live process must not be reported as exited");
let crate::error::Error::Unassessable { source, .. } = err else {
panic!("a denial must not read as an I/O failure: {err:?}");
};
let io = source.expect("the OS error is preserved");
assert_eq!(
crate::error::first_os_code(&io),
Some(ERROR_ACCESS_DENIED.0 as i32),
"{io}"
);
}
#[skuld::test]
fn wait_of_a_synchronize_denied_but_exited_process_reports_exited() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(wait_of_a_synchronize_denied_but_exited_process_reports_exited),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let id =
crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, child.handle(), child.pid())
.expect("the owned handle always yields an identity");
child.terminate();
assert!(!child.is_running(), "precondition: the subject must have exited");
assert_eq!(id.is_alive(), Liveness::Dead, "precondition: provably dead");
assert!(exited_now(id).expect("waiting on an already-exited process is success"));
}
#[skuld::test]
fn kill_of_a_terminate_denied_but_exited_process_reports_success() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(kill_of_a_terminate_denied_but_exited_process_reports_success),
crate::test_spawn::spawn,
) else {
return;
};
use windows::Win32::System::Threading::PROCESS_TERMINATE;
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");
assert!(
matches!(
super::open_classified(child.pid(), PROCESS_TERMINATE | PROCESS_QUERY_LIMITED_INFORMATION),
super::Opened::Denied(_)
),
"precondition: the open with the terminate right must be denied"
);
child.terminate();
assert!(!child.is_running(), "precondition: the subject must have exited");
assert_eq!(id.is_alive(), Liveness::Dead, "precondition: provably dead");
let process = crate::os_process::open(id)
.expect("the open of an exited process")
.expect("the exited process is still held by its fixture");
crate::os_process::kill(&*process).expect("killing an already-exited process is success");
}
const OPTIONAL: [PROCESS_ACCESS_RIGHTS; 2] = [PROCESS_SYNCHRONIZE, PROCESS_TERMINATE];
fn granted_on(pid: u32) -> Option<PROCESS_ACCESS_RIGHTS> {
match open_dropping_refused(pid, &OPTIONAL) {
(Opened::Found(handle), granted) => {
super::close(handle);
Some(granted)
}
(Opened::Denied(_), _) => None,
(Opened::Gone, _) => panic!("pid {pid} is live"),
}
}
#[skuld::test]
fn open_dropping_refused_drops_each_refused_right() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(open_dropping_refused_drops_each_refused_right),
crate::test_spawn::spawn,
) else {
return;
};
let query = PROCESS_QUERY_LIMITED_INFORMATION;
let query_only = crate::identity::windows_fixture::spawn_query_only(&done);
assert_eq!(granted_on(query_only.pid()), Some(query));
let restricted = spawn_restricted(&done, query.0);
assert_eq!(granted_on(restricted.pid()), Some(query | PROCESS_TERMINATE));
assert_eq!(
granted_on(std::process::id()),
Some(query | PROCESS_SYNCHRONIZE | PROCESS_TERMINATE)
);
let unkillable = crate::identity::windows_fixture::spawn_unkillable(&done);
assert_eq!(granted_on(unkillable.pid()), None, "QUERY itself is refused");
}
fn liveness_of(look: windows::core::Result<Look>) -> Liveness {
match look {
Ok(Look::Running) => Liveness::Alive,
Ok(Look::Exited) => Liveness::Dead,
Err(_) => Liveness::Unknown,
}
}
#[skuld::test]
fn exit_look_agrees_with_is_running() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(exit_look_agrees_with_is_running),
crate::test_spawn::spawn,
) else {
return;
};
let look_at = |pid: u32| {
let (Opened::Found(handle), granted) = open_dropping_refused(pid, &[PROCESS_SYNCHRONIZE]) else {
panic!("pid {pid} opens with QUERY");
};
let look = exit_look(handle, granted.0 & PROCESS_SYNCHRONIZE.0 != 0);
super::close(handle);
look
};
let token_of = |pid: u32| match start_token(pid) {
Resolved::Found(t) => t,
other => panic!("pid {pid} resolves: {other:?}"),
};
let me = std::process::id();
assert_eq!(is_running(&HostKernel, me, token_of(me)), liveness_of(look_at(me)));
assert_eq!(is_running(&HostKernel, me, token_of(me)), Liveness::Alive);
let query_only = crate::identity::windows_fixture::spawn_query_only(&done);
let token = token_of(query_only.pid());
assert_eq!(
is_running(&HostKernel, query_only.pid(), token),
liveness_of(look_at(query_only.pid()))
);
assert_eq!(
is_running(&HostKernel, query_only.pid(), token),
Liveness::Unknown,
"running, unprovable"
);
query_only.terminate();
assert_eq!(
is_running(&HostKernel, query_only.pid(), token),
liveness_of(look_at(query_only.pid()))
);
assert_eq!(
is_running(&HostKernel, query_only.pid(), token),
Liveness::Dead,
"an exit code proves it"
);
let unkillable = crate::identity::windows_fixture::spawn_unkillable(&done);
let token = creation_token(unkillable.handle()).expect("the owned handle reads the token");
assert_eq!(
is_running(&HostKernel, unkillable.pid(), token),
Liveness::Unknown,
"QUERY refused: no look"
);
}
#[skuld::test]
fn wait_on_a_stale_identity_over_a_readable_pid_reports_exited() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(wait_on_a_stale_identity_over_a_readable_pid_reports_exited),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let real =
crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, child.handle(), child.pid())
.expect("the owned handle always yields an identity");
let stale = real.with_other_value();
assert!(child.is_running(), "precondition: the pid's occupant is live");
assert!(exited_now(stale).expect("a readable stranger on the pid proves the stale identity is gone"));
}
#[skuld::test]
fn wait_on_a_stale_identity_over_an_unreadable_pid_is_an_error() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(wait_on_a_stale_identity_over_an_unreadable_pid_is_an_error),
crate::test_spawn::spawn,
) else {
return;
};
let child = crate::identity::windows_fixture::spawn_unkillable(&done);
let real =
crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, child.handle(), child.pid())
.expect("the owned handle always yields an identity");
let stale = real.with_other_value();
assert!(
child.is_running(),
"precondition: the pid's occupant is live and unreadable"
);
exited_now(stale).expect_err("an unreadable occupant cannot prove the original identity exited");
}
fn spawn_blocked_child() -> std::process::Child {
crate::test_spawn::spawn(
std::process::Command::new("cmd")
.args(["/C", "more"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null()),
)
.expect("spawn cmd /C more")
}
fn handle_of(child: &std::process::Child) -> windows::Win32::Foundation::HANDLE {
use std::os::windows::io::AsRawHandle;
windows::Win32::Foundation::HANDLE(child.as_raw_handle())
}
fn end(mut child: std::process::Child) {
child.kill().expect("end the fixture");
child.wait().expect("reap the fixture");
}
struct Limited(
windows::Win32::Foundation::HANDLE,
windows::Win32::System::Threading::PROCESS_ACCESS_RIGHTS,
);
impl Limited {
fn of(child: &std::process::Child, rights: windows::Win32::System::Threading::PROCESS_ACCESS_RIGHTS) -> Limited {
let h = unsafe { windows::Win32::System::Threading::OpenProcess(rights, false, child.id()) }
.expect("open a limited handle on the fixture");
Limited(h, rights)
}
fn terminate(&self) -> super::Termination {
super::terminate_held(&HostKernel, self.0, self.1)
}
}
impl Drop for Limited {
fn drop(&mut self) {
super::close(self.0);
}
}
#[skuld::test]
fn a_refusal_on_a_handle_holding_terminate_is_the_exit() {
let child = spawn_blocked_child();
let kernel = refusing_the_next_terminate();
let outcome = super::terminate_held(&kernel, handle_of(&child), CREATE_PROCESS_ACCESS);
end(child);
assert!(
kernel.all_rows_taken(),
"the kill reached TerminateProcess to be refused"
);
assert!(
matches!(outcome, super::Termination::Exited),
"got a failure or a terminate"
);
}
#[skuld::test]
fn a_real_refusal_on_a_handle_holding_terminate_of_an_exited_process_is_the_exit() {
let mut child = spawn_blocked_child();
child.kill().expect("end the fixture");
child.wait().expect("reap the fixture");
let outcome = super::terminate_held(&HostKernel, handle_of(&child), CREATE_PROCESS_ACCESS);
assert!(
matches!(outcome, super::Termination::Exited),
"got a failure or a terminate"
);
}
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_is_failed_while_the_process_runs() {
let child = spawn_blocked_child();
let limited = Limited::of(&child, PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
let outcome = limited.terminate();
drop(limited);
end(child);
let super::Termination::Failed(e) = outcome else {
panic!("a live process is not an exit");
};
assert!(super::is_access_denied(&e), "{e:?}");
}
#[skuld::test]
fn an_exiting_refusal_on_a_handle_without_terminate_is_failed_while_unsignalled() {
let child = spawn_blocked_child();
let limited = Limited::of(&child, PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
let kernel = refusing_the_next_terminate();
let outcome = super::terminate_held(&kernel, limited.0, limited.1);
drop(limited);
end(child);
assert!(
kernel.all_rows_taken(),
"the kill reached TerminateProcess to be refused"
);
let super::Termination::Failed(e) = outcome else {
panic!("an unsignalled process is not shown to have exited");
};
assert!(super::is_access_denied(&e), "{e:?}");
}
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_or_synchronize_is_failed() {
let child = spawn_blocked_child();
let limited = Limited::of(&child, PROCESS_QUERY_LIMITED_INFORMATION);
let outcome = limited.terminate();
drop(limited);
end(child);
let super::Termination::Failed(e) = outcome else {
panic!("a handle that cannot be waited on shows no exit");
};
assert!(super::is_access_denied(&e), "{e:?}");
}
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_is_the_exit_once_the_process_is_signalled() {
let mut child = spawn_blocked_child();
let limited = Limited::of(&child, PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION);
child.kill().expect("end the fixture");
child.wait().expect("reap the fixture");
let outcome = limited.terminate();
drop(limited);
assert!(
matches!(outcome, super::Termination::Exited),
"a signalled process is the exit"
);
}
#[skuld::test]
fn a_refusal_on_a_handle_without_terminate_or_synchronize_is_the_exit_once_the_code_is_recorded() {
let mut child = spawn_blocked_child();
let limited = Limited::of(&child, PROCESS_QUERY_LIMITED_INFORMATION);
child.kill().expect("end the fixture");
child.wait().expect("reap the fixture");
let outcome = limited.terminate();
drop(limited);
assert!(
matches!(outcome, super::Termination::Exited),
"an exit code other than STILL_ACTIVE is the exit"
);
}
fn order_key_of(id: crate::identity::ProcessId) -> crate::identity::OrderKey {
match id.order_key().0 {
Resolved::Found(key) => key,
other => panic!("the order key of {id:?} should read, got {other:?}"),
}
}
#[skuld::test]
fn the_order_key_is_not_the_creation_time() {
let me = crate::identity::ProcessId::current();
let (read, _) =
crate::identity::ProcessId::of_ordered_with(&crate::identity::HostKernel, me.pid(), None, None, None);
let Resolved::Found((id, key)) = read else {
panic!("our own order key must read, got {read:?}");
};
assert_eq!(id, me, "the id of the ordered read is the id of the by-pid read");
assert_ne!(key, Some(crate::identity::OrderKey::for_test(me.key().raw())));
assert_eq!(key, Some(order_key_of(me)), "both reads of one process agree");
}
#[skuld::test]
fn a_child_orders_after_its_creator_and_a_grandchild_after_the_child() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(a_child_orders_after_its_creator_and_a_grandchild_after_the_child),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted_shell(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let grandchild = child.wait_for_child();
let me = crate::identity::ProcessId::current();
let child_id = crate::identity::ProcessId::of(child.pid())
.found()
.expect("the child is ours");
let grandchild_id = crate::identity::ProcessId::of(grandchild)
.found()
.expect("the grandchild is ours");
let keys = (order_key_of(me), order_key_of(child_id), order_key_of(grandchild_id));
assert!(keys.0 < keys.1, "child {:?} after creator {:?}", keys.1, keys.0);
assert!(keys.1 < keys.2, "grandchild {:?} after child {:?}", keys.2, keys.1);
}
#[skuld::test]
fn the_order_key_of_a_stale_id_is_gone() {
let stale = crate::identity::ProcessId::current().with_other_value();
assert_eq!(stale.order_key().0, Resolved::Gone);
}
#[skuld::test]
fn the_walk_keeps_a_real_child_and_grandchild_of_a_real_spawned_root() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(the_walk_keeps_a_real_child_and_grandchild_of_a_real_spawned_root),
crate::test_spawn::spawn,
) else {
return;
};
let child = spawn_restricted_shell(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let grandchild = child.wait_for_child();
let root = crate::identity::ProcessId::of(child.pid())
.found()
.expect("the root is ours");
let direct = root.children(crate::Recursive::No);
let below_us = crate::identity::ProcessId::current().children(crate::Recursive::Yes);
let direct = direct.expect("the root's children are enumerable");
assert!(
direct.iter().any(|id| id.pid() == grandchild),
"{grandchild} not among {direct:?}"
);
let below_us = below_us.expect("our descendants are enumerable");
for pid in [root.pid(), grandchild] {
assert!(
below_us.iter().any(|id| id.pid() == pid),
"{pid} not among {below_us:?}"
);
}
}
const REFUSED: windows::Win32::Foundation::NTSTATUS = windows::Win32::Foundation::NTSTATUS(0xC000_0004_u32 as i32);
const ACCESS_DENIED: windows::Win32::Foundation::NTSTATUS =
windows::Win32::Foundation::NTSTATUS(0xC000_0022_u32 as i32);
fn refused_key() -> windows::core::Result<Key> {
Err(windows::core::Error::from_hresult(windows::core::HRESULT::from_win32(
windows::Win32::Foundation::ERROR_ACCESS_DENIED.0,
)))
}
fn cause_text(cause: crate::identity::UnknownCause) -> Option<String> {
cause.into_error("pid 7").map(|e| e.to_string())
}
const REFUSED_CODE: i32 = windows::Win32::Foundation::ERROR_BAD_LENGTH.0 as i32;
fn cause_code(cause: crate::identity::UnknownCause) -> Option<i32> {
cause.into_error("pid 7").and_then(|e| crate::error::first_os_code(&e))
}
#[skuld::test]
fn two_good_reads_are_the_key_and_the_sequence_number() {
let key = Key::for_test(5);
for expected in [None, Some(key)] {
let (read, cause) = super::combine(7, expected, Ok(key), Ok(9));
assert_eq!(read, Resolved::Found((key, crate::identity::OrderKey::for_test(9))));
assert_eq!(cause_text(cause), None);
}
}
#[skuld::test]
fn a_refused_sequence_number_on_the_expected_process_is_unknown_with_its_status() {
let key = Key::for_test(5);
for expected in [None, Some(key)] {
let (read, cause) = super::combine(7, expected, Ok(key), Err(REFUSED));
assert_eq!(read, Resolved::Unknown);
let error = cause.into_error("pid 7").expect("the refusal is the cause");
let text = error.to_string();
assert!(
text.contains("pid 7") && text.contains("process sequence number query failed"),
"{text}"
);
assert_eq!(
crate::error::first_os_code(&error),
Some(REFUSED_CODE),
"the status is the source"
);
}
}
#[skuld::test]
fn another_processs_key_is_gone_whether_or_not_its_sequence_number_reads() {
let (read, cause) = super::combine(7, Some(Key::for_test(5)), Ok(Key::for_test(6)), Err(REFUSED));
assert_eq!(read, Resolved::Gone);
assert_eq!(cause_text(cause), None);
let (read, _) = super::combine(7, Some(Key::for_test(5)), Ok(Key::for_test(6)), Ok(9));
assert_eq!(read, Resolved::Gone);
}
#[skuld::test]
fn two_failed_reads_keep_both_failures() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
let (read, cause) = super::combine(70_007, Some(Key::for_test(5)), refused_key(), Err(REFUSED));
assert_eq!(read, Resolved::Unknown);
assert!(
cause_code(cause) == Some(REFUSED_CODE),
"the sequence-number refusal is the cause"
);
assert!(
crate::log_capture::contains_since(mark, "GetProcessTimes failed on an opened handle for pid 70007"),
"the key's failure is logged"
);
let (read, cause) = super::combine(70_008, None, refused_key(), Ok(9));
assert_eq!(read, Resolved::Unknown);
assert_eq!(cause_text(cause), None, "a key refusal alone has no status to name");
}
fn assert_reads_on_every_process_that_opens<T>(
what: &str,
read: fn(windows::Win32::Foundation::HANDLE) -> Result<T, windows::Win32::Foundation::NTSTATUS>,
) {
let parents =
crate::containment::enumerate::process_parents(&crate::identity::HostKernel).expect("the process snapshot");
let (mut opened, mut refused, mut gone) = (0usize, 0usize, 0usize);
let mut failures = Vec::new();
for (pid, _) in parents {
match super::open_classified(pid, PROCESS_QUERY_LIMITED_INFORMATION) {
Opened::Found(handle) => {
opened += 1;
if let Err(status) = read(handle) {
failures.push(format!("pid {pid}: NTSTATUS {:#010x}", status.0 as u32));
}
super::close(handle);
}
Opened::Denied(_) => refused += 1,
Opened::Gone => gone += 1,
}
}
eprintln!("{what} read on {opened} processes; {refused} refused to open, {gone} gone");
assert!(opened > 0, "no process could be opened: {refused} refused, {gone} gone");
assert!(
failures.is_empty(),
"{what} failed on {} of {opened} opened processes ({refused} refused to open): {failures:?}",
failures.len()
);
}
#[skuld::test]
fn the_sequence_number_reads_on_every_process_that_opens() {
assert_reads_on_every_process_that_opens("class 92", super::sequence_number);
}
#[skuld::test]
fn the_creator_pid_reads_on_every_process_that_opens() {
assert_reads_on_every_process_that_opens("class 0", super::creator_pid);
}
#[skuld::test]
fn the_creator_pid_survives_the_creators_exit() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(the_creator_pid_survives_the_creators_exit),
crate::test_spawn::spawn,
) else {
return;
};
let shell = spawn_restricted_shell(&done, PROCESS_QUERY_LIMITED_INFORMATION.0);
let grandchild = shell.wait_for_child();
let Opened::Found(handle) = super::open_classified(grandchild, PROCESS_QUERY_LIMITED_INFORMATION) else {
panic!("the grandchild is ours and openable");
};
let before = super::creator_pid(handle);
shell.terminate();
let after = super::creator_pid(handle);
super::close(handle);
assert_eq!(before, Ok(shell.pid() as usize));
assert_eq!(after, Ok(shell.pid() as usize));
}
#[skuld::test]
fn a_candidate_is_unknown_when_its_parent_cannot_be_opened_to_prove_it() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(a_candidate_is_unknown_when_its_parent_cannot_be_opened_to_prove_it),
crate::test_spawn::spawn,
) else {
return;
};
let shell = spawn_restricted_shell(&done, PROCESS_SYNCHRONIZE.0);
let candidate = shell.wait_for_child();
assert!(shell.is_running(), "precondition: the parent must be live");
let parent = crate::identity::ProcessId::from_parts_for_test(shell.pid(), Key::for_test(1));
let (read, cause) =
crate::identity::ProcessId::of_ordered_with(&crate::identity::HostKernel, candidate, Some(parent), None, None);
assert_eq!(read, Resolved::Unknown);
assert_eq!(cause.blamed_parent(), Some(shell.pid()));
}
#[skuld::test]
fn a_refused_creator_query_puts_its_status_in_the_cause() {
use super::{creator_read, ParentRead};
use crate::identity::UnknownCause;
let (parent, cause) = creator_read(7, Err(REFUSED), UnknownCause::default());
assert_eq!(parent, ParentRead::Unreadable);
assert_eq!(cause.creator_status, Some(REFUSED));
let (parent, cause) = creator_read(7, Ok(5), UnknownCause::default());
assert_eq!(parent, ParentRead::Is(5));
assert_eq!(cause.creator_status, None);
}
#[skuld::test]
fn a_refused_creator_query_does_not_displace_a_refused_sequence_number_in_the_error() {
use super::{creator_read, ParentRead};
use crate::identity::UnknownCause;
let (parent, cause) = creator_read(7, Err(ACCESS_DENIED), UnknownCause::of_sequence_status(REFUSED));
assert_eq!(parent, ParentRead::Unreadable);
let error = cause.into_error("pid 7").expect("a refusal is the cause");
let text = error.to_string();
assert!(
text.contains("process sequence number query failed") && !text.contains("creator"),
"{text}"
);
assert_eq!(
crate::error::first_os_code(&error),
Some(REFUSED_CODE),
"the sequence status, not the creator's"
);
}
#[skuld::test]
fn a_held_handle_whose_creation_time_is_refused_reads_unknown_with_the_os_error() {
use std::os::windows::io::{BorrowedHandle, RawHandle};
use windows::core::HRESULT;
use windows::Win32::System::Threading::GetCurrentProcess;
use crate::identity::{FakeKernel, ProcessId};
let kernel = FakeKernel::new().on_creation_token(Err(HRESULT::from_win32(ERROR_ACCESS_DENIED.0)));
let own = unsafe { BorrowedHandle::borrow_raw(GetCurrentProcess().0 as RawHandle) };
let (read, cause) = ProcessId::of_held_with(&kernel, std::process::id(), own);
assert!(matches!(read, Resolved::Unknown), "{read:?}");
let error = crate::child::spawn::spawn_identity_error(read, cause);
assert_eq!(
crate::error::first_os_code(&error),
Some(ERROR_ACCESS_DENIED.0 as i32),
"the refusal is the error's source: {error:?}"
);
assert!(kernel.all_rows_taken());
}