//! Functional test of the generated `phoebus-session-mango` script. //! //! Runs the real script against stub `mango`/`dms` binaries so the startup //! ordering, the Wayland-socket discovery, DMS launch, 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_MANGO_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(); } /// mango stub: creates a real unix socket at $XDG_RUNTIME_DIR/wayland-0 and exits /// after a moment so the session ends. const MANGO_OK: &str = r#"#!/bin/sh echo mango-started >> "$SESSION_TEST_DIR/mango.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)' "#; /// mango stub that fails before creating the socket. const MANGO_FAIL: &str = "#!/bin/sh\nexit 1\n"; /// DMS stub: records whether the Wayland socket existed when it started, /// stores its pid, then sleeps so cleanup has something to kill. const DMS_STUB: &str = r#"#!/bin/sh if [ -S "$XDG_RUNTIME_DIR/wayland-0" ]; then s=ok; else s=missing; fi echo "dms $s wayland_display=${WAYLAND_DISPLAY:-missing}" >> "$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(mango_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-mango"); write_exec(&script, PHOEBUS_MANGO_SESSION_SCRIPT); write_exec(&bin.join("mango"), mango_body); write_exec(&bin.join("dms"), DMS_STUB); 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(MANGO_OK); let (status, stderr) = fixture.run(); assert!(status.success(), "session failed: {stderr}"); // Mango started and DMS observed the socket and exported WAYLAND_DISPLAY. let mango_log = fixture.read("mango.log"); assert!(mango_log.contains("mango-started")); let log = fixture.read("helpers.log"); assert!(log.contains("dms ok"), "helper log: {log:?}"); assert!( log.contains("wayland_display=wayland-0"), "helper log: {log:?}" ); assert!( !log.contains("missing"), "a helper raced the socket: {log:?}" ); // The helpers were killed when mango 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(MANGO_FAIL); let (status, stderr) = fixture.run(); assert!(!status.success(), "a dead compositor must fail the session"); assert!( stderr.contains("mango exited before creating a Wayland socket"), "{stderr}" ); assert!(fixture.read("helpers.log").is_empty()); }