cosca 0.8.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
use super::{parse_pgrp, parse_ppid, parse_starttime_jiffies, parse_state, running_from_stat, stat_fixture};
use crate::identity::Liveness;

fn tok(v: u64) -> u64 {
    v
}

// After the comm field's LAST ')', the whitespace-split tail starts at field 3
// (state, index 0); starttime is field 22 (index 19).
#[skuld::test]
fn parses_simple_stat() {
    let stat = b"1234 (my proc) S 1 1234 1234 0 -1 4194560 100 0 0 0 1 2 0 0 20 0 1 0 8675309 0 0";
    assert_eq!(parse_starttime_jiffies(stat), Some(8675309));
    assert_eq!(parse_state(stat), Some(b'S'));
}

#[skuld::test]
fn handles_comm_containing_parens_and_spaces() {
    // comm = "a) b" — embedded ')' and space; rposition(')') must find the LAST one.
    let stat = b"7 (a) b) R 0 7 7 0 -1 0 0 0 0 0 0 0 0 0 20 0 1 0 55 0 0";
    assert_eq!(parse_starttime_jiffies(stat), Some(55));
    assert_eq!(parse_state(stat), Some(b'R'));
}

// ppid is field 4 (index 1 of the post-comm tail). Comm-safe via the LAST ')'.
#[skuld::test]
fn parses_ppid_simple() {
    let stat = b"1234 (my proc) S 1 1234 1234 0 -1 4194560 100 0 0 0 1 2 0 0 20 0 1 0 8675309 0 0";
    assert_eq!(parse_ppid(stat), Some(1));
}

#[skuld::test]
fn parses_ppid_with_comm_containing_parens_and_spaces() {
    // comm = "a) b" — embedded ')' and space; ppid (field 4) must still be 7.
    let stat = b"9 (a) b) R 7 9 9 0 -1 0 0 0 0 0 0 0 0 0 20 0 1 0 55 0 0";
    assert_eq!(parse_ppid(stat), Some(7));
}

#[skuld::test]
fn ppid_rejects_truncated_stat() {
    let stat = b"1 (init) S"; // no ppid token after the state
    assert_eq!(parse_ppid(stat), None);
}

/// Field 5 is the process-group id. Offsets are anchored on the last ')', so a
/// comm containing spaces and parens must not shift the answer.
#[skuld::test]
fn parse_pgrp_reads_field_five() {
    // pid 1234, comm "sh", state S, ppid 1, pgrp 4321, session 4321, ...
    let stat = b"1234 (sh) S 1 4321 4321 0 -1 4194304 100 0 0 0 1 2 3 4 20 0 1 0 987 0 0";
    assert_eq!(parse_pgrp(stat), Some(4321));
}

#[skuld::test]
fn parse_pgrp_survives_a_hostile_comm() {
    let stat = b"1234 (evil ) S 9 9 9) S 1 4321 4321 0 -1 4194304 100 0 0 0 1 2 3 4 20 0 1 0 987 0 0";
    assert_eq!(parse_pgrp(stat), Some(4321));
}

#[skuld::test]
fn parse_pgrp_rejects_a_truncated_record() {
    assert_eq!(parse_pgrp(b"1234 (sh) S 1"), None);
    assert_eq!(parse_pgrp(b"no parens here"), None);
}

#[skuld::test]
fn detects_zombie_state() {
    let stat = b"9 (gone) Z 1 9 9 0 -1 0 0 0 0 0 0 0 0 0 20 0 1 0 4242 0 0";
    assert_eq!(parse_state(stat), Some(b'Z'));
}

#[skuld::test]
fn rejects_truncated_stat() {
    let stat = b"1 (init) S 0 1 1"; // far fewer than 22 fields
    assert_eq!(parse_starttime_jiffies(stat), None);
    assert_eq!(parse_state(stat), Some(b'S')); // state still readable here
}

#[skuld::test]
fn parse_state_of_a_record_without_a_state_field_is_none() {
    assert_eq!(parse_state(b""), None);
    assert_eq!(parse_state(b"42 (comm)"), None);
}

#[skuld::test]
fn rejects_missing_close_paren() {
    let stat = b"1 (init S 0 1";
    assert_eq!(parse_starttime_jiffies(stat), None);
    assert_eq!(parse_state(stat), None);
}

// running_from_stat =====

#[skuld::test]
fn running_from_stat_running_state_matching_token() {
    let stat = stat_fixture("R", 8675309);
    assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Alive);
}

#[skuld::test]
fn running_from_stat_sleeping_state_matching_token() {
    let stat = stat_fixture("S", 8675309);
    assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Alive);
}

#[skuld::test]
fn running_from_stat_zombie_is_dead() {
    let stat = stat_fixture("Z", 8675309);
    assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Dead);
}

#[skuld::test]
fn running_from_stat_dead_uppercase_is_dead() {
    let stat = stat_fixture("X", 8675309);
    assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Dead);
}

#[skuld::test]
fn running_from_stat_dead_lowercase_is_dead() {
    let stat = stat_fixture("x", 8675309);
    assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Dead);
}

#[skuld::test]
fn running_from_stat_token_mismatch_is_dead() {
    let stat = stat_fixture("R", 8675309);
    assert_eq!(running_from_stat(&stat, tok(9999999)), Liveness::Dead);
}

#[skuld::test]
fn running_from_stat_unparseable_record_is_unknown() {
    // We read the file, so the process is there — we just could not read its state.
    // Distinct from a token mismatch, which positively identifies a stranger.
    assert_eq!(running_from_stat(b"not a stat line at all", tok(1)), Liveness::Unknown);
}