Files
douyin_guaji-main/tests/test_wifi_adb.py
T
CodexandClaude Opus 4.7 23d370ae22 fix(wifi_adb): 修 3 处导致 WiFi-ADB 自动连接失败的解析 bug
1) get_device_ip 把 ip route 输出里的网段当 IP 返回
   原代码看到 "192.168.5.0/24 ... src 192.168.5.2" 时,扫到的第一个含 3 个点的字段是
   "192.168.5.0/24",结果用 0/24 当 IP 调 adb connect 必然失败。
   改成显式从 "src <ip>" 取,并优先 wlan 接口;加 ip addr show / ifconfig 兜底。

2) auto_connect_wifi_adb 顺序错误
   原代码先 tcpip 5555(让设备 adbd 重启、USB 链路断开)再 get_device_ip(需要 USB),
   USB 已断必然取不到 IP。改成先 get_device_ip 再 enable_wifi_adb 再 connect。

3) connect_wifi_adb 把"未授权 / 连接被拒"当成功
   adb connect 在 unauthorized / cannot connect / failed to authenticate 等场景下
   returncode 也是 0;以前只看 returncode + "connected" 子串,未授权也判成功。
   改成显式列出失败关键词,并对未授权给清晰的中文提示。

新增 20 个 wifi_adb 单元测试覆盖 3 处回归场景。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 19:32:04 +08:00

237 lines
9.2 KiB
Python
Raw 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"
# ============================================================
# 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