From 7efe00f0226c859146a2328562a4b7b8e3cbf174 Mon Sep 17 00:00:00 2001 From: isaacyang Date: Wed, 9 Oct 2024 11:42:54 +0800 Subject: [PATCH 1/7] Fix get_getway can correct parse the ip only --- .../base/bin/wifi_client_test_netplan.py | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/providers/base/bin/wifi_client_test_netplan.py b/providers/base/bin/wifi_client_test_netplan.py index 7ec0f5fd6..e29d33a1e 100755 --- a/providers/base/bin/wifi_client_test_netplan.py +++ b/providers/base/bin/wifi_client_test_netplan.py @@ -22,6 +22,7 @@ import time import shutil import sys +import ipaddress import yaml from gateway_ping_test import ping @@ -315,12 +316,32 @@ def print_route_info(): print() +def _validate_gateway_ip(gateway): + if not gateway: + return "" + # Check if the gateway is a valid IP address + # Examples: + # 192.168.144.1 (TP-Link 123) + # 192.168.144.1 + ip_part = gateway.split()[0] # Get the first part before any space + return ip_part if __validate_ip(ip_part) else "" + + +def __validate_ip(ip): + try: + ipaddress.ip_address(ip) + return True + except ValueError: + return False + + def get_gateway(interface, renderer): - gateway = None info = get_interface_info(interface, renderer) - gateway = info.get("gateway", None) - print("Got gateway address: {}".format(gateway)) - return gateway + gateway = info.get("gateway") + validated_gateway = _validate_gateway_ip(gateway) + print(f"Got gateway address: {gateway}") + print(f"Validated gateway address: {validated_gateway}") + return validated_gateway def perform_ping_test(interface, renderer): @@ -492,4 +513,4 @@ def main(): # Check if executed with root if os.geteuid() != 0: raise SystemExit("Error: please run this command as root") - main() + main() \ No newline at end of file From 3b28836381214a00adba4c78c80c2047bd206f54 Mon Sep 17 00:00:00 2001 From: isaacyang Date: Wed, 9 Oct 2024 11:43:23 +0800 Subject: [PATCH 2/7] Add unit test --- .../tests/test_wifi_client_test_netplan.py | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/providers/base/tests/test_wifi_client_test_netplan.py b/providers/base/tests/test_wifi_client_test_netplan.py index d492c627a..1def6ade8 100644 --- a/providers/base/tests/test_wifi_client_test_netplan.py +++ b/providers/base/tests/test_wifi_client_test_netplan.py @@ -443,6 +443,17 @@ def test_get_interface_info_networkd(self, mock_check_output): info = get_interface_info(interface, renderer) self.assertEqual(info["state"], "routable") self.assertEqual(info["gateway"], "192.168.1.1") + + @patch("subprocess.check_output") + def test_get_interface_info_networkd_any_name(self, mock_check_output): + mock_check_output.return_value = ( + b"State: routable\nGateway: 192.168.1.1 (TP-Link 123)\nPath: pci-0000:02:00.0" + ) + interface = "wlan0" + renderer = "networkd" + info = get_interface_info(interface, renderer) + self.assertEqual(info["state"], "routable") + self.assertEqual(info["gateway"], "192.168.1.1 (TP-Link 123)") @patch("subprocess.check_output") def test_get_interface_info_networkd_no_state(self, mock_check_output): @@ -665,6 +676,21 @@ def test_gateway_found(self, mock_get_interface_info): "Got gateway address: 192.168.1.1", captured_output.getvalue() ) + @patch( + "wifi_client_test_netplan.get_interface_info", + return_value={"gateway": "192.168.1.1 (TP-Link 123)"}, + ) + def test_gateway_found_any_name(self, mock_get_interface_info): + captured_output = io.StringIO() + sys.stdout = captured_output + result = get_gateway("wlan0", "networkd") + sys.stdout = sys.__stdout__ + self.assertEqual(result, "192.168.1.1") + mock_get_interface_info.assert_called_once_with("wlan0", "networkd") + self.assertIn( + "Got gateway address: 192.168.1.1", captured_output.getvalue() + ) + @patch( "wifi_client_test_netplan.get_interface_info", return_value={}, @@ -674,9 +700,9 @@ def test_gateway_not_found(self, mock_get_interface_info): sys.stdout = captured_output result = get_gateway("wlan0", "networkd") sys.stdout = sys.__stdout__ - self.assertIsNone(result) + self.assertEqual(result, "") mock_get_interface_info.assert_called_once_with("wlan0", "networkd") - self.assertIn("Got gateway address: None", captured_output.getvalue()) + self.assertIn("Got gateway address: ", captured_output.getvalue()) @patch( "wifi_client_test_netplan.get_interface_info", From 382fb86c7e3dfb9248aa386a3b0bb655b5606347 Mon Sep 17 00:00:00 2001 From: isaacyang Date: Wed, 9 Oct 2024 11:53:55 +0800 Subject: [PATCH 3/7] Fix Black format --- providers/base/bin/wifi_client_test_netplan.py | 2 +- providers/base/tests/test_wifi_client_test_netplan.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/providers/base/bin/wifi_client_test_netplan.py b/providers/base/bin/wifi_client_test_netplan.py index e29d33a1e..8ed5b6c8f 100755 --- a/providers/base/bin/wifi_client_test_netplan.py +++ b/providers/base/bin/wifi_client_test_netplan.py @@ -513,4 +513,4 @@ def main(): # Check if executed with root if os.geteuid() != 0: raise SystemExit("Error: please run this command as root") - main() \ No newline at end of file + main() diff --git a/providers/base/tests/test_wifi_client_test_netplan.py b/providers/base/tests/test_wifi_client_test_netplan.py index 1def6ade8..cef31fab8 100644 --- a/providers/base/tests/test_wifi_client_test_netplan.py +++ b/providers/base/tests/test_wifi_client_test_netplan.py @@ -443,11 +443,12 @@ def test_get_interface_info_networkd(self, mock_check_output): info = get_interface_info(interface, renderer) self.assertEqual(info["state"], "routable") self.assertEqual(info["gateway"], "192.168.1.1") - + @patch("subprocess.check_output") def test_get_interface_info_networkd_any_name(self, mock_check_output): mock_check_output.return_value = ( - b"State: routable\nGateway: 192.168.1.1 (TP-Link 123)\nPath: pci-0000:02:00.0" + b"State: routable\nGateway: 192.168.1.1 (TP-Link 123)\n" + b"Path: pci-0000:02:00.0" ) interface = "wlan0" renderer = "networkd" From a12746e509031d1c9376381640464b2085aa2b8f Mon Sep 17 00:00:00 2001 From: isaacyang Date: Wed, 9 Oct 2024 13:04:24 +0800 Subject: [PATCH 4/7] Add invalid ip unit test --- .../base/tests/test_wifi_client_test_netplan.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/providers/base/tests/test_wifi_client_test_netplan.py b/providers/base/tests/test_wifi_client_test_netplan.py index cef31fab8..60d28b35f 100644 --- a/providers/base/tests/test_wifi_client_test_netplan.py +++ b/providers/base/tests/test_wifi_client_test_netplan.py @@ -705,6 +705,19 @@ def test_gateway_not_found(self, mock_get_interface_info): mock_get_interface_info.assert_called_once_with("wlan0", "networkd") self.assertIn("Got gateway address: ", captured_output.getvalue()) + @patch( + "wifi_client_test_netplan.get_interface_info", + return_value={"gateway": "192.168.1.1.100 (TP-Link 123)"}, + ) + def test_gateway_ip_not_valid(self, mock_get_interface_info): + captured_output = io.StringIO() + sys.stdout = captured_output + result = get_gateway("wlan0", "networkd") + sys.stdout = sys.__stdout__ + self.assertEqual(result, "") + mock_get_interface_info.assert_called_once_with("wlan0", "networkd") + self.assertIn("Got gateway address: ", captured_output.getvalue()) + @patch( "wifi_client_test_netplan.get_interface_info", return_value={"gateway": ""}, From 03dd9102d72880a91e37971416a278af866af9d0 Mon Sep 17 00:00:00 2001 From: isaacyang Date: Wed, 9 Oct 2024 13:08:49 +0800 Subject: [PATCH 5/7] Fix flask8 format --- providers/base/bin/wifi_client_test_netplan.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/providers/base/bin/wifi_client_test_netplan.py b/providers/base/bin/wifi_client_test_netplan.py index 8ed5b6c8f..28e4cb503 100755 --- a/providers/base/bin/wifi_client_test_netplan.py +++ b/providers/base/bin/wifi_client_test_netplan.py @@ -339,8 +339,8 @@ def get_gateway(interface, renderer): info = get_interface_info(interface, renderer) gateway = info.get("gateway") validated_gateway = _validate_gateway_ip(gateway) - print(f"Got gateway address: {gateway}") - print(f"Validated gateway address: {validated_gateway}") + print("Got gateway address: {}".format(gateway)) + print("Validated gateway address: {}".format(validated_gateway)) return validated_gateway From f9d1280fa963118e8a423d28ac0686b1eb55b25b Mon Sep 17 00:00:00 2001 From: Isaac Yang <47034756+seankingyang@users.noreply.github.com> Date: Mon, 14 Oct 2024 18:00:02 +0800 Subject: [PATCH 6/7] Update providers/base/bin/wifi_client_test_netplan.py Co-authored-by: Massimiliano --- providers/base/bin/wifi_client_test_netplan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/base/bin/wifi_client_test_netplan.py b/providers/base/bin/wifi_client_test_netplan.py index 28e4cb503..e8d5b4fd1 100755 --- a/providers/base/bin/wifi_client_test_netplan.py +++ b/providers/base/bin/wifi_client_test_netplan.py @@ -337,7 +337,7 @@ def __validate_ip(ip): def get_gateway(interface, renderer): info = get_interface_info(interface, renderer) - gateway = info.get("gateway") + gateway = info.get("gateway") or "" validated_gateway = _validate_gateway_ip(gateway) print("Got gateway address: {}".format(gateway)) print("Validated gateway address: {}".format(validated_gateway)) From b691e529aa1e81b051a3d0a98a2e865a128cf6b7 Mon Sep 17 00:00:00 2001 From: isaacyang Date: Mon, 14 Oct 2024 18:10:25 +0800 Subject: [PATCH 7/7] Squash the _validate_gateway_ip and __validate_ip function --- providers/base/bin/wifi_client_test_netplan.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/providers/base/bin/wifi_client_test_netplan.py b/providers/base/bin/wifi_client_test_netplan.py index e8d5b4fd1..2a1861a7a 100755 --- a/providers/base/bin/wifi_client_test_netplan.py +++ b/providers/base/bin/wifi_client_test_netplan.py @@ -323,16 +323,12 @@ def _validate_gateway_ip(gateway): # Examples: # 192.168.144.1 (TP-Link 123) # 192.168.144.1 - ip_part = gateway.split()[0] # Get the first part before any space - return ip_part if __validate_ip(ip_part) else "" - - -def __validate_ip(ip): + ip_address = gateway.split()[0] # Get the first part before any space try: - ipaddress.ip_address(ip) - return True + ipaddress.ip_address(ip_address) + return ip_address except ValueError: - return False + return "" def get_gateway(interface, renderer):