//! 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"))] use std::sync::Arc; 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 { lookups: Arc, } #[zbus::interface(name = "org.freedesktop.portal.ProxyResolver")] impl Resolver { fn lookup(&self, uri: &str) -> Vec { self.lookups.fetch_add(1, Ordering::SeqCst); if uri.starts_with("https://capped.example") { vec!["socks://127.0.0.1:2080".into()] } else { vec!["direct://".into()] } } #[zbus(property)] fn version(&self) -> u32 { 1 } } /// 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(ON_ITS_OWN_BUS).is_none() { on_a_bus_of_its_own(); return; } let lookups = Arc::new(AtomicU32::new(0)); let portal = zbus::connection::Builder::session() .unwrap() .name("org.freedesktop.portal.Desktop") .unwrap() .serve_at("/org/freedesktop/portal/desktop", Resolver { lookups: lookups.clone() }) .unwrap() .build() .await .unwrap(); let proxy = proxy_for("https://capped.example:443/").await; assert_eq!(proxy.as_deref(), Some("socks5h://127.0.0.1:2080")); assert_eq!(proxy_for("https://other.example/").await, None); assert_eq!(lookups.load(Ordering::SeqCst), 2); // Kept: the same scheme, host and port are not asked again. let proxy = proxy_for("https://capped.example/a/b.png").await; assert_eq!(proxy.as_deref(), Some("socks5h://127.0.0.1:2080")); assert_eq!(lookups.load(Ordering::SeqCst), 2); // The portal leaves the bus: missing for a minute, not asked meanwhile. const PORTAL: &str = "org.freedesktop.portal.Desktop"; assert!(portal.release_name(PORTAL).await.unwrap()); assert_eq!(proxy_for("https://gone.example/").await, None); portal.request_name(PORTAL).await.unwrap(); assert_eq!(proxy_for("https://capped.example:8443/").await, None); assert_eq!(lookups.load(Ordering::SeqCst), 2); }