use super::super::snapshot;
#[test]
fn the_pid_list_is_at_least_as_large_as_the_kernels_own_sizing_return() {
let needed = unsafe { libc::proc_listallpids(std::ptr::null_mut(), 0) };
assert!(needed > 0, "the sizing call must report a positive count");
let (pids, _, _) = snapshot();
assert!(
pids.len() * 2 >= needed as usize,
"the pid list ({}) is far short of the kernel's own sizing return ({needed}) — \
consistent with a units mistake (bytes read as a pid count), which undercounts by \
roughly 4x",
pids.len()
);
}
#[test]
fn the_pid_list_contains_launchd_and_this_process() {
let (pids, _, _) = snapshot();
assert!(pids.contains(&1), "launchd must be in the pid list");
assert!(
pids.contains(&std::process::id()),
"the running test process must be in the pid list"
);
}
#[test]
fn every_still_live_pid_that_ps_reports_is_in_the_snapshot() {
let _serialize = crate::child::spawn::spawn_lock();
let out = std::process::Command::new("/bin/ps")
.args(["-Ao", "pid="])
.output()
.expect("run ps");
let ps_pids: Vec<u32> = String::from_utf8_lossy(&out.stdout)
.split_whitespace()
.filter_map(|s| s.parse().ok())
.collect();
assert!(!ps_pids.is_empty(), "ps reported no processes; the oracle is broken");
let (pids, _, _) = snapshot();
let set: std::collections::HashSet<u32> = pids.into_iter().collect();
for pid in ps_pids {
if set.contains(&pid) {
continue;
}
let gone = matches!(crate::identity::ProcessId::of(pid), crate::identity::Resolved::Gone);
assert!(
gone,
"pid {pid} is live and reported by ps but missing from the snapshot"
);
}
}
#[test]
fn every_ppid_edge_names_a_pid_from_the_same_snapshot() {
let (pids, parents, _) = snapshot();
let set: std::collections::HashSet<u32> = pids.into_iter().collect();
for (pid, _) in &parents {
assert!(
set.contains(pid),
"pid {pid} has a ppid edge but is not in the pid list"
);
}
}