cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! Tests for the pure parts of `exit_only`.

#[cfg(unix)]
mod unix {
    use std::os::unix::process::ExitStatusExt;
    use std::process::ExitStatus;

    use crate::os_process::child::ExitRecord;
    use crate::wait::exit_only::{exit_record_of, is_exit_record, Record};

    fn status(code: i32, si_status: i32) -> ExitRecord {
        exit_record_of(Record {
            si_code: code,
            si_status,
        })
    }

    /// `CLD_EXITED` gives the low byte as the code, dropping every other bit of `si_status`
    /// (XNU ORs `p_xhighbits` into its high byte, `kern_exit.c:3255`).
    ///
    /// Mutant: a mask-free mapping keeps the high bits in the raw status.
    #[skuld::test]
    fn an_exited_record_gives_the_low_byte_as_the_code() {
        assert_eq!(
            status(libc::CLD_EXITED, 0x0A00_0107),
            ExitRecord::Status(ExitStatus::from_raw(7 << 8))
        );
    }

    /// `CLD_KILLED` and `CLD_DUMPED` give the signal, and a core dump sets the core bit.
    ///
    /// Mutant: a mask-free mapping; `CLD_DUMPED` mapped like `CLD_KILLED`.
    #[skuld::test]
    fn killed_and_dumped_records_give_the_signal() {
        assert_eq!(
            status(libc::CLD_KILLED, 0x0A00_0009),
            ExitRecord::Status(ExitStatus::from_raw(9))
        );
        assert_eq!(
            status(libc::CLD_DUMPED, 0x0A00_000B),
            ExitRecord::Status(ExitStatus::from_raw(0x0B | 0x80))
        );
    }

    /// A ptrace stop is not an exit record, and the mapping is total: it becomes `Unreadable`,
    /// never a panic.
    ///
    /// Mutant: a trap read as an exit.
    #[skuld::test]
    fn a_trapped_record_is_not_an_exit_record() {
        for code in [libc::CLD_TRAPPED, libc::CLD_STOPPED, libc::CLD_CONTINUED, 0, 99] {
            assert!(!is_exit_record(code), "si_code {code}");
            assert_eq!(status(code, 5), ExitRecord::Unreadable { si_code: code });
        }
        for code in [libc::CLD_EXITED, libc::CLD_KILLED, libc::CLD_DUMPED] {
            assert!(is_exit_record(code), "si_code {code}");
        }
    }
}

#[cfg(target_os = "linux")]
mod linux {
    use std::os::fd::AsFd as _;
    use std::os::unix::process::ExitStatusExt;
    use std::process::ExitStatus;

    use rustix::process::{pidfd_open, Pid, PidfdFlags};

    use crate::identity::{FakeKernel, HostKernel, Record};
    use crate::os_process::child::ExitRecord;
    use crate::wait::exit_only::{try_reap, Reap, Target};

    /// A reap consumes through the kernel that peeked, so a peek and its consume answer from one
    /// authority: a stated exit, peeked and consumed, is reaped as stated, and the real child, which
    /// no `waitid` was made for, is left running.
    ///
    /// Mutant: `try_reap` consumes with a `waitid` of its own, not through the target's kernel (it
    /// finds the running child unexited, and says `Running`).
    #[skuld::test]
    fn a_reap_consumes_through_the_kernel_that_peeked() {
        let child = crate::test_child::blocker();
        let pid = child.id();
        let pidfd = pidfd_open(Pid::from_raw(pid as i32).expect("a positive pid"), PidfdFlags::empty())
            .expect("the child's pidfd opens");
        let exited = || {
            Ok(Some(Record {
                si_code: libc::CLD_EXITED,
                si_status: 7,
            }))
        };
        let kernel = FakeKernel::over(&HostKernel)
            .on_peek(pid, exited())
            .on_consume(pid, exited());

        let reaped = try_reap(&Target::pidfd(&kernel, pidfd.as_fd())).expect("the reap answers");

        assert_eq!(reaped, Reap::Reaped(ExitRecord::Status(ExitStatus::from_raw(7 << 8))));
        assert!(
            kernel.all_rows_taken(),
            "the look was the stated peek and the reap the stated consume"
        );
    }
}

#[cfg(windows)]
mod windows {
    use std::os::windows::io::AsHandle as _;

    use crate::wait::exit_only::{peek, Target};

    /// A wait on our own process handle that fails is a contract breach, not an answer: debug
    /// builds assert, release builds return the OS error.
    ///
    /// Mutant: the `debug_assert!` removed from the failed arm of `peek`.
    #[skuld::test]
    fn a_failed_wait_on_our_own_handle_is_a_contract_breach() {
        let unwaitable = crate::test_child::unwaitable::unwaitable_handle();
        let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            peek(&Target::Handle(unwaitable.as_handle()))
        }));
        if cfg!(debug_assertions) {
            assert!(outcome.is_err(), "a debug build asserts the contract");
        } else {
            outcome.expect("release returns").expect_err("a breach is an error");
        }
    }
}