From d1242fa30b2c9d18631fc2f37b2a1d615f92a9af Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:58:14 -0500 Subject: [PATCH] test(ghidra): consolidate saved project selection and history (#12920) --- src/ghidra/GhidraProvider.behavior.test.ts | 82 +++++++++---- src/ghidra/GhidraRiscvProfile.test.ts | 17 +-- .../ghidra/ghidraProjectSnapshot.test.ts | 112 ++++++++---------- 3 files changed, 109 insertions(+), 102 deletions(-) diff --git a/src/ghidra/GhidraProvider.behavior.test.ts b/src/ghidra/GhidraProvider.behavior.test.ts index e34c735da4..8ce391c36c 100644 --- a/src/ghidra/GhidraProvider.behavior.test.ts +++ b/src/ghidra/GhidraProvider.behavior.test.ts @@ -6964,31 +6964,54 @@ it("keeps saved project reports as shared historical Evidence without applying f { name: "Decompiler Parameter ID", status: "disabled" as const }, ], }, + riscv_calling_convention: { + policy: "ilp32-prototype-v1" as const, + functions: 11, + definitions: 8, + }, }; const close = vi.fn(async () => ok(null)); + let options: GhidraHeadlessLauncher["options"] | undefined; const ghidra = provider( installationHost(), - () => ({ - start: async () => - ok({ - ...sessionInfo(), - read_only: true, - existing_project: true as const, - analysis_complete: null, - ...prior, - capabilities: GHIDRA_SESSION_CAPABILITIES.filter( - (name) => !GHIDRA_MUTATING_OPERATIONS.has(name), - ), - }), - callTool: async () => ok([]), - close, - }), + ({ launcher }) => { + if (!(launcher instanceof GhidraHeadlessLauncher)) + throw new Error("Expected the saved-project launcher"); + options = launcher.options; + return { + start: async () => + ok({ + ...sessionInfo(), + target: { + ...sessionInfo().target, + language_id: "RISCV:LE:32:default", + }, + read_only: true, + existing_project: true as const, + analysis_complete: null, + ...prior, + capabilities: GHIDRA_SESSION_CAPABILITIES.filter( + (name) => !GHIDRA_MUTATING_OPERATIONS.has(name), + ), + }), + callTool: async () => ok([]), + close, + }; + }, { REA_GHIDRA_PROJECT_PATH: join(root, "history.gpr") }, ); onTestFinished(async () => { await ghidra.closeOwnedResources(); }); - const target = executableTarget("elf", "x86_64"); + const target: BinaryTarget = { + path: "/tmp/fixture", + sha256: "a".repeat(64), + kind: "executable", + format: "elf", + architecture: "riscv", + availableArchitectures: ["riscv"], + riscv: { elfClass: 32, byteOrder: "little", type: 2, flags: 1 }, + }; const resolved = await ghidra.resolveAnalysisProfile(target); if (!resolved.ok || resolved.value.profile === null) throw new Error("expected existing profile"); @@ -6999,16 +7022,31 @@ it("keeps saved project reports as shared historical Evidence without applying f const first = await client.execute("list_procedures", {}); if (!first.ok) throw first.error; expect(close).not.toHaveBeenCalled(); + expect(options).toMatchObject({ + existingProject: { projectName: "history" }, + }); + for (const freshOverride of [ + "analysisSeeds", + "disabledAnalyzers", + "riscvCallingConvention", + ]) + expect(options).not.toHaveProperty(freshOverride); + expect(first.value.sourceEvidence).toHaveLength(1); - expect(first.value.sourceEvidence?.[0]).toMatchObject({ + const history = first.value.sourceEvidence?.[0]; + if (history === undefined) throw new Error("Expected historical Evidence"); + expect(history).toMatchObject({ operation: "health", + confidence: "observed", normalized_result: { origin: "caller-project", ...prior }, }); - const repeated = await client.execute("list_procedures", {}); - if (!repeated.ok) throw repeated.error; - expect(repeated.value.sourceEvidence?.[0]).toBe( - first.value.sourceEvidence?.[0], - ); + const search = await client.execute("search_procedures", { + pattern: "fixture", + }); + if (!search.ok) throw search.error; + expect( + search.value.sourceEvidence?.map((record) => record.evidence_id), + ).toEqual([history.evidence_id]); }); it.each(["search_procedures", "search_strings"] as const)( diff --git a/src/ghidra/GhidraRiscvProfile.test.ts b/src/ghidra/GhidraRiscvProfile.test.ts index 3002bb3fea..d27a504372 100644 --- a/src/ghidra/GhidraRiscvProfile.test.ts +++ b/src/ghidra/GhidraRiscvProfile.test.ts @@ -25,11 +25,7 @@ import { ghidraRiscvUnsupportedReason, type GhidraRiscvCallingConventionReport, } from "./GhidraRiscvProfile.js"; -import { ghidraProjectHistoryEvidence } from "./GhidraProjectHistory.js"; -import { - GHIDRA_SESSION_CAPABILITIES, - type GhidraSessionInfo, -} from "./GhidraSessionValues.js"; +import { GHIDRA_SESSION_CAPABILITIES } from "./GhidraSessionValues.js"; import { createSimulatedInstallDirectory, ghidraProfileInstallationHost as host, @@ -803,15 +799,4 @@ describe("RISC-V calling-convention handshake", () => { expect(counts.starts).toBe(0); } }); - - it("keeps a caller project's stored report as history", async () => { - const resolved = await ghidra().resolveAnalysisProfile(ilp32()); - if (!resolved.ok || resolved.value.profile === null) - throw new Error("expected an ilp32 profile"); - const info: GhidraSessionInfo = sessionInfo(report); - expect( - ghidraProjectHistoryEvidence(ilp32(), resolved.value.profile, info) - ?.normalized_result, - ).toEqual({ origin: "caller-project", riscv_calling_convention: report }); - }); }); diff --git a/tests/boundary/providers/ghidra/ghidraProjectSnapshot.test.ts b/tests/boundary/providers/ghidra/ghidraProjectSnapshot.test.ts index d39bce8718..5f136ae0df 100644 --- a/tests/boundary/providers/ghidra/ghidraProjectSnapshot.test.ts +++ b/tests/boundary/providers/ghidra/ghidraProjectSnapshot.test.ts @@ -3,6 +3,7 @@ import { rm, mkdir, readFile, + readlink, readdir, realpath, stat, @@ -29,10 +30,10 @@ beforeEach(() => { afterEach(async () => { await resources.close(); }); -const fixture = async () => { +const fixture = async ({ sharedParent = false } = {}) => { const root = await createTestTempDirectory("rea-ghidra-selected-project-"); const source = join(root, "source"); - const destination = join(root, "runtime", "project"); + const destination = join(sharedParent ? source : root, "runtime", "project"); await mkdir(join(source, "selected.rep"), { recursive: true }); await mkdir(destination, { recursive: true }); await writeFile(join(source, "selected.gpr"), "marker"); @@ -40,42 +41,53 @@ const fixture = async () => { return { source, destination }; }; -it.skipIf(process.platform === "win32")( - "copies and fingerprints only the selected project, leaving unrelated siblings untouched", - async () => { - const { source, destination } = await fixture(); - await symlink("unrelated-missing-project", join(source, "unrelated.gpr")); - const commitment = await resolveGhidraProject( - { path: join(source, "selected.gpr"), programName: "target.elf" }, - { path: "/tmp/target.elf" }, - resources, - ); - await copyExistingGhidraProject(source, destination, { - projectName: "selected", - expectedSha256: commitment.sha256, - resources, - }); - expect((await readdir(destination)).sort()).toEqual([ - "selected.gpr", - "selected.rep", - ]); - expect(await fingerprintGhidraProject(source, "selected", resources)).toBe( - commitment.sha256, - ); - expect( - await readFile(join(destination, "selected.rep", "data"), "utf8"), - ).toBe("original program"); - }, -); +it("copies only the selected project beside the caller's project and its unrelated entries", async () => { + const { source, destination } = await fixture({ sharedParent: true }); + const unrelated = join(source, "unrelated.gpr"); + if (process.platform === "win32") + await writeFile(unrelated, "unrelated marker"); + else await symlink("unrelated-missing-project", unrelated); -it("rejects a project whose copied content differs from the committed analysis profile", async () => { + const commitment = await resolveGhidraProject( + { path: join(source, "selected.gpr"), programName: "target.elf" }, + { path: "/tmp/target.elf" }, + resources, + ); + await copyExistingGhidraProject(source, destination, { + projectName: "selected", + expectedSha256: commitment.sha256, + resources, + }); + expect((await readdir(destination)).sort()).toEqual([ + "selected.gpr", + "selected.rep", + ]); + expect(await readFile(join(destination, "selected.gpr"), "utf8")).toBe( + "marker", + ); + expect( + await readFile(join(destination, "selected.rep", "data"), "utf8"), + ).toBe("original program"); + expect(await fingerprintGhidraProject(source, "selected", resources)).toBe( + commitment.sha256, + ); + if (process.platform === "win32") + expect(await readFile(unrelated, "utf8")).toBe("unrelated marker"); + else expect(await readlink(unrelated)).toBe("unrelated-missing-project"); +}); + +it("rejects a copy whose last block differs from the committed project", async () => { const { source, destination } = await fixture(); + const data = join(source, "selected.rep", "data"); + const contents = Buffer.alloc(64 * 1024 + 1, 0x61); + await writeFile(data, contents); const original = await fingerprintGhidraProject( source, "selected", resources, ); - await writeFile(join(source, "selected.rep", "data"), "modified program"); + contents[contents.length - 1] = 0x62; + await writeFile(data, contents); expect( await fingerprintGhidraProject(source, "selected", resources), ).not.toBe(original); @@ -97,6 +109,7 @@ it("parses an opt-in project once for shared CLI/MCP configuration and rejects i const environment = { REA_GHIDRA_PROJECT_PATH: join(process.cwd(), "existing.gpr"), REA_GHIDRA_PROGRAM_NAME: "program.elf", + REA_GHIDRA_DISABLED_ANALYZERS: ", ,", }; expect(parseConfig(environment)).toMatchObject({ ok: true, @@ -108,6 +121,7 @@ it("parses an opt-in project once for shared CLI/MCP configuration and rejects i }, }); for (const extra of [ + { REA_GHIDRA_DISABLED_ANALYZERS: "Decompiler Parameter ID" }, { REA_GHIDRA_LANGUAGE_ID: "x86:LE:64:default" }, { REA_GHIDRA_SEED_FILE: join(process.cwd(), "seeds") }, { REA_GHIDRA_PROGRAM_NAME: "*" }, @@ -118,22 +132,6 @@ it("parses an opt-in project once for shared CLI/MCP configuration and rejects i ); }); -it("accepts a blank analyzer override beside an existing project and rejects a named one", () => { - const environment = { - REA_GHIDRA_PROJECT_PATH: join(process.cwd(), "existing.gpr"), - }; - for (const blank of ["", " ", ", ,"]) - expect( - parseConfig({ ...environment, REA_GHIDRA_DISABLED_ANALYZERS: blank }).ok, - ).toBe(true); - expect( - parseConfig({ - ...environment, - REA_GHIDRA_DISABLED_ANALYZERS: "Decompiler Parameter ID", - }).ok, - ).toBe(false); -}); - it("reports an invalid derived program name at its selection boundary", async () => { const { source } = await fixture(); await expect( @@ -169,22 +167,6 @@ it("derives a bundle program from the resolved executable while preserving expli ).toBe("CallerSelected"); }); -it("allows selected project files beside an owned runtime under the same parent", async () => { - const { source } = await fixture(); - const destination = join(source, "owned-runtime", "project"); - await mkdir(destination, { recursive: true }); - const sha256 = await fingerprintGhidraProject(source, "selected", resources); - await copyExistingGhidraProject(source, destination, { - projectName: "selected", - expectedSha256: sha256, - resources, - }); - expect((await readdir(destination)).sort()).toEqual([ - "selected.gpr", - "selected.rep", - ]); -}); - it.each([null, "caller stopped", { kind: "stop-project-read" }])( "preserves project cancellation and its exact reason: %j", async (reason) => { @@ -250,9 +232,11 @@ it.skipIf(process.platform === "win32")( project_path: await realpath(join(root, "actual")), project_name: "project", }); + await writeFile(join(root, "missing.gpr"), "sibling decoy"); + await mkdir(join(root, "missing.rep")); await expect( resolveGhidraProject( - { path: join(root, "missing.gpr") }, + { path: `${root}/link/../missing.gpr` }, { path: join(root, "target") }, resources, ),