physical_byte_agent/tests/reg-tool.test.ts
d-robotics d671f174fe feat(v3): P0 全落地 — T01-T05b 安全骨架 + 命门根治
P0 六项任务全部完成,1044 单测全绿,tsc 0 错。

- T01 真 createAgentSession boot 离线验证(e2e-runtime + e2e-smoke 71 测试)
- T02 safety-gate 正则与 hex 归一联动,堵 devmem 十进制砖板漏洞;devmem-via-bash 硬门
- T03 reg-tool.ts:datasheet 地址校验寄存器工具,prepareArguments 接 hex 归一;barrel 22 工厂
- T04 src/core/tool-pipeline.ts:五段流水线 + 单调 guard 不变量;safety-gate 委托 decide();flash finalizeFlashContent
- T05a sandbox-policy.ts:fail-closed unusable→exitCode126 不 spawn;board-link 包 argv
- T05b native/landlock-run/landlock-run.c:C11 raw-landlock self-restrict-then-exec + Makefile + README;probeViaBinary 接线

测试方法:等价类/边界值/决策表/状态转换/变异/对抗/元蜕变(TDD test-first)。
文档:架构设计文档_v3.md 进度表 + Docs/postmortem/0001-e2e-smoke.md。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-14 12:12:24 +08:00

132 lines
5.1 KiB
TypeScript
Raw 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.

import { describe, it, expect } from "vitest";
import {
KNOWN_REGISTERS,
normalizeAddress,
validateRegisterAddress,
planRegOp,
} from "../src/extensions/reg-tool.js";
describe("reg-tool normalizeAddress —— 等价类 + 边界值", () => {
it.each([
["0x40020000", "0x40020000"], // 已 hex 小写
["0X40020000", "0x40020000"], // 大写 0X → 小写
["1073872896", "0x40020000"], // 十进制 32 位
["1099511627776", "0x10000000000"], // 64 位十进制(BigInt 保精度)
["0", "0x0"], // 零
])("归一为 0x 小写:%s → %s", (inp, exp) => {
expect(normalizeAddress(inp)).toBe(exp);
});
it.each([
["0xGGGG", null], // 非 hex 字符
["gpio33", null], // 非地址
["", null], // 空串
["0x", null], // 0x 无数字
[" ", null], // 空白
])("非法地址返回 null:%s", (inp, exp) => {
expect(normalizeAddress(inp)).toBe(exp);
});
});
describe("reg-tool validateRegisterAddress —— 决策表(地址已知性 × 可写性 × op)", () => {
// 决策表:address ∈ {已知rw, 已知ro, 未知, 非法} × op ∈ {read, write}
it.each([
// 已知 rw 寄存器
["0x40020000", "read", true, "GPIO_CTRL"], // rw read → ok
["0x40020000", "write", true, "GPIO_CTRL"], // rw write → ok
["1073872896", "write", true, "GPIO_CTRL"], // 十进制归一 → ok(命门二联动)
// 已知 ro 寄存器
["0x4002000c", "read", true, "GPIO_INTSTATUS"], // ro read → ok
["0x4002000c", "write", false, undefined], // ro write → fail-closed
// 未知地址
["0xdeadbeef", "read", false, undefined], // 未知 → fail-closed
["0xdeadbeef", "write", false, undefined],
// 非法地址
["gpio33", "read", false, undefined],
["", "read", false, undefined],
])("决策行 «%s» op=%s → ok=%s reg=%s", (addr, op, ok, name) => {
const v = validateRegisterAddress(addr, op as "read" | "write");
expect(v.ok).toBe(ok);
if (name) expect(v.register?.name).toBe(name);
if (!ok) expect(v.reason).toBeTruthy();
});
});
describe("reg-tool planRegOp —— 边界值 + 不变式", () => {
it("read 已知地址 → plan 含归一地址 + datasheetRef", () => {
const r = planRegOp({ address: "0x40020000", op: "read" });
expect(r.ok).toBe(true);
expect(r.plan?.address).toBe("0x40020000");
expect(r.plan?.register.name).toBe("GPIO_CTRL");
expect(r.plan?.datasheetRef).toBe("RDK-X5 TRM §12.4");
});
it("write 已知 rw 地址带 value → plan.value 归一", () => {
const r = planRegOp({ address: "0x40020008", op: "write", value: "1" });
expect(r.ok).toBe(true);
expect(r.plan?.value).toBe("0x1");
});
it("write 缺 value → fail-closed", () => {
const r = planRegOp({ address: "0x40020000", op: "write" });
expect(r.ok).toBe(false);
expect(r.error).toContain("value");
});
it("write ro 寄存器 → fail-closed(防误写状态寄存器)", () => {
const r = planRegOp({ address: "0x4002000c", op: "write", value: "0x1" });
expect(r.ok).toBe(false);
expect(r.error).toContain("read-only");
});
it("未知地址 → fail-closed(防砖板,T03 核心)", () => {
const r = planRegOp({ address: "0xffffffff", op: "read" });
expect(r.ok).toBe(false);
expect(r.error).toContain("not in datasheet");
});
it("十进制地址(glm-5.2 命门二)经归一命中已知寄存器", () => {
// 1073872896 = 0x40020000
const r = planRegOp({ address: "1073872896", op: "read" });
expect(r.ok).toBe(true);
expect(r.plan?.register.name).toBe("GPIO_CTRL");
});
});
describe("reg-tool 变异测试 —— 植入 bug 必被抓住", () => {
// M1:若归一被破坏(人为不归一十进制),未知十进制应漏过查表 → fail。
// 证 normalizeAddress 非冗余:十进制必须归一才能查表。
it("M1: 十进制不归一则查表失败(normalizeAddress 非冗余)", () => {
// 直接用十进制串查 KNOWN_REGISTERS → miss(证归一是查表前提)
expect(KNOWN_REGISTERS["1073872896"]).toBeUndefined();
// 归一后命中
expect(KNOWN_REGISTERS[normalizeAddress("1073872896") ?? ""]).toBeDefined();
});
// M2:ro 寄存器 write 必须被拦。若 access 检查被移除(模拟),ro write 会过 → 证可写性检查非冗余。
it("M2: ro 寄存器 access=ro,write 必被拦(可写性检查非冗余)", () => {
const roReg = Object.values(KNOWN_REGISTERS).find((r) => r.access === "ro");
expect(roReg).toBeDefined();
const r = planRegOp({ address: roReg!.address, op: "write", value: "0x1" });
expect(r.ok).toBe(false);
});
});
describe("reg-tool KNOWN_REGISTERS —— 不变式(冻结 + 地址小写)", () => {
it("表冻结:增删属性 throw(严格模式)", () => {
expect(() => {
// biome-ignore lint/suspicious/noExplicitAny: 测试冻结性
(KNOWN_REGISTERS as any)["0x99999999"] = { address: "x" };
}).toThrow(TypeError);
});
it("所有 key 是 0x 小写形式(查表一致性不变式)", () => {
for (const key of Object.keys(KNOWN_REGISTERS)) {
expect(key).toMatch(/^0x[0-9a-f]+$/);
expect(key).toBe(key.toLowerCase());
// key 与条目 .address 一致
expect(KNOWN_REGISTERS[key].address).toBe(key);
}
});
});