mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 23:09:34 +00:00
* feat(artifacts): inspect Game Boy and GBA ROM headers Game Boy and Game Boy Color images (game-boy-rom) are claimed when at least the first 24 bytes of the boot logo at 0x104 match, the half every boot ROM compares. Root metadata reports the entry code and its JP target, full or top-half logo match, title and manufacturer code, CGB and SGB flags, mapper and features, declared ROM and RAM sizes, destination, licensee codes, version, and declared and computed header and global checksums. The global checksum is summed over the file in bounded reads. Game Boy Advance images (gba-rom) are claimed from an ARM branch at 0, the fixed 0x96 at 0xB2, and the boot logo or a matching complement check. Root metadata reports the entry branch target, logo match, title, game and maker codes, fixed value, unit and device type, version, the complement check, and the multiboot fields when that layout could apply. Both serve header.bin and program.bin as stored ranges. Logos are compared by SHA-256 digest, so no logo bytes are carried. Checksum, logo and size disagreements leave coverage unknown; a header cut short is refused as truncated with the offset where the file ends. * fix(artifacts): keep missing GBA multiboot fields unknown and pin checksum vectors A GBA file that ends inside the multiboot fields reported boot_mode and slave_id as zero. They are now null, with a limitation naming where the file ends. The Game Boy reader charges artifact_limits.expanded_bytes from the file size before it sums the file for the global checksum. Checksum tests now use hand-derived vectors instead of copying the parser's own result. * refactor(artifacts): read Game Boy and GBA header fields through BinaryReader The GBA parser decoded words with a local reader that read missing bytes as zero. Both parsers now read header fields through BinaryReader, as the tool-design guide requires, after their length checks. The fixtures write literal offsets instead of the parsers' field constants, and the guides say inventory reads the stored members to hash them. * fix(artifacts): read documented GB size codes and MBC4, refuse truncated GBA headers * style(artifacts): format the handheld ROM test --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
179 lines
6.1 KiB
TypeScript
179 lines
6.1 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { readFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { classifyArtifactBytesAtPath } from "../../tests/fixtures/artifactClassification.js";
|
|
import { readArtifactEntries } from "../../tests/fixtures/artifactEntries.js";
|
|
import {
|
|
inventoryArtifact,
|
|
materializeArtifactInventory,
|
|
scanCanonicalArtifactInventory,
|
|
} from "../../tests/fixtures/artifactInventory.js";
|
|
import {
|
|
createTestTempDirectory,
|
|
createTestTempFile,
|
|
} from "../../tests/fixtures/temporaryDirectory.js";
|
|
import { parseBinaryTarget } from "../application/BinaryTargetResolver.js";
|
|
import { gameBoyRom, gbaRom } from "../domain/handheldRom.fixture.js";
|
|
import {
|
|
GameBoyRomArtifactReader,
|
|
GbaRomArtifactReader,
|
|
} from "./HandheldRomArtifactReader.js";
|
|
|
|
const digest = (bytes: Uint8Array): string =>
|
|
createHash("sha256").update(bytes).digest("hex");
|
|
|
|
const write = (bytes: Buffer, name: string): Promise<string> =>
|
|
createTestTempFile("rea-handheld-", name, bytes);
|
|
|
|
describe("Game Boy ROM reader", () => {
|
|
it("serves the header and the whole image as stored ranges", async () => {
|
|
const rom = gameBoyRom({ romSizeCode: 0x06, cartridgeType: 0x13 });
|
|
// A 2 MiB image takes more than one checksum read.
|
|
expect(rom.length).toBe(2 * 1024 * 1024);
|
|
const result = await readArtifactEntries(
|
|
new GameBoyRomArtifactReader(await write(rom, "game.gb")),
|
|
);
|
|
expect(result.metadata).toMatchObject({
|
|
kind: "game-boy-rom",
|
|
mapper: "mbc3",
|
|
features: ["ram", "battery"],
|
|
declared_rom_bytes: rom.length,
|
|
header_checksum: { status: "match" },
|
|
global_checksum: { status: "match" },
|
|
});
|
|
expect(result.coverage).toEqual([{ status: "complete", limitations: [] }]);
|
|
expect(result.entries.map((entry) => entry.formatMetadata)).toEqual([
|
|
{ kind: "game-boy-rom-entry", role: "header" },
|
|
{ kind: "game-boy-rom-entry", role: "program" },
|
|
]);
|
|
expect(
|
|
result.contents.get("header.bin")?.equals(rom.subarray(0x100, 0x150)),
|
|
).toBe(true);
|
|
expect(result.contents.get("program.bin")?.equals(rom)).toBe(true);
|
|
});
|
|
|
|
it("classifies by content, ignoring the name", async () => {
|
|
const rom = gameBoyRom();
|
|
expect(classifyArtifactBytesAtPath("rom.bin", rom)).toEqual({
|
|
kind: "container",
|
|
format: "game-boy-rom",
|
|
});
|
|
expect(await parseBinaryTarget(await write(rom, "rom.bin"))).toMatchObject({
|
|
ok: true,
|
|
value: { kind: "archive", format: "game-boy-rom" },
|
|
});
|
|
});
|
|
|
|
it("inventories a bad global checksum as unknown coverage", async () => {
|
|
const rom = gameBoyRom({ badGlobalChecksum: true });
|
|
const graph = await inventoryArtifact(await write(rom, "game.gbc"));
|
|
expect(graph.manifest.root_format).toBe("game-boy-rom");
|
|
expect(graph.manifest.root_sha256).toBe(digest(rom));
|
|
expect(graph.coverage.status).toBe("unknown");
|
|
expect(
|
|
graph.occurrences.find((row) => row.logical_path === ".")
|
|
?.format_metadata,
|
|
).toMatchObject({
|
|
kind: "game-boy-rom",
|
|
global_checksum: { status: "mismatch" },
|
|
});
|
|
});
|
|
|
|
it("refuses a header cut short with the offset where the file ends", async () => {
|
|
const rom = gameBoyRom().subarray(0, 0x140);
|
|
await expect(
|
|
readArtifactEntries(
|
|
new GameBoyRomArtifactReader(await write(rom, "short.gb")),
|
|
),
|
|
).rejects.toMatchObject({
|
|
reason: "truncated",
|
|
parseFailure: {
|
|
kind: "truncated",
|
|
format: "game-boy-rom",
|
|
offset: 0x140,
|
|
},
|
|
});
|
|
expect(await parseBinaryTarget(await write(rom, "short.gb"))).toMatchObject(
|
|
{ ok: false, error: { constraint: "file_format_rejected" } },
|
|
);
|
|
});
|
|
|
|
it("charges both members against artifact_limits.expanded_bytes", async () => {
|
|
const rom = gameBoyRom();
|
|
const reader = new GameBoyRomArtifactReader(
|
|
await write(rom, "game.gb"),
|
|
undefined,
|
|
16,
|
|
);
|
|
try {
|
|
await expect(async () => {
|
|
for await (const entry of reader.entries()) void entry;
|
|
}).rejects.toMatchObject({
|
|
reason: "limit",
|
|
readerLimit: {
|
|
parameter: "artifact_limits.expanded_bytes",
|
|
effective_value: 16,
|
|
required_value: 0x50 + rom.length,
|
|
},
|
|
});
|
|
} finally {
|
|
await reader.close();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("Game Boy Advance ROM reader", () => {
|
|
it("serves the header and the whole image as stored ranges", async () => {
|
|
const rom = gbaRom();
|
|
const result = await readArtifactEntries(
|
|
new GbaRomArtifactReader(await write(rom, "game.gba")),
|
|
);
|
|
expect(result.metadata).toMatchObject({
|
|
kind: "gba-rom",
|
|
entry: { target_offset: 0xc0 },
|
|
nintendo_logo: "match",
|
|
game_code: "AREE",
|
|
complement_check: { status: "match" },
|
|
multiboot_layout: "possible",
|
|
});
|
|
expect(result.coverage).toEqual([{ status: "complete", limitations: [] }]);
|
|
expect(
|
|
result.contents.get("header.bin")?.equals(rom.subarray(0, 0xc0)),
|
|
).toBe(true);
|
|
expect(result.contents.get("program.bin")?.equals(rom)).toBe(true);
|
|
});
|
|
|
|
it("classifies by content and extracts program.bin byte-identical", async () => {
|
|
const rom = gbaRom({ badComplement: true });
|
|
expect(classifyArtifactBytesAtPath("rom.bin", rom)).toEqual({
|
|
kind: "container",
|
|
format: "gba-rom",
|
|
});
|
|
const path = await write(rom, "game.gba");
|
|
const snapshot = await scanCanonicalArtifactInventory(path);
|
|
expect(snapshot.coverage.status).toBe("unknown");
|
|
const selected = snapshot.occurrences
|
|
.filter((row) => row.logical_path === "program.bin")
|
|
.map((row) => row.occurrence_id);
|
|
const output = join(await createTestTempDirectory("rea-gba-out-"), "out");
|
|
await materializeArtifactInventory(
|
|
{
|
|
inputPath: path,
|
|
inputFormat: "gba-rom",
|
|
outputRoot: output,
|
|
occurrenceIds: selected,
|
|
environment: process.env,
|
|
integrityPolicy: "fail",
|
|
},
|
|
path,
|
|
snapshot,
|
|
);
|
|
expect((await readFile(join(output, "program.bin"))).equals(rom)).toBe(
|
|
true,
|
|
);
|
|
});
|
|
});
|