use crate::containment::enumerate::backend_fault::force_snapshot_failure;
use crate::error::Error;
use crate::identity::ProcessId;
use crate::test_child::{live_findstr_blocker as live_member, open_live, release_findstr_unterminated as release};
use crate::Recursive;
fn assert_names_toolhelp(result: Result<(), Error>, what: &str) {
match result {
Err(Error::Unassessable { detail, .. }) => {
assert!(detail.contains("CreateToolhelp32Snapshot"), "{what}: {detail}")
}
other => panic!("{what}: expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn every_walk_over_a_failed_snapshot_errors_and_terminates_nothing() {
type Walk = fn(&crate::Process) -> Result<(), Error>;
let walks: [(&str, Walk); 2] = [
("hard_kill", |p| {
let hk = &crate::identity::HostKernel;
super::hard_kill(hk, p.id(), &super::ByIdentity::new(hk, p.id()))
.outcome()
.into_result()
.map_err(crate::containment::dispatch::Unkilled::into_error)
}),
("kill_tree", |p| p.kill_tree()),
];
for (name, walk) in walks {
let (child, id) = live_member();
let process = open_live(id);
let forced = force_snapshot_failure();
let result = walk(&process);
drop(forced);
assert_names_toolhelp(result, name);
release(child);
}
}
#[skuld::test]
fn children_over_a_failed_snapshot_are_unassessable() {
let me = ProcessId::current();
let forced = force_snapshot_failure();
let results = [
("children(No)", me.children(Recursive::No).map(|_| ())),
("children(Yes)", me.children(Recursive::Yes).map(|_| ())),
];
drop(forced);
for (name, result) in results {
assert_names_toolhelp(result, name);
}
}
#[skuld::test]
fn a_walk_from_a_root_that_lost_its_pid_terminates_nothing() {
let me = ProcessId::current();
let stale = me.with_other_value();
let (child, _) = live_member();
assert!(
matches!(
super::hard_kill(
&crate::identity::HostKernel,
stale,
&super::ByIdentity::new(&crate::identity::HostKernel, stale)
)
.outcome(),
crate::containment::dispatch::TreeKillOutcome::NoRoot
),
"a root that lost its pid is nothing to do"
);
release(child);
}
#[skuld::test]
fn a_failed_snapshot_is_unassessable_with_its_win32_code() {
let me = ProcessId::current();
let forced = force_snapshot_failure();
let result = me.children(Recursive::No);
drop(forced);
let Err(Error::Unassessable {
detail,
source: Some(source),
}) = result
else {
panic!("expected Unassessable with a source, got {result:?}");
};
let Error::Io(source) = &*source else {
panic!("expected an Io source, got {source:?}");
};
assert_eq!(
detail,
"the process snapshot could not be taken: CreateToolhelp32Snapshot failed"
);
assert_eq!(
source.raw_os_error(),
Some(windows::Win32::Foundation::ERROR_ACCESS_DENIED.0 as i32),
"{source:?}"
);
assert_eq!(source.kind(), std::io::ErrorKind::PermissionDenied, "{source:?}");
}