use super::{read, read_link, ReadOutcome};
use crate::identity::{FakeKernel, HostKernel, Kernel};
#[derive(Debug, Clone, Copy)]
enum Call {
Open,
Read,
}
#[derive(Debug, Clone, Copy)]
enum Recheck {
Answers,
Fails(Call, i32),
}
#[derive(Debug)]
enum Want {
Exited,
Hidden,
Refused(i32),
Unreadable(&'static str),
RecheckFailed(i32),
}
#[skuld::test]
fn a_failed_read_is_what_its_call_and_errno_say() {
use Call::{Open, Read};
use Recheck::{Answers, Fails};
let dir = HostKernel.open_proc().expect("/proc opens");
let rows = [
(Open, libc::ENOENT, None, Want::Hidden),
(Open, libc::EACCES, None, Want::Hidden),
(Open, libc::EPERM, Some(Answers), Want::Hidden),
(Open, libc::ESRCH, None, Want::Exited),
(Read, libc::ESRCH, None, Want::Exited),
(Open, libc::ENOSYS, None, Want::Refused(libc::ENOSYS)),
(Open, libc::ENODEV, None, Want::Refused(libc::ENODEV)),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::EPERM)),
Want::Refused(libc::EPERM),
),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::ENOSYS)),
Want::Refused(libc::ENOSYS),
),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::ENODEV)),
Want::Refused(libc::ENODEV),
),
(Open, libc::EXDEV, None, Want::Unreadable("lies beyond a mount")),
(Open, libc::ELOOP, None, Want::Unreadable("lies beyond a mount")),
(Open, libc::EIO, None, Want::Unreadable("could not be read")),
(Open, libc::EMFILE, None, Want::Unreadable("could not be read")),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::EMFILE)),
Want::RecheckFailed(libc::EMFILE),
),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::ESRCH)),
Want::RecheckFailed(libc::ESRCH),
),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::ENOENT)),
Want::RecheckFailed(libc::ENOENT),
),
(
Open,
libc::EPERM,
Some(Fails(Open, libc::EACCES)),
Want::RecheckFailed(libc::EACCES),
),
(
Open,
libc::EPERM,
Some(Fails(Read, libc::ENOSYS)),
Want::RecheckFailed(libc::ENOSYS),
),
(
Open,
libc::EPERM,
Some(Fails(Read, libc::ESRCH)),
Want::RecheckFailed(libc::ESRCH),
),
(Read, libc::EPERM, None, Want::Unreadable("could not be read")),
(Read, libc::ENOSYS, None, Want::Unreadable("could not be read")),
(Read, libc::ENOENT, None, Want::Unreadable("could not be read")),
(Read, libc::EXDEV, None, Want::Unreadable("could not be read")),
(Read, libc::EIO, None, Want::Unreadable("could not be read")),
];
for (call, errno, recheck, want) in rows {
fn row<'a>(kernel: FakeKernel<'a>, path: &str, call: Call, errno: i32) -> FakeKernel<'a> {
match call {
Open => kernel.on_read(path, Err(errno)),
Read => kernel.on_read_failing_after_open(path, errno),
}
}
let mut kernel = row(FakeKernel::new(), "4242/stat", call, errno);
kernel = match recheck {
None => kernel,
Some(Answers) => kernel.on_read("self/stat", Ok(b"")),
Some(Fails(call, errno)) => row(kernel, "self/stat", call, errno),
};
let name = format!("{call:?} {errno}, re-check {recheck:?}");
match (read(&kernel, &dir, 4242, "stat"), want) {
(Ok(ReadOutcome::Exited(why)), Want::Exited) | (Ok(ReadOutcome::Hidden(why)), Want::Hidden) => {
assert_eq!(why.raw_os_error(), Some(errno), "{name}: the pid's own error is why");
}
(Ok(ReadOutcome::Refused(refusal)), Want::Refused(refusing)) => {
assert_eq!(crate::error::first_os_code(&refusal.cause()), Some(refusing), "{name}");
assert!(refusal.requirement().contains("requires openat2"), "{name}");
}
(Err(unreadable), Want::Unreadable(words)) => {
assert!(unreadable.to_string().contains(words), "{name}: {unreadable}");
}
(Err(mut unreadable), Want::RecheckFailed(recheck)) => {
assert!(
unreadable.to_string().contains("re-check of self/stat failed"),
"{name}: {unreadable}"
);
assert_eq!(crate::error::first_os_code(&unreadable), Some(recheck), "{name}");
let peer = unreadable.take_peer().and_then(|e| e.raw_os_error());
assert_eq!(peer, Some(libc::EPERM), "{name}: the pid's EPERM is the peer");
}
(got, want) => panic!("{name}: expected {want:?}, got {got:?}"),
}
}
}
#[skuld::test]
fn a_failed_link_read_is_what_its_call_and_errno_say() {
use Call::{Open, Read};
let rows = [
(Open, libc::ESRCH, Want::Exited),
(Open, libc::ENOENT, Want::Hidden),
(Read, libc::EACCES, Want::Hidden),
(Read, libc::ENOENT, Want::Exited),
(Read, libc::ESRCH, Want::Exited),
(Read, libc::EPERM, Want::Unreadable("could not be read")),
(Read, libc::EIO, Want::Unreadable("could not be read")),
];
let dir = HostKernel.open_proc().expect("/proc opens");
for (call, errno, want) in rows {
let kernel = match call {
Open => FakeKernel::new().on_read("4242/ns/pid", Err(errno)),
Read => FakeKernel::new().on_read_failing_after_open("4242/ns/pid", errno),
};
let name = format!("{call:?} {errno}");
match (read_link(&kernel, &dir, 4242, "ns/pid"), want) {
(Ok(ReadOutcome::Exited(why)), Want::Exited) | (Ok(ReadOutcome::Hidden(why)), Want::Hidden) => {
assert_eq!(
crate::error::first_os_code(&why),
Some(errno),
"{name}: the pid's own error is why"
);
}
(Err(unreadable), Want::Unreadable(words)) => {
assert!(unreadable.to_string().contains(words), "{name}: {unreadable}");
}
(got, want) => panic!("{name}: expected {want:?}, got {got:?}"),
}
}
}
#[skuld::test]
fn a_pids_cgroup_file_is_read_like_its_stat_and_named_in_the_failure() {
let dir = HostKernel.open_proc().expect("/proc opens");
let kernel = FakeKernel::new()
.on_read("4242/cgroup", Ok(b"0::/\n"))
.on_read("4243/cgroup", Err(libc::EIO));
assert!(matches!(
read(&kernel, &dir, 4242, "cgroup"),
Ok(ReadOutcome::Read(bytes)) if bytes == b"0::/\n"
));
let Err(unreadable) = read(&kernel, &dir, 4243, "cgroup") else {
panic!("EIO is no skip");
};
assert!(unreadable.to_string().contains("4243/cgroup"), "{unreadable}");
}
#[skuld::test]
fn an_unreadable_pid_has_the_reads_error_as_its_source() {
let dir = HostKernel.open_proc().expect("/proc opens");
let kernel = FakeKernel::new().on_read("4242/stat", Err(libc::EIO));
let Err(unreadable) = read(&kernel, &dir, 4242, "stat") else {
panic!("EIO is no skip");
};
let source = std::error::Error::source(&unreadable).expect("an unreadable pid has a source");
assert_eq!(
source
.downcast_ref::<std::io::Error>()
.and_then(std::io::Error::raw_os_error),
Some(libc::EIO)
);
}
#[skuld::test]
fn an_unreadable_pid_says_what_failed_and_not_the_os_error() {
let dir = HostKernel.open_proc().expect("/proc opens");
let os_error = std::io::Error::from_raw_os_error(libc::EIO).to_string();
let kernel = FakeKernel::new().on_read("4242/stat", Err(libc::EIO));
let Err(unreadable) = read(&kernel, &dir, 4242, "stat") else {
panic!("EIO is no skip");
};
assert!(!unreadable.to_string().contains(&os_error), "{unreadable}");
assert!(unreadable.to_string().contains("4242/stat"), "{unreadable}");
let beyond = FakeKernel::new().on_read("4242/stat", Err(libc::EXDEV));
let Err(unreadable) = read(&beyond, &dir, 4242, "stat") else {
panic!("EXDEV is no skip");
};
let os_error = std::io::Error::from_raw_os_error(libc::EXDEV).to_string();
assert!(!unreadable.to_string().contains(&os_error), "{unreadable}");
}
#[skuld::test]
fn the_other_failure_of_a_refused_pid_is_kept_beside_the_refusal() {
use crate::error::Error;
let dir = HostKernel.open_proc().expect("/proc opens");
for recheck in [libc::EPERM, libc::ENOSYS, libc::ENODEV] {
let kernel = FakeKernel::new()
.on_read("4242/stat", Err(libc::EPERM))
.on_read("self/stat", Err(recheck));
let Ok(ReadOutcome::Refused(refusal)) = read(&kernel, &dir, 4242, "stat") else {
panic!("{recheck}: a refusing directory is a refusal");
};
let cause = refusal.cause();
let Error::Several(causes) = &cause else {
panic!("{recheck}: expected Several, got {cause:?}");
};
let codes: Vec<_> = causes.iter().map(|c| crate::error::first_os_code(&**c)).collect();
assert_eq!(codes, [Some(recheck), Some(libc::EPERM)], "{recheck}");
let second = &**causes.iter().nth(1).expect("a second cause");
let words = crate::error::Report(second).to_string();
assert!(
words.contains("4242/stat was refused, and the checked /proc refused self/stat too"),
"{recheck}: {words}"
);
assert_eq!(
crate::error::first_os_code(second),
Some(libc::EPERM),
"{recheck}: {words}"
);
}
let kernel = FakeKernel::new().on_read("4242/stat", Err(libc::ENOSYS));
let Ok(ReadOutcome::Refused(refusal)) = read(&kernel, &dir, 4242, "stat") else {
panic!("ENOSYS is a refusal");
};
assert!(
matches!(refusal.cause(), Error::Io(_)),
"a lone refusal has no second cause"
);
}
#[skuld::test]
fn the_pids_eperm_is_kept_as_the_peer_of_a_failed_recheck() {
let dir = HostKernel.open_proc().expect("/proc opens");
let kernel = FakeKernel::new()
.on_read("4242/stat", Err(libc::EPERM))
.on_read("self/stat", Err(libc::EMFILE));
let Err(mut unreadable) = read(&kernel, &dir, 4242, "stat") else {
panic!("a failed directory is no skip");
};
let kept = unreadable.take_peer().and_then(|e| e.raw_os_error());
assert_eq!(kept, Some(libc::EPERM));
assert_eq!(crate::error::first_os_code(&unreadable), Some(libc::EMFILE));
assert_eq!(
unreadable.take_peer().and_then(|e| e.raw_os_error()),
None,
"taken once"
);
let beyond = FakeKernel::new().on_read("4242/stat", Err(libc::EIO));
let Err(mut unreadable) = read(&beyond, &dir, 4242, "stat") else {
panic!("EIO is no skip");
};
assert!(unreadable.take_peer().is_none(), "a plain failure has no peer");
}
#[skuld::test]
fn a_recheck_that_fails_without_refusing_is_its_own_error_and_the_source() {
let dir = HostKernel.open_proc().expect("/proc opens");
for recheck in [libc::EMFILE, libc::ENOMEM, libc::EIO] {
let kernel = FakeKernel::new()
.on_read("4242/stat", Err(libc::EPERM))
.on_read("self/stat", Err(recheck));
let unreadable = match read(&kernel, &dir, 4242, "stat") {
Err(unreadable) => unreadable,
other => panic!("{recheck}: a failed re-check is neither a skip nor a refusal: {other:?}"),
};
let text = unreadable.to_string();
assert!(text.starts_with("4242/stat was refused"), "{recheck}: {text}");
assert!(text.contains("re-check of self/stat failed"), "{recheck}: {text}");
let os_text = std::io::Error::from_raw_os_error(recheck).to_string();
assert!(
!text.contains(&os_text),
"the cause is the source, not in the words: {text}"
);
assert_eq!(
crate::error::first_os_code(&unreadable),
Some(recheck),
"{recheck}: {text}"
);
}
}