Skip to content

Commit 0a57af5

Browse files
committed
state/SSID: handle non-ascii SSIDs
This workaround is intended to properly decode non-ascii SSIDs returned by networkctl. In such a case, a sequence of bytes (in octal notation) will be returned and we were displaying this string in netplan status.
1 parent ad3757f commit 0a57af5

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

netplan_cli/cli/state.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,18 @@ def ssid(self) -> str:
350350
if self.type == 'wifi':
351351
# XXX: available from networkctl's JSON output as of v250:
352352
# https://github.com/systemd/systemd/commit/da7c995
353+
# TODO: Retrieving the SSID from systemd seems to not be reliable.
354+
# Sometimes it will return "(null)".
353355
for line in self._networkctl.splitlines():
354356
line = line.strip()
355357
key = r'^Wi-?Fi access point: (.*) \(.*\)'
356358
if match := re.match(key, line):
357359
ssid = match.group(1)
360+
# TODO: Find a better way to retrieve the SSID
361+
# networkctl will return a non-ascii SSID using the octal notation below:
362+
# '\\303\\241\\303\\251\\303\\255\\303\\263\\303...
363+
# Here we handle the escaping, the encoding of individual bytes and the final decoding to utf-8
364+
ssid = ssid.encode('latin1').decode('unicode-escape').encode('latin1').decode('utf-8')
358365
return ssid if ssid else None
359366
return None
360367

tests/cli/test_state.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,22 +440,23 @@ def test_json_nm_wlan0_2(self, networkctl_mock, nm_ssid_mock):
440440

441441
@patch('netplan_cli.cli.state.Interface.query_nm_ssid')
442442
@patch('netplan_cli.cli.state.Interface.query_networkctl')
443-
def test_json_nm_wlan1(self, networkctl_mock, nm_ssid_mock):
444-
SSID = 'áéíóúççção€€€'
445-
nm_ssid_mock.return_value = SSID
443+
def test_json_nm_wlan1_non_ascii(self, networkctl_mock, nm_ssid_mock):
444+
ND_SSID = '\\303\\241\\303\\251\\303\\255\\303\\263\\303\\272\\302\\242\\302\\242\\302\\242\\302\\243\\302\\243\\302\\243'
445+
NM_SSID = 'áéíóú¢¢¢£££'
446+
nm_ssid_mock.return_value = NM_SSID
446447
# networkctl mock output reduced to relevant lines
447448
# Newer versions of systemd changed WiFi to Wi-Fi
448449
# See systemd commit 8fff78a1dded105e1ee87bc66e29ef2fd61bf8c9
449450
networkctl_mock.return_value = \
450-
'Wi-Fi access point: {} (b4:fb:e4:75:c6:21)'.format(SSID)
451+
'Wi-Fi access point: {} (b4:fb:e4:75:c6:21)'.format(ND_SSID)
451452

452453
data = {'ifname': 'wlan1', 'ifindex': 123}
453454
nd = [{'Index': 123, 'Type': 'wlan', 'Name': 'wlan1'}]
454455
nm = SystemConfigState.process_nm(NMCLI)
455456

456457
itf = Interface(data, nd, nm, (None, None), (None, None))
457458
_, json = itf.json()
458-
self.assertEqual(json.get('ssid'), SSID)
459+
self.assertEqual(json.get('ssid'), NM_SSID)
459460

460461
@patch('netplan_cli.cli.state.Interface.query_networkctl')
461462
def test_json_nd_enp0s31f6(self, networkctl_mock):

0 commit comments

Comments
 (0)