mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
test(formats): consolidate BEAM and DOL boundary checks (#12832)
* test(erlang): consolidate BEAM path and atom budget checks * test(dol): prove session contracts through the MCP client
This commit is contained in:
1 parent
bb7bdd2fba
commit
c9caa0aa5b
6 files changed
+91
-169
No files matched your search
@@ -6,13 +6,6 @@ import { openBinaryInputSchema } from "./sessionLifecycleInputs.js";
|
||||
const base = { path: "/tmp/game.dol" };
|
||||
|
||||
describe("open_binary gamecube-dol input contract", () => {
|
||||
it("accepts format=gamecube-dol with no architecture", () => {
|
||||
expect(
|
||||
openBinaryInputSchema.safeParse({ ...base, format: "gamecube-dol" })
|
||||
.success,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("accepts format=gamecube-dol with a matching entry syntax", () => {
|
||||
expect(
|
||||
openBinaryInputSchema.safeParse({
|
||||
@@ -47,23 +40,6 @@ describe("open_binary gamecube-dol input contract", () => {
|
||||
});
|
||||
|
||||
describe("gamecube-dol output contract", () => {
|
||||
it("validates the open_binary DOL lifecycle result", () => {
|
||||
const schema = sessionOutputSchemas.open_binary;
|
||||
const sha256 = "a".repeat(64);
|
||||
expect(
|
||||
schema.safeParse({
|
||||
result: {
|
||||
path: "/tmp/game.dol",
|
||||
kind: "executable",
|
||||
format: "gamecube-dol",
|
||||
sha256,
|
||||
architecture: "powerpc",
|
||||
entry_point: "0x80003100",
|
||||
},
|
||||
}).success,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("does not let the detected-format branch carry gamecube-dol", () => {
|
||||
const schema = sessionOutputSchemas.open_binary;
|
||||
expect(
|
||||
|
||||
@@ -110,17 +110,6 @@ export const erlangBeamRowsFixture = (
|
||||
}),
|
||||
);
|
||||
|
||||
/** BEAM form whose atom table exceeds a caller-selected atom budget. */
|
||||
export const erlangBeamManyAtomsFixture = (count: number): Buffer => {
|
||||
const names = Array.from(
|
||||
{ length: count },
|
||||
(_, index) => `a${String(index)}`,
|
||||
);
|
||||
const chunks = atoms(names);
|
||||
const form = concat(Buffer.from("BEAM", "latin1"), chunks);
|
||||
return concat(Buffer.from("FOR1", "latin1"), u32(form.length), form);
|
||||
};
|
||||
|
||||
/**
|
||||
* Compiler-produced BEAM: `erlc +deterministic` from Erlang/OTP 29 (compiler
|
||||
* 10.0.2), which writes the OTP 28+ compact AtU8 layout. Source:
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
erlangBeamRowsFixture,
|
||||
erlangBeamFixture,
|
||||
erlangBeamLegacyAtomsFixture,
|
||||
erlangBeamManyAtomsFixture,
|
||||
erlangBeamOtp29Fixture,
|
||||
} from "./Beam.fixture.js";
|
||||
import { parseErlangBeam } from "./BeamParser.js";
|
||||
@@ -365,25 +364,50 @@ it("rejects combined table rows before allocating them", () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
bytes: erlangBeamManyAtomsFixture(3),
|
||||
limit: { name: "max_atoms", maximum: 1, required: 3 },
|
||||
["legacy Atom", "Atom", [0, 0, 0, 2], [1, 2]],
|
||||
["classic AtU8", "AtU8", [0, 0, 0, 2], [1, 2]],
|
||||
["compact AtU8", "AtU8", [0xff, 0xff, 0xff, 0xfe], [0x10, 0x20]],
|
||||
] as const)(
|
||||
"checks only the admitted atom lengths in %s",
|
||||
(_name, chunk, count, [validLength, invalidLength]) => {
|
||||
const fixture = (firstLength: number, lastByte: number) =>
|
||||
erlangBeamChunksFixture([
|
||||
[chunk, Buffer.from([...count, firstLength, 0x61, lastByte])],
|
||||
]);
|
||||
const input = { ...defaults, max_atoms: 1 };
|
||||
const valid = fixture(validLength, 0);
|
||||
expect(parse(valid).atoms).toEqual([
|
||||
{ index: 1, name: "a" },
|
||||
{ index: 2, name: "" },
|
||||
]);
|
||||
for (const bytes of [valid, fixture(validLength, validLength)]) {
|
||||
// The second name is complete or truncated, beyond the admitted prefix.
|
||||
expect(() => parse(bytes, input)).toThrow(
|
||||
expect.objectContaining({
|
||||
failure: expect.objectContaining({
|
||||
kind: "limit",
|
||||
format: "beam",
|
||||
limit: { name: "max_atoms", maximum: 1, required: 2 },
|
||||
}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
// The first name would consume the second entry's required length byte.
|
||||
expect(() => parse(fixture(invalidLength, 0x62), input)).toThrow(
|
||||
expect.objectContaining({ reason: "format" }),
|
||||
);
|
||||
},
|
||||
{
|
||||
bytes: erlangBeamFixture(),
|
||||
limit: { name: "max_chunks", maximum: 1 },
|
||||
},
|
||||
])("retains the exhausted $limit.name allowance", ({ bytes, limit }) => {
|
||||
const input = inspectErlangBeamInputSchema.parse({
|
||||
path: defaults.path,
|
||||
[limit.name]: limit.maximum,
|
||||
});
|
||||
expect(() => parse(bytes, input)).toThrow(
|
||||
);
|
||||
|
||||
it("retains the exhausted chunk allowance", () => {
|
||||
expect(() =>
|
||||
parse(erlangBeamFixture(), { ...defaults, max_chunks: 1 }),
|
||||
).toThrow(
|
||||
expect.objectContaining({
|
||||
failure: expect.objectContaining({
|
||||
kind: "limit",
|
||||
format: "beam",
|
||||
limit,
|
||||
limit: { name: "max_chunks", maximum: 1 },
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -66,13 +66,14 @@ const extendedFirstAtomLength = (): Buffer => {
|
||||
};
|
||||
|
||||
cliTest(
|
||||
"reports BEAM tables and code through the public CLI",
|
||||
"reports BEAM tables and code for a path relative to the CLI working directory",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-beam-cli-");
|
||||
const path = join(root, "app.beam");
|
||||
await writeFile(path, erlangBeamFixture("cli_mod"));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-erlang-beam", path, "--json"],
|
||||
arguments: ["inspect-erlang-beam", "app.beam", "--json"],
|
||||
cwd: root,
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completeBeamEvidenceSchema.parse(result.json);
|
||||
@@ -497,21 +498,6 @@ cliTest(
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"resolves a relative BEAM path against the working directory",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-beam-cli-rel-");
|
||||
await writeFile(join(root, "app.beam"), erlangBeamFixture());
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-erlang-beam", "app.beam", "--json"],
|
||||
cwd: root,
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completeBeamEvidenceSchema.parse(result.json);
|
||||
expect(evidence.subject?.local_path).toBe(join(root, "app.beam"));
|
||||
},
|
||||
);
|
||||
|
||||
cliTest("rejects an empty BEAM path before reading", async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-beam-cli-path-");
|
||||
const result = await cli.run({
|
||||
|
||||
@@ -76,10 +76,12 @@ mcpTest(
|
||||
name: "inspect_erlang_beam",
|
||||
arguments: { path: "app.beam" },
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
expect(parseMcpToolError(response).error).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: { operation: "inspect_erlang_beam" },
|
||||
details: {
|
||||
operation: "inspect_erlang_beam",
|
||||
issues: [{ path: ["path"], reason: "invalid_format" }],
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
await session.close();
|
||||
@@ -99,7 +101,9 @@ mcpTest(
|
||||
for (const [label, bytes, settings, budget, remediation] of [
|
||||
[
|
||||
"atoms",
|
||||
erlangBeamFixture(),
|
||||
erlangBeamChunksFixture([
|
||||
["AtU8", Buffer.from([0xff, 0xff, 0xff, 0xfe, 0x10, 0x61, 0])],
|
||||
]),
|
||||
{ max_atoms: 1 },
|
||||
{ max_atoms: 1 },
|
||||
"Raise max_atoms above 1",
|
||||
@@ -136,7 +140,6 @@ mcpTest(
|
||||
name: "inspect_erlang_beam",
|
||||
arguments: { path, ...settings },
|
||||
});
|
||||
expect(response.isError, label).toBe(true);
|
||||
const error = parseMcpToolError(response).error;
|
||||
expect(error, label).toMatchObject({
|
||||
code: "resource_constraint",
|
||||
@@ -145,51 +148,21 @@ mcpTest(
|
||||
details: { resource: "capacity", reported_limits: budget },
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
mcpTest(
|
||||
"checks admitted atom lengths before reporting capacity",
|
||||
async ({ mcp }) => {
|
||||
const { client, session } = await connect(mcp);
|
||||
const root = await createTestTempDirectory("rea-beam-atom-framing-");
|
||||
try {
|
||||
for (const [chunk, count, validLength, invalidLength] of [
|
||||
["Atom", [0, 0, 0, 2], 1, 2],
|
||||
["AtU8", [0, 0, 0, 2], 1, 2],
|
||||
["AtU8", [0xff, 0xff, 0xff, 0xfe], 0x10, 0x20],
|
||||
] as const) {
|
||||
for (const [length, finalByte, malformedPrefix] of [
|
||||
[validLength, 0, false],
|
||||
[validLength, validLength, false], // The truncated second name is outside the scan.
|
||||
[invalidLength, 0x62, true],
|
||||
] as const) {
|
||||
const path = join(root, "atoms.beam");
|
||||
const payload = Buffer.from([...count, length, 0x61, finalByte]);
|
||||
await writeFile(path, erlangBeamChunksFixture([[chunk, payload]]));
|
||||
const response = await client.callTool({
|
||||
name: "inspect_erlang_beam",
|
||||
arguments: { path, max_atoms: 1 },
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
expect(parseMcpToolError(response).error).toMatchObject(
|
||||
malformedPrefix
|
||||
? {
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [{ path: ["path"], reason: "invalid_format" }],
|
||||
},
|
||||
}
|
||||
: {
|
||||
code: "resource_constraint",
|
||||
details: { reported_limits: { max_atoms: 1 } },
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
const malformed = join(root, "malformed-atoms.beam");
|
||||
await writeFile(
|
||||
malformed,
|
||||
erlangBeamChunksFixture([
|
||||
["AtU8", Buffer.from([0xff, 0xff, 0xff, 0xfe, 0x20, 0x61, 0x62])],
|
||||
]),
|
||||
);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_erlang_beam",
|
||||
arguments: { path: malformed, max_atoms: 1 },
|
||||
});
|
||||
expect(parseMcpToolError(response).error).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: { issues: [{ path: ["path"], reason: "invalid_format" }] },
|
||||
});
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
import { canonicalOutputSchema } from "../../fixtures/canonicalOutputSchema.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import type { Client } from "@modelcontextprotocol/client";
|
||||
import type { CallToolResult } from "@modelcontextprotocol/server";
|
||||
import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import { expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
|
||||
import { readAnalysisSnapshot } from "../../../src/application/binary/AnalysisSnapshotFiles.js";
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { buildGamecubeDol } from "../../../src/domain/gamecubeDol.fixture.js";
|
||||
import { silentLogger } from "../../../src/logger.js";
|
||||
@@ -21,48 +15,38 @@ import {
|
||||
} from "../../fixtures/binarySession.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
const validateAdvertised = (
|
||||
tools: Awaited<ReturnType<Client["listTools"]>>["tools"],
|
||||
name: "open_binary" | "binary_session",
|
||||
response: CallToolResult,
|
||||
) => {
|
||||
const tool = tools.find((candidate) => candidate.name === name);
|
||||
if (tool === undefined) throw new Error(`missing ${name} output schema`);
|
||||
const validate = new Ajv2020({
|
||||
strict: false,
|
||||
validateFormats: false,
|
||||
}).compile(canonicalOutputSchema(tool.name));
|
||||
expect(
|
||||
validate(response.structuredContent),
|
||||
JSON.stringify(validate.errors),
|
||||
).toBe(true);
|
||||
// The zod contract must accept the same payload the advertised JSON schema does.
|
||||
const parsed = toolContract(name).outputSchema.parse(
|
||||
response.structuredContent,
|
||||
);
|
||||
return z.record(z.string(), z.unknown()).parse(parsed.result);
|
||||
};
|
||||
|
||||
it("opens a GameCube DOL and validates binary_session status against its output schema", async () => {
|
||||
it("opens a GameCube DOL and reports its target through the advertised lifecycle schemas", async () => {
|
||||
const root = await createTestTempDirectory("rea-dol-session-mcp-");
|
||||
const path = join(root, "game.dol");
|
||||
await writeFile(path, buildGamecubeDol());
|
||||
const { client } = await createApplicationMcpHarness({
|
||||
session: createTestBinarySession(createCacheProvider([])),
|
||||
serverOptions: { logger: silentLogger },
|
||||
clientIdentity: { name: "dol-binary-session-mcp", version: "1" },
|
||||
serverOptions: {
|
||||
logger: silentLogger,
|
||||
environment: { REA_MCP_OUTPUT_SCHEMA_PROFILE: "compact" },
|
||||
},
|
||||
});
|
||||
const tools = (await client.listTools()).tools;
|
||||
// The SDK validates successful replies against the listed output schemas.
|
||||
const { tools } = await client.listTools();
|
||||
for (const name of ["open_binary", "binary_session"]) {
|
||||
expect(
|
||||
tools.find((tool) => tool.name === name)?.outputSchema,
|
||||
name,
|
||||
).toBeDefined();
|
||||
}
|
||||
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path, format: "gamecube-dol" },
|
||||
});
|
||||
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
|
||||
expect(validateAdvertised(tools, "open_binary", opened)).toMatchObject({
|
||||
format: "gamecube-dol",
|
||||
architecture: "powerpc",
|
||||
entry_point: "0x80003100",
|
||||
expect(opened.structuredContent).toMatchObject({
|
||||
result: {
|
||||
format: "gamecube-dol",
|
||||
kind: "executable",
|
||||
architecture: "powerpc",
|
||||
entry_point: "0x80003100",
|
||||
},
|
||||
});
|
||||
|
||||
const status = await client.callTool({
|
||||
@@ -70,11 +54,13 @@ it("opens a GameCube DOL and validates binary_session status against its output
|
||||
arguments: {},
|
||||
});
|
||||
expect(status.isError, JSON.stringify(status)).not.toBe(true);
|
||||
expect(validateAdvertised(tools, "binary_session", status)).toMatchObject({
|
||||
format: "gamecube-dol",
|
||||
kind: "executable",
|
||||
architecture: "powerpc",
|
||||
entry_point: "0x80003100",
|
||||
expect(status.structuredContent).toMatchObject({
|
||||
result: {
|
||||
format: "gamecube-dol",
|
||||
kind: "executable",
|
||||
architecture: "powerpc",
|
||||
entry_point: "0x80003100",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -136,18 +122,6 @@ it("saves a GameCube DOL snapshot and replays it after reopening", async () => {
|
||||
arguments: { path, format: "gamecube-dol", snapshot_path: snapshotPath },
|
||||
});
|
||||
expect(reopened.isError, JSON.stringify(reopened)).not.toBe(true);
|
||||
expect(
|
||||
validateAdvertised(
|
||||
(await client.listTools()).tools,
|
||||
"open_binary",
|
||||
reopened,
|
||||
),
|
||||
).toMatchObject({
|
||||
format: "gamecube-dol",
|
||||
kind: "executable",
|
||||
architecture: "powerpc",
|
||||
entry_point: "0x80003100",
|
||||
});
|
||||
const replayed = parseEvidence(
|
||||
(await client.callTool(query)).structuredContent,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user