use crate::identity::Resolved;
use super::super::{join_edges, ppid_of, process_parents, push_denied_sample, DENIED_SAMPLE_CAP};
#[test]
fn join_edges_filters_non_positive_pids() {
let me = std::process::id() as libc::c_int;
let parent_before = std::os::unix::process::parent_id();
let (out, denied, sample) = join_edges(&[0, -1, me]);
let parent_after = std::os::unix::process::parent_id();
assert_eq!(denied, 0, "0 and -1 must not be counted as denied ppid lookups");
assert!(
sample.is_empty(),
"nothing was attempted-and-denied, so nothing should be sampled"
);
assert_eq!(out.len(), 1, "only the real pid should produce an edge");
let (pid, ppid) = out[0];
assert_eq!(pid, me as u32, "the filtered edge must be for the real pid");
assert!(
ppid == parent_before || ppid == parent_after,
"edge's ppid ({ppid}) matched neither parent read ({parent_before}, {parent_after})"
);
}
#[test]
fn join_edges_does_not_count_gone_pids_as_denied() {
let unresolvable = vec![libc::c_int::MAX; 8];
let (out, denied, sample) = join_edges(&unresolvable);
assert!(out.is_empty(), "none of these pids can resolve to an edge");
assert_eq!(denied, 0, "a pid that is simply gone must not be counted as denied");
assert!(
sample.is_empty(),
"a Gone pid must not be sampled either — only Unknown is"
);
}
#[test]
fn push_denied_sample_caps_at_the_limit() {
let mut sample = Vec::new();
for pid in 0..(DENIED_SAMPLE_CAP as libc::c_int + 2) {
push_denied_sample(&mut sample, pid);
}
assert_eq!(
sample.len(),
DENIED_SAMPLE_CAP,
"the sample must be capped, not grow with every push"
);
assert_eq!(
sample,
(0..DENIED_SAMPLE_CAP as libc::c_int).collect::<Vec<_>>(),
"a capped sample must keep the FIRST N pushes, not the last"
);
}
#[test]
fn parents_contains_this_process_edge() {
let me = std::process::id();
let parent_before = std::os::unix::process::parent_id();
let parents = process_parents();
let parent_after = std::os::unix::process::parent_id();
assert!(
parents.contains(&(me, parent_before)) || parents.contains(&(me, parent_after)),
"own edge missing from a {}-edge snapshot (parent read as {parent_before} before, \
{parent_after} after)",
parents.len()
);
}
#[test]
fn ppid_of_resolves_a_different_users_process_via_the_sysctl_fallback() {
assert_ne!(
unsafe { libc::geteuid() },
0,
"this test's claim only holds if proc_pidinfo(1, ..) is actually denied, forcing the \
sysctl fallback - re-run as a non-root user"
);
assert_eq!(
ppid_of(1),
Resolved::Found(0),
"pid 1 (launchd)'s parent is the kernel (ppid 0), resolvable via the sysctl fallback"
);
}
#[test]
fn ppid_of_reports_gone_for_an_unallocatable_pid() {
assert_eq!(
ppid_of(libc::c_int::MAX),
Resolved::Gone,
"a pid beyond PID_MAX can never resolve to a ppid, and is not a denial"
);
}