Files

275 lines
11 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""gui_core_pkg.wifi_adb 单元测试。
覆盖最关键的解析分支(修复 get_device_ip 把网段当 IP / connect_wifi_adb
把 unauthorized 当成功 / auto_connect_wifi_adb 在 tcpip 后才取 IP 等)。
"""
from __future__ import annotations
import subprocess
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
import pytest
from gui.gui_core_pkg import wifi_adb
def _proc(stdout: str = "", stderr: str = "", returncode: int = 0):
return SimpleNamespace(stdout=stdout, stderr=stderr, returncode=returncode)
# ============================================================
# is_device_wifi_connected
# ============================================================
class TestIsDeviceWifiConnected:
def test_ip_port_format(self):
assert wifi_adb.is_device_wifi_connected("/adb", "192.168.1.5:5555") is True
def test_usb_serial(self):
assert wifi_adb.is_device_wifi_connected("/adb", "MB75DEFQZHZTGMLJ") is False
def test_empty(self):
assert wifi_adb.is_device_wifi_connected("", "") is False
# ============================================================
# get_device_ip — 关键 bug:以前会把 "192.168.5.0/24" 当 IP 返回
# ============================================================
class TestGetDeviceIp:
IP_ROUTE_NORMAL = "192.168.5.0/24 dev wlan0 proto kernel scope link src 192.168.5.2"
IP_ROUTE_MULTI = (
"default via 10.0.0.1 dev wlan0\n"
"10.0.0.0/24 dev wlan0 proto kernel scope link src 10.0.0.42\n"
"192.168.42.0/24 dev rmnet_data0 proto kernel scope link src 192.168.42.1\n"
)
IP_ADDR_NORMAL = (
"1: lo: <LOOPBACK,UP,LOWER_UP>\n"
" inet 127.0.0.1/8 scope host lo\n"
"33: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP>\n"
" inet 192.168.5.2/24 brd 192.168.5.255 scope global wlan0\n"
)
def test_extracts_src_ip_not_subnet(self):
"""关键回归:'192.168.5.0/24 ... src 192.168.5.2' 应返回 192.168.5.2 而不是 .0。"""
with patch("subprocess.run", return_value=_proc(self.IP_ROUTE_NORMAL, "", 0)):
ok, ip = wifi_adb.get_device_ip("/adb", "DEVICE")
assert ok is True
assert ip == "192.168.5.2"
def test_prefers_wlan_over_other_interfaces(self):
with patch("subprocess.run", return_value=_proc(self.IP_ROUTE_MULTI, "", 0)):
ok, ip = wifi_adb.get_device_ip("/adb", "DEVICE")
assert ok is True
assert ip == "10.0.0.42"
def test_falls_back_to_addr_show(self):
"""ip route 取不到 → ip addr show 兜底。"""
calls = {"n": 0}
def fake_run(cmd, **kwargs):
calls["n"] += 1
if "route" in cmd:
return _proc("", "", 0) # 路由空
if "addr" in cmd:
return _proc(self.IP_ADDR_NORMAL, "", 0)
return _proc("", "", 1)
with patch("subprocess.run", side_effect=fake_run):
ok, ip = wifi_adb.get_device_ip("/adb", "DEVICE")
assert ok is True
assert ip == "192.168.5.2"
def test_all_methods_fail(self):
with patch("subprocess.run", return_value=_proc("", "", 1)):
ok, ip = wifi_adb.get_device_ip("/adb", "DEVICE")
assert ok is False
def test_empty_args(self):
ok, _ = wifi_adb.get_device_ip("", "DEVICE")
assert ok is False
ok, _ = wifi_adb.get_device_ip("/adb", "")
assert ok is False
def test_timeout_returns_false(self):
with patch(
"subprocess.run",
side_effect=subprocess.TimeoutExpired(cmd="adb", timeout=10),
):
ok, msg = wifi_adb.get_device_ip("/adb", "DEVICE")
assert ok is False
assert "超时" in msg
# ============================================================
# connect_wifi_adb — 关键 bug:unauthorized / failed 也曾被认成功
# ============================================================
class TestConnectWifiAdb:
def test_success(self):
with patch(
"subprocess.run", return_value=_proc("connected to 192.168.5.2:5555", "", 0)
):
ok, msg = wifi_adb.connect_wifi_adb("/adb", "192.168.5.2")
assert ok is True
assert "192.168.5.2:5555" in msg
def test_already_connected(self):
with patch(
"subprocess.run",
return_value=_proc("already connected to 192.168.5.2:5555", "", 0),
):
ok, _ = wifi_adb.connect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is True
def test_unauthorized_returns_false_with_clear_message(self):
"""以前 'failed to authenticate' 但 returncode=0 会被当成功。"""
with patch(
"subprocess.run",
return_value=_proc("failed to authenticate to 192.168.5.2:5555", "", 0),
):
ok, msg = wifi_adb.connect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is False
assert "未授权" in msg
def test_cannot_connect(self):
with patch(
"subprocess.run",
return_value=_proc(
"cannot connect to 192.168.5.2:5555: Connection refused", "", 1
),
):
ok, msg = wifi_adb.connect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is False
assert "连接失败" in msg
def test_timeout(self):
with patch(
"subprocess.run",
side_effect=subprocess.TimeoutExpired(cmd="adb", timeout=10),
):
ok, msg = wifi_adb.connect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is False
assert "超时" in msg
def test_appends_default_port_when_missing(self):
captured = {}
def fake_run(cmd, **kwargs):
captured["cmd"] = cmd
return _proc("connected to 192.168.5.2:5555", "", 0)
with patch("subprocess.run", side_effect=fake_run):
wifi_adb.connect_wifi_adb("/adb", "192.168.5.2")
# adb connect 192.168.5.2:5555
assert captured["cmd"][-1] == "192.168.5.2:5555"
def test_default_port_failure_falls_back_to_mdns_connect_port_for_same_ip(self):
calls = []
def fake_run(cmd, **kwargs):
calls.append(cmd)
target = cmd[-1]
if target == "192.168.10.102:5555":
return _proc("cannot connect to 192.168.10.102:5555", "", 1)
if target == "192.168.10.102:45321":
return _proc("connected to 192.168.10.102:45321", "", 0)
return _proc("unexpected", "", 1)
with patch("subprocess.run", side_effect=fake_run), \
patch(
"gui.gui_core_pkg.wifi_adb._discover_mdns_connect_targets",
return_value=[("192.168.10.102", 45321)],
):
ok, msg = wifi_adb.connect_wifi_adb("/adb", "192.168.10.102")
assert ok is True
assert "192.168.10.102:45321" in msg
assert calls[0][-1] == "192.168.10.102:5555"
assert calls[1][-1] == "192.168.10.102:45321"
class TestDisconnectWifiAdb:
def test_ip_port_input_does_not_duplicate_port(self):
captured = {}
def fake_run(cmd, **kwargs):
captured["cmd"] = cmd
return _proc("disconnected 192.168.5.2:5555", "", 0)
with patch("subprocess.run", side_effect=fake_run):
ok, _ = wifi_adb.disconnect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is True
assert captured["cmd"][-1] == "192.168.5.2:5555"
# ============================================================
# auto_connect_wifi_adb — 关键 bug:以前先 tcpip 再 get_device_ip 必然失败
# ============================================================
class TestAutoConnectWifiAdb:
def test_already_connected_via_wifi(self):
with patch.object(wifi_adb, "list_adb_devices", return_value=["192.168.5.2:5555"]):
ok, msg = wifi_adb.auto_connect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is True
assert "已通过 WiFi 连接" in msg
def test_ip_port_input_directly_connects(self):
with patch.object(wifi_adb, "list_adb_devices", return_value=[]), \
patch.object(wifi_adb, "connect_wifi_adb", return_value=(True, "ok")):
ok, msg = wifi_adb.auto_connect_wifi_adb("/adb", "192.168.5.2:5555")
assert ok is True
assert "直接连接成功" in msg
def test_usb_serial_gets_ip_before_tcpip(self):
"""关键回归:USB 序列号入参时,必须先 get_device_ip 再 enable_wifi_adb。"""
order = []
def get_ip(adb, dev):
order.append("get_ip")
return True, "192.168.5.2"
def enable(adb, dev):
order.append("tcpip")
return True, "ok"
def connect(adb, target, port="5555"):
order.append("connect")
return True, "ok"
with patch.object(wifi_adb, "list_adb_devices", return_value=["MB75DEFQZHZTGMLJ"]), \
patch.object(wifi_adb, "get_device_ip", side_effect=get_ip), \
patch.object(wifi_adb, "enable_wifi_adb", side_effect=enable), \
patch.object(wifi_adb, "connect_wifi_adb", side_effect=connect), \
patch.object(wifi_adb.time, "sleep", return_value=None):
ok, msg = wifi_adb.auto_connect_wifi_adb("/adb", "MB75DEFQZHZTGMLJ")
assert ok is True
# get_ip 必须在 tcpip 之前
assert order.index("get_ip") < order.index("tcpip")
assert order.index("tcpip") < order.index("connect")
def test_get_ip_failure_returns_false_without_tcpip(self):
"""get_device_ip 失败时不应该再切 tcpip 模式。"""
with patch.object(wifi_adb, "list_adb_devices", return_value=["MB75DEFQZHZTGMLJ"]), \
patch.object(wifi_adb, "get_device_ip", return_value=(False, "no ip")), \
patch.object(wifi_adb, "enable_wifi_adb") as mock_enable:
ok, msg = wifi_adb.auto_connect_wifi_adb("/adb", "MB75DEFQZHZTGMLJ")
assert ok is False
assert "获取IP地址失败" in msg
mock_enable.assert_not_called()
def test_unauthorized_after_tcpip_returns_clear_message(self):
with patch.object(wifi_adb, "list_adb_devices", return_value=["MB75DEFQZHZTGMLJ"]), \
patch.object(wifi_adb, "get_device_ip", return_value=(True, "192.168.5.2")), \
patch.object(wifi_adb, "enable_wifi_adb", return_value=(True, "ok")), \
patch.object(
wifi_adb,
"connect_wifi_adb",
return_value=(False, "未授权: 192.168.5.2:5555 尚未授权"),
), \
patch.object(wifi_adb.time, "sleep", return_value=None):
ok, msg = wifi_adb.auto_connect_wifi_adb("/adb", "MB75DEFQZHZTGMLJ")
assert ok is False
assert "尚未授权" in msg