cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! What a started program's end looks like to cosca.

use super::*;
use crate::elevation::shim::hooks::Inject;
use crate::elevation::shim::link::KillOutcome;
use crate::elevation::shim::protocol::{Errno, NotExecuted, Signal};

#[skuld::test]
fn real_link_and_real_shim_with_no_front() {
    let rig = ShimRig::new();
    let mut run = rig.spawn(Spec::sh("exit 42"));
    run.wait_for("first byte: A");
    assert_eq!(rig.link.link.wait().unwrap(), LinkOutcome::Exited(42 << 8));
    let done = run.finish();
    assert_eq!(done.code, Some(42), "{}", done.stderr);
}

fn started(rig: &ShimRig, spec: Spec) -> rig::Run {
    let mut run = rig.spawn(spec);
    run.wait_for("first byte: A");
    run
}

fn not_executed(cause: NotExecuted) -> LinkOutcome {
    LinkOutcome::NotStarted(NotStarted {
        shim_connected: true,
        cause: NotStartedCause::NotExecuted(cause),
    })
}

#[skuld::test]
fn fork_pipe_and_exec_failures_are_distinct_causes() {
    let cases = [
        (
            Spec::sh("true").inject(Inject::ForkFails),
            NotExecuted::ForkFailed(Errno(libc::EAGAIN)),
        ),
        (
            Spec::sh("true").inject(Inject::PipeFails),
            NotExecuted::SetupFailed(Errno(libc::EMFILE)),
        ),
        (
            Spec::new("/nonexistent/tool", &[]),
            NotExecuted::ExecFailed(Errno(libc::ENOENT)),
        ),
    ];
    for (spec, cause) in cases {
        let rig = ShimRig::new();
        let run = started(&rig, spec);
        assert_eq!(rig.link.link.wait().unwrap(), not_executed(cause));
        run.finish();
    }
}

/// A child killed in its steps writes no failure record, but its channel ends with no ready record,
/// which proves that no `execve` ran: the program was not started, however the child died.
///
/// Mutant: an end with no ready record is read as the `exec`, and the death is the program's status.
#[skuld::test]
fn sigkill_before_exec_is_not_started_though_the_child_wrote_no_record() {
    let rig = ShimRig::new();
    let mut run = started(&rig, Spec::sh("exit 0").child_gate());
    run.wait_for("child: waiting at gate");
    assert_eq!(rig.link.link.kill().unwrap(), KillOutcome::Delivered);
    run.wait_for("control: signal Kill");
    assert_eq!(
        rig.link.link.wait().unwrap(),
        not_executed(NotExecuted::TerminatedBeforeExec(Signal(libc::SIGKILL)))
    );
    assert_eq!(run.finish().code, Some(117));
}