From fd3f10a0eb4ae0acbd6f4ca7a6975bb430775cbc Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:35:04 -0500 Subject: [PATCH] test(ghidra): verify metadata reads and doctor probe admission (#12911) test(ghidra): prove metadata descriptor and probe admission --- .../cli/ghidraInstallationProperties.test.ts | 20 +++++++++-- .../filesystem/ghidraExecutableHeader.test.ts | 34 +++++++++++-------- .../ghidraInstallationProperties.test.ts | 6 +++- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/tests/boundary/cli/ghidraInstallationProperties.test.ts b/tests/boundary/cli/ghidraInstallationProperties.test.ts index 0edba30c03..f8fd73dcf0 100644 --- a/tests/boundary/cli/ghidraInstallationProperties.test.ts +++ b/tests/boundary/cli/ghidraInstallationProperties.test.ts @@ -1,6 +1,6 @@ import { execFile } from "node:child_process"; import { promisify } from "node:util"; -import { mkdir, truncate, writeFile } from "node:fs/promises"; +import { access, mkdir, truncate, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; @@ -18,6 +18,18 @@ cliTest.for(["pipe", "capacity"] as const)( const root = await createTestTempDirectory("rea-doctor-properties-"); const install = join(root, "ghidra"); const path = join(install, "Ghidra/application.properties"); + const javaHome = join(root, "java-home"); + const java = join(javaHome, "bin", "java"); + if (process.platform !== "win32") { + await mkdir(join(javaHome, "bin"), { recursive: true }); + await writeFile( + java, + '#!/bin/sh\nprintf "launched\\n" > "$0.was-run"\n', + { + mode: 0o700, + }, + ); + } await mkdir(join(install, "Ghidra"), { recursive: true }); if (kind === "pipe") await promisify(execFile)("mkfifo", [path]); else { @@ -28,12 +40,16 @@ cliTest.for(["pipe", "capacity"] as const)( arguments: ["doctor", "--provider", "ghidra", "--json"], environment: { GHIDRA_INSTALL_DIR: install, - JAVA_HOME: join(root, "missing-jdk"), + JAVA_HOME: javaHome, REA_LOG_LEVEL: "silent", }, }); expect(result.exitCode, `${result.stdout}\n${result.stderr}`).toBe(1); expect(result.signal).toBeNull(); + if (process.platform !== "win32") + await expect(access(`${java}.was-run`)).rejects.toMatchObject({ + code: "ENOENT", + }); expect(result.json).toMatchObject({ code: kind === "pipe" ? "execution_failure" : "resource_constraint", details: { diff --git a/tests/boundary/filesystem/ghidraExecutableHeader.test.ts b/tests/boundary/filesystem/ghidraExecutableHeader.test.ts index 871b1f839a..f38ae43fe1 100644 --- a/tests/boundary/filesystem/ghidraExecutableHeader.test.ts +++ b/tests/boundary/filesystem/ghidraExecutableHeader.test.ts @@ -38,10 +38,13 @@ it.each([0, 23, 8192])( async (length) => { const root = await createTestTempDirectory("rea-header-prefix-"); const path = join(root, "decompile"); - const bytes = Buffer.alloc(length, 0x42); + const bytes = Buffer.from( + Array.from({ length }, (_, index) => index % 251), + ); await writeFile(path, bytes); let bytesRead = 0; - controls.file = (file) => { + controls.file = (file, opened) => { + if (opened !== path) return; const read = file.read.bind(file); vi.spyOn(file, "read").mockImplementation(async (options) => { const result = await read({ @@ -54,18 +57,18 @@ it.each([0, 23, 8192])( }; const resources = createInspectionResources(); try { - expect( - await readGhidraExecutableHeader(path, { - resourceOwner: resources.owner, - }), - ).toEqual({ - ok: true, - value: { - bytes: bytes.subarray(0, 4096), - size: length, - }, + const result = await readGhidraExecutableHeader(path, { + resourceOwner: resources.owner, }); - expect(bytesRead).toBe(Math.min(length, 4096)); + if (!result.ok) throw result.error; + const examined = Math.min(length, 4096); + expect(result.value.size).toBe(length); + expect(result.value.bytes).toHaveLength(examined); + expect( + result.value.bytes.findIndex((value, index) => value !== bytes[index]), + "first mismatched prefix offset", + ).toBe(-1); + expect(bytesRead).toBe(examined); } finally { await resources.close(); } @@ -102,8 +105,9 @@ it.each(["replace", "grow", "shrink", "disappear"] as const)( const path = join(root, "decompile"), replacement = join(root, "replacement"); await writeFile(path, "original"); - await writeFile(replacement, "original"); - controls.file = (file) => { + if (change === "replace") await writeFile(replacement, "original"); + controls.file = (file, opened) => { + if (opened !== path) return; const read = file.read.bind(file); vi.spyOn(file, "read").mockImplementationOnce( async (...args: Parameters) => { diff --git a/tests/boundary/filesystem/ghidraInstallationProperties.test.ts b/tests/boundary/filesystem/ghidraInstallationProperties.test.ts index f01ac1a491..6513de6240 100644 --- a/tests/boundary/filesystem/ghidraInstallationProperties.test.ts +++ b/tests/boundary/filesystem/ghidraInstallationProperties.test.ts @@ -141,8 +141,10 @@ it("refuses an oversized descriptor before reading file bytes", async () => { await writeFile(path, properties); await truncate(path, MAX_GHIDRA_INSTALLATION_PROPERTIES_BYTES + 1); let reads = 0; + const admitted: FileHandle[] = []; controls.file = (handle, opened) => { if (opened === path) { + admitted.push(handle); const read = handle.read.bind(handle); vi.spyOn(handle, "read").mockImplementation( (...args: Parameters) => { @@ -185,8 +187,10 @@ it("refuses an oversized descriptor before reading file bytes", async () => { }, }); expect(reads).toBe(0); + expect(admitted.map(({ fd }) => fd)).toEqual([-1]); } finally { await resources.close(); + for (const handle of admitted) if (handle.fd !== -1) await handle.close(); } }); @@ -305,7 +309,7 @@ it.each(["replace", "grow", "shrink", "disappear"] as const)( const path = join(root, "application.properties"); const replacement = join(root, "replacement"); await writeFile(path, properties); - await writeFile(replacement, properties); + if (change === "replace") await writeFile(replacement, properties); controls.file = (handle, opened) => { if (opened !== path) return; controls.file = undefined;