import { readdir, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { describe, expect } from "vitest"; import { decompileDirectoryIndexSchema } from "../../../src/contracts/decompileDirectory.js"; import { elf } from "../../../src/domain/binaryTarget.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { cliTest } from "../../support/cli/cliFixture.js"; import { writeFakeKuna } from "../providers/kuna/fakeKuna.js"; const FUNCTIONS = [ { name: "main", addr: "0x1000" }, { name: "parse", addr: "0x1040" }, { name: "late", addr: "0x1080" }, ] as const; describe("decompile-all CLI", () => { cliTest( "maps filters, limit and a relative output directory to the export", async ({ cli }) => { const root = await createTestTempDirectory("rea-decompile-all-cli-"); const kuna = await writeFakeKuna(root, { functions: FUNCTIONS }); const target = join(root, "target.elf"); await writeFile(target, elf(2, 1, 62)); const environment = { REA_LOG_LEVEL: "silent", REA_KUNA_COMMAND: kuna, REA_KUNA_CACHE_DIR: join(root, "cache"), }; const result = await cli.run({ arguments: [ "decompile-all", target, "--provider", "kuna", "--output", "out", "--address-start", "0X1000", "--address-end", "0X1040", "--limit", "1", "--format", "json", ], cwd: root, environment, }); expect(result.exitCode, JSON.stringify(result.json)).toBe(0); expect(result.json).toMatchObject({ operation: "decompile_all", parameters: { output_directory: join(root, "out"), address_start: "0X1000", address_end: "0X1040", limit: 1, }, normalized_result: { output_directory: join(root, "out"), // The fake inventory also lists the import puts at 0x2000. counts: { selected: 2, excluded: 2, written: 1, remaining: 1 }, }, }); expect(result.json).not.toHaveProperty("parameters.layout"); expect(result.json).not.toHaveProperty("parameters.resume"); expect(await readdir(join(root, "out", "functions"))).toEqual([ "0x1000.c", ]); expect(JSON.stringify(result.json)).not.toContain( await readFile(join(root, "out", "functions", "0x1000.c"), "utf8"), ); const resumed = await cli.run({ arguments: [ "decompile-all", target, "--provider", "kuna", "--output", "out", "--resume", "--layout", "address", "--address-start", "0X1000", "--address-end", "0X1040", "--limit", "100001", "--format", "json", ], cwd: root, environment, }); expect(resumed.exitCode, JSON.stringify(resumed.json)).toBe(0); expect(resumed.json).toMatchObject({ parameters: { limit: 100001, resume: true, layout: "address" }, normalized_result: { counts: { selected: 2, written: 1, skipped: 1, remaining: 0 }, }, }); // Kuna lacks procedure_info, so every retained record keeps an // unavailable body. A resume that writes nothing still summarizes them. const bodyCaveat = expect.stringMatching( /^2 exported function record.*procedure_info is unavailable/u, ); expect(resumed.json).toMatchObject({ normalized_result: { limitations: [bodyCaveat] }, }); const noop = await cli.run({ arguments: [ "decompile-all", target, "--provider", "kuna", "--output", "out", "--resume", "--address-start", "0X1000", "--address-end", "0X1040", "--format", "json", ], cwd: root, environment, }); expect(noop.exitCode, JSON.stringify(noop.json)).toBe(0); expect(noop.json).toMatchObject({ normalized_result: { counts: { selected: 2, requested: 0, skipped: 2 }, limitations: [bodyCaveat], }, }); const index = decompileDirectoryIndexSchema.parse( JSON.parse( await readFile(join(root, "out", "decompile-index.json"), "utf8"), ), ); expect(index.limitations).toEqual([bodyCaveat]); expect(index.functions.map((record) => record.body.available)).toEqual([ false, false, ]); }, ); });