use crate::identity::{HostKernel, Resolved};
use super::super::{join_edges, ppid_of, process_parents, push_denied_sample, DENIED_SAMPLE_CAP};
#[skuld::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(&HostKernel, &[0, -1, me]).expect("edge buffer");
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})"
);
}
#[skuld::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(&HostKernel, &unresolvable).expect("edge buffer");
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"
);
}
#[skuld::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"
);
}
#[skuld::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(&HostKernel).expect("the process snapshot");
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()
);
}
#[skuld::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(&HostKernel, 1),
Resolved::Found(0),
"pid 1 (launchd)'s parent is the kernel (ppid 0), resolvable via the sysctl fallback"
);
}
#[skuld::test]
fn ppid_of_reports_gone_for_an_unallocatable_pid() {
assert_eq!(
ppid_of(&HostKernel, libc::c_int::MAX),
Resolved::Gone,
"a pid beyond PID_MAX can never resolve to a ppid, and is not a denial"
);
}
#[skuld::test]
fn a_failed_pid_listing_is_unassessable_not_an_empty_snapshot() {
super::super::force_blind_snapshots(1);
match process_parents(&HostKernel) {
Err(crate::error::Error::Unassessable { detail, source }) => {
assert!(detail.contains("proc_listallpids"), "{detail}");
assert!(source.is_some());
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn a_denied_ppid_read_is_unassessable_naming_the_count_and_a_sample() {
let me = std::process::id() as libc::c_int;
let _forced = super::super::fault::force_denied(&[me]);
match process_parents(&HostKernel) {
Err(crate::error::Error::Unassessable { detail, source }) => {
assert!(detail.contains("1 of "), "{detail}");
assert!(detail.contains(&format!("sample: [{me}]")), "{detail}");
assert!(source.is_none());
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn snapshot_reports_a_denied_ppid_read_as_a_count_not_an_error() {
let me = std::process::id() as libc::c_int;
let _forced = super::super::fault::force_denied(&[me]);
let (pids, edges, denied) = super::super::snapshot();
assert_eq!(denied, 1);
assert!(pids.contains(&(me as u32)));
assert!(!edges.iter().any(|&(pid, _)| pid == me as u32));
}
#[skuld::test]
fn a_failed_edge_allocation_is_unassessable_not_an_empty_snapshot() {
let _forced = super::super::fault::force_join_alloc_failure();
match process_parents(&HostKernel) {
Err(crate::error::Error::Unassessable { detail, source }) => {
assert!(detail.contains("the process snapshot could not be taken"), "{detail}");
let cause = source.expect("the failed allocation is the source");
let said = crate::error::said(&*cause);
assert!(said.contains("an edge buffer could not be allocated"), "{said}");
}
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn snapshot_is_an_empty_blind_pass_when_the_edge_allocation_fails() {
let _forced = super::super::fault::force_join_alloc_failure();
assert_eq!(super::super::snapshot(), (Vec::new(), Vec::new(), 0));
}
#[skuld::test]
fn a_fork_in_progress_resolves_and_is_not_counted_as_denied() {
use crate::identity::{FakeKernel, Kernel};
let me = std::process::id() as libc::c_int;
let Ok(record) = HostKernel.bsd_info(me as u32) else {
panic!("this process has a record");
};
let with_parent = |ppid| {
let mut record = record;
record.pbi_ppid = ppid;
Ok(record)
};
let kernel = FakeKernel::new()
.on_bsd_info(me as u32, with_parent(0))
.on_bsd_info(me as u32, with_parent(0))
.on_bsd_info(me as u32, with_parent(1));
let (edges, denied, _) = join_edges(&kernel, &[me]).expect("edge buffer");
assert_eq!(denied, 0);
assert_eq!(edges, [(me as u32, 1)]);
}
#[skuld::test]
fn a_persistent_refusal_is_a_denial_after_one_read() {
use crate::identity::{FakeKernel, ReadErr};
let me = std::process::id() as libc::c_int;
let kernel = FakeKernel::over(&HostKernel)
.on_bsd_info(me as u32, Err(ReadErr::Refused(libc::EPERM)))
.on_kinfo_refused(me as u32, libc::EPERM);
match process_parents(&kernel) {
Err(crate::error::Error::Unassessable { detail, .. }) => {
assert!(detail.contains(&format!("sample: [{me}]")), "{detail}")
}
other => panic!("expected Unassessable, got {other:?}"),
}
}