cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! A tree walk over a failed macOS process listing errors and signals nothing. macOS only: the
//! seam is `enumerate::backend_fault`.

use crate::containment::enumerate::backend_fault::{force_blind_snapshot, force_denied, force_join_alloc_failure};
use crate::error::Error;
use crate::identity::ProcessId;
use crate::test_child::{live_exiting_member as live_member, open_live, release_unsignalled as release};
use crate::Recursive;

fn assert_unassessable_naming(result: Result<(), Error>, cause: &str) {
    match result {
        Err(Error::Unassessable { detail, .. }) => {
            assert!(detail.contains(cause), "the error must name {cause:?}: {detail}")
        }
        other => panic!("expected Unassessable naming {cause:?}, got {other:?}"),
    }
}

type Walk = fn(&crate::Process) -> Result<(), Error>;

fn walks() -> [(&'static str, Walk); 4] {
    [
        ("hard_kill", |p| {
            super::hard_kill(&crate::identity::HostKernel, p.id())
                .into_result()
                .map_err(crate::containment::dispatch::Unkilled::into_error)
        }),
        ("terminate", |p| super::terminate(&crate::identity::HostKernel, p.id())),
        ("kill_tree", |p| p.kill_tree()),
        ("terminate_tree", |p| p.terminate_tree()),
    ]
}

type Query = fn(&ProcessId) -> Result<(), Error>;

/// The queries that list the table. `parent` does not: it reads `self` alone.
fn queries() -> [(&'static str, Query); 2] {
    [
        ("children(No)", |p| p.children(Recursive::No).map(|_| ())),
        ("children(Yes)", |p| p.children(Recursive::Yes).map(|_| ())),
    ]
}

/// Mutants: "the walk reads an empty snapshot for a failed listing" (`Ok`, nothing found); "signal
/// the root, then return the error".
#[skuld::test]
fn every_walk_over_a_failed_listing_errors_and_signals_nothing() {
    for (name, walk) in walks() {
        let (child, id) = live_member();
        let process = open_live(id);
        let forced = force_blind_snapshot();
        let result = walk(&process);
        drop(forced);
        assert!(
            matches!(&result, Err(Error::Unassessable { detail, .. }) if detail.contains("proc_listallpids")),
            "{name}: {result:?}"
        );
        release(child);
    }
}

/// Mutant: "`children` maps the snapshot's error to `Ok(vec![])`".
#[skuld::test]
fn children_over_a_failed_listing_are_unassessable() {
    let me = ProcessId::current();
    for (name, query) in queries() {
        let forced = force_blind_snapshot();
        let result = query(&me);
        drop(forced);
        assert!(
            matches!(&result, Err(Error::Unassessable { detail, .. }) if detail.contains("proc_listallpids")),
            "{name}: {result:?}"
        );
    }
}

/// A pid denied its ppid read leaves its subtree out of any walk, so the snapshot is
/// `Unassessable` naming the count and the pid, and no walk signals. Mutant: "`denied > 0` still
/// returns `Ok`".
#[skuld::test]
fn every_walk_over_a_snapshot_with_a_denied_ppid_errors_and_signals_nothing() {
    for (name, walk) in walks() {
        let (child, id) = live_member();
        let process = open_live(id);
        let forced = force_denied(&[id.pid() as libc::c_int]);
        let result = walk(&process);
        drop(forced);
        assert!(
            matches!(&result, Err(Error::Unassessable { detail, .. }) if detail.contains(&format!("sample: [{}]", id.pid()))),
            "{name}: {result:?}"
        );
        release(child);
    }
}

/// Mutant: "`children` ignores a denied ppid elsewhere in the table".
#[skuld::test]
fn children_over_a_snapshot_with_a_denied_ppid_are_unassessable() {
    let (child, id) = live_member();
    let me = ProcessId::current();
    for (_, query) in queries() {
        let forced = force_denied(&[id.pid() as libc::c_int]);
        let result = query(&me);
        drop(forced);
        assert_unassessable_naming(result, &format!("sample: [{}]", id.pid()));
    }
    release(child);
}

/// An allocation failure joining the edges is `Unassessable` too, not an empty tree. Mutant:
/// "`join_edges`' failure is an empty snapshot".
#[skuld::test]
fn every_walk_over_a_failed_edge_allocation_errors_and_signals_nothing() {
    for (name, walk) in walks() {
        let (child, id) = live_member();
        let process = open_live(id);
        let forced = force_join_alloc_failure();
        let result = walk(&process);
        drop(forced);
        assert!(
            matches!(&result, Err(Error::Unassessable { detail, .. }) if detail.contains("edge buffer")),
            "{name}: {result:?}"
        );
        release(child);
    }
}

/// A root that no longer holds its pid has no descendants: its pid's new owner's children are not
/// its own, and are never signalled. `terminate` is `Ok`; `hard_kill` killed nothing, and is
/// `Unassessable`. Mutants: "walk without an anchor"; "a kill that found no root is `Ok`". A stale
/// identity cannot be opened, so only the identity-level walks run.
#[skuld::test]
fn every_walk_from_a_root_that_lost_its_pid_signals_nothing_and_a_kill_is_unassessable() {
    let me = ProcessId::current();
    let stale = me.with_other_value();

    let (child, _) = live_member();
    let result = super::hard_kill(&crate::identity::HostKernel, stale)
        .into_result()
        .map_err(crate::containment::dispatch::Unkilled::into_error);
    assert_unassessable_naming(result, "no walk ran");
    release(child);

    let (child, _) = live_member();
    let result = super::terminate(&crate::identity::HostKernel, stale);
    assert!(result.is_ok(), "terminate: {result:?}");
    release(child);
}