From a2821a98adb85225792780e10e09e9496a85a7c9 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Thu, 27 Aug 2020 17:04:14 +0200 Subject: [PATCH] Fix gvar for dbus if binary is not a string (#1984) --- supervisor/utils/gdbus.py | 13 ++++++++- tests/utils/test_gvariant_parser.py | 45 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/supervisor/utils/gdbus.py b/supervisor/utils/gdbus.py index 8b10e6b28..a5456542b 100644 --- a/supervisor/utils/gdbus.py +++ b/supervisor/utils/gdbus.py @@ -58,6 +58,17 @@ MONITOR: str = "gdbus monitor --system --dest {bus}" DBUS_METHOD_GETALL: str = "org.freedesktop.DBus.Properties.GetAll" +def _convert_bytes(value: str) -> str: + """Convert bytes to string or byte-array.""" + data: bytes = bytes(int(char, 0) for char in value.split(", ")) + try: + text = data.decode() + except UnicodeDecodeError: + return f"<[{', '.join(str(char) for char in data)}]>" + + return f"<'{text}'>" + + class DBus: """DBus handler.""" @@ -113,7 +124,7 @@ class DBus: """Parse GVariant input to python.""" # Process first string json_raw = RE_GVARIANT_BINARY.sub( - lambda x: f"<'{bytes([int(char, 0) for char in x.group(1).split(', ')]).decode()}'>", + lambda x: _convert_bytes(x.group(1)), raw, ) json_raw = RE_GVARIANT_STRING_ESC.sub( diff --git a/tests/utils/test_gvariant_parser.py b/tests/utils/test_gvariant_parser.py index 101c8bc4e..e3122c884 100644 --- a/tests/utils/test_gvariant_parser.py +++ b/tests/utils/test_gvariant_parser.py @@ -357,3 +357,48 @@ def test_networkmanager_binary_data(): "proxy": {}, } ] + + raw = "({'802-11-wireless': {'mac-address-blacklist': <@as []>, 'mac-address': <[byte 0xca, 0x0b, 0x61, 0x00, 0xd8, 0xbd]>, 'mode': <'infrastructure'>, 'security': <'802-11-wireless-security'>, 'seen-bssids': <['7C:2E:BD:98:1B:06']>, 'ssid': <[byte 0x4e, 0x45, 0x54, 0x54]>}, 'connection': {'id': <'NETT'>, 'interface-name': <'wlan0'>, 'permissions': <@as []>, 'timestamp': , 'type': <'802-11-wireless'>, 'uuid': <'13f9af79-a6e9-4e07-9353-165ad57bf1a8'>}, 'ipv6': {'address-data': <@aa{sv} []>, 'addresses': <@a(ayuay) []>, 'dns': <@aay []>, 'dns-search': <@as []>, 'method': <'auto'>, 'route-data': <@aa{sv} []>, 'routes': <@a(ayuayu) []>}, '802-11-wireless-security': {'auth-alg': <'open'>, 'key-mgmt': <'wpa-psk'>}, 'ipv4': {'address-data': <@aa{sv} []>, 'addresses': <@aau []>, 'dns': <@au []>, 'dns-search': <@as []>, 'method': <'auto'>, 'route-data': <@aa{sv} []>, 'routes': <@aau []>}, 'proxy': {}},)" + + data = DBus.parse_gvariant(raw) + + assert data == [ + { + "802-11-wireless": { + "mac-address": [202, 11, 97, 0, 216, 189], + "mac-address-blacklist": [], + "mode": "infrastructure", + "security": "802-11-wireless-security", + "seen-bssids": ["7C:2E:BD:98:1B:06"], + "ssid": "NETT", + }, + "802-11-wireless-security": {"auth-alg": "open", "key-mgmt": "wpa-psk"}, + "connection": { + "id": "NETT", + "interface-name": "wlan0", + "permissions": [], + "timestamp": 1598526799, + "type": "802-11-wireless", + "uuid": "13f9af79-a6e9-4e07-9353-165ad57bf1a8", + }, + "ipv4": { + "address-data": [], + "addresses": [], + "dns": [], + "dns-search": [], + "method": "auto", + "route-data": [], + "routes": [], + }, + "ipv6": { + "address-data": [], + "addresses": [], + "dns": [], + "dns-search": [], + "method": "auto", + "route-data": [], + "routes": [], + }, + "proxy": {}, + } + ]