685a366890
原来 test/host 一层混了三类东西:51 个主机侧单测模块目录、25 个台架驱动 与探针、若干专用目录。台架脚本平铺在顶层,与单测目录并列,看不出哪个是 「连板跑一次的」,也看不出加新脚本该往哪放。 现在按被验对象分域归档,单测模块目录一律不动(22 根 pre-commit 钩子逐个 点名它们,50 个运行器按固定层数推仓库根): - bench/bench_defaults.py 台架连接默认值单一真源 - bench/ftp/ (10) FTP 功能、速率、鉴权、中止、完整性、背靠背复现 - bench/net/ (9) CH395F socket、keepalive 判死、挂机、复连压力、PASV、 UDP 队列、链路时延对端 - bench/iec104/ (2) 主站客户端与它的离线样帧自检 - bench/wave/ (1) 录波段文件 PC 侧解码 - bench/screenlink/ (2) 屏板链路顶替与拉帧压测 顶层条目 78 -> 53。逐脚本作用、运行前置与「加新脚本往哪放」写在新建的 test/host/bench/README.md;test/host/README.md 增目录地图表,把原平铺的 网络/FTP 两节压成指针。 配套修正(移动后不修就会坏): - 17 个脚本加上 bench/ 到 sys.path 的 bootstrap(sys.path[0] 是脚本自身 目录,不补就 import bench_defaults 失败) - cal/cal_write.py 本来就坏的(只插 cal/ 到自己 sys.path,import bench_defaults 直接 ModuleNotFoundError),补两段路径后可用 - e6_soak 与 wave_read 用 dirname 固定层数推仓库根,多两层后分别指向 test/test 与 test/ci-artifacts(后者静默写错目录、不报错),层数已改对 - scripts/board_ui_nav.py 与 MDK-ARM 两个探针的 bench_defaults 搜索路径 清理与标注:删掉 test/host/iec104/(旧自研协议层单测退役后只剩一个零引用 的 stub 桩头);文档里对 test/host/iec104 与 iec104_serv 的引用加上「已 删除/已不存在 2026-11-18」状态词。冻结历史证据(带日期戳的台架日志)与 编号别名登记表里的旧路径按原样保留。
185 lines
7.6 KiB
Python
185 lines
7.6 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""FTP 鉴权台架回归(需真机在线)
|
||
|
||
验证固件侧 lftpd 鉴权集成的完整行为矩阵(主机单测 test/host/ftp_auth 覆盖
|
||
不到的"协议接线"部分:命令门、530/331/230/550 响应码、跨会话锁定):
|
||
|
||
场景「匿名登录」(anonymous) → 期望 530 拒
|
||
场景「未登录直接 NLST」 → 期望 530(命令门)
|
||
场景「对用户名 + 错口令 ×5」 → 期望 530 ×5(并触发锁定)
|
||
T4 锁定窗内正确口令 → 期望 530(LOCKED,不算哈希)
|
||
T5 等待锁定期满后正确口令 → 期望 230(60s + 余量)
|
||
T6 登录后 NLST / STOR / RETR / DELE → 期望全通(出厂 write=1)
|
||
T7 会话结束重连、错口令一次 → 期望 530 且计数已清零(T5 成功清零)
|
||
|
||
注意:CH395F 单连接——每步用完必须 quit() 释放 socket,否则下一步连不上;
|
||
跑之前确保 UI 远程显示(7003)客户端未占用 socket(对时 :8888 已退役,
|
||
SNTP 只占 UDP 通道,与 TCP 单连接语义无关)。
|
||
|
||
用法(设备默认见 bench_defaults.DEVICE_IP,出厂口令 admin/admin):
|
||
& '.venv\\Scripts\\python.exe' test/host/ftp_auth_bench.py
|
||
改过口令: ... --pwd <新口令>
|
||
跳过 60s 锁定等待(只验锁定触发不验解锁): ... --skip-unlock
|
||
|
||
退出码:0=全部通过 / 1=任一失败 / 2=环境错误(连不上设备)
|
||
创建日期:2026-10-11
|
||
"""
|
||
|
||
import os
|
||
import argparse
|
||
import io
|
||
import socket
|
||
import sys
|
||
import time
|
||
from ftplib import FTP, error_perm
|
||
|
||
# 台架连接默认值单一真源已随域归档到 test/host/bench/bench_defaults.py:
|
||
# Python 的 sys.path[0] 是**脚本自身所在目录**,本脚本在 bench/<域>/ 下直接
|
||
# `import bench_defaults` 会 ModuleNotFoundError —— 先把 bench/ 补进 sys.path。
|
||
_BENCH_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
if _BENCH_DIR not in sys.path:
|
||
sys.path.insert(0, _BENCH_DIR)
|
||
|
||
import bench_defaults # noqa: E402 (台架连接默认值单一真源)
|
||
|
||
LOCK_WAIT_S = 62 # 固件锁定 60 s + 余量
|
||
|
||
|
||
def _try_login(host, port, timeout, user, pwd):
|
||
"""尝试一次登录;返回 (ok, code_or_exc)。失败不断言连接状态。
|
||
|
||
单连接 socket:上一次会话结束后 net 层 auto_relisten ~50 ms,但 accept
|
||
失败会走"毁监听→重建"路径(3 s 重试窗)→ connect 被拒时重试 3 次。
|
||
"""
|
||
f = FTP()
|
||
last_err = None
|
||
for attempt in range(3):
|
||
try:
|
||
time.sleep(0.5)
|
||
f.connect(host, port, timeout=timeout)
|
||
break
|
||
except (ConnectionRefusedError, socket.timeout, OSError) as e:
|
||
last_err = e
|
||
f = FTP()
|
||
else:
|
||
return None, repr(last_err), None
|
||
try:
|
||
try:
|
||
f.login(user, pwd)
|
||
return True, "230", f
|
||
except error_perm as e:
|
||
f.close()
|
||
return False, str(e), None
|
||
except (socket.timeout, OSError) as e:
|
||
return None, repr(e), None
|
||
|
||
|
||
def main(argv=None):
|
||
ap = argparse.ArgumentParser(description="FTP auth bench matrix")
|
||
ap.add_argument("--host", default=bench_defaults.DEVICE_IP)
|
||
ap.add_argument("--port", type=int, default=21)
|
||
ap.add_argument("--timeout", type=int, default=15)
|
||
ap.add_argument("--user", default="admin")
|
||
ap.add_argument("--pwd", default="admin")
|
||
ap.add_argument("--skip-unlock", action="store_true",
|
||
help="不验 60s 解锁(省 1 分钟;锁定触发仍验)")
|
||
args = ap.parse_args(argv)
|
||
|
||
fails = []
|
||
|
||
def expect(name, cond, detail=""):
|
||
tag = "PASS" if cond else "FAIL"
|
||
print(" [%s] %s %s" % (tag, name, detail))
|
||
if not cond:
|
||
fails.append(name)
|
||
|
||
# ---- 场景「匿名登录被拒」----
|
||
ok, code, conn = _try_login(args.host, args.port, args.timeout, "anonymous", "x@x")
|
||
if ok is None:
|
||
print("[env] 连不上 %s:%d → %s" % (args.host, args.port, code))
|
||
return 2
|
||
expect("匿名登录 → 530", (ok is False) and code.startswith("530"), code)
|
||
if conn:
|
||
conn.quit()
|
||
|
||
# ---- 场景「未登录直接 NLST → 530(命令门)」----
|
||
time.sleep(0.5) # 单连接 socket:等 auto_relisten 沉降(~50 ms + 余量)
|
||
f = FTP()
|
||
f.connect(args.host, args.port, timeout=args.timeout)
|
||
try:
|
||
f.sendcmd("NLST")
|
||
expect("未登录 NLST → 530", False, "居然放行")
|
||
except error_perm as e:
|
||
expect("未登录 NLST → 530", str(e).startswith("530"), str(e))
|
||
except Exception as e: # noqa: BLE001
|
||
expect("未登录 NLST → 530", False, repr(e))
|
||
# ⚠ 必须 close:单连接模型下不关 = 占住 socket,后续步骤全被拒
|
||
# (2026-10-11 台架实测:QUIT 后不 close → 芯片视角旧连接仍 ESTABLISHED)
|
||
try:
|
||
f.quit()
|
||
except Exception: # noqa: BLE001
|
||
try:
|
||
f.close()
|
||
except Exception: # noqa: BLE001
|
||
pass
|
||
time.sleep(0.5)
|
||
|
||
# ---- 场景「错口令 ×5 → 530 ×5」----
|
||
for i in range(1, 6):
|
||
ok, code, conn = _try_login(args.host, args.port, args.timeout,
|
||
args.user, "wrong-pass-%d" % i)
|
||
expect("错口令 %d → 530" % i, (ok is False) and code.startswith("530"), code)
|
||
if conn:
|
||
conn.quit()
|
||
|
||
# ---- T4 锁定窗内正确口令 → 530 ----
|
||
ok, code, conn = _try_login(args.host, args.port, args.timeout, args.user, args.pwd)
|
||
expect("T4 锁定窗内对口令 → 530", (ok is False) and code.startswith("530"), code)
|
||
if conn:
|
||
conn.quit()
|
||
|
||
if args.skip_unlock:
|
||
print("[skip] 未验 60s 解锁(--skip-unlock)")
|
||
else:
|
||
# ---- T5 期满正确口令 → 230 ----
|
||
print(" [wait] 等锁定期满 %d s ..." % LOCK_WAIT_S)
|
||
time.sleep(LOCK_WAIT_S)
|
||
ok, code, conn = _try_login(args.host, args.port, args.timeout,
|
||
args.user, args.pwd)
|
||
expect("T5 期满对口令 → 230", ok is True, code)
|
||
if conn:
|
||
# ---- T6 登录后读写全通(出厂 write=1)----
|
||
try:
|
||
names = conn.nlst()
|
||
expect("T6a NLST", len(names) >= 0, "%d 项" % len(names))
|
||
payload = b"N19-auth-bench-" + b"X" * 500
|
||
conn.storbinary("STOR n19t.tmp", io.BytesIO(payload))
|
||
got = io.BytesIO()
|
||
conn.retrbinary("RETR n19t.tmp", got.write)
|
||
expect("T6b STOR/RETR 往返一致", got.getvalue() == payload)
|
||
conn.delete("n19t.tmp")
|
||
expect("T6c DELE", True)
|
||
except Exception as e: # noqa: BLE001
|
||
expect("T6 登录后读写", False, repr(e))
|
||
# ---- T7 成功已清零计数:本会话退出后错一次只算 1(不锁)----
|
||
conn.quit()
|
||
ok, code, conn = _try_login(args.host, args.port, args.timeout,
|
||
args.user, "one-wrong")
|
||
expect("T7 成功后错 1 次 → 530 且未锁(下次对口令可登)",
|
||
(ok is False) and code.startswith("530"), code)
|
||
if conn:
|
||
conn.quit()
|
||
ok, code, conn = _try_login(args.host, args.port, args.timeout,
|
||
args.user, args.pwd)
|
||
expect("T7b 紧接对口令 → 230", ok is True, code)
|
||
if conn:
|
||
conn.quit()
|
||
|
||
print("\nVERDICT: %s (%d fail)" % ("PASS" if not fails else "FAIL", len(fails)))
|
||
return 0 if not fails else 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|