Files
tdc-rs/scripts/upstream-test-census.py
adminandClaude Code 1a0fdbf879 feat(upstream-parity): 移植上游测试用例 —— 29 条对位用例 + 第五把尺子
**影响**
- 生产档弱密钥守卫从「只比默认值」升级为上游 `ProdGuard` 的**六条判据**(纯函数),
  弱令牌用「默认值加后缀」绕过、弱标记词、长度下限三类都拦得住;`CIPHER_KEY`(协议共享值)**不受管**。
- 出向 TDS 超时**外置到 env**(`SSW_TDS_CONNECT_TIMEOUT_MS` / `SSW_TDS_READ_TIMEOUT_MS`),
  且**只有 cause 链里真有超时**才改写文案 ⇒ 连接被拒 / DNS 失败不再被误报成「超时」(排查方向不被带偏)。
- 上游 15 个测试类全部**登记在册**:3 个已移植(29 条对位用例)、12 个判定超范围(逐条记理由)。
- 新增第五把尺子 `scripts/check-upstream-tests.sh`:台账 ⇄ 代码 ⇄ 上游测试类集合三向对账,
  上游新增测试类而台账没登记 ⇒ **红**(不靠"记得看"防漂移)。
- 回归网:`cargo test` 246 → **278**(+32),其余四把尺子与引用自检**全绿**。

**改动**
- `src/platform/guard.rs`(新):上游 `ProdGuard.violations` 的纯函数移植(弱值清单 / 弱标记词整体词匹配 / `MIN_LEN=12` / 逐条点名)。
- `src/platform/config.rs`:`check_secret_guard` 改走守卫判据;新增 `guard_values`(只放本部署真有的键)
  与出向超时两项配置(非法值回落默认,不让连接器起不来)。
- `src/tdc/client/tds.rs`:新增 `cause_chain_has_timeout` / `timeout_text_or_null`(cause 链遍历**有界** `depth<16`),
  `TdsClient::with_timeouts` 接受超时。
- `tests/upstream_guard_parity.rs`(新 · 19 条)+ `tests/upstream_tds_timeout_parity.rs`(新 · 10 条)。
- `scripts/upstream-test-ledger.tsv`(台账真源)+ `scripts/upstream-test-census.py`(上游测试类普查/对账)
  + `scripts/check-upstream-tests.sh`(第五把尺子)。
- 文档:`docs/UPSTREAM-TEST-PARITY.md`(新)+ `docs/known-limitations.md` L8 + `docs/decisions.md` D22/D23
  + `CLAUDE.md` 同步(尺子表 / 目录树 / 命令速查 / 实现细节 22–23 / 坑位表)。

**设计**
- 上游 hop 1 的「反射读 socket 工厂」**抄不了**(`reqwest::Client` 不暴露超时 getter)⇒ 改用**行为级**判别
  (同一延迟 TCP 服务器:短超时⇒超时错、长超时⇒成功)⇒ 结论等价且更强(真跑 socket 层)。
- 上游 `*SelfTest.java` **surefire 不跑**(无统一入口)⇒ 判据落成 `cargo test` 常规用例(纯函数 ⇒ 可离线断言)。
- 超范围判定原则:**本仓没有对应功能 ⇒ 不搬**(硬搬只会是假绿);「机制」已覆盖的(P2P 那条)按机制记账。

Co-Authored-By: Claude Code <[email protected]>
2026-10-27 09:04:08 +08:00

183 lines
6.7 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.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""上游(TDC / TDS / ssw-common)测试用例**普查** + 与台账对账。
用法:
python3 scripts/upstream-test-census.py # 打印普查结果(TSV)
python3 scripts/upstream-test-census.py --check # 与台账对账(有差异 ⇒ 退出码 1)
上游克隆路径(可用环境变量覆盖):
UPSTREAM_TDC_CLONE 默认 /tmp/sswtdc-probe
UPSTREAM_TDS_CLONE 默认 /tmp/sswtds-probe
台账:
scripts/upstream-test-ledger.tsv(默认)
🔴 **为什么要有它**:上游的测试用例**没有统一入口** ——
`*Test.java` 靠 surefire 跑,`*SelfTest.java`(纯 main)surefire **不跑**、
上游自己靠 `tools/run-java-selftests.sh` 人工跑 ⇒
「上游到底有哪些测试用例、我们搬没搬」这件事**必须有一份可复算的清单**,
否则上游新增一个类而我们没看见 ⇒ 静默漂移。
🔴 **克隆不在场 ⇒ 不判红**(只跳过该侧的完整性校验)——
台账里登记的类**仍然**按台账校验(PORTED 行必须真有对位用例)。
"""
import argparse
import os
import re
import sys
DEFAULT_TDC_CLONE = os.environ.get("UPSTREAM_TDC_CLONE", "/tmp/sswtdc-probe")
DEFAULT_TDS_CLONE = os.environ.get("UPSTREAM_TDS_CLONE", "/tmp/sswtds-probe")
DEFAULT_LEDGER = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "upstream-test-ledger.tsv"
)
REPO_LABEL = {"tdc": "TDC", "tds": "TDS"}
def scan_clone(root, label):
"""扫一个克隆根,返回 {类全名: (仓库标签, 相对路径, kind)}。"""
found = {}
if not os.path.isdir(root):
return found, False
present = True
for dirpath, _dirs, files in os.walk(root):
if "/src/test/java" not in dirpath.replace(os.sep, "/"):
continue
for f in files:
if not f.endswith(".java"):
continue
path = os.path.join(dirpath, f)
rel = os.path.relpath(path, root)
try:
with open(path, encoding="utf-8", errors="replace") as fh:
text = fh.read()
except OSError:
continue
m = re.search(r"^\s*package\s+([\w.]+)\s*;", text, re.M)
pkg = m.group(1) if m else "?"
cls = f[:-5]
if "@Test" in text:
kind = "JUNIT"
elif re.search(r"public\s+static\s+void\s+main", text):
kind = "SELFTEST"
else:
kind = "OTHER"
found["%s.%s" % (pkg, cls)] = (label, rel, kind)
return found, present
def read_ledger(path):
"""读台账 ⇒ {类全名: 行字典}。"""
rows = {}
if not os.path.isfile(path):
return rows
with open(path, encoding="utf-8") as fh:
for lineno, raw in enumerate(fh, 1):
line = raw.rstrip("\n")
if not line.strip() or line.lstrip().startswith("#"):
continue
parts = line.split("\t")
if len(parts) < 8:
sys.stderr.write(
"⚠️ 台账 %s:%d 列数不足 8(应为 8 列)\n" % (path, lineno)
)
continue
rows[parts[0].strip()] = {
"class": parts[0].strip(),
"repo": parts[1].strip(),
"file": parts[2].strip(),
"kind": parts[3].strip(),
"assertions": parts[4].strip(),
"counterpart": parts[5].strip(),
"status": parts[6].strip(),
"reason": parts[7].strip(),
"lineno": lineno,
}
return rows
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--tdc-clone", default=DEFAULT_TDC_CLONE)
ap.add_argument("--tds-clone", default=DEFAULT_TDS_CLONE)
ap.add_argument("--ledger", default=DEFAULT_LEDGER)
ap.add_argument("--check", action="store_true")
args = ap.parse_args()
census = {}
missing_clones = []
for root, label in ((args.tdc_clone, "TDC"), (args.tds_clone, "TDS")):
found, present = scan_clone(root, label)
if not present:
missing_clones.append("%s(%s)" % (label, root))
for cls, (repo, rel, kind) in found.items():
if cls in census:
# ssw-common 在两个克隆里都有 ⇒ 合并仓库标签
prev_repo = census[cls][0]
if prev_repo != repo:
census[cls] = ("COMMON", census[cls][1], census[cls][2])
continue
census[cls] = (repo, rel, kind)
if not args.check:
print("class\trepo\tfile\tkind")
for cls in sorted(census):
repo, rel, kind = census[cls]
print("%s\t%s\t%s\t%s" % (cls, repo, rel, kind))
return 0
ledger = read_ledger(args.ledger)
problems = []
# ① 台账里的每一行 ⇒ 上游文件必须真的在(克隆在场时)
for cls, row in sorted(ledger.items()):
root = args.tdc_clone if row["repo"] in ("TDC", "COMMON") else args.tds_clone
if not os.path.isdir(root):
continue
path = os.path.join(root, row["file"])
if not os.path.isfile(path):
problems.append(
"台账 %s:%d 引用的上游文件不存在:%s"
% (args.ledger, row["lineno"], path)
)
# ② 上游有的测试类 ⇒ 台账必须登记(克隆在场时)
if census:
for cls in sorted(census):
if cls not in ledger:
repo, rel, kind = census[cls]
problems.append(
"上游新增测试类未登记台账:%s(%s,%s)⇒ 请在 %s 补一行并判定搬/不搬"
% (cls, repo, rel, args.ledger)
)
# ③ 台账里登记了、上游却找不到了(两侧克隆都在场时)
if census and not missing_clones:
for cls in sorted(ledger):
if cls not in census:
problems.append(
"台账登记了上游已不存在的测试类:%s(台账 %s:%d)"
% (cls, args.ledger, ledger[cls]["lineno"])
)
print("普查:上游测试类 %d 个 · 台账登记 %d 个" % (len(census), len(ledger)))
if missing_clones:
print(
"⚠️ 上游克隆不在场(%s)⇒ 跳过「上游有、台账无」与「上游文件存在」两项校验"
% "、".join(missing_clones)
)
if problems:
print("❌ 对账不通过:")
for p in problems:
print(" · %s" % p)
return 1
print("✅ 对账通过:上游测试类与台账一致")
return 0
if __name__ == "__main__":
sys.exit(main())