use std::os::fd::{AsRawFd, OwnedFd};
use super::{adopted_id, failed_spawn_error, read_report, register, seams, set_nonblocking, Report, Unadoptable};
use crate::error::Error;
fn pipe() -> (OwnedFd, std::io::PipeWriter) {
let (r, w) = std::io::pipe().expect("pipe");
let r = OwnedFd::from(r);
set_nonblocking(&r).expect("non-blocking");
(r, w)
}
fn report_bytes(tag: u32, value: u64) -> Vec<u8> {
let mut bytes = tag.to_ne_bytes().to_vec();
bytes.extend_from_slice(&value.to_ne_bytes());
bytes
}
#[skuld::test]
fn a_report_with_an_id_reads_as_the_id() {
use std::io::Write;
let (r, mut w) = pipe();
w.write_all(&report_bytes(1, 0xfeed_beef_1234)).unwrap();
assert!(matches!(read_report(&r), Report::Id(0xfeed_beef_1234)));
}
#[skuld::test]
fn a_report_with_an_errno_reads_as_the_childs_refusal() {
use std::io::Write;
let (r, mut w) = pipe();
w.write_all(&report_bytes(2, libc::EPERM as u64)).unwrap();
assert!(matches!(read_report(&r), Report::ChildRefused(e) if e == libc::EPERM));
}
#[skuld::test]
fn a_short_report_then_eof_is_missing() {
use std::io::Write;
let (r, mut w) = pipe();
w.write_all(&report_bytes(1, 7)[..5]).unwrap();
drop(w);
assert!(matches!(read_report(&r), Report::Missing));
}
#[skuld::test]
fn an_empty_pipe_at_eof_is_missing() {
let (r, w) = pipe();
drop(w);
assert!(matches!(read_report(&r), Report::Missing));
}
#[skuld::test]
fn the_channels_read_end_is_non_blocking() {
let mut cmd = std::process::Command::new("/usr/bin/true");
let pending = register(&mut cmd);
let guard = crate::child::spawn::spawn_lock();
let channel = pending.open(&guard).expect("open");
let flags = unsafe { libc::fcntl(channel.read_end.as_raw_fd(), libc::F_GETFL) };
assert_ne!(flags & libc::O_NONBLOCK, 0, "the read end must be non-blocking");
assert!(matches!(read_report(&channel.read_end), Report::Unwritten));
}
#[skuld::test]
fn an_open_empty_pipe_is_unwritten_not_missing() {
let (r, _w) = pipe();
set_nonblocking(&r).expect("non-blocking");
assert!(matches!(read_report(&r), Report::Unwritten));
}
#[skuld::test]
fn an_unknown_tag_is_a_bad_tag() {
use std::io::Write;
let (r, mut w) = pipe();
w.write_all(&report_bytes(99, 0)).unwrap();
assert!(matches!(read_report(&r), Report::BadTag(99)));
}
#[skuld::test]
fn a_missing_report_is_a_child_that_died_before_exec() {
let not_adopted = adopted_id(Report::Missing, 4242).expect_err("no id");
assert_eq!(not_adopted.why, Unadoptable::DiedBeforeExec);
let Error::Io(e) = not_adopted.error else {
panic!("a missing report is an io error, not an Unassessable refusal")
};
assert!(e.to_string().contains("died before exec"), "{e}");
assert_eq!(e.raw_os_error(), None, "no errno is made up");
}
#[skuld::test]
fn a_refusal_under_an_ok_spawn_means_the_program_did_not_start() {
let not_adopted = adopted_id(Report::ChildRefused(libc::EPERM), 9).expect_err("no id");
assert_eq!(not_adopted.why, Unadoptable::DiedBeforeExec);
let Error::Unassessable { detail, .. } = not_adopted.error else {
panic!("a refusal is Unassessable")
};
assert!(detail.contains("did not start"), "{detail}");
}
#[skuld::test]
fn a_failed_read_is_unassessable_with_its_error_and_not_a_corpse() {
let not_adopted =
adopted_id(Report::ReadFailed(std::io::Error::from_raw_os_error(libc::EIO)), 7).expect_err("no id");
assert_eq!(not_adopted.why, Unadoptable::Unverified);
assert!(matches!(not_adopted.error, Error::Unassessable { source: Some(_), .. }));
}
#[skuld::test]
fn an_unwritten_report_may_have_started() {
let not_adopted = adopted_id(Report::Unwritten, 31).expect_err("no id");
assert_eq!(not_adopted.why, Unadoptable::Unreported);
let error = not_adopted.error;
assert!(
!error.to_string().contains("died before exec"),
"an unreported child is not a corpse: {error}"
);
assert!(
matches!(error, Error::Io(ref e) if e.raw_os_error().is_none()),
"{error:?}"
);
}
#[skuld::test]
fn only_a_refusal_or_a_missing_report_proves_no_exec() {
use super::proves_no_exec;
assert!(proves_no_exec(&Report::ChildRefused(libc::EPERM)));
assert!(proves_no_exec(&Report::Missing));
assert!(!proves_no_exec(&Report::Id(1)));
assert!(!proves_no_exec(&Report::Unwritten));
assert!(!proves_no_exec(&Report::ReadFailed(std::io::Error::from_raw_os_error(
libc::EIO
))));
assert!(!proves_no_exec(&Report::BadTag(9)));
}
#[skuld::test]
fn only_a_childs_refusal_changes_a_failed_spawns_error() {
let original = || Error::Io(std::io::Error::from_raw_os_error(libc::ENOENT));
for report in [
Report::Missing,
Report::ReadFailed(std::io::Error::from_raw_os_error(libc::EIO)),
Report::Id(1),
] {
let kept = failed_spawn_error(original(), &report);
assert!(
matches!(&kept, Error::Io(e) if e.raw_os_error() == Some(libc::ENOENT)),
"{report:?}: {kept:?}"
);
}
assert!(matches!(
failed_spawn_error(original(), &Report::ChildRefused(libc::EPERM)),
Error::Unassessable { .. }
));
}
fn run_through_channel(cmd: &mut std::process::Command) -> (std::io::Result<std::process::Child>, Report) {
let pending = register(cmd);
let guard = crate::child::spawn::spawn_lock();
let channel = pending.open(&guard).expect("open");
#[allow(clippy::disallowed_methods, reason = "spawn_lock is held by `guard`")]
channel.run(|| cmd.spawn())
}
#[skuld::test]
fn a_nonexistent_program_fails_with_stds_io_error() {
let mut cmd = std::process::Command::new("/nonexistent/cosca-no-such-program");
let (spawned, report) = run_through_channel(&mut cmd);
let io_error = spawned.expect_err("exec fails");
assert!(matches!(report, Report::Id(_)), "{report:?}");
let mapped = failed_spawn_error(Error::Io(io_error), &report);
assert!(matches!(mapped, Error::Io(_)), "{mapped:?}");
}
#[skuld::test]
fn own_unique_id_is_the_unique_id_of_this_process() {
use crate::identity::{uniq_info, ReadPurpose, UniqRead};
let own = crate::identity::own_unique_id().expect("own id");
let UniqRead::Found(by_pid) = uniq_info(std::process::id(), ReadPurpose::Kill) else {
panic!("this process has a unique id")
};
assert_eq!(own, by_pid.unique_id);
}
#[skuld::test]
fn a_child_killed_before_its_report_is_missing_on_ok() {
let _forced = seams::force_child_killed_before_report();
let mut cmd = std::process::Command::new("/usr/bin/true");
let (spawned, report) = run_through_channel(&mut cmd);
let mut child = spawned.expect("std reports Ok for a child killed before exec");
assert!(matches!(report, Report::Missing), "{report:?}");
child.wait().expect("reap the corpse");
}
#[skuld::test]
fn an_unwritten_report_then_a_killed_child_is_answered_may_be_running() {
let _killed = seams::force_child_killed_before_report();
let _unwritten = seams::find_the_report_unwritten();
let mut cmd = crate::command::Command::new();
cmd.args(["/usr/bin/true"]);
let error = cmd.spawn().expect_err("an unreported child is not adopted");
let (_error, fate) = crate::child::spawn::failure::expect_may_have_started_with(error);
assert_eq!(fate, crate::error::ChildFate::Running { id: None });
}