From c471fc945c1f9538f45e8df6ac530813c44a2965 Mon Sep 17 00:00:00 2001 From: Jimi Chen Date: Thu, 6 Mar 2025 13:00:28 +0000 Subject: [PATCH] Use 'ip link' to prevent permission errors in non-root environments The 'ip link show' requires root permissions, causing errors. This change replaces 'ip link show' with 'ip link'. Bug: 401123634 Test: atest NetworkStaticLibHostPythonTests Change-Id: I82b219ae9179f23e3233f797aeb4aa3740e6d3ec --- staticlibs/tests/unit/host/python/apf_utils_test.py | 4 ++++ staticlibs/testutils/host/python/apf_utils.py | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/staticlibs/tests/unit/host/python/apf_utils_test.py b/staticlibs/tests/unit/host/python/apf_utils_test.py index 419b33895f..348df3bf83 100644 --- a/staticlibs/tests/unit/host/python/apf_utils_test.py +++ b/staticlibs/tests/unit/host/python/apf_utils_test.py @@ -103,8 +103,12 @@ IpClient.wlan1 self, mock_adb_shell: MagicMock ) -> None: mock_adb_shell.return_value = """ +45: rmnet29: mtu 1500 qdisc ... + link/ether 73:01:23:45:df:e3 brd ff:ff:ff:ff:ff:ff 46: wlan0: mtu 1500 qdisc mq ... link/ether 72:05:77:82:21:e0 brd ff:ff:ff:ff:ff:ff +47: wlan1: mtu 1500 qdisc ... + link/ether 6a:16:81:ff:82:9b brd ff:ff:ff:ff:ff:ff """ mac_address = get_hardware_address(self.mock_ad, "wlan0") asserts.assert_equal(mac_address, "72:05:77:82:21:E0") diff --git a/staticlibs/testutils/host/python/apf_utils.py b/staticlibs/testutils/host/python/apf_utils.py index 55ac860982..49ffad6129 100644 --- a/staticlibs/testutils/host/python/apf_utils.py +++ b/staticlibs/testutils/host/python/apf_utils.py @@ -162,14 +162,18 @@ def get_hardware_address( """ # Run the "ip link" command and get its output. - ip_link_output = adb_utils.adb_shell(ad, f"ip link show {iface_name}") + ip_link_output = adb_utils.adb_shell(ad, f"ip link") # Regular expression to extract the MAC address. # Parse hardware address from ip link output like below: + # 45: rmnet29: mtu 1500 qdisc ... + # link/ether 73:01:23:45:df:e3 brd ff:ff:ff:ff:ff:ff # 46: wlan0: mtu 1500 qdisc mq ... # link/ether 72:05:77:82:21:e0 brd ff:ff:ff:ff:ff:ff - pattern = r"link/ether (([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})" - match = re.search(pattern, ip_link_output) + # 47: wlan1: mtu 1500 qdisc ... + # link/ether 6a:16:81:ff:82:9b brd ff:ff:ff:ff:ff:ff" + pattern = rf"{iface_name}:.*?link/ether (([0-9a-fA-F]{{2}}:){{5}}[0-9a-fA-F]{{2}})" + match = re.search(pattern, ip_link_output, re.DOTALL) if match: return match.group(1).upper() # Extract the MAC address string.