mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
test(devicetree): qualify reservation oracle on current parser boundary Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
113 lines
4.0 KiB
TypeScript
113 lines
4.0 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
import { z } from "zod";
|
|
|
|
import { BINARY_DIAGNOSTICS_TOOL_CONTRACTS } from "../../src/contracts/native/binaryDiagnosticsToolContracts.js";
|
|
import { devicetreeFixture } from "../../src/domain/firmware/devicetree.fixture.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
|
|
const devicetreeContract = (() => {
|
|
const contract = BINARY_DIAGNOSTICS_TOOL_CONTRACTS.find(
|
|
(item) => item.name === "inspect_devicetree",
|
|
);
|
|
if (contract === undefined)
|
|
throw new Error("Missing inspect_devicetree contract");
|
|
return contract;
|
|
})();
|
|
|
|
const oracle = z
|
|
.object({
|
|
cases: z
|
|
.array(
|
|
z.object({
|
|
version: z.union([z.literal(16), z.literal(17)]),
|
|
table_hex: z.string().regex(/^(?:[a-f0-9]{2})+$/u),
|
|
reservations: z.array(
|
|
z.object({ address: z.string(), size: z.string() }),
|
|
),
|
|
}),
|
|
)
|
|
.refine(
|
|
(cases) =>
|
|
cases.length === 2 &&
|
|
new Set(cases.map(({ version }) => version)).size === 2,
|
|
"Expected exactly one libfdt witness for each supported version (16 and 17)",
|
|
),
|
|
})
|
|
.parse(
|
|
JSON.parse(
|
|
await readFile(
|
|
new URL(
|
|
"../fixtures/devicetree-reservations/oracle.json",
|
|
import.meta.url,
|
|
),
|
|
"utf8",
|
|
),
|
|
),
|
|
);
|
|
|
|
/** Reframe the producer's exact table bytes; do not re-encode its 64-bit words. */
|
|
const carrier = (version: 16 | 17, tableHex: string): Buffer => {
|
|
const original = devicetreeFixture(undefined, version);
|
|
const reservationStart = original.readUInt32BE(16);
|
|
const structureStart = original.readUInt32BE(8);
|
|
const table = Buffer.from(tableHex, "hex");
|
|
const delta = table.length - (structureStart - reservationStart);
|
|
const bytes = Buffer.concat([
|
|
original.subarray(0, reservationStart),
|
|
table,
|
|
original.subarray(structureStart),
|
|
]);
|
|
// The synthetic carrier stores reservations immediately before structure.
|
|
for (const column of [4, 8, 12])
|
|
bytes.writeUInt32BE(original.readUInt32BE(column) + delta, column);
|
|
return bytes;
|
|
};
|
|
|
|
cliTest(
|
|
"preserves libfdt 64-bit reservations through CLI and stdio MCP Evidence",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-dtb-reservation-width-");
|
|
const { call } = await connectLocalToolsMcp();
|
|
for (const sample of oracle.cases) {
|
|
const path = join(root, `board-${sample.version}.dtb`);
|
|
const bytes = carrier(sample.version, sample.table_hex);
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-devicetree", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = devicetreeContract.outputSchema.parse(result.json);
|
|
const report = evidence.normalized_result;
|
|
if (!("reservations" in report))
|
|
throw new Error("Expected complete DTB report");
|
|
expect(report.header.version).toBe(sample.version);
|
|
expect(report.artifact).toEqual({
|
|
path,
|
|
bytes: bytes.length,
|
|
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
});
|
|
expect(report.reservations).toEqual(
|
|
sample.reservations.map((row, index) => ({
|
|
offset: bytes.readUInt32BE(16) + index * 16,
|
|
...row,
|
|
})),
|
|
);
|
|
expect(report.coverage.status).toBe("complete");
|
|
if (sample.version === 17) {
|
|
const response = await call("inspect_devicetree", { path });
|
|
expect(response.isError).not.toBe(true);
|
|
const mcp = devicetreeContract.outputSchema.parse(
|
|
response.structuredContent,
|
|
);
|
|
expect(mcp.normalized_result).toEqual(report);
|
|
}
|
|
expect((await readFile(path)).equals(bytes)).toBe(true);
|
|
}
|
|
},
|
|
);
|