bunpen: dbus_proxy: define a function to parse the DBUS_SESSION_BUS_ADDRESS

This commit is contained in:
2025-01-03 03:01:06 +00:00
parent 0b9b9a8271
commit 7c857f39e6

View File

@@ -56,11 +56,12 @@
// - best is to use exclusively `--call=...` rules (and `--own`), no --see or --talk // - best is to use exclusively `--call=...` rules (and `--own`), no --see or --talk
// use config; // use config;
// use errors; use errors;
// use errors::ext; // use errors::ext;
// use os; use strings;
use os;
// use os::exec; // use os::exec;
// //
// // instantiate a `xdg-dbus-proxy` process, // // instantiate a `xdg-dbus-proxy` process,
// // which talks to the bus on the sandboxed process's behalf, // // which talks to the bus on the sandboxed process's behalf,
// // selectively forwarding traffic based on the passed policy. // // selectively forwarding traffic based on the passed policy.
@@ -83,23 +84,64 @@
// errors::ext::check("setup_dbus_proxy: fork", e); // errors::ext::check("setup_dbus_proxy: fork", e);
// }; // };
// }; // };
//
// fn get_dbus_socket() (str | errors::error) = { fn parse_dbus_socket(address: (str | void)) (str | errors::invalid) = {
// // dbus address is specified like: // dbus address is specified like:
// // DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/colin/dbus/bus // DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/colin/dbus/bus
// return match (os::getenv("DBUS_SESSION_BUS_ADDRESS")) { return match (address) {
// case void => yield errors::invalid; case void => yield errors::invalid;
// case let value: str => case let value: str =>
// let expected_prefix = "unix:path="; let expected_prefix = "unix:path=";
// if (!strings::hasprefix(value, expected_prefix)) { if (!strings::hasprefix(value, expected_prefix)) {
// yield errors::invalid; return errors::invalid;
// }; };
// value = strings::sub(value, len(expected_prefix)); value = strings::sub(value, len(expected_prefix));
// if (!strings::hasprefix(value, "/")) { if (!strings::hasprefix(value, "/")) {
// // expect the dbus bus address to be an absolute path // expect the dbus bus address to be an absolute path
// // TODO: consider parsing this as an actual path? // TODO: consider parsing this as an actual path?
// yield errors::invalid; return errors::invalid;
// }; };
// yield value; yield value;
// }; };
// }; };
fn get_dbus_socket() (str | errors::invalid) = {
return parse_dbus_socket(os::getenv("DBUS_SESSION_BUS_ADDRESS"));
};
fn _dbus_socket_eq(value: (str | void), expect: (str | errors::invalid)) bool = {
return match (expect) {
case let s: str => yield match (parse_dbus_socket(value)) {
case let s2: str => yield s2 == s;
case => yield false;
};
case let e: errors::invalid => yield match (parse_dbus_socket(value)) {
case errors::invalid => yield true;
case => yield false;
};
};
};
@test fn get_dbus_socket_good() void = {
assert(_dbus_socket_eq("unix:path=/dbus/good", "/dbus/good"));
};
@test fn get_dbus_socket_relative_path() void = {
assert(_dbus_socket_eq("unix:path=dbus/bad", errors::invalid));
};
@test fn get_dbus_socket_empty_path() void = {
assert(_dbus_socket_eq("unix:path=", errors::invalid));
};
@test fn get_dbus_socket_missing_path() void = {
assert(_dbus_socket_eq("unix:path", errors::invalid));
};
@test fn get_dbus_socket_missing_type() void = {
assert(_dbus_socket_eq("", errors::invalid));
};
@test fn get_dbus_socket_wrong_type() void = {
assert(_dbus_socket_eq("msnt:path=/dbus/path", errors::invalid));
};
@test fn get_dbus_socket_unset() void = {
assert(_dbus_socket_eq(void, errors::invalid));
};