Skip to content

test(config): 令牌门禁改用不变量测试,不再靠枚举例子 - #27

Merged
modusensus merged 1 commit into
mainfrom
test/loopback-invariants
Sep 20, 2026
Merged

modusensus merged 1 commit into
mainfrom
test/loopback-invariants

Conversation

@modusensus

Copy link
Copy Markdown
Collaborator

上一轮修 is_loopback_host 时,我加的用例是手写清单127.corp.example 之类)。这次换成不变量——因为连着两次出问题的都是"按什么判定",而不是"哪几个例子":

  1. 旧实现拿 startswith("127.") 当网段用,于是放行了 127.corp.example 这类主机名
  2. 我的修法把判定建在 IPv6Address.is_loopback 上,而它对 v4-mapped 形式的支持是随 Python 版本加的——同一个地址,3.11 要令牌、3.12 不要。

枚举式的用例天生抓不住这两类:改判定依据不会让旧例子失效。所以这里改成四条不变量。

语料由生成得到

127.{a}.{b}.{c} 由交叉相乘得到(125 项),"像回环"的名字由 127.<a>.<b>.<c>.example 生成,另加上 RFC1918 / CGNAT 100.64/10 / link-local 169.254(含云厂商元数据端点)/ 未指定地址 / NAT64 / 文档网段。要加新的"像不像"形状,改生成器而不是补一行例子。

四条不变量

  • 对外的东西,改拼写不能翻回来:加空白、套方括号、改大小写后判定必须一致,并且 ensure_bindable 一律拒绝。任何依赖字符串形状的判定都会在这里露出来。
  • ::ffff:A.B.C.DA.B.C.D 判定一致:这条本身与解释器版本无关,而它盯住的 bug 是版本相关的——所以它把"3.11 与 3.12 不一致"翻译成了一个在任何版本上都成立的要求,CI 的 3.11 腿就能盯住它。
  • 我们放行的写法,系统真绑一次后选中的必须是回环地址:这个方向是问系统而不是问自己,localhost 尤其重要——它走的是本机解析器,与我们的字符串逻辑无关。只覆盖系统确实肯绑的写法(macOS 只为 lo 配了 127.0.0.1,绑 127.1.2.3 会被拒,那属于"能不能用"而非"判得对不对"),遇到 OSError 跳过而不是假装通过。
  • 开箱默认值必须免令牌DEFAULT_BIND_HOSTServeConfig().host 都要过 ensure_bindable(..., ""),否则 ponte serve 默认就起不来。

验证

  • pytest 413 passed,覆盖率 86.75%;ruffmypy(本机与 --platform linux)、_smoke_test.py 全过。
  • 用例被证明抓得住那个 bug:把实现换回未归一化版本、并模拟 3.11 的 is_loopback 语义后,三条用例失败(含新加的 ::ffff: 那条);同一 3.11 语义配当前实现则全套照过——所以这些不变量在旧版本上也不是误报。

补充一句边界:不变量测试能盯住"判定与要求不一致",但盯不住解释器行为差异本身——v4-mapped 那类差异最终仍要靠版本矩阵来暴露。这条测试做的是把它变成本地可复现、且在任何版本上都该成立的要求。

is_loopback_host 是 ensure_bindable 唯一的判据,而它连续两次出问题都说明同一件事:
缺陷不在"哪几个例子"里,而在"按什么判定"里(拿字符串前缀当网段、拿库函数当版本
无关的真相)。逐例枚举的用例天生抓不住这类错误,所以改成四条不变量:

- 语料由生成得到,不是手写清单:127/8 的写法交叉相乘,'像回环'的名字由
  127.<a>.<b>.<c>.example 生成,另有 RFC1918/CGNAT/link-local/云元数据端点。
- 判为对外的东西,拼写怎么改(空白、方括号、大小写)都不能翻回来。
- ::ffff:A.B.C.D 与 A.B.C.D 的判定必须一致:这条与解释器版本无关,而它盯住的 bug
  恰恰是版本相关的(3.11 的 IPv6Address.is_loopback 不认 v4-mapped),于是 CI 的
  3.11 腿就能盯住它。
- 我们放行的写法,系统真绑一次后选中的必须是回环地址(localhost 走本机解析器,与
  我们的字符串逻辑无关);只覆盖系统确实肯绑的写法,OSError 跳过而不是假装通过。
- 开箱默认值(DEFAULT_BIND_HOST / ServeConfig().host)必须免令牌。

验证:把实现换回未归一化版本并模拟 3.11 语义时三条用例失败(含新加的那条);
同一语义配当前实现则全套照过,没有误报。

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
Copilot AI lite review requested due to automatic review settings September 20, 2026 03:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: b9e7f61b-5a08-430c-9738-28931e723cf1


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@modusensus
modusensus merged commit 08bfa7b into main Sep 20, 2026
11 checks passed
@modusensus
modusensus deleted the test/loopback-invariants branch September 20, 2026 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants