cosca 0.3.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! The ppid join: `join_edges`, `process_parents`, and `ppid_of`'s failure branches.

use crate::identity::Resolved;

use super::super::{join_edges, ppid_of, process_parents, push_denied_sample, DENIED_SAMPLE_CAP};

/// The `pid <= 0` filter, pinned deterministically with a synthetic pid list rather than a
/// live one: depending on a live run to happen to contain pid 0 (a kernel-internal fact
/// this filter's correctness should not hinge on to be exercised) would let the test pass
/// vacuously on a host/kernel where it's absent. `getpid()` is a real, live pid guaranteed
/// to resolve (`proc_pidinfo` on SELF is always permitted), so it proves the non-positive
/// entries are what's filtered, not that every entry is.
#[test]
fn join_edges_filters_non_positive_pids() {
    let me = std::process::id() as libc::c_int;
    // Read before and after, same reasoning as `parents_contains_this_process_edge`:
    // nothing holds this process's real parent fixed across the call.
    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})"
    );
}

/// A `Gone` pid (one that can never resolve because it's beyond `PID_MAX`) is a legitimate
/// exclusion, not a denial: it must produce no edge and must NOT be counted in `denied`, no
/// matter how many are batched together.
#[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"
    );
}

/// The denied-sample cap, pinned directly against [`push_denied_sample`] rather than through
/// a live `join_edges` call: there is no deterministic way to force `Resolved::Unknown` from
/// a real `ppid_of` call (see `snapshot`'s doc), so this is the only way to exercise the cap
/// at all. Pushing `DENIED_SAMPLE_CAP + 2` entries must still keep exactly the first
/// `DENIED_SAMPLE_CAP` of them — capped, not merely bounded by coincidence, and not silently
/// replacing earlier entries with later ones.
#[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"
    );
}

/// The delivered `(pid, ppid)` snapshot carries this test process's own edge.
///
/// This is the only edge that can be asserted unconditionally: `proc_pidinfo` on SELF is
/// always permitted, so `ppid_of(getpid())` cannot be denied. A broader assertion — every
/// same-uid pid in `all_pids()` has an edge — would be flaky, because a same-uid process
/// that exits between the two calls is a legitimate ESRCH drop. The EPERM gap that bounds
/// this layer is pinned separately, in `ppid_of_resolves_a_different_users_process_via_the_sysctl_fallback`.
///
/// `parent_id()` is read both before and after `process_parents()`, and either is accepted:
/// nothing holds this process's real parent fixed across the call, so a single read compared
/// for exact equality would be a race against reparenting (rare, but a real TOCTOU, not a
/// hypothetical one) rather than a pin on the join.
#[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()
    );
}

/// The EPERM gap the module docs traced is now CLOSED for this case: pid 1 (launchd) is
/// guaranteed to exist and, outside a root process, guaranteed to be owned by a different
/// (root) user - a deterministic trigger for the sysctl fallback without spawning a
/// cross-uid process. Its real ppid is 0 (the kernel) on every macOS version - the one case
/// `identity::macos::trusted_ppid`'s "`e_ppid == 0` is never trusted" rule exempts by pid
/// rather than discards (see that function's doc).
///
/// The precondition assertion is load-bearing, not decoration: the whole point of this test
/// is that the FALLBACK resolves pid 1, but whether the fallback is even reached depends
/// entirely on the runner's privilege - root's `proc_pidinfo(1, ..)` succeeds outright, so a
/// privileged run (`sudo cargo test`, a root CI container) would satisfy `Found(0)` via the
/// PRIMARY path alone and never exercise the fallback at all, leaving this test green while
/// silently testing nothing. Asserting non-root up front makes that case fail loudly instead.
#[test]
fn ppid_of_resolves_a_different_users_process_via_the_sysctl_fallback() {
    // SAFETY: geteuid takes no arguments and cannot fail.
    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"
    );
}

/// The OTHER cause of `ppid_of`'s non-`Found` branch: `Gone`, a pid that does not exist.
/// Triggered deterministically: XNU caps real pids at `PID_MAX` (99999), so `libc::c_int::MAX`
/// can never be live.
#[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"
    );
}