Files
faumarayandClaude Sonnet 5.5 c35bbb5ba8 Run the portal proxy test on a bus of its own, or say it checked nothing
voelin-platform's tests/proxy.rs says to run it under dbus-run-session, and
without DBUS_SESSION_BUS_ADDRESS it returned at once. With one set it ran on
the desktop's bus and failed at "the portal leaves the bus": the test has its
stand-in release org.freedesktop.portal.Desktop, and the first call afterwards
starts the real xdg-desktop-portal, which the bus can start by name; it
answers instead of the "missing" the test expects for a minute. A private bus
from plain dbus-run-session starts it the same way (the test failed the same
three times under it here), so the documented way did not work on a desktop
that has the portal installed.

The test now runs itself again under dbus-run-session with a bus configuration
of its own, a session bus without service directories, so that nothing on it
can be started by name. Without dbus-run-session, or when the bus does not
come up, it says "skipped, nothing was checked" with the reason and passes;
when the test ran on its bus and failed, it fails with the output. The
stand-in and the assertions are the ones that were there.

On the bus with the default services the same inner test still fails as
before (checked by hand), and on the bus of its own it passes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-24 14:32:53 +03:00

131 lines
4.4 KiB
Rust

//! 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<AtomicU32>,
}
#[zbus::interface(name = "org.freedesktop.portal.ProxyResolver")]
impl Resolver {
fn lookup(&self, uri: &str) -> Vec<String> {
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#"<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<type>session</type>
<listen>unix:tmpdir={}</listen>
<auth>EXTERNAL</auth>
<policy context="default">
<allow send_destination="*" eavesdrop="true"/>
<allow eavesdrop="true"/>
<allow own="*"/>
</policy>
</busconfig>
"#,
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);
}