Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wifi netplan gateway parser (Bugfix) #1536

Merged
merged 7 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions providers/base/bin/wifi_client_test_netplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import time
import shutil
import sys
import ipaddress
import yaml

from gateway_ping_test import ping
Expand Down Expand Up @@ -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
seankingyang marked this conversation as resolved.
Show resolved Hide resolved


def get_gateway(interface, renderer):
gateway = None
info = get_interface_info(interface, renderer)
gateway = info.get("gateway", None)
gateway = info.get("gateway")
seankingyang marked this conversation as resolved.
Show resolved Hide resolved
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):
Expand Down
44 changes: 42 additions & 2 deletions providers/base/tests/test_wifi_client_test_netplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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={},
Expand All @@ -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",
Expand Down
Loading