Network module: Wi-Fi and wired status, connect to Wi-Fi networks, Wi-Fi and airplane switches
CI / test (push) Failing after 18s
CI / test (push) Failing after 18s
- New `network` panel module (in the default panel, before the battery and the clock): signal bars and the
Wi-Fi network's name, a plug when wired, crossed-out bars offline or in airplane mode. Its
popup shows the status and the last action's outcome ("Couldn't connect to X: wrong
password?"), switches for Wi-Fi and airplane mode, and the networks in range (strongest
access point per name, the one in use first). Click a saved or open network to connect, a new
secured one to type its password in a field under it (Enter or Connect), the one in use to
disconnect, or "Forget" on a saved one. Opening the popup asks for a scan.
- compositor/network: NetworkManager over D-Bus through a small D-Bus client of our own
(wire format, EXTERNAL auth, async calls) on the existing @sys socket calls, so neither host
needed new imports. One ObjectManager.GetManagedObjects + GetAll every 2 s; GetSettings once
per saved connection; RequestScan, Device.Disconnect, WirelessEnabled / WwanEnabled,
ActivateConnection (saved), AddAndActivateConnection (new: open, WPA/WPA2 PSK, WPA3 SAE) and
Settings.Connection.Delete. Without NetworkManager it falls back to read-only status from
/proc/net/dev, /proc/net/wireless and /sys/class/net (no network names there), and looks for
NetworkManager again every 30 s.
- New `network` capability (wit/capabilities.wit): status, wifi-networks, scan, connect,
disconnect, forget, set-wifi-enabled, set-airplane-mode. Served from host tables 8/9 and a
`network(network-request)` request by the wasmer/wasmtime stub (compositor/wasm), host-wasm
and the MoonBit wasmhost. The stub's build_list learned bool fields; request strings are
wiped after use (Wi-Fi passwords).
- Running out of module instances now says "too many modules loaded".
- Deferred: VPN, hotspot, hidden networks, WEP and 802.1X (enterprise) networks, IP settings,
mobile broadband beyond the airplane switch, D-Bus signals (state is polled).
- Tests: D-Bus wire format (incl. the well-known Hello bytes), NetworkManager objects -> status
and list, security settings, /proc parsing; the module through the shell against a fake
backend (list, connect/forget/disconnect, password entry, switches, read-only); a host-wasm
test; and scripts/test-network-dbus.sh, which runs the backend against python-dbusmock's
NetworkManager on a private bus.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hizqNXcNDuef3gxa7D
This commit is contained in:
43 files changed
+4101
-32
No files matched your search
@@ -0,0 +1,388 @@
|
||||
///|
|
||||
fn hello() -> DMessage {
|
||||
DMessage::call(
|
||||
"org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "Hello", [],
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
test "a Hello call has the well-known wire bytes" {
|
||||
let b = hello().encode(1)
|
||||
// l, method call, no flags, version 1; empty body; serial 1; 109 bytes of header fields.
|
||||
let head = [
|
||||
b'l', b'\x01', b'\x00', b'\x01', b'\x00', b'\x00', b'\x00', b'\x00', b'\x01', b'\x00', b'\x00',
|
||||
b'\x00', b'\x6d', b'\x00', b'\x00', b'\x00',
|
||||
]
|
||||
for i = 0; i < head.length(); i = i + 1 {
|
||||
assert_eq(b[i], head[i])
|
||||
}
|
||||
assert_eq(b.length(), 128)
|
||||
assert_eq(message_length(b.to_array()), 128)
|
||||
assert_eq(message_length(b.to_array()[:15].to_array()), -1)
|
||||
}
|
||||
|
||||
///|
|
||||
test "calls round-trip through the wire format" {
|
||||
let args = [
|
||||
dict("a{sv}", [
|
||||
("802-11-wireless", dict("v", [("ssid", Variant(byte_array("Café")))])),
|
||||
(
|
||||
"802-11-wireless-security",
|
||||
dict("v", [("key-mgmt", Variant(Str("wpa-psk"))), ("psk", Variant(Str("hunter22")))]),
|
||||
),
|
||||
]),
|
||||
Path("/org/freedesktop/NetworkManager/Devices/3"),
|
||||
Path("/"),
|
||||
Struct([U32(100U), U32(7U)]),
|
||||
I64(-5L),
|
||||
Bool(true),
|
||||
Arr("s", []),
|
||||
]
|
||||
let m = DMessage::call(
|
||||
"org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager", "org.freedesktop.NetworkManager",
|
||||
"AddAndActivateConnection", args,
|
||||
)
|
||||
assert_eq(m.sig, "a{sa{sv}}oo(uu)xbas")
|
||||
let back = decode(m.encode(42).to_array()).unwrap()
|
||||
assert_eq(back.serial, 42)
|
||||
assert_eq(back.meth, "AddAndActivateConnection")
|
||||
assert_eq(back.dest, "org.freedesktop.NetworkManager")
|
||||
assert_eq(back.sig, m.sig)
|
||||
assert_eq(back.body, args)
|
||||
let groups = dict_map(back.body[0])
|
||||
assert_eq(dict_map(groups.get("802-11-wireless").unwrap()).get("ssid").unwrap().bytes_text(), "Café")
|
||||
assert_eq(settings_ssid(back.body[0]), "Café")
|
||||
}
|
||||
|
||||
///|
|
||||
test "malformed messages are refused" {
|
||||
let b = hello().encode(1).to_array()
|
||||
b[12] = b'\xff' // a header field array longer than the message
|
||||
assert_eq(decode(b) is None, true)
|
||||
assert_eq(decode([b'l', b'\x01']) is None, true)
|
||||
}
|
||||
|
||||
///| A NetworkManager as GetManagedObjects / GetAll would describe it: Wi-Fi connected to "Home",
|
||||
/// two more networks in range (one twice), an Ethernet device unplugged, one saved connection.
|
||||
fn sample_objects() -> (Map[String, DValue], Map[String, Map[String, Map[String, DValue]]]) {
|
||||
let nm = "/org/freedesktop/NetworkManager"
|
||||
let obj = fn(ifaces : Array[(String, Array[(String, DValue)])]) {
|
||||
Arr("{sa{sv}}", ifaces.map(i => Entry(Str(i.0), dict("v", i.1.map(p => (p.0, Variant(p.1)))))))
|
||||
}
|
||||
let ap = fn(ssid : String, strength : Int, flags : Int, wpa : Int, rsn : Int) {
|
||||
obj([
|
||||
(
|
||||
ap_iface,
|
||||
[
|
||||
("Ssid", byte_array(ssid)),
|
||||
("Strength", Byte(strength)),
|
||||
("Flags", U32(flags.reinterpret_as_uint())),
|
||||
("WpaFlags", U32(wpa.reinterpret_as_uint())),
|
||||
("RsnFlags", U32(rsn.reinterpret_as_uint())),
|
||||
],
|
||||
),
|
||||
])
|
||||
}
|
||||
let objects = Arr("{oa{sa{sv}}}", [
|
||||
Entry(
|
||||
Path(nm + "/Devices/1"),
|
||||
obj([
|
||||
(
|
||||
dev_iface,
|
||||
[
|
||||
("DeviceType", U32(1U)),
|
||||
("State", U32(20U)),
|
||||
("Interface", Str("enp3s0")),
|
||||
("ActiveConnection", Path("/")),
|
||||
],
|
||||
),
|
||||
]),
|
||||
),
|
||||
Entry(
|
||||
Path(nm + "/Devices/2"),
|
||||
obj([
|
||||
(
|
||||
dev_iface,
|
||||
[
|
||||
("DeviceType", U32(2U)),
|
||||
("State", U32(100U)),
|
||||
("StateReason", Struct([U32(100U), U32(0U)])),
|
||||
("Interface", Str("wlan0")),
|
||||
("ActiveConnection", Path(nm + "/ActiveConnection/5")),
|
||||
],
|
||||
),
|
||||
(
|
||||
wifi_iface,
|
||||
[
|
||||
("ActiveAccessPoint", Path(nm + "/AccessPoint/10")),
|
||||
(
|
||||
"AccessPoints",
|
||||
Arr("o", [
|
||||
Path(nm + "/AccessPoint/10"),
|
||||
Path(nm + "/AccessPoint/11"),
|
||||
Path(nm + "/AccessPoint/12"),
|
||||
Path(nm + "/AccessPoint/13"),
|
||||
Path(nm + "/AccessPoint/14"),
|
||||
]),
|
||||
),
|
||||
],
|
||||
),
|
||||
]),
|
||||
),
|
||||
Entry(Path(nm + "/AccessPoint/10"), ap("Home", 72, 1, 0, 0x188)),
|
||||
Entry(Path(nm + "/AccessPoint/11"), ap("Cafe", 40, 0, 0, 0)),
|
||||
Entry(Path(nm + "/AccessPoint/12"), ap("Office", 30, 1, 0, 0x188)),
|
||||
Entry(Path(nm + "/AccessPoint/13"), ap("Office", 55, 1, 0, 0x188)),
|
||||
Entry(Path(nm + "/AccessPoint/14"), ap("", 90, 1, 0, 0x188)),
|
||||
Entry(
|
||||
Path(nm + "/ActiveConnection/5"),
|
||||
obj([
|
||||
(
|
||||
active_iface,
|
||||
[
|
||||
("Id", Str("Home")),
|
||||
("Type", Str("802-11-wireless")),
|
||||
("Devices", Arr("o", [Path(nm + "/Devices/2")])),
|
||||
],
|
||||
),
|
||||
]),
|
||||
),
|
||||
Entry(Path(nm + "/Settings/1"), obj([(setting_iface, [("Unsaved", Bool(false))])])),
|
||||
])
|
||||
let root = dict("v", [
|
||||
("State", Variant(U32(70U))),
|
||||
("PrimaryConnection", Variant(Path(nm + "/ActiveConnection/5"))),
|
||||
("ActivatingConnection", Variant(Path("/"))),
|
||||
("WirelessEnabled", Variant(Bool(true))),
|
||||
("WwanEnabled", Variant(Bool(true))),
|
||||
("NetworkingEnabled", Variant(Bool(true))),
|
||||
])
|
||||
// Through the wire and back, as a reply would arrive.
|
||||
let reply = fn(v : DValue) {
|
||||
let m = { ..DMessage::call("", "", "", "", [v]), mtype: method_return, reply_serial: 3 }
|
||||
decode(m.encode(9).to_array()).unwrap().body[0]
|
||||
}
|
||||
(dict_map(reply(root)), objects_map(reply(objects)))
|
||||
}
|
||||
|
||||
///|
|
||||
test "NetworkManager's objects become the panel's status and network list" {
|
||||
let (root, objs) = sample_objects()
|
||||
let saved = { "/org/freedesktop/NetworkManager/Settings/1": "Office" }
|
||||
let v = nm_view(root, objs, saved)
|
||||
assert_eq(v.status.state, Wifi)
|
||||
assert_eq(v.status.name, "Home")
|
||||
assert_eq(v.status.signal, 72)
|
||||
assert_eq(v.status.wifi_enabled, true)
|
||||
assert_eq(v.status.airplane, false)
|
||||
assert_eq(v.status.has_wifi, true)
|
||||
assert_eq(v.wifi_dev, "/org/freedesktop/NetworkManager/Devices/2")
|
||||
assert_eq(v.wifi_state, 100)
|
||||
// The one in use first, then by signal; the strongest "Office"; no hidden network.
|
||||
assert_eq(v.networks.map(n => n.ssid), ["Home", "Office", "Cafe"])
|
||||
assert_eq(v.networks[0].active, true)
|
||||
assert_eq(v.networks[1], { ssid: "Office", signal: 55, secured: true, active: false, saved: true })
|
||||
assert_eq(v.networks[2].secured, false)
|
||||
assert_eq(v.aps.get("Office").unwrap().path, "/org/freedesktop/NetworkManager/AccessPoint/13")
|
||||
}
|
||||
|
||||
///|
|
||||
test "wired, connecting, Wi-Fi off and airplane mode" {
|
||||
let (root, objs) = sample_objects()
|
||||
let nm = "/org/freedesktop/NetworkManager"
|
||||
// Ethernet becomes the primary connection.
|
||||
let conn : Map[String, Map[String, DValue]] = {}
|
||||
conn.set(active_iface, {
|
||||
"Id": Str("Wired connection 1"),
|
||||
"Type": Str("802-3-ethernet"),
|
||||
"Devices": Arr("o", [Path(nm + "/Devices/1")]),
|
||||
})
|
||||
objs.set(nm + "/ActiveConnection/6", conn)
|
||||
root.set("PrimaryConnection", Path(nm + "/ActiveConnection/6"))
|
||||
let v = nm_view(root, objs, {})
|
||||
assert_eq(v.status.state, Wired)
|
||||
assert_eq(v.status.name, "Wired connection 1")
|
||||
assert_eq(v.status.signal, 0)
|
||||
// Nothing up yet, a connection on its way.
|
||||
root.set("PrimaryConnection", Path("/"))
|
||||
root.set("State", U32(40U))
|
||||
root.set("ActivatingConnection", Path(nm + "/ActiveConnection/6"))
|
||||
let c = nm_view(root, objs, {}).status
|
||||
assert_eq(c.state, Connecting)
|
||||
assert_eq(c.name, "Wired connection 1")
|
||||
// Wi-Fi off: no list; airplane mode is Wi-Fi and mobile broadband both off.
|
||||
root.set("WirelessEnabled", Bool(false))
|
||||
let off = nm_view(root, objs, {})
|
||||
assert_eq(off.networks.length(), 0)
|
||||
assert_eq(off.status.wifi_enabled, false)
|
||||
assert_eq(off.status.airplane, false)
|
||||
root.set("WwanEnabled", Bool(false))
|
||||
assert_eq(nm_view(root, objs, {}).status.airplane, true)
|
||||
// Networking off altogether.
|
||||
root.set("NetworkingEnabled", Bool(false))
|
||||
assert_eq(nm_view(root, objs, {}).status.state, Offline)
|
||||
}
|
||||
|
||||
///|
|
||||
test "security settings for new networks" {
|
||||
let ap = fn(flags : Int, wpa : Int, rsn : Int) {
|
||||
{ path: "/ap", ssid: "x", signal: 50, flags, wpa, rsn }
|
||||
}
|
||||
assert_eq(security_for(ap(0, 0, 0), ""), Ok(None))
|
||||
assert_eq(security_for(ap(1, 0, 0), "k"), Err("WEP networks aren't supported"))
|
||||
assert_eq(security_for(ap(1, 0, 0x200), "k"), Err("Enterprise (802.1X) networks aren't supported yet"))
|
||||
assert_eq(security_for(ap(1, 0, 0x188), ""), Err("A password is needed"))
|
||||
let psk = security_for(ap(1, 0, 0x188), "secret").unwrap().unwrap()
|
||||
assert_eq(dict_map(psk).get("key-mgmt"), Some(Str("wpa-psk")))
|
||||
assert_eq(dict_map(psk).get("psk"), Some(Str("secret")))
|
||||
let sae = security_for(ap(1, 0, 0x400), "secret").unwrap().unwrap()
|
||||
assert_eq(dict_map(sae).get("key-mgmt"), Some(Str("sae")))
|
||||
}
|
||||
|
||||
///|
|
||||
test "status from /proc/net and /sys/class/net" {
|
||||
let dev = "Inter-| Receive | Transmit\n face |bytes packets errs\n lo: 100 1 0\n eth0: 5 1 0\nwlan0: 7 1 0\ndocker0: 1 1 0\n"
|
||||
assert_eq(proc_net_dev_names(dev), ["lo", "eth0", "wlan0", "docker0"])
|
||||
let wl = "Inter-| sta-| Quality | Discarded packets\n face | tus | link level noise | nwid crypt\nwlan0: 0000 49. -61. -256 0 0\n"
|
||||
assert_eq(proc_net_wireless(wl), { "wlan0": 70 })
|
||||
let eth = { name: "eth0", wireless: false, physical: true, up: false, quality: -1 }
|
||||
let wlan = { name: "wlan0", wireless: true, physical: true, up: true, quality: 70 }
|
||||
let docker = { name: "docker0", wireless: false, physical: false, up: true, quality: -1 }
|
||||
let s = sysfs_status([eth, wlan, docker])
|
||||
assert_eq(s.state, Wifi)
|
||||
assert_eq(s.name, "wlan0")
|
||||
assert_eq(s.signal, 70)
|
||||
assert_eq(s.managed, false)
|
||||
assert_eq(s.has_wifi, true)
|
||||
assert_eq(sysfs_status([{ ..eth, up: true }, wlan]).state, Wired)
|
||||
assert_eq(sysfs_status([eth, docker]).state, Offline)
|
||||
}
|
||||
|
||||
///|
|
||||
test "action codes" {
|
||||
assert_eq(NetAction::from_code(0, "", ""), Some(Scan))
|
||||
assert_eq(NetAction::from_code(3, "", ""), Some(SetWifi(true)))
|
||||
assert_eq(NetAction::from_code(5, "", ""), Some(SetAirplane(true)))
|
||||
assert_eq(NetAction::from_code(6, "Home", "pw"), Some(Connect("Home", "pw")))
|
||||
assert_eq(NetAction::from_code(6, "", "pw"), None)
|
||||
assert_eq(NetAction::from_code(7, "Home", ""), Some(Forget("Home")))
|
||||
assert_eq(NetAction::from_code(9, "", ""), None)
|
||||
}
|
||||
|
||||
///| Call a python-dbusmock method (scripts/test-network-dbus.sh runs the mock NetworkManager).
|
||||
fn mock_call(bus : DBus, path : String, meth : String, args : Array[DValue]) -> DMessage {
|
||||
let r = bus.call_sync(DMessage::call(nm_name, path, "org.freedesktop.DBus.Mock", meth, args), 2000).unwrap()
|
||||
if r.mtype == error_reply {
|
||||
abort(meth + ": " + r.error_text())
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
///| Tick `nm` until `ok` holds (or 5 s pass).
|
||||
fn settle(nm : NmBackend, ok : (NmBackend) -> Bool) -> Bool {
|
||||
let until = @sys.monotonic_ms() + 5000UL
|
||||
while @sys.monotonic_ms() < until {
|
||||
NetBackend::tick(nm, @sys.monotonic_ms())
|
||||
if nm.inflight <= 0 && ok(nm) {
|
||||
return true
|
||||
}
|
||||
@sys.sleep_ms(20)
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///| Against a real bus: auth, Hello, framing, and the NetworkManager calls, with python-dbusmock's
|
||||
/// NetworkManager template standing in. Skipped unless CHANDRA_NM_E2E=1 (the script sets it).
|
||||
test "NetworkManager over a real bus (python-dbusmock)" {
|
||||
if @sys.getenv("CHANDRA_NM_E2E") != "1" {
|
||||
return
|
||||
}
|
||||
let bus = DBus::open(system_bus_path()).unwrap()
|
||||
let mock = "/org/freedesktop"
|
||||
let s = fn(m : DMessage) {
|
||||
match m.body {
|
||||
[Str(x), ..] => x
|
||||
_ => abort("no path")
|
||||
}
|
||||
}
|
||||
let dev = s(mock_call(bus, mock, "AddWiFiDevice", [Str("wifi0"), Str("wlan0"), I32(100)]))
|
||||
let ap1 = s(
|
||||
mock_call(bus, mock, "AddAccessPoint", [
|
||||
Str(dev),
|
||||
Str("ap1"),
|
||||
Str("Home"),
|
||||
Str("00:11:22:33:44:55"),
|
||||
U32(2U),
|
||||
U32(2437U),
|
||||
U32(54000U),
|
||||
Byte(72),
|
||||
U32(0x100U),
|
||||
]),
|
||||
)
|
||||
ignore(
|
||||
mock_call(bus, mock, "AddAccessPoint", [
|
||||
Str(dev),
|
||||
Str("ap2"),
|
||||
Str("Cafe"),
|
||||
Str("00:11:22:33:44:66"),
|
||||
U32(2U),
|
||||
U32(2412U),
|
||||
U32(54000U),
|
||||
Byte(40),
|
||||
U32(0U),
|
||||
]),
|
||||
)
|
||||
let con = s(mock_call(bus, mock, "AddWiFiConnection", [Str(dev), Str("con1"), Str("Home"), Str("wpa-psk")]))
|
||||
let act = s(
|
||||
mock_call(bus, mock, "AddActiveConnection", [Arr("s", [Str(dev)]), Str(con), Str(ap1), Str("act1"), U32(2U)]),
|
||||
)
|
||||
ignore(mock_call(bus, nm_path, "AddProperty", [Str(nm_name), Str("PrimaryConnection"), Variant(Path(act))]))
|
||||
ignore(mock_call(bus, dev, "AddProperty", [Str(wifi_iface), Str("ActiveAccessPoint"), Variant(Path(ap1))]))
|
||||
ignore(mock_call(bus, mock, "SetProperty", [Str(nm_path), Str(nm_name), Str("WwanEnabled"), Variant(Bool(true))]))
|
||||
let nm = NmBackend::connect().unwrap()
|
||||
assert_eq(
|
||||
settle(nm, n => NetBackend::status(n).state == Wifi && NetBackend::networks(n).length() == 2 && NetBackend::networks(n)[0].saved),
|
||||
true,
|
||||
)
|
||||
assert_eq(NetBackend::status(nm).name, "Home")
|
||||
assert_eq(NetBackend::status(nm).signal, 72)
|
||||
assert_eq(NetBackend::status(nm).managed, true)
|
||||
assert_eq(
|
||||
NetBackend::networks(nm),
|
||||
[
|
||||
{ ssid: "Home", signal: 72, secured: true, active: true, saved: true },
|
||||
{ ssid: "Cafe", signal: 40, secured: false, active: false, saved: false },
|
||||
],
|
||||
)
|
||||
// Wi-Fi off and on again; airplane mode.
|
||||
NetBackend::act(nm, SetWifi(false))
|
||||
assert_eq(settle(nm, n => !NetBackend::status(n).wifi_enabled && NetBackend::networks(n).length() == 0), true)
|
||||
assert_eq(NetBackend::status(nm).airplane, false)
|
||||
NetBackend::act(nm, SetAirplane(true))
|
||||
assert_eq(settle(nm, n => NetBackend::status(n).airplane), true)
|
||||
NetBackend::act(nm, SetAirplane(false))
|
||||
assert_eq(settle(nm, n => NetBackend::status(n).wifi_enabled && !NetBackend::status(n).airplane), true)
|
||||
// A new open network: AddAndActivateConnection makes a saved connection for it.
|
||||
NetBackend::act(nm, Scan)
|
||||
NetBackend::act(nm, Connect("Cafe", ""))
|
||||
assert_eq(settle(nm, n => NetBackend::networks(n).iter().any(w => w.ssid == "Cafe" && w.saved)), true)
|
||||
// A secured one with no password is refused here, before NetworkManager.
|
||||
ignore(mock_call(bus, mock, "AddAccessPoint", [
|
||||
Str(dev),
|
||||
Str("ap3"),
|
||||
Str("Office"),
|
||||
Str("00:11:22:33:44:77"),
|
||||
U32(2U),
|
||||
U32(5180U),
|
||||
U32(54000U),
|
||||
Byte(30),
|
||||
U32(0x100U),
|
||||
]))
|
||||
assert_eq(settle(nm, n => NetBackend::networks(n).length() == 3), true)
|
||||
NetBackend::act(nm, Connect("Office", ""))
|
||||
assert_eq(NetBackend::status(nm).message, "A password is needed")
|
||||
// Forget deletes the saved connection.
|
||||
NetBackend::act(nm, Forget("Cafe"))
|
||||
assert_eq(settle(nm, n => NetBackend::networks(n).iter().any(w => w.ssid == "Cafe" && !w.saved)), true)
|
||||
bus.close()
|
||||
}
|
||||
Reference in new issue
Block a user