sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use super::refresh::{Parts, parse_stat_file, split_content};
use std::ffi::OsString;

// A process marked nonexistent (for example, hidden by `hidepid=invisible` for one refresh) must be reported as existing again once it can be read.
#[test]
fn test_exists_after_being_nonexistent() {
    use crate::{Pid, ProcessRefreshKind, ProcessesToUpdate, System};

    let pid = Pid::from_u32(std::process::id());
    let mut s = System::new().unwrap();
    s.refresh_processes(ProcessesToUpdate::Some(&[pid]), false);
    s.process(pid).unwrap();
    s.inner
        .processes_mut()
        .get_mut(&pid)
        .unwrap()
        .inner
        .set_nonexistent();
    assert!(!s.process(pid).unwrap().exists());
    s.refresh_processes_specifics(
        ProcessesToUpdate::Some(&[pid]),
        false,
        ProcessRefreshKind::nothing(),
    );
    assert!(s.process(pid).unwrap().exists());
}

// Verifies that the data splits into all expected parts.
#[test]
fn test_copy_file() {
    assert_eq!(split_content(b"hello\0"), vec![OsString::from("hello")]);
    assert_eq!(split_content(b"hello"), vec![OsString::from("hello")]);
    assert_eq!(
        split_content(b"hello\0b"),
        vec![OsString::from("hello"), "b".into()]
    );
    assert_eq!(
        split_content(b"hello\0\0\0\0b"),
        vec![OsString::from("hello"), "b".into()]
    );
}

#[test]
fn test_parse_stat_file_does_not_panic_on_short_input() {
    for input in [
        b"".as_slice(),
        b"X".as_slice(),
        b"X ".as_slice(),
        b"X )",
        b"X;\xDF )",
        b")",
        b"1 ()",
        b"1 (x)",
    ] {
        let _ = parse_stat_file(input, None);
    }
    assert!(parse_stat_file(b"X;\xDF )", None).is_none());
}

#[test]
fn test_parse_stat_file_random_data_does_not_panic() {
    let mut state = 0x1234_5678_u32;
    for len in 0..512 {
        let mut data = vec![0_u8; len];
        for byte in &mut data {
            state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
            *byte = (state >> 24) as u8;
        }
        let _ = parse_stat_file(&data, None);
    }
}

#[test]
fn test_parse_stat_file() {
    // Trimmed content of a stat file.
    let content = b"1 (blob) S 2 0 0 0 -1 2129984 0 0 0 0 14 28 0 0 20 0 1 0 21 66 77";
    let data = parse_stat_file(content, None).unwrap();
    assert_eq!(
        data,
        Parts {
            short_exe: "blob".into(),
            status: crate::ProcessStatus::Sleep,
            parent_pid: Some(2),
            flags: Some(2129984),
            user_time: 14,
            start_time: 21,
            system_time: 28,
            virtual_size: 66,
            resident_set_size: 77,
        }
    );
}

// Verifies that parentheses in the short executable name are handled correctly.
#[test]
fn test_parse_stat_file_short_exe() {
    // Trimmed content of a stat file.
    let content = b"1 (bl()ob) S 2 0 0 0 -1 2129984 0 0 0 0 14 28 0 0 20 0 1 0 21 66 77";
    let data = parse_stat_file(content, None).unwrap();
    assert_eq!(
        data,
        Parts {
            short_exe: "bl()ob".into(),
            status: crate::ProcessStatus::Sleep,
            parent_pid: Some(2),
            flags: Some(2129984),
            user_time: 14,
            start_time: 21,
            system_time: 28,
            virtual_size: 66,
            resident_set_size: 77,
        }
    );
}

#[test]
fn test_parse_stat_file_long_exe() {
    // This is a real short executable name.
    let content = b"1 (nvidia-modeset/deferred_close_kthread_q) S 2 0 0 0 -1 2129984 0 0 0 0 14 28 0 0 20 0 1 0 21 66 77";
    let data = parse_stat_file(content, None).unwrap();
    assert_eq!(
        data,
        Parts {
            short_exe: "nvidia-modeset/deferred_close_kthread_q".into(),
            status: crate::ProcessStatus::Sleep,
            parent_pid: Some(2),
            flags: Some(2129984),
            user_time: 14,
            start_time: 21,
            system_time: 28,
            virtual_size: 66,
            resident_set_size: 77,
        }
    );
}