test(ghidra): verify metadata reads and doctor probe admission (#12911)

test(ghidra): prove metadata descriptor and probe admission
This commit is contained in:
morluto authored and GitHub committed 2026-11-20 14:35:04 -05:00
1 parent 0bca5dbbe8
commit fd3f10a0eb
3 files changed
+42 -18

No files matched your search

@@ -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: {
@@ -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<typeof file.read>) => {
@@ -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<typeof handle.read>) => {
@@ -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;