# -*- coding: utf-8 -*- r'''修因为"管理页换路径"而过时的测试。 1. test_admin_*.py 从 / 抓管理页 —— 现在 / 是官网(公开,没有 script), 管理页挪到 /zhingzhuanzhan 了。这些测试要跟着改。 2. test_pair_once_flow.py 里的"入侵者"还在用**正确的码**, 但安全边界已改成"错误的码必须被拒"。 ''' from __future__ import annotations import ast import re import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") TESTS = ROOT / "_tests" # ---------- 1. 管理页测试改用新路径 ---------- ADMIN_FILES = ("test_admin_js.py", "test_admin_batch_delete.py", "test_admin_delete.py", "test_admin_button.py", "test_admin_netcheck.py", "test_relay_admin_login.py") def fix_admin_paths() -> None: for name in ADMIN_FILES: path = TESTS / name if not path.is_file(): continue text = path.read_text(encoding="utf-8") original = text # 抓页面的地方:"/" -> "/zhingzhuanzhan" text = re.sub(r'(base\s*\+\s*)"/"', r'\1"/zhingzhuanzhan"', text) text = re.sub(r'(URL\s*=\s*[^\n]*?)"/"', r'\1"/zhingzhuanzhan"', text) text = text.replace('urlopen(base + "/")', 'urlopen(base + "/zhingzhuanzhan")') text = text.replace('get("/")', 'get("/zhingzhuanzhan")') text = text.replace("f\"{base}/\"", "f\"{base}/zhingzhuanzhan\"") text = text.replace("f'{base}/'", "f'{base}/zhingzhuanzhan'") # 登录接口:现在要账号 text = text.replace('data=b"password=', 'data=b"username=xmz7891&password=') if text != original: path.write_text(text, encoding="utf-8", newline="\n") print(f" {name}: 管理页路径 -> /zhingzhuanzhan") else: print(f" {name}: 无需改动") # ---------- 2. 入侵者用错误的码 ---------- def fix_intruder() -> None: path = TESTS / "test_pair_once_flow.py" text = path.read_text(encoding="utf-8") old = ''' intruder = Talk(PORT) bad = intruder.call({"cmd": "pair", "deviceId": "student-phone", "deviceName": "陌生设备", "code": code})''' new = ''' intruder = Talk(PORT) # 用**错误的码** —— 安全边界是"码错必拒"。 # (以前这里用正确的码也期待被拒,但行为改成 # "配对码本身就是密钥,码对就放行"了。) wrong = "000000" if code != "000000" else "111111" bad = intruder.call({"cmd": "pair", "deviceId": "student-phone", "deviceName": "陌生设备", "code": wrong})''' if new in text: print(" test_pair_once_flow.py: 已经是新版") elif old in text: path.write_text(text.replace(old, new, 1), encoding="utf-8", newline="\n") print(" test_pair_once_flow.py: 入侵者改用错误的码") else: print(" [失败] test_pair_once_flow.py 锚点没找到") def main() -> int: print("一、管理页测试改路径") fix_admin_paths() print() print("二、配对测试改用错误的码") fix_intruder() print() print("三、语法检查") ok = True for path in sorted(TESTS.glob("*.py")): try: ast.parse(path.read_text(encoding="utf-8")) except SyntaxError as exc: print(f" [失败] {path.name}: 行 {exc.lineno}: {exc.msg}") ok = False print(" 全部语法 OK" if ok else " 有语法错误") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())