The test runs itself again under dbus-run-session and, on a zero exit, returned without looking at what the child printed. A child that ran no test at all (its --exact filter matching nothing, as after a rename of the test) also exits 0 and printed "running 0 tests ... 0 passed", so the wrapper passed having checked nothing. A zero exit now has to come with libtest's "test result: ok. 1 passed;" in the child's output, else the wrapper fails with that output. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
136 lines
4.6 KiB
Rust
136 lines
4.6 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() {
|
|
// A child that ran no test (its filter matched nothing) exits 0 as well.
|
|
assert!(
|
|
text.contains("test result: ok. 1 passed;"),
|
|
"the run on its own bus exited 0 without reporting `1 passed`:\n{text}"
|
|
);
|
|
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);
|
|
}
|