**影响** - 生产档弱密钥守卫从「只比默认值」升级为上游 `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]>
157 lines
6.3 KiB
Bash
157 lines
6.3 KiB
Bash
#!/usr/bin/env bash
|
||
# 上游测试用例**移植台账**自检(第五把尺子 · 上游对齐向)
|
||
#
|
||
# 用法:
|
||
# bash scripts/check-upstream-tests.sh # 自检(绿 = 0)
|
||
# bash scripts/check-upstream-tests.sh --report # 只出报告,不判红(排障用)
|
||
#
|
||
# 退出码:0 = 绿 / 1 = 有失败 / 2 = 前置错
|
||
#
|
||
# 🔴 **纯静态**:不起后端、不连库、不跑 cargo(秒级)⇒ 可进任何一道闸门。
|
||
# (对位用例**真的跑不跑**由 `cargo test` 负责;本尺子只管「台账与代码**对得上**」)
|
||
#
|
||
# 检查项:
|
||
# ① 台账每行 8 列、status 合法、reason 非空
|
||
# ② PORTED 行的对位文件**存在**、每个登记的用例函数**真在**那个文件里
|
||
# ③ OUT-OF-SCOPE 行的对位必须为空(`-`)
|
||
# ④ PORTED 行登记的用例数 == 对位文件里**实际**的移植用例数(防少登记)
|
||
# ⑤ 上游克隆在场时:上游测试类集合 == 台账登记集合(`upstream-test-census.py --check`)
|
||
#
|
||
# 上游克隆路径可用 `UPSTREAM_TDC_CLONE` / `UPSTREAM_TDS_CLONE` 覆盖;
|
||
# 克隆不在场 ⇒ 跳过 ⑤(不判红)。
|
||
|
||
set -u
|
||
|
||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||
LEDGER="${LEDGER:-${ROOT}/scripts/upstream-test-ledger.tsv}"
|
||
PY_BIN="${PY_BIN-$(command -v python3 2>/dev/null || true)}"
|
||
REPORT_ONLY=0
|
||
[ "${1:-}" = "--report" ] && REPORT_ONLY=1
|
||
|
||
[ -n "${PY_BIN}" ] || { echo "❌ 前置错误:找不到 python3。"; exit 2; }
|
||
[ -f "${LEDGER}" ] || { echo "❌ 前置错误:台账 ${LEDGER} 不存在。"; exit 2; }
|
||
|
||
TMP="$(mktemp -d)"
|
||
trap 'rm -rf "${TMP}"' EXIT
|
||
|
||
cat >"${TMP}/check.py" <<'PYEOF'
|
||
import os
|
||
import re
|
||
import sys
|
||
|
||
ROOT = sys.argv[1]
|
||
LEDGER = sys.argv[2]
|
||
REPORT_ONLY = sys.argv[3] == "report"
|
||
|
||
failures = []
|
||
ported = 0
|
||
out_of_scope = 0
|
||
|
||
# ④ 用「文件级集合相等」防少登记 / 多登记:
|
||
# 台账登记的用例集合 == 对位文件里**实际**的移植用例集合。
|
||
registered = {} # 文件 → {用例名}
|
||
actual = {} # 文件 → {用例名}
|
||
|
||
with open(LEDGER, 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")
|
||
cls = parts[0].strip()
|
||
if len(parts) < 8:
|
||
failures.append("台账 %d 行:列数 %d(应为 8)⇒ %s" % (lineno, len(parts), cls))
|
||
continue
|
||
repo, upfile, kind, asserts_n, counterpart, status, reason = (
|
||
parts[1].strip(), parts[2].strip(), parts[3].strip(), parts[4].strip(),
|
||
parts[5].strip(), parts[6].strip(), parts[7].strip(),
|
||
)
|
||
if status not in ("PORTED", "OUT-OF-SCOPE"):
|
||
failures.append("台账 %d 行:status=%s(只能是 PORTED / OUT-OF-SCOPE)⇒ %s"
|
||
% (lineno, status, cls))
|
||
continue
|
||
if not reason:
|
||
failures.append("台账 %d 行:reason 为空(搬/不搬都要写理由)⇒ %s" % (lineno, cls))
|
||
continue
|
||
if not upfile or not upfile.endswith(".java"):
|
||
failures.append("台账 %d 行:upstream_file 不合法 ⇒ %s" % (lineno, cls))
|
||
continue
|
||
if kind not in ("JUNIT", "SELFTEST"):
|
||
failures.append("台账 %d 行:kind=%s(只能是 JUNIT / SELFTEST)⇒ %s"
|
||
% (lineno, kind, cls))
|
||
continue
|
||
|
||
if status == "OUT-OF-SCOPE":
|
||
out_of_scope += 1
|
||
if counterpart != "-":
|
||
failures.append("台账 %d 行:OUT-OF-SCOPE 的对位必须是 `-` ⇒ %s" % (lineno, cls))
|
||
continue
|
||
|
||
# ── PORTED ──
|
||
ported += 1
|
||
if "::" not in counterpart:
|
||
failures.append("台账 %d 行:PORTED 的对位必须是 `文件::fn1+fn2` ⇒ %s"
|
||
% (lineno, cls))
|
||
continue
|
||
path, _, fns = counterpart.partition("::")
|
||
abs_path = os.path.join(ROOT, path)
|
||
if not os.path.isfile(abs_path):
|
||
failures.append("台账 %d 行:对位文件不存在:%s ⇒ %s" % (lineno, path, cls))
|
||
continue
|
||
with open(abs_path, encoding="utf-8") as fh2:
|
||
src = fh2.read()
|
||
names = [n.strip() for n in fns.split("+") if n.strip()]
|
||
if not names:
|
||
failures.append("台账 %d 行:对位用例列表为空 ⇒ %s" % (lineno, cls))
|
||
continue
|
||
for n in names:
|
||
if not re.search(r"\bfn\s+" + re.escape(n) + r"\b", src):
|
||
failures.append("台账 %d 行:对位用例 `%s` 不在 %s 里 ⇒ %s"
|
||
% (lineno, n, path, cls))
|
||
registered.setdefault(path, set()).update(names)
|
||
|
||
# ④ 文件级集合相等:台账登记的 == 文件里实际有的移植用例
|
||
# (前缀约定:移植用例一律带下列前缀之一 ⇒ 辅助函数不会被误计)
|
||
PORTED_PREFIX = ("upstream_", "hop1_", "hop2_", "runtime_guard_", "prod_guard_")
|
||
for path, names in sorted(registered.items()):
|
||
abs_path = os.path.join(ROOT, path)
|
||
with open(abs_path, encoding="utf-8") as fh2:
|
||
src = fh2.read()
|
||
found = set(re.findall(r"\bfn\s+([A-Za-z0-9_]+)\b", src))
|
||
found = {n for n in found if n.startswith(PORTED_PREFIX)}
|
||
for n in sorted(found - names):
|
||
failures.append("%s 里的移植用例 `%s` **未登记台账** ⇒ 补台账(或它不该存在)" % (path, n))
|
||
for n in sorted(names - found):
|
||
failures.append("台账登记的 `%s` 在 %s 里找不到(已改名/已删?)" % (n, path))
|
||
|
||
print("台账:%d 行(PORTED %d / OUT-OF-SCOPE %d)"
|
||
% (ported + out_of_scope, ported, out_of_scope))
|
||
if failures:
|
||
print("❌ 台账自检不通过:")
|
||
for f in failures:
|
||
print(" · %s" % f)
|
||
sys.exit(1)
|
||
print("✅ 台账自检通过:每条 PORTED 的对位用例都在、每条 OUT-OF-SCOPE 都写了理由")
|
||
PYEOF
|
||
|
||
"${PY_BIN}" "${TMP}/check.py" "${ROOT}" "${LEDGER}" "${REPORT_ONLY}"
|
||
STATIC_RC=$?
|
||
|
||
# ⑤ 上游克隆在场时:上游测试类集合 ⇄ 台账登记集合
|
||
CENSUS_RC=0
|
||
if "${PY_BIN}" "${ROOT}/scripts/upstream-test-census.py" --check; then
|
||
:
|
||
else
|
||
CENSUS_RC=$?
|
||
fi
|
||
|
||
if [ "${REPORT_ONLY}" = "1" ]; then
|
||
echo "(--report:不判红)"
|
||
exit 0
|
||
fi
|
||
|
||
if [ "${STATIC_RC}" != "0" ] || [ "${CENSUS_RC}" != "0" ]; then
|
||
exit 1
|
||
fi
|
||
exit 0
|