diff --git a/crates/voelin-platform/tests/proxy.rs b/crates/voelin-platform/tests/proxy.rs index 079420b..bead427 100644 --- a/crates/voelin-platform/tests/proxy.rs +++ b/crates/voelin-platform/tests/proxy.rs @@ -1,8 +1,10 @@ -//! The desktop's proxy through a stand-in xdg-desktop-portal on the -//! session bus. Run it on a private bus: -//! `dbus-run-session -- cargo test -p voelin-platform --test proxy`; -//! without a session bus it checks nothing. Its own test binary: answers -//! are kept process-wide. +//! The desktop's proxy through a stand-in xdg-desktop-portal on a session +//! bus. The test runs itself again under `dbus-run-session` on a bus of its +//! own that starts no services: on a desktop's bus the real portal is running +//! or startable, and when the stand-in leaves the bus (as the test has it do) +//! the real one is started and answers instead. Without `dbus-run-session`, +//! or a bus daemon for it, the test says so and checks nothing. Its own test +//! binary: answers are kept process-wide. #![cfg(all(unix, not(target_os = "macos"), feature = "portal"))] @@ -11,6 +13,9 @@ use std::sync::atomic::{AtomicU32, Ordering}; use voelin_platform::proxy::proxy_for; +/// Set for the run on the bus of its own. +const ON_ITS_OWN_BUS: &str = "VOELIN_PROXY_TEST_ON_ITS_OWN_BUS"; + /// What the GNOME portal answers: a SOCKS proxy for one host, none for the /// others. struct Resolver { @@ -34,10 +39,67 @@ impl Resolver { } } +/// The configuration of the bus of its own: a session bus without the service +/// directories, so that nothing on it can be started by name. Its socket is +/// made in `dir`. +fn bus_config(dir: &std::path::Path) -> String { + format!( + r#" + + session + unix:tmpdir={} + EXTERNAL + + + + + + +"#, + dir.display() + ) +} + +/// Run this test again under `dbus-run-session` on a bus of its own, and say +/// nothing was checked when that bus cannot be had. +fn on_a_bus_of_its_own() { + let dir = std::env::temp_dir(); + let config = dir.join(format!("voelin-proxy-test-bus-{}.conf", std::process::id())); + std::fs::write(&config, bus_config(&dir)).unwrap(); + let run = std::process::Command::new("dbus-run-session") + .arg(format!("--config-file={}", config.display())) + .arg("--") + .arg(std::env::current_exe().unwrap()) + .args(["--exact", "the_portal_names_the_proxy", "--nocapture", "--test-threads=1"]) + .env(ON_ITS_OWN_BUS, "1") + .output(); + let _ = std::fs::remove_file(&config); + let run = match run { + Ok(run) => run, + Err(e) => { + eprintln!("skipped, nothing was checked: dbus-run-session cannot be run ({e})"); + return; + } + }; + let text = + format!("{}{}", String::from_utf8_lossy(&run.stdout), String::from_utf8_lossy(&run.stderr)); + if run.status.success() { + return; + } + // The test ran and failed, or the bus never came up. + assert!( + !text.contains("running 1 test"), + "the test failed on its own bus ({}):\n{text}", + run.status + ); + eprintln!("skipped, nothing was checked: no bus of its own could be started:\n{text}"); +} + #[tokio::test] async fn the_portal_names_the_proxy() { - if std::env::var_os("DBUS_SESSION_BUS_ADDRESS").is_none() { - eprintln!("no session bus: run under dbus-run-session"); + if std::env::var_os(ON_ITS_OWN_BUS).is_none() { + on_a_bus_of_its_own(); return; } let lookups = Arc::new(AtomicU32::new(0));