Files
STM32F4-Base/test/host/ftp_abort_probe.py
T
wandering1 7efa58d9a1
CI / 静态门禁 (pre-commit) (push) Failing after 25s
CI / 主机侧单测 (MSVC) (push) Successful in 48s
network: 台架 IP 迁到 192.168.31.100,并把重复写死的值收进单一真源
分两段(用户裁定 2026-11-02):先做地址迁移,再把同类字面量收敛成
单一真源,以后再改地址只动一处。

一、IP 迁移 192.168.1.100 → 192.168.31.100(gw 同步 → .31.1)
- 工厂默认:Drivers/BSP/NETCFG/net_cfg.c 的 NET_CFG_DEF_IP/_GW
- 注释/文档示例、主机与板级测试、台架 py 脚本、两份 sim_config.ini
- 字节序教学点同步换算 hex:0x6401A8C0 → 0x641FA8C0(内存
  C0 A8 1F 64),旧版示例引文与历史 Trap 记录按冻结纪律不动
- UI 黄金基准:网口设置 4 态 + shot_info 共 5 张 .bin 由
  check_content_pages.py --update 重刷(渲染值 IP 变了,属预期变更)
- 保留判定:客户点表引用(IEC104/继电保护规划)、芯片手册摘录、
  日期冻结的变更记录行、已退役台架档案 MDK-ARM/retracted_bench_*

二、单一真源收口(A+B+C+D,用户批准全做)
- 新建 test/host/bench_defaults.py:台架 py 默认值唯一定义点
  (DEVICE_IP/DEVICE_GW/PC_IP/NET_BCAST),22 个脚本改为引用;
  MDK-ARM 两探针与 scripts/board_ui_nav.py 经 sys.path shim 引入
- test/board/net_test_task.h 新增 TEST_PC_PEER_IP,板测三处
  PHASE*_REMOTE_IP 改引该宏(保留 -D 覆盖);Phase1 断言改
  memcmp 数组比较,数组各只出现一次
- simulator/tests/check_content_pages.py 从 net_cfg.c 正则解析
  NET_CFG_DEF_* 推导期望值(解析失败即报错、不静默兜底),
  脚本内台架 IP 字面量清零
- 装饰性注释去具体值(net_cfg.h/net_socket.h/net_socket.c/
  net_socket_sim.c);字节序教学 hex 对刻意保留为唯一写值点
- 刻意不动:主机单测夹具 IP(逐文件钉住的任意值是正确测试
  实践)、文档正文 IP(那是文档内容本身)

顺带:scripts/gen_task_table.py 规范串清掉「,B5」——编号清退提交
72b4b094 改了受管区没重跑生成器,task-table 门禁因此一直红

验证:产品 @build 0/0;CH395F 套件开关构建 0/0 后还原产品态;
6 套主机单测 rc=0;content_pages(含宏推导路径)/菜单 8 态/
推流位比对全 PASS;bom-drift 99 无漂移;doc-links/coding-standard/
cppcheck 绿;py_compile 23 文件 + --help 冒烟(含 shim 绝对路径)
2026-11-02 14:46:42 +08:00

140 lines
5.9 KiB
Python

# -*- coding: utf-8 -*-
# Trustworthy abort/robustness probe for the production FTP path (no firmware probes).
#
# Scenario: client starts a STOR upload, sends part of the data, then ABORTS by
# resetting the data connection (SO_LINGER 0 -> RST), which is the common real-world
# pattern (user cancels an upload, client crashes, link drops). Then it checks:
# 1. the control channel still answers (NOOP on a fresh connection),
# 2. a FOLLOW-UP normal upload+download still passes the SHA-256 check,
# 3. the device's relisten actually happened (observable via a successful follow-up).
#
# Usage: ftp_abort_probe.py [--ip ..] [--port 21] [--rounds 10] [--send 8192] [--gap 2]
# [--graceful] [--check-abort]
# Output: TS <epoch> / ABORT <n> sent=<b> ctrl=<reply> alive=<yes|no> followup=<OK|FAIL:x>
# abort5xx=<OK|FAIL|-> (only with --check-abort: RST expects 4xx/5xx on the
# aborted STOR = no false 226 (TRAP-NET-18 hardening); graceful FIN expects 2xx
# = protocol boundary, still reported as success)
# SUMMARY rounds=<n> pass=<n> fail=<n>
# Exit 0 all rounds pass, 1 otherwise.
import argparse, hashlib, io, random, re, socket, struct, sys, time
from ftplib import FTP, all_errors
import bench_defaults # 台架连接默认值单一真源
def log(m):
sys.stdout.write(m + "\n"); sys.stdout.flush()
def rd(sock):
try:
return sock.recv(4096).decode("latin1").strip()
except Exception:
return ""
def raw_login(ip, port, timeout=10.0):
s = socket.create_connection((ip, port), timeout=timeout)
rd(s) # 220 banner
# FTP auth is mandatory: anonymous login is rejected (530) -> use the factory default account
s.sendall(b"USER admin\r\n"); rd(s)
s.sendall(b"PASS admin\r\n"); rd(s)
s.sendall(b"TYPE I\r\n"); rd(s)
return s
def pasv_data_socket(ctl):
ctl.sendall(b"PASV\r\n")
r = rd(ctl)
m = re.search(r"\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)", r)
if not m:
raise RuntimeError("PASV failed: " + r)
host = ".".join(m.group(i) for i in (1, 2, 3, 4))
port = (int(m.group(5)) << 8) | int(m.group(6))
return socket.create_connection((host, port), timeout=10.0)
def followup_ok(ip, port, size=4096):
"""normal upload + download + sha256 -> True/False"""
data = bytes(random.getrandbits(8) for _ in range(size))
want = hashlib.sha256(data).hexdigest()
try:
f = FTP(); f.connect(ip, port, timeout=15.0); f.login("admin", "admin")
f.storbinary("STOR /ab_ok.bin", io.BytesIO(data))
buf = bytearray(); f.retrbinary("RETR /ab_ok.bin", buf.extend)
try:
f.delete("/ab_ok.bin")
except all_errors:
pass
f.quit()
return hashlib.sha256(bytes(buf)).hexdigest() == want
except all_errors:
return False
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--ip", default=bench_defaults.DEVICE_IP)
ap.add_argument("--port", type=int, default=21)
ap.add_argument("--rounds", type=int, default=10)
ap.add_argument("--send", type=int, default=8192, help="bytes sent before the RST")
ap.add_argument("--gap", type=float, default=2.0)
ap.add_argument("--graceful", action="store_true",
help="close the data connection with FIN instead of RST")
ap.add_argument("--check-abort", action="store_true",
help="also assert the aborted STOR's control reply: RST -> 4xx/5xx "
"(no false 226, TRAP-NET-18 hardening); graceful FIN -> 2xx (boundary)")
a = ap.parse_args()
npass = nfail = 0
for n in range(1, a.rounds + 1):
log("TS %d" % int(time.time()))
ctl_reply = "NA"
alive = "no"
try:
ctl = raw_login(a.ip, a.port)
ds = pasv_data_socket(ctl)
ctl.sendall(("STOR /ab%04d.bin\r\n" % n).encode())
ctl_reply = rd(ctl) # 150
chunk = bytes(random.getrandbits(8) for _ in range(a.send))
ds.sendall(chunk)
if a.graceful:
ds.shutdown(socket.SHUT_RDWR) # graceful FIN (client cancel)
else:
# abort: RST the data connection (linger 0)
ds.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
ds.close()
time.sleep(0.5)
ctl_reply = rd(ctl) # 426/550/226
try:
ctl.sendall(b"QUIT\r\n"); rd(ctl)
except Exception:
pass
try:
ctl.close()
except Exception:
pass
except Exception as e:
ctl_reply = "EXC:%s" % type(e).__name__
# control-channel liveness on a fresh connection
try:
c2 = raw_login(a.ip, a.port)
c2.sendall(b"NOOP\r\n")
alive = "yes" if rd(c2).startswith("2") else "no"
c2.sendall(b"QUIT\r\n"); c2.close()
except Exception:
alive = "no"
fu = followup_ok(a.ip, a.port)
abort_ok = True
if a.check_abort:
code = ctl_reply[0] if (ctl_reply and ctl_reply[0].isdigit()) else "?"
# RST: device must report 4xx/5xx on the aborted STOR (450); graceful
# FIN still gets 226 (STOR has no length declaration - protocol boundary)
abort_ok = (code == "2") if a.graceful else (code in ("4", "5"))
ok = (alive == "yes" and fu and abort_ok)
npass += 1 if ok else 0
nfail += 0 if ok else 1
log("ABORT %d sent=%d ctrl=%s alive=%s followup=%s abort5xx=%s" %
(n, a.send, ctl_reply.replace("\r\n", "|")[:40], alive,
"OK" if fu else "FAIL", "OK" if abort_ok else "FAIL" if a.check_abort else "-"))
if a.gap > 0 and n < a.rounds:
time.sleep(a.gap)
log("SUMMARY rounds=%d pass=%d fail=%d" % (a.rounds, npass, nfail))
return 0 if nfail == 0 else 1
sys.exit(main())