use super::super::{allocate_pids, collect_pids};
fn kernel_with(total: usize) -> impl FnMut(&mut [libc::c_int]) -> std::io::Result<usize> {
move |buf| {
let n = total.min(buf.len());
for (slot, pid) in buf[..n].iter_mut().zip(1..) {
*slot = pid;
}
Ok(n)
}
}
#[test]
fn grows_until_the_buffer_is_not_the_limit() {
let filled = collect_pids(4, kernel_with(100), allocate_pids).expect("fill succeeds");
assert_eq!(
filled.pids.len(),
100,
"a saturated answer must not be taken as the whole set"
);
assert_eq!(filled.rounds, 6, "each saturated fill doubles the buffer and retries");
}
#[test]
fn one_round_when_the_first_buffer_has_room() {
let filled = collect_pids(16, kernel_with(3), allocate_pids).expect("fill succeeds");
assert_eq!(
filled.pids,
[1, 2, 3],
"the buffer is cut back to what the kernel wrote"
);
assert_eq!(filled.rounds, 1, "a buffer with room needs no retry");
}
#[test]
fn a_failing_fill_is_an_error_not_a_short_list() {
let err = collect_pids(
16,
|_| Err(std::io::Error::from_raw_os_error(libc::EPERM)),
allocate_pids,
)
.expect_err("a denied fill must not look like an empty process table");
assert_eq!(err.raw_os_error(), Some(libc::EPERM));
}
#[test]
fn an_immediate_overflow_ends_in_an_error_without_allocating() {
let err = collect_pids(libc::c_int::MAX as usize, |buf| Ok(buf.len()), allocate_pids)
.expect_err("a pid count this large cannot be described in the size argument");
assert!(
err.to_string().contains("int size argument"),
"expected the size-argument refusal, got: {err}"
);
}
fn allocate_with_ceiling(limit: usize) -> impl FnMut(usize) -> std::io::Result<Vec<libc::c_int>> {
move |cap| {
if cap > limit {
return Err(std::io::Error::other(format!(
"synthetic ceiling of {limit} pids exceeded"
)));
}
Ok(vec![0; cap])
}
}
#[test]
fn doubling_then_refusal_ends_in_an_error() {
let err = collect_pids(4, |buf| Ok(buf.len()), allocate_with_ceiling(100))
.expect_err("a ceiling crossed after several successful rounds must still be an error");
assert!(
err.to_string().contains("ceiling"),
"expected the ceiling refusal, got: {err}"
);
}
#[test]
fn a_failing_allocation_is_an_error_not_an_abort() {
let err = collect_pids(
16,
|buf| Ok(buf.len()),
|_cap| Err(std::io::Error::other("synthetic allocation failure")),
)
.expect_err("an allocation failure must be an Err, not an abort");
assert!(
err.to_string().contains("synthetic allocation failure"),
"expected the injected allocation failure, got: {err}"
);
}
#[test]
fn an_allocate_returning_an_empty_buffer_is_an_error_not_an_infinite_loop() {
let err = collect_pids(4, |buf| Ok(buf.len()), |_cap| Ok(Vec::new()))
.expect_err("an empty buffer for a nonzero capacity cannot make progress");
assert!(
err.to_string().contains("did not grow"),
"expected the no-growth refusal, got: {err}"
);
}
#[test]
fn an_allocate_that_ignores_cap_is_an_error_not_an_infinite_loop() {
let err = collect_pids(4, |buf| Ok(buf.len()), |_cap| Ok(vec![0; 4]))
.expect_err("a buffer that never grows cannot make progress");
assert!(
err.to_string().contains("did not grow"),
"expected the no-growth refusal, got: {err}"
);
}
#[test]
fn a_fill_reporting_more_than_the_buffer_holds_is_an_error() {
let err = collect_pids(4, |buf| Ok(buf.len() + 1), allocate_pids)
.expect_err("written > n must not be treated as saturation");
assert!(
err.to_string().contains("more pids than the buffer holds"),
"expected the over-report refusal, got: {err}"
);
}
#[test]
fn a_zero_capacity_is_an_error_not_an_infinite_loop() {
let err = collect_pids(0, |_| Ok(0), allocate_pids).expect_err("a zero-capacity buffer cannot make progress");
assert!(
err.to_string().contains("nonzero starting capacity"),
"expected the zero-capacity refusal, got: {err}"
);
}