use super::*;
#[test]
fn kernel_writes_exactly_our_kinfo_proc_size() {
let mut buf = [0u8; 2 * std::mem::size_of::<kinfo_proc>()];
let mut size = buf.len();
let mut mib = [
libc::CTL_KERN,
libc::KERN_PROC,
libc::KERN_PROC_PID,
std::process::id() as libc::c_int,
];
let rc = unsafe {
libc::sysctl(
mib.as_mut_ptr(),
mib.len() as libc::c_uint,
buf.as_mut_ptr() as *mut libc::c_void,
&mut size,
std::ptr::null_mut(),
0,
)
};
assert_eq!(rc, 0, "sysctl fetch failed: {}", std::io::Error::last_os_error());
assert_eq!(
size,
std::mem::size_of::<kinfo_proc>(),
"kernel kinfo_proc record size disagrees with our layout"
);
}
#[cfg_attr(debug_assertions, should_panic(expected = "sysctl(KERN_PROC"))]
#[test]
fn read_record_flags_an_invalid_selector() {
let r = super::read_record(std::process::id() as super::super::RawPid, -1);
assert!(r.is_none(), "an invalid selector must never yield a record");
}
#[cfg_attr(debug_assertions, should_panic(expected = "synthetic"))]
#[test]
fn contract_violation_traces_then_trips() {
crate::log_capture::install();
let mark = crate::log_capture::mark();
super::super::contract_violation(format_args!("synthetic contract violation (test)"));
assert!(
crate::log_capture::contains_since(mark, "synthetic contract violation (test)"),
"the warn must have fired before the (compiled-out) tripwire"
);
}
#[test]
fn sysctl_token_matches_libproc_for_a_live_process() {
let pid = std::process::id() as libc::c_int;
let ours = kinfo(pid as super::super::RawPid).expect("self resolves via sysctl");
let ours = {
let t = unsafe { ours.kp_proc.p_un.p_starttime };
t.tv_sec as u64 * 1_000_000 + t.tv_usec as u64
};
let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() };
let size = std::mem::size_of::<libc::proc_bsdinfo>() as libc::c_int;
let n = unsafe {
libc::proc_pidinfo(
pid,
libc::PROC_PIDTBSDINFO,
0,
&mut info as *mut _ as *mut libc::c_void,
size,
)
};
assert_eq!(n, size, "proc_pidinfo oracle failed for self");
let theirs = info.pbi_start_tvsec * 1_000_000 + info.pbi_start_tvusec;
assert_eq!(ours, theirs, "sysctl and libproc disagree on self's start token");
}