use super::{origin, Origin};
#[skuld::test]
fn a_child_of_this_process_is_ours() {
let mut cmd = std::process::Command::new("/bin/cat");
cmd.stdin(std::process::Stdio::piped());
let guard = crate::child::spawn::spawn_lock();
#[allow(clippy::disallowed_methods, reason = "spawn_lock is held by `guard`")]
let mut child = cmd.spawn().expect("spawn cat");
drop(guard);
assert_eq!(
origin(child.id()),
Origin::Ours {
parent: std::process::id()
}
);
drop(child.stdin.take());
child.wait().expect("reap cat");
}
#[skuld::test]
fn the_parent_of_this_process_is_not_ours() {
let parent = unsafe { libc::getppid() } as u32;
assert_eq!(origin(parent), Origin::NotOurs);
}
#[skuld::test]
fn a_stale_parent_at_attach_does_not_make_an_older_process_ours() {
let parent = unsafe { libc::getppid() } as u32;
super::kinfo::fault::force_oppid_once(std::process::id() as libc::pid_t);
assert_eq!(origin(parent), Origin::NotOurs);
}
fn live_child() -> (std::process::Child, std::process::ChildStdin) {
let mut cmd = std::process::Command::new("/bin/cat");
cmd.stdin(std::process::Stdio::piped());
let guard = crate::child::spawn::spawn_lock();
#[allow(clippy::disallowed_methods, reason = "spawn_lock is held by `guard`")]
let mut child = cmd.spawn().expect("spawn cat");
drop(guard);
let stdin = child.stdin.take().expect("piped stdin");
(child, stdin)
}
fn end(mut child: std::process::Child, stdin: std::process::ChildStdin) {
drop(stdin);
child.wait().expect("reap cat");
}
#[skuld::test]
fn a_pid_with_no_process_is_gone() {
use super::{link, uniq_fault, Link, ReadPurpose, UniqRead};
let (child, stdin) = live_child();
{
let _gone = uniq_fault::force_uniq_read_once(ReadPurpose::Origin, UniqRead::Gone);
assert_eq!(origin(child.id()), Origin::Gone);
}
{
let _gone = uniq_fault::force_uniq_read_once(ReadPurpose::Link, UniqRead::Gone);
assert_eq!(link(child.id()), Link::Gone);
}
end(child, stdin);
}
#[skuld::test]
fn a_refused_unique_id_read_is_refused() {
use super::{link, uniq_fault, Link, ReadPurpose, UniqRead};
let (child, stdin) = live_child();
{
let _refused = uniq_fault::force_uniq_read_once(ReadPurpose::Origin, UniqRead::Refused(libc::EPERM));
assert_eq!(origin(child.id()), Origin::Refused(libc::EPERM));
}
{
let _refused = uniq_fault::force_uniq_read_once(ReadPurpose::Link, UniqRead::Refused(libc::EPERM));
assert_eq!(link(child.id()), Link::Refused(libc::EPERM));
}
end(child, stdin);
}
#[skuld::test]
fn a_refused_kinfo_read_is_unreadable() {
use super::{link, Link};
let (child, stdin) = live_child();
super::kinfo::fault::force_unreadable_once();
assert_eq!(origin(child.id()), Origin::Unreadable);
super::kinfo::fault::force_unreadable_once();
assert_eq!(link(child.id()), Link::Unreadable);
end(child, stdin);
}
#[skuld::test]
fn an_untraced_child_links_to_this_process() {
use super::{link, uniq_info, Link, ReadPurpose, UniqRead};
let (child, stdin) = live_child();
let UniqRead::Found(info) = uniq_info(child.id(), ReadPurpose::Kill) else {
panic!("the child's unique id must be readable")
};
assert_eq!(
link(child.id()),
Link::Found {
unique: info.unique_id,
parent: std::process::id(),
at_attach: 0,
}
);
end(child, stdin);
}