use super::descendants_with;
use crate::identity::{ProcessId, RawPid};
use std::collections::HashMap;
const ROOT_PID: RawPid = 100;
const ROOT_TOKEN: u64 = 1_000;
fn id(pid: RawPid, token: u64) -> ProcessId {
ProcessId::from_parts_for_test(pid, token)
}
fn resolver(tokens: &HashMap<RawPid, u64>) -> impl Fn(RawPid) -> Option<ProcessId> + '_ {
move |pid| tokens.get(&pid).map(|&t| id(pid, t))
}
fn pids(mut v: Vec<ProcessId>) -> Vec<RawPid> {
v.sort_by_key(|i| i.pid());
v.into_iter().map(|i| i.pid()).collect()
}
fn root() -> ProcessId {
id(ROOT_PID, ROOT_TOKEN)
}
#[skuld::test]
fn keeps_genuine_later_child_under_both_rules() {
let parents = [(200, ROOT_PID)];
let tokens = HashMap::from([(200u32, ROOT_TOKEN + 5)]);
for allow_equal in [true, false] {
let got = descendants_with(root(), &parents, allow_equal, resolver(&tokens));
assert_eq!(pids(got), [200], "later child kept (allow_equal={allow_equal})");
}
}
#[skuld::test]
fn same_tick_child_included_when_allow_equal_true() {
let parents = [(200, ROOT_PID)];
let tokens = HashMap::from([(200u32, ROOT_TOKEN)]); let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert_eq!(pids(got), [200], "same-tick child included with allow_equal=true");
}
#[skuld::test]
fn same_tick_child_excluded_when_allow_equal_false() {
let parents = [(200, ROOT_PID)];
let tokens = HashMap::from([(200u32, ROOT_TOKEN)]); let got = descendants_with(root(), &parents, false, resolver(&tokens));
assert!(got.is_empty(), "same-tick child excluded with allow_equal=false");
}
#[skuld::test]
fn stale_ppid_impostor_excluded_under_both_rules() {
let parents = [(200, ROOT_PID)];
let tokens = HashMap::from([(200u32, ROOT_TOKEN - 1)]);
for allow_equal in [true, false] {
let got = descendants_with(root(), &parents, allow_equal, resolver(&tokens));
assert!(got.is_empty(), "impostor excluded (allow_equal={allow_equal})");
}
}
#[skuld::test]
fn recurses_into_deep_tree() {
let parents = [(200, ROOT_PID), (300, 200), (400, 300)];
let tokens = HashMap::from([
(200u32, ROOT_TOKEN + 1),
(300u32, ROOT_TOKEN + 2),
(400u32, ROOT_TOKEN + 3),
]);
let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert_eq!(pids(got), [200, 300, 400]);
}
#[skuld::test]
fn impostor_prunes_its_whole_subtree() {
let parents = [(200, ROOT_PID), (300, 200)];
let tokens = HashMap::from([
(200u32, ROOT_TOKEN - 5), (300u32, ROOT_TOKEN + 5), ]);
let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert!(got.is_empty(), "subtree under a rejected impostor is pruned");
}
#[skuld::test]
fn reparented_orphan_is_missed() {
let parents = [(300, 1)];
let tokens = HashMap::from([(300u32, ROOT_TOKEN + 9)]);
let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert!(got.is_empty(), "reparented orphan is honestly missed (documented)");
}
#[skuld::test]
fn root_is_never_included() {
let parents = [(ROOT_PID, ROOT_PID), (200, ROOT_PID)];
let tokens = HashMap::from([(ROOT_PID, ROOT_TOKEN), (200u32, ROOT_TOKEN + 1)]);
let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert_eq!(pids(got), [200], "root excluded; genuine child kept");
}
#[skuld::test]
fn duplicate_edge_yields_pid_once() {
let parents = [(200, ROOT_PID), (200, ROOT_PID)];
let tokens = HashMap::from([(200u32, ROOT_TOKEN + 1)]);
let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert_eq!(pids(got), [200], "duplicate edge collapses to a single result");
}
#[skuld::test]
fn unresolvable_candidate_is_skipped() {
let parents = [(200, ROOT_PID), (300, 200)];
let tokens = HashMap::from([(300u32, ROOT_TOKEN + 5)]); let got = descendants_with(root(), &parents, true, resolver(&tokens));
assert!(got.is_empty(), "gone candidate and its subtree are skipped");
}
#[skuld::test]
fn children_of_keeps_one_level_genuine_children_only() {
use super::{children_of_with, ALLOW_EQUAL_TOKEN};
use crate::identity::ProcessId;
let root = ProcessId::from_parts_for_test(100, 50);
let parents = [(101, 100), (102, 101), (103, 100)];
let resolve = |pid: u32| match pid {
101 => Some(ProcessId::from_parts_for_test(101, 60)),
102 => Some(ProcessId::from_parts_for_test(102, 70)),
103 => Some(ProcessId::from_parts_for_test(103, 40)),
_ => None,
};
let pids: Vec<u32> = children_of_with(root, &parents, ALLOW_EQUAL_TOKEN, resolve)
.iter()
.map(|id| id.pid())
.collect();
assert_eq!(pids, vec![101]);
}
#[skuld::test]
fn children_of_same_tick_child_depends_on_allow_equal() {
use super::children_of_with;
let root = ProcessId::from_parts_for_test(100, ROOT_TOKEN);
let parents = [(101, 100)];
let resolve = |pid: u32| (pid == 101).then(|| ProcessId::from_parts_for_test(101, ROOT_TOKEN));
assert_eq!(
pids(children_of_with(root, &parents, true, resolve)),
[101],
"same-tick child included with allow_equal=true"
);
assert!(
children_of_with(root, &parents, false, resolve).is_empty(),
"same-tick child excluded with allow_equal=false"
);
}
#[skuld::test]
fn children_of_duplicate_edge_yields_pid_once() {
use super::children_of_with;
let root = ProcessId::from_parts_for_test(100, ROOT_TOKEN);
let parents = [(101, 100), (101, 100)];
let resolve = |pid: u32| (pid == 101).then(|| ProcessId::from_parts_for_test(101, ROOT_TOKEN + 1));
assert_eq!(
pids(children_of_with(root, &parents, true, resolve)),
[101],
"duplicate edge collapses to a single child"
);
}
#[cfg(windows)]
#[skuld::test]
fn resolve_or_drop_drops_an_access_denied_pid_and_warns() {
use windows::Win32::System::Threading::PROCESS_SYNCHRONIZE;
crate::log_capture::install();
let denied = crate::identity::windows_fixture::spawn_restricted(PROCESS_SYNCHRONIZE.0);
assert!(denied.is_running(), "precondition: the subject must be live");
let mark = crate::log_capture::mark();
assert!(
super::resolve_or_drop(denied.pid()).is_none(),
"the walk cannot keep what it cannot verify"
);
assert!(crate::log_capture::contains_since(
mark,
&format!("treewalk: pid {} could not be queried", denied.pid())
));
assert!(denied.is_running(), "and dropping it must not have touched it");
}
#[cfg(windows)]
#[skuld::test]
fn kill_by_identity_reports_not_attempted_when_the_open_is_denied() {
let child = crate::identity::windows_fixture::spawn_unkillable();
let id = crate::identity::windows_identity_from_handle(child.handle(), child.pid())
.expect("the owned handle always yields an identity");
assert!(child.is_running(), "precondition: the subject must be live");
assert_eq!(super::kill_by_identity(id), super::KillOutcome::NotAttempted);
assert!(child.is_running(), "a denied open must leave the process running");
}
#[cfg(windows)]
#[skuld::test]
fn kill_by_identity_spares_a_stale_identity_and_kills_a_matching_one() {
use windows::Win32::System::Threading::PROCESS_QUERY_LIMITED_INFORMATION;
let child = crate::identity::windows_fixture::spawn_restricted(PROCESS_QUERY_LIMITED_INFORMATION.0);
let real = crate::identity::windows_identity_from_handle(child.handle(), child.pid())
.expect("the owned handle always yields an identity");
let stale = crate::identity::ProcessId::from_parts_for_test(real.pid(), real.start_token_raw() ^ 1);
assert_eq!(
super::kill_by_identity(stale),
super::KillOutcome::AlreadyGone,
"a stale identity names a process that is gone - nothing to terminate"
);
assert!(
child.is_running(),
"a stale identity must never terminate the pid-s occupant"
);
assert_eq!(
super::kill_by_identity(real),
super::KillOutcome::Terminated,
"a matching identity must reach TerminateProcess - otherwise the wait below is unbounded"
);
child.await_exit();
assert!(!child.is_running(), "a matching identity must terminate the process");
}