diff --git a/providers/base/bin/wifi_client_test_netplan.py b/providers/base/bin/wifi_client_test_netplan.py index 7ec0f5fd6..2a1861a7a 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,28 @@ 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_address = gateway.split()[0] # Get the first part before any space + try: + ipaddress.ip_address(ip_address) + return ip_address + except ValueError: + return "" + + def get_gateway(interface, renderer): - gateway = None info = get_interface_info(interface, renderer) - gateway = info.get("gateway", None) + gateway = info.get("gateway") or "" + validated_gateway = _validate_gateway_ip(gateway) print("Got gateway address: {}".format(gateway)) - return gateway + print("Validated gateway address: {}".format(validated_gateway)) + return validated_gateway def perform_ping_test(interface, renderer): diff --git a/providers/base/tests/test_wifi_client_test_netplan.py b/providers/base/tests/test_wifi_client_test_netplan.py index d492c627a..60d28b35f 100644 --- a/providers/base/tests/test_wifi_client_test_netplan.py +++ b/providers/base/tests/test_wifi_client_test_netplan.py @@ -444,6 +444,18 @@ def test_get_interface_info_networkd(self, mock_check_output): 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)\n" + b"Path: 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): mock_check_output.return_value = ( @@ -665,6 +677,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 +701,22 @@ 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", + 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",