Files
reverse-engineering-anything/tests/acceptance/hexRecordsCli.test.ts
T
Aadityaandgensokyo-dev c36971953c feat(artifacts): inventory and extract Intel HEX and Motorola S-record regions (#7136)
* feat(artifacts): inventory and extract Intel HEX and Motorola S-record regions

open_binary refused Intel HEX and S-record programming files as an
unsupported format, so inspect_artifact and extract_artifact could not
answer which bytes load at which addresses.

Recognize both encodings by a fully valid first record and decode them
statically into address-contiguous regions, each an inventory entry named
0x<start>-0x<end>.bin and extractable as raw bytes. Root format_metadata
records the encoding and variant, record counts, unique and duplicate
bytes, address bounds, termination, declared start address, S0 header and
validated S5/S6 count. Gaps are never filled; start addresses are declared
facts, not proven entry points.

Checksum, record-type, byte-count, line-length, overlap-conflict, address
wrap and post-termination errors fail closed with the line number; a
missing termination record leaves coverage unknown. Decoding is bounded by
a 128 MiB file ceiling (decoded bytes are at most half the file and are
reserved after the size check) and 65,536 address-discontiguous runs.

Refs #7129

* fix(artifacts): refuse Intel HEX extended address records with a non-zero address

* fix(artifacts): bound HEX region copies and retain stream cancellation

---------

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-10-28 12:35:55 +08:00

104 lines
3.8 KiB
TypeScript

import { readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, onTestFinished } from "vitest";
import { cliTest } from "../support/cli/cliFixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import {
INTEL_HEX_I32_GAP,
SREC_S19,
} from "../../src/domain/firmware/hexRecords.fixture.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js";
import {
artifactExtractionResultSchema,
artifactInventoryResultSchema,
} from "../../src/domain/artifactGraph.js";
const cases = [
{
encoding: "intel-hex",
name: "firmware.hex",
text: INTEL_HEX_I32_GAP,
root: { variant: "I32HEX", start_address: 0x0800_0101 },
regions: {
"0x08000000-0x0800001f.bin": Buffer.from(
Array.from({ length: 32 }, (_, index) => index),
),
"0x08000100-0x08000103.bin": Buffer.from("deadbeef", "hex"),
},
},
{
encoding: "srec",
name: "firmware.s19",
text: SREC_S19,
root: { variant: "S19", header_text: "HDR", start_address_record: "S9" },
regions: {
"0x00000000-0x0000001f.bin": Buffer.from(
Array.from({ length: 32 }, (_, index) => index),
),
},
},
] as const;
cliTest.for(cases)(
"inventories and extracts $encoding regions through the compiled CLI",
async ({ encoding, name, text, root, regions }, { cli }) => {
const directory = await createTestTempDirectory("rea-hex-cli-");
const source = join(directory, name);
await writeFile(source, text);
const inspected = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
const inspection = artifactInspectionResultSchema.parse(
parseEvidence(inspected.json).normalized_result,
);
const graph = artifactInventoryResultSchema.parse(
inspection.substeps[0]?.evidence.normalized_result,
);
expect(graph.manifest.root_format).toBe(encoding);
expect(graph.coverage.status).toBe("complete");
expect(
graph.occurrences.find((row) => row.logical_path === ".")
?.format_metadata,
).toMatchObject({ kind: "hex-records", encoding, ...root });
expect(
graph.occurrences
.filter((row) => row.logical_path !== ".")
.map((row) => row.logical_path),
).toEqual(Object.keys(regions));
expect(inspection.next_probes.map((probe) => probe.operation)).toEqual([
"open_binary",
]);
const extracted = await cli.run({
arguments: ["extract-artifact", source, "--json"],
environment: { TMPDIR: directory, TMP: directory, TEMP: directory },
});
expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
const result = artifactExtractionResultSchema.parse(
parseEvidence(extracted.json).normalized_result,
);
onTestFinished(() =>
rm(result.output_root, { recursive: true, force: true }),
);
expect(result.containment_verified).toBe(true);
for (const [path, bytes] of Object.entries(regions))
expect(
Buffer.compare(await readFile(join(result.output_root, path)), bytes),
).toBe(0);
},
);
cliTest("rejects a malformed record through the CLI", async ({ cli }) => {
const directory = await createTestTempDirectory("rea-hex-invalid-cli-");
const source = join(directory, "firmware.hex");
await writeFile(source, INTEL_HEX_I32_GAP.replace("BEEFC3", "BEEFC4"));
const result = await cli.run({
arguments: ["inspect-artifact", source, "--json"],
});
expect(result.exitCode).not.toBe(0);
expect(result.stdout + result.stderr).toContain(
"Intel HEX is malformed: line 4: record type 00 checksum 0xc4 does not match computed 0xc3",
);
});