diff --git a/supervisor/utils/gdbus.py b/supervisor/utils/gdbus.py index e1deec431..8b10e6b28 100644 --- a/supervisor/utils/gdbus.py +++ b/supervisor/utils/gdbus.py @@ -31,6 +31,7 @@ RE_GVARIANT_STRING_ESC: re.Pattern[Any] = re.compile( RE_GVARIANT_STRING: re.Pattern[Any] = re.compile( r"(?<=(?: |{|\[|\(|<))'(.*?)'(?=(?:|]|}|,|\)|>))" ) +RE_GVARIANT_BINARY: re.Pattern[Any] = re.compile(r"<\[byte (.*?)\]>") RE_GVARIANT_TUPLE_O: re.Pattern[Any] = re.compile(r"\"[^\"\\]*(?:\\.[^\"\\]*)*\"|(\()") RE_GVARIANT_TUPLE_C: re.Pattern[Any] = re.compile( r"\"[^\"\\]*(?:\\.[^\"\\]*)*\"|(,?\))" @@ -111,8 +112,12 @@ class DBus: def parse_gvariant(raw: str) -> Any: """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()}'>", + raw, + ) json_raw = RE_GVARIANT_STRING_ESC.sub( - lambda x: x.group(0).replace('"', '\\"'), raw + lambda x: x.group(0).replace('"', '\\"'), json_raw ) json_raw = RE_GVARIANT_STRING.sub(r'"\1"', json_raw) diff --git a/tests/utils/test_gvariant_parser.py b/tests/utils/test_gvariant_parser.py index 50aabef7d..101c8bc4e 100644 --- a/tests/utils/test_gvariant_parser.py +++ b/tests/utils/test_gvariant_parser.py @@ -310,3 +310,50 @@ def test_networkmanager_dns_properties_empty(): data = DBus.parse_gvariant(raw) assert data == [{"Mode": "default", "RcManager": "resolvconf", "Configuration": []}] + + +def test_networkmanager_binary_data(): + """Test NetworkManager Binary datastrings.""" + raw = "({'802-11-wireless': {'mac-address-blacklist': <@as []>, '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-blacklist": [], + "mode": "infrastructure", + "security": "802-11-wireless-security", + "seen-bssids": ["7C:2E:BD:98:1B:06"], + "ssid": "NETT", + }, + "connection": { + "id": "NETT", + "interface-name": "wlan0", + "permissions": [], + "timestamp": 1598526799, + "type": "802-11-wireless", + "uuid": "13f9af79-a6e9-4e07-9353-165ad57bf1a8", + }, + "ipv6": { + "address-data": [], + "addresses": [], + "dns": [], + "dns-search": [], + "method": "auto", + "route-data": [], + "routes": [], + }, + "802-11-wireless-security": {"auth-alg": "open", "key-mgmt": "wpa-psk"}, + "ipv4": { + "address-data": [], + "addresses": [], + "dns": [], + "dns-search": [], + "method": "auto", + "route-data": [], + "routes": [], + }, + "proxy": {}, + } + ]