use super::{
exiting_from_stat, parse_flags, parse_num_threads, parse_pgrp, parse_ppid, parse_starttime_jiffies, parse_state,
running_from_stat, stat_fixture, PF_EXITING,
};
use crate::identity::Liveness;
fn tok(v: u64) -> u64 {
v
}
#[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() {
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'));
}
#[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() {
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"; assert_eq!(parse_ppid(stat), None);
}
#[skuld::test]
fn parse_pgrp_reads_field_five() {
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 parse_flags_reads_field_nine() {
let before = b"1234 (sh) S 1 4321 4321 0 -1 4194624 100 0 0 0 1 2 3 4 20 0 1 0 987 0 0";
let held = b"1234 (sh) S 1 4321 4321 0 -1 4194636 100 0 0 0 1 2 3 4 20 0 1 0 987 0 0";
assert_eq!(parse_flags(before), Some(0x40_0140));
assert_eq!(parse_flags(held), Some(0x40_014c));
}
#[skuld::test]
fn parse_flags_survives_a_hostile_comm() {
let stat = b"1234 (evil ) S 9 9 9 4) S 1 4321 4321 0 -1 4194636 100 0 0 0 1 2 3 4 20 0 1 0 987 0 0";
assert_eq!(parse_flags(stat), Some(0x40_014c));
}
#[skuld::test]
fn parse_flags_rejects_a_record_without_a_flags_field() {
assert_eq!(parse_flags(b"1234 (sh) S 1 4321 4321 0 -1"), None);
assert_eq!(parse_flags(b"1234 (sh) S 1 4321 4321 0 -1 many"), None);
assert_eq!(parse_flags(b"no parens here"), None);
}
#[skuld::test]
fn exiting_from_stat_tests_the_exit_bit() {
let with =
|flags: u32| format!("1234 (sh) S 1 4321 4321 0 -1 {flags} 100 0 0 0 1 2 3 4 20 0 1 0 987 0 0").into_bytes();
assert_eq!(PF_EXITING, 0x4);
assert_eq!(exiting_from_stat(&with(0x40_0140)), Some(false));
assert_eq!(exiting_from_stat(&with(0x40_014c)), Some(true));
assert_eq!(exiting_from_stat(&with(0x40_814c)), Some(true));
assert_eq!(
exiting_from_stat(&with(0x8)),
Some(false),
"0x8 alone is not the exit flag"
);
assert_eq!(exiting_from_stat(b"1234 (sh) S 1"), 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"; assert_eq!(parse_starttime_jiffies(stat), None);
assert_eq!(parse_state(stat), Some(b'S')); }
#[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);
}
#[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);
}
fn zombie_with_threads(threads: u64) -> Vec<u8> {
let stat = String::from_utf8(stat_fixture("Z", 8675309)).expect("a utf-8 fixture");
stat.replacen(" 20 0 1 0 ", &format!(" 20 0 {threads} 0 "), 1)
.into_bytes()
}
#[skuld::test]
fn running_from_stat_zombie_leader_with_a_live_thread_is_alive() {
let stat = zombie_with_threads(2);
assert_eq!(parse_num_threads(&stat), Some(2));
assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Alive);
}
#[skuld::test]
fn running_from_stat_zombie_with_one_thread_is_dead() {
assert_eq!(parse_num_threads(&zombie_with_threads(1)), Some(1));
assert_eq!(running_from_stat(&zombie_with_threads(1), tok(8675309)), Liveness::Dead);
}
#[skuld::test]
fn running_from_stat_zombie_with_an_unreadable_thread_count_is_unknown() {
let stat = String::from_utf8(stat_fixture("Z", 8675309))
.expect("a utf-8 fixture")
.replacen(" 20 0 1 0 ", " 20 0 many 0 ", 1)
.into_bytes();
assert_eq!(parse_num_threads(&stat), None);
assert_eq!(running_from_stat(&stat, tok(8675309)), Liveness::Unknown);
}
#[skuld::test]
fn a_truncated_record_parses_to_nothing_and_is_unknown() {
let stat = b"1234 (myproc) Z 1 1234 1234 0 -1 4194560 100 0 0 0 1 2 0 0 20 0 2";
assert_eq!(parse_num_threads(stat), Some(2));
assert_eq!(parse_starttime_jiffies(stat), None);
assert_eq!(running_from_stat(stat, tok(8675309)), Liveness::Unknown);
assert_eq!(
parse_num_threads(b"1234 (myproc) Z 1 1234 1234 0 -1 4194560 100 0 0 0 1 2 0 0 20 0"),
None
);
}
#[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() {
assert_eq!(running_from_stat(b"not a stat line at all", tok(1)), Liveness::Unknown);
}