bunpen: dbus_proxy: define a function to parse the DBUS_SESSION_BUS_ADDRESS
This commit is contained in:
@@ -56,11 +56,12 @@
|
||||
// - best is to use exclusively `--call=...` rules (and `--own`), no --see or --talk
|
||||
|
||||
// use config;
|
||||
// use errors;
|
||||
use errors;
|
||||
// use errors::ext;
|
||||
// use os;
|
||||
use strings;
|
||||
use os;
|
||||
// use os::exec;
|
||||
//
|
||||
//
|
||||
// // instantiate a `xdg-dbus-proxy` process,
|
||||
// // which talks to the bus on the sandboxed process's behalf,
|
||||
// // selectively forwarding traffic based on the passed policy.
|
||||
@@ -83,23 +84,64 @@
|
||||
// errors::ext::check("setup_dbus_proxy: fork", e);
|
||||
// };
|
||||
// };
|
||||
//
|
||||
// fn get_dbus_socket() (str | errors::error) = {
|
||||
// // dbus address is specified like:
|
||||
// // DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/colin/dbus/bus
|
||||
// return match (os::getenv("DBUS_SESSION_BUS_ADDRESS")) {
|
||||
// case void => yield errors::invalid;
|
||||
// case let value: str =>
|
||||
// let expected_prefix = "unix:path=";
|
||||
// if (!strings::hasprefix(value, expected_prefix)) {
|
||||
// yield errors::invalid;
|
||||
// };
|
||||
// value = strings::sub(value, len(expected_prefix));
|
||||
// if (!strings::hasprefix(value, "/")) {
|
||||
// // expect the dbus bus address to be an absolute path
|
||||
// // TODO: consider parsing this as an actual path?
|
||||
// yield errors::invalid;
|
||||
// };
|
||||
// yield value;
|
||||
// };
|
||||
// };
|
||||
|
||||
fn parse_dbus_socket(address: (str | void)) (str | errors::invalid) = {
|
||||
// dbus address is specified like:
|
||||
// DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/colin/dbus/bus
|
||||
return match (address) {
|
||||
case void => yield errors::invalid;
|
||||
case let value: str =>
|
||||
let expected_prefix = "unix:path=";
|
||||
if (!strings::hasprefix(value, expected_prefix)) {
|
||||
return errors::invalid;
|
||||
};
|
||||
value = strings::sub(value, len(expected_prefix));
|
||||
if (!strings::hasprefix(value, "/")) {
|
||||
// expect the dbus bus address to be an absolute path
|
||||
// TODO: consider parsing this as an actual path?
|
||||
return errors::invalid;
|
||||
};
|
||||
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));
|
||||
};
|
||||
|
Reference in New Issue
Block a user