Run the generated session script against stub dwl/helper binaries: helpers must start only after the Wayland socket exists, the polkit agent must start, and every helper must be reaped when the compositor exits. A compositor that dies before creating the socket fails the session loudly.
155 lines
5.2 KiB
Rust
155 lines
5.2 KiB
Rust
//! Functional test of the generated `phoebus-session` script.
|
|
//!
|
|
//! Runs the real script against stub `dwl`/helper binaries so the startup
|
|
//! ordering, the Wayland-socket wait, the polkit agent and helper cleanup are
|
|
//! exercised without a physical seat or a Wayland compositor.
|
|
|
|
use std::path::Path;
|
|
use std::process::{Child, Command, Stdio};
|
|
use std::time::{Duration, Instant};
|
|
|
|
use phoebus_core::system::PHOEBUS_SESSION_SCRIPT;
|
|
|
|
fn write_exec(path: &Path, body: &str) {
|
|
std::fs::write(path, body).unwrap();
|
|
use std::os::unix::fs::PermissionsExt;
|
|
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
|
|
}
|
|
|
|
/// dwl stub: creates a real unix socket at $XDG_RUNTIME_DIR/wayland-0 and exits
|
|
/// after a moment so the session ends.
|
|
const DWL_OK: &str = r#"#!/bin/sh
|
|
echo dwl-started >> "$SESSION_TEST_DIR/dwl.log"
|
|
exec python3 -c 'import os,socket,time; s=socket.socket(socket.AF_UNIX); s.bind(os.path.join(os.environ["XDG_RUNTIME_DIR"],"wayland-0")); s.listen(1); time.sleep(3)'
|
|
"#;
|
|
|
|
/// dwl stub that fails before creating the socket.
|
|
const DWL_FAIL: &str = "#!/bin/sh\nexit 1\n";
|
|
|
|
/// Helper stub: records whether the Wayland socket existed when it started,
|
|
/// stores its pid, then sleeps so cleanup has something to kill.
|
|
const HELPER: &str = r#"#!/bin/sh
|
|
name="$(basename "$0")"
|
|
if [ -S "$XDG_RUNTIME_DIR/wayland-0" ]; then s=ok; else s=missing; fi
|
|
echo "$name $s" >> "$SESSION_TEST_DIR/helpers.log"
|
|
echo $$ >> "$SESSION_TEST_DIR/helper-pids"
|
|
exec sleep 30
|
|
"#;
|
|
|
|
fn wait_timeout(child: &mut Child, secs: u64) -> std::process::ExitStatus {
|
|
let deadline = Instant::now() + Duration::from_secs(secs);
|
|
loop {
|
|
if let Some(status) = child.try_wait().unwrap() {
|
|
return status;
|
|
}
|
|
if Instant::now() >= deadline {
|
|
let _ = child.kill();
|
|
panic!("session script did not exit within {secs}s");
|
|
}
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
}
|
|
}
|
|
|
|
struct Fixture {
|
|
dir: tempfile::TempDir,
|
|
script: std::path::PathBuf,
|
|
bin: std::path::PathBuf,
|
|
runtime: std::path::PathBuf,
|
|
}
|
|
|
|
impl Fixture {
|
|
fn new(dwl_body: &str) -> Self {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let bin = dir.path().join("bin");
|
|
let runtime = dir.path().join("runtime");
|
|
std::fs::create_dir_all(&bin).unwrap();
|
|
std::fs::create_dir_all(&runtime).unwrap();
|
|
let script = dir.path().join("phoebus-session");
|
|
write_exec(&script, PHOEBUS_SESSION_SCRIPT);
|
|
write_exec(&bin.join("dwl"), dwl_body);
|
|
for helper in [
|
|
"waybar",
|
|
"mako",
|
|
"swaybg",
|
|
"polkit-gnome-authentication-agent-1",
|
|
] {
|
|
write_exec(&bin.join(helper), HELPER);
|
|
}
|
|
Self {
|
|
dir,
|
|
script,
|
|
bin,
|
|
runtime,
|
|
}
|
|
}
|
|
|
|
fn run(&self) -> (std::process::ExitStatus, String) {
|
|
let mut child = Command::new("bash")
|
|
.arg(&self.script)
|
|
.env_clear()
|
|
.env("PATH", format!("{}:/usr/bin:/bin", self.bin.display()))
|
|
.env("XDG_RUNTIME_DIR", &self.runtime)
|
|
// Skip the dbus-run-session re-exec; the session bus is not what
|
|
// this test exercises.
|
|
.env("DBUS_SESSION_BUS_ADDRESS", "unix:path=/run/test-bus")
|
|
.env("SESSION_TEST_DIR", self.dir.path())
|
|
.env("HOME", self.dir.path())
|
|
.stdout(Stdio::null())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.unwrap();
|
|
let status = wait_timeout(&mut child, 20);
|
|
let mut stderr = String::new();
|
|
use std::io::Read;
|
|
if let Some(mut e) = child.stderr.take() {
|
|
let _ = e.read_to_string(&mut stderr);
|
|
}
|
|
(status, stderr)
|
|
}
|
|
|
|
fn read(&self, name: &str) -> String {
|
|
std::fs::read_to_string(self.dir.path().join(name)).unwrap_or_default()
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn helpers_wait_for_the_socket_and_are_reaped() {
|
|
let fixture = Fixture::new(DWL_OK);
|
|
let (status, stderr) = fixture.run();
|
|
assert!(status.success(), "session failed: {stderr}");
|
|
|
|
// Every helper observed the socket: they did not race the compositor.
|
|
let log = fixture.read("helpers.log");
|
|
for name in [
|
|
"waybar",
|
|
"mako",
|
|
"swaybg",
|
|
"polkit-gnome-authentication-agent-1",
|
|
] {
|
|
assert!(log.contains(&format!("{name} ok")), "helper log: {log:?}");
|
|
}
|
|
assert!(
|
|
!log.contains("missing"),
|
|
"a helper raced the socket: {log:?}"
|
|
);
|
|
|
|
// The helpers were killed when dwl exited (logout leaves nothing behind).
|
|
let pids = fixture.read("helper-pids");
|
|
assert!(!pids.trim().is_empty(), "no helper pids recorded");
|
|
std::thread::sleep(Duration::from_millis(300));
|
|
for pid in pids.lines() {
|
|
assert!(
|
|
!Path::new(&format!("/proc/{}", pid.trim())).exists(),
|
|
"helper {pid} survived logout"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn failure_to_create_the_socket_is_reported() {
|
|
let fixture = Fixture::new(DWL_FAIL);
|
|
let (status, stderr) = fixture.run();
|
|
assert!(!status.success(), "a dead compositor must fail the session");
|
|
assert!(stderr.contains("dwl exited before creating"), "{stderr}");
|
|
assert!(fixture.read("helpers.log").is_empty());
|
|
}
|