import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, test } from "vitest"; import { GhidraHeadlessLauncher, GhidraLaunchError, type GhidraLaunchSession, } from "./GhidraLauncher.js"; import { bindGhidraSessionFailure } from "./GhidraSessionError.js"; // The POSIX `bin/java` fixture cannot execute on Windows (extensionless sh script // handed to execFile), so these tests only run on POSIX hosts. const it = test.skipIf(process.platform === "win32"); /** Projection bound per launcher-command stream asserted by the tests. */ const EXPECTED_OUTPUT_BOUND_BYTES = 8 * 1024; const launchSession = (runtimeRoot: string): GhidraLaunchSession => ({ runtimeRoot, transport: "unix-socket", endpointPath: join(runtimeRoot, "endpoint.sock"), token: "test-token", runId: "test-run", targetPath: join(runtimeRoot, "target.bin"), targetSha256: "0".repeat(64), providerVersion: "test", profileDigest: "test", }); /** Stand-in "java" whose LaunchSupport probe fails the way a broken JVM does. */ const failingJavaHome = async (script: string): Promise => { const javaHome = await mkdtemp(join(tmpdir(), "rea-fake-java-")); await mkdir(join(javaHome, "bin"), { recursive: true }); await writeFile(join(javaHome, "bin", "java"), script, { mode: 0o755 }); return javaHome; }; const probeLauncher = (javaHome: string, runtimeRoot: string) => new GhidraHeadlessLauncher({ environment: {}, platform: "darwin", analyzeHeadlessPath: join(runtimeRoot, "support", "analyzeHeadless"), javaHome, bridgeScriptPath: join(runtimeRoot, "bridge", "ReaGhidraBridge.java"), }); const launchOutcomeOf = (error: Error): Record | undefined => { const sessionError = bindGhidraSessionFailure(() => ({}))( "start", error.message, error, ); const cause = sessionError.diagnostics["failure_cause"] as Record< string, unknown >; const outcome = cause["launch_outcome"] as Record; return outcome === undefined ? undefined : outcome; }; describe("Ghidra launch failure diagnostics", () => { it("retains the failed launcher command outcome on the launch error", async () => { const javaHome = await failingJavaHome( "#!/bin/sh\necho 'jvm probe exploded' >&2\nexit 3\n", ); const runtimeRoot = await mkdtemp(join(tmpdir(), "rea-ghidra-launch-")); try { const launched = await probeLauncher(javaHome, runtimeRoot).launch( launchSession(runtimeRoot), ); expect(launched.ok).toBe(false); if (launched.ok) throw new Error("expected the launch to fail"); expect(launched.error).toBeInstanceOf(GhidraLaunchError); expect(launched.error.message).toBe("Ghidra headless launch failed"); const captured = launched.error instanceof GhidraLaunchError ? launched.error.capturedOutput : undefined; expect(captured).toBeDefined(); expect(captured?.exit_code).toBe(3); expect(captured?.signal).toBeNull(); expect(captured?.stdout).toBe(""); expect(captured?.stderr).toContain("jvm probe exploded"); expect(captured?.truncated).toBe(false); } finally { await rm(javaHome, { recursive: true, force: true }); await rm(runtimeRoot, { recursive: true, force: true }); } }); it("surfaces the retained launcher outcome in the session failure_cause", async () => { const javaHome = await failingJavaHome( "#!/bin/sh\necho 'jvm probe exploded' >&2\nexit 3\n", ); const runtimeRoot = await mkdtemp(join(tmpdir(), "rea-ghidra-launch-")); try { const launched = await probeLauncher(javaHome, runtimeRoot).launch( launchSession(runtimeRoot), ); expect(launched.ok).toBe(false); if (launched.ok) throw new Error("expected the launch to fail"); const outcome = launchOutcomeOf(launched.error); expect(outcome).toBeDefined(); expect(outcome?.["exit_code"]).toBe(3); expect(outcome?.["signal"]).toBeNull(); expect(outcome?.["stderr"]).toContain("jvm probe exploded"); expect(outcome?.["truncated"]).toBe(false); } finally { await rm(javaHome, { recursive: true, force: true }); await rm(runtimeRoot, { recursive: true, force: true }); } }); it("bounds projected launcher output and marks truncation explicitly", async () => { const oversized = `${"x".repeat(EXPECTED_OUTPUT_BOUND_BYTES)}UNRETAINED-TAIL`; const javaHome = await failingJavaHome( `#!/bin/sh\nprintf '%s' '${oversized}' >&2\nexit 1\n`, ); const runtimeRoot = await mkdtemp(join(tmpdir(), "rea-ghidra-launch-")); try { const launched = await probeLauncher(javaHome, runtimeRoot).launch( launchSession(runtimeRoot), ); expect(launched.ok).toBe(false); if (launched.ok) throw new Error("expected the launch to fail"); const outcome = launchOutcomeOf(launched.error); expect(outcome?.["truncated"]).toBe(true); expect(outcome?.["exit_code"]).toBe(1); expect(outcome?.["stderr_bytes"]).toBe(oversized.length); expect((outcome?.["stderr"] as string).length).toBeLessThanOrEqual( EXPECTED_OUTPUT_BOUND_BYTES, ); expect(outcome?.["stderr"] as string).not.toContain("UNRETAINED-TAIL"); } finally { await rm(javaHome, { recursive: true, force: true }); await rm(runtimeRoot, { recursive: true, force: true }); } }); it("leaves non-command launch failures without retained output", async () => { const runtimeRoot = await mkdtemp(join(tmpdir(), "rea-ghidra-launch-")); try { const launcher = new GhidraHeadlessLauncher({ environment: {}, platform: "darwin", analyzeHeadlessPath: join(runtimeRoot, "support", "analyzeHeadless"), bridgeScriptPath: join(runtimeRoot, "bridge", "ReaGhidraBridge.java"), }); const launched = await launcher.launch(launchSession(runtimeRoot)); expect(launched.ok).toBe(false); if (launched.ok) throw new Error("expected the launch to fail"); expect(launched.error).toBeInstanceOf(GhidraLaunchError); expect(launched.error.message).toBe("Ghidra headless launch failed"); expect( launched.error instanceof GhidraLaunchError ? launched.error.capturedOutput : undefined, ).toBeUndefined(); expect(launchOutcomeOf(launched.error)).toBeUndefined(); } finally { await rm(runtimeRoot, { recursive: true, force: true }); } }); });