use rustix::event::{PollFd, PollFlags};
use super::super::fake_shim::{next_acceptor_event, FakeShim, Rig};
use super::super::probe::LinkEvent;
use super::super::{KillError, KillOutcome, LinkOutcome, NotOwner, StartState, WaitError};
use crate::elevation::shim::protocol::{Command, Frame};
#[skuld::test]
fn teardown_unlinks_before_the_final_drain() {
let Rig {
link,
probe,
events,
tmp: _tmp,
} = Rig::new();
probe.hold_acceptor();
let mut queued = super::super::fake_shim::FakeShim::connect(link.dir()).unwrap();
queued.hello();
drop(link);
let drain = events
.try_iter()
.find(|e| matches!(e, LinkEvent::DrainStarted { .. }))
.expect("teardown joined the acceptor, so its drain ran");
assert_eq!(drain, LinkEvent::DrainStarted { path_exists: false });
assert_eq!(queued.read_byte(), Some(b'N'), "the backlog is answered N");
}
#[skuld::test]
fn wait_then_drop_does_not_report_shim_lost() {
let rig = Rig::new();
let marker = rig.log_marker();
let Rig {
link,
probe: _probe,
events: _events,
tmp: _tmp,
} = rig;
let mut shim = super::super::fake_shim::FakeShim::connect(link.dir()).unwrap();
shim.hello();
shim.send_frame(Frame::Status(0));
assert_eq!(shim.read_byte(), Some(b'A'));
shim.close();
assert_eq!(link.wait().unwrap(), LinkOutcome::Exited(0));
let mark = crate::log_capture::mark();
drop(link);
let problems: Vec<_> = crate::log_capture::levels_since(mark, &marker)
.into_iter()
.filter(|l| *l <= log::Level::Warn)
.collect();
assert!(
problems.is_empty(),
"teardown logged {problems:?} after a clean outcome"
);
}
#[skuld::test]
fn teardown_removes_the_directory_and_closes_the_connection() {
let rig = Rig::new();
let dir = rig.link.dir().to_owned();
let mut shim = rig.live();
let Rig { link, tmp: _tmp, .. } = rig;
drop(link);
assert!(!dir.exists(), "the private directory is removed");
assert_eq!(shim.read_byte(), None, "the connection is closed");
}
fn owner_survives_a_fork_copy(in_copy: fn(super::super::ShimLink)) {
let rig = Rig::new();
let socket = rig.link.dir().join(super::super::SOCKET_NAME);
assert!(socket.exists());
let Rig {
link,
probe,
events,
tmp: _tmp,
} = rig;
let pid = unsafe { libc::fork() };
assert!(pid >= 0, "fork");
if pid == 0 {
let ran = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| in_copy(link)));
unsafe { libc::_exit(if ran.is_ok() { 0 } else { 101 }) };
}
let waiter = {
let probe = probe.clone();
std::thread::spawn(move || {
let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
let waited =
unsafe { libc::waitid(libc::P_PID, pid as libc::id_t, &mut info, libc::WEXITED | libc::WNOWAIT) };
assert_eq!(waited, 0, "waitid");
probe.inject(LinkEvent::ChildExited);
})
};
let reap = || {
let mut status = 0;
assert_eq!(unsafe { libc::waitpid(pid, &mut status, 0) }, pid);
status
};
let first = next_acceptor_event(&events);
if first != LinkEvent::ChildExited {
unsafe { libc::kill(pid, libc::SIGKILL) };
waiter.join().unwrap();
reap();
panic!("the copy's drop disturbed the owner: {first:?}");
}
waiter.join().unwrap();
let status = reap();
assert!(
libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0,
"child status {status:#x}"
);
assert!(socket.exists(), "a fork copy does not unlink the path");
let mut shim = FakeShim::connect(link.dir()).unwrap();
shim.hello();
assert_eq!(next_acceptor_event(&events), LinkEvent::Accepted);
assert_eq!(next_acceptor_event(&events), LinkEvent::Answered(Command::Allow));
assert_eq!(shim.read_byte(), Some(b'A'));
assert_eq!(link.observe().unwrap().start, StartState::Live);
assert_eq!(link.kill().unwrap(), KillOutcome::Delivered);
}
#[skuld::test]
fn fork_copy_drop_leaves_the_owner_intact() {
owner_survives_a_fork_copy(drop);
}
#[skuld::test]
fn unknown_origin_in_a_fork_copy_leaves_the_owner_intact() {
owner_survives_a_fork_copy(|link| {
link.owner.make_unreadable();
drop(link);
});
}
#[skuld::test]
fn unknown_origin_in_the_original_stops_the_acceptor_and_removes_nothing() {
let Rig {
link,
probe,
events,
tmp: _tmp,
} = Rig::new();
let dir = link.dir().to_owned();
let shared = link.shared.clone();
let wake = link.wake.clone();
probe.hold_acceptor();
link.owner.make_unreadable();
drop(link);
assert!(
crate::wait::any_ready_now(&mut [PollFd::new(&wake.reader, PollFlags::IN)]).unwrap(),
"the stop byte is in the wake pipe"
);
probe.release_acceptor();
assert!(
shared.is_stopping(),
"the stop request is set before the byte is written"
);
let mut seen = Vec::new();
while !seen.iter().any(|e| matches!(e, LinkEvent::UnknownOriginHandled { .. })) {
seen.push(next_acceptor_event(&events));
}
assert!(
seen.contains(&LinkEvent::UnknownOriginHandled {
refused: true,
stop_written: true
}),
"{seen:?}"
);
while !seen.contains(&LinkEvent::AcceptorExited) {
seen.push(next_acceptor_event(&events));
}
assert!(
seen.contains(&LinkEvent::DrainStarted { path_exists: true }),
"{seen:?}"
);
assert!(dir.join(super::super::SOCKET_NAME).exists(), "nothing is unlinked");
assert!(dir.is_dir(), "nothing is removed");
}
#[skuld::test]
fn control_and_drop_with_a_full_fd_table_do_not_panic() {
let Some(done) = crate::test_own_process::own_process(
crate::test_own_process::test_path!(control_and_drop_with_a_full_fd_table_do_not_panic),
crate::test_spawn::spawn,
) else {
return;
};
let rig = Rig::new();
let marker = rig.log_marker();
let mut shim = rig.live();
let dir = rig.link.dir().to_owned();
let Rig { link, tmp: _tmp, .. } = rig;
let _restore = crate::test_child::exhaust_fds(&done);
assert!(
std::fs::File::open("/dev/null").is_err(),
"the precondition: no descriptor can be opened"
);
assert_eq!(link.observe().unwrap().start, StartState::Live);
assert_eq!(link.try_wait().unwrap(), None);
assert_eq!(link.kill().unwrap(), KillOutcome::Delivered);
assert_eq!(shim.read_byte(), Some(b'K'));
let mark = crate::log_capture::mark();
drop(link);
assert!(!dir.exists(), "teardown still removed the directory");
let levels = crate::log_capture::levels_since(mark, &marker);
assert!(!levels.contains(&log::Level::Error), "{levels:?}");
assert_eq!(
levels.contains(&log::Level::Warn),
cfg!(target_os = "linux"),
"the accept at teardown: {levels:?}"
);
}
#[skuld::test]
fn control_calls_from_a_fork_copy_are_refused() {
owner_survives_a_fork_copy(|link| {
assert!(matches!(link.kill(), Err(KillError::NotOwner(_))));
assert!(matches!(link.wait(), Err(WaitError::NotOwner(_))));
assert!(matches!(link.try_wait(), Err(NotOwner)));
assert!(matches!(link.observe(), Err(NotOwner)));
});
}
fn at_capacity(writer: &std::io::PipeWriter) {
use std::os::fd::AsFd;
while super::super::sys::write_byte(writer.as_fd()).is_ok() {}
}
fn is_nonblocking(fd: impl std::os::fd::AsFd) -> bool {
rustix::fs::fcntl_getfl(fd)
.unwrap()
.contains(rustix::fs::OFlags::NONBLOCK)
}
#[skuld::test]
fn teardown_completes_with_the_wake_pipe_full() {
let rig = Rig::new();
assert!(is_nonblocking(&rig.link.wake.writer), "the wake pipe's write end");
assert!(
is_nonblocking(&rig.link.shared.settled.1),
"the settled pipe's write end"
);
rig.probe.hold_acceptor();
let _wakes = rig.connect();
at_capacity(&rig.link.wake.writer);
let Rig { link, tmp: _tmp, .. } = rig;
drop(link);
}
#[skuld::test]
fn the_outcome_settles_with_the_settled_pipe_full() {
let rig = Rig::new();
let mut shim = rig.live();
assert!(
is_nonblocking(&rig.link.shared.settled.1),
"the settled pipe's write end"
);
at_capacity(&rig.link.shared.settled.1);
shim.send_frame(Frame::Status(0x2a00));
assert_eq!(rig.link.wait().unwrap(), LinkOutcome::Exited(0x2a00));
}
#[skuld::test]
fn every_descriptor_is_close_on_exec() {
use std::os::fd::AsFd;
let cloexec = |fd: std::os::fd::BorrowedFd<'_>| {
rustix::io::fcntl_getfd(fd)
.unwrap()
.contains(rustix::io::FdFlags::CLOEXEC)
};
let rig = Rig::new();
let _shim = rig.live();
assert_eq!(rig.probe.listener_cloexec(), Some(true), "the listener");
assert!(cloexec(rig.link.wake.reader.as_fd()), "the wake pipe's read end");
assert!(cloexec(rig.link.wake.writer.as_fd()), "the wake pipe's write end");
assert!(
cloexec(rig.link.shared.settled.0.as_fd()),
"the settled pipe's read end"
);
assert!(
cloexec(rig.link.shared.settled.1.as_fd()),
"the settled pipe's write end"
);
let conn = rig.link.shared.conn.get().expect("Live has a connection");
assert!(cloexec(conn.as_fd()), "the accepted connection");
}
#[skuld::test]
fn a_failed_stop_write_leaks_instead_of_joining() {
let Rig {
link,
probe,
events,
tmp: _tmp,
} = Rig::new();
let dir = link.dir().to_owned();
let wake = link.wake.clone();
probe.fail_next_stop_write(rustix::io::Errno::IO);
let dropped = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(link)));
assert_eq!(dropped.is_err(), cfg!(debug_assertions), "the debug assertion");
assert!(dir.is_dir(), "nothing is removed");
super::super::sys::write_byte(std::os::fd::AsFd::as_fd(&wake.writer)).unwrap();
assert_eq!(
next_acceptor_event(&events),
LinkEvent::DrainStarted { path_exists: false }
);
assert_eq!(next_acceptor_event(&events), LinkEvent::AcceptorExited);
}
#[skuld::test]
fn a_failed_settled_write_still_settles_the_outcome() {
let rig = Rig::new();
let mut shim = rig.live();
rig.probe.fail_next_settled_write(rustix::io::Errno::IO);
shim.send_frame(Frame::Status(0x2a00));
let polled = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| rig.link.try_wait()));
assert_eq!(polled.is_err(), cfg!(debug_assertions), "the debug assertion");
assert_eq!(rig.link.observe().unwrap().outcome, Some(LinkOutcome::Exited(0x2a00)));
}