diff --git a/src/ghidra/GhidraDisabledAnalyzers.test.ts b/src/ghidra/GhidraDisabledAnalyzers.test.ts index aaf339c038..bdb5c98a9a 100644 --- a/src/ghidra/GhidraDisabledAnalyzers.test.ts +++ b/src/ghidra/GhidraDisabledAnalyzers.test.ts @@ -1,81 +1,56 @@ import { describe, expect, it } from "vitest"; -import { MAX_GHIDRA_DISABLED_ANALYZER_NAMES } from "../config/ghidraDisabledAnalyzers.js"; import { parseConfig } from "../config/parseConfig.js"; const INSTALL = "/opt/ghidra_12.1.4_PUBLIC"; - -const configured = (setting?: string) => { - const config = parseConfig({ +const parseSetting = (setting?: string) => + parseConfig({ GHIDRA_INSTALL_DIR: INSTALL, ...(setting === undefined ? {} : { REA_GHIDRA_DISABLED_ANALYZERS: setting }), }); + +const configured = (setting?: string) => { + const config = parseSetting(setting); if (!config.ok) throw config.error; return config.value.ghidraDisabledAnalyzers; }; describe("REA_GHIDRA_DISABLED_ANALYZERS", () => { - it("stays unset without the setting", () => { - expect(configured(undefined)).toBeUndefined(); + it("leaves absent, empty, and whitespace-only settings unset", () => { + for (const setting of [undefined, "", " , "]) + expect(configured(setting)).toBeUndefined(); }); - it("splits a comma-separated list and trims each name", () => { + it("trims analyzer names and drops empty fields in order", () => { expect( - configured(" Objective-C Message Analyzer,Decompiler Switch Analysis "), + configured(" Objective-C Message Analyzer, ,Decompiler Switch Analysis "), ).toEqual(["Objective-C Message Analyzer", "Decompiler Switch Analysis"]); }); - it("treats an empty or blank list as unset", () => { - expect(configured("")).toBeUndefined(); - expect(configured(" , ")).toBeUndefined(); - }); - - it("keeps empty names out of the list instead of failing", () => { - expect( - configured("Objective-C Message Analyzer,,Decompiler Switch Analysis"), - ).toEqual(["Objective-C Message Analyzer", "Decompiler Switch Analysis"]); + const namesAtLimit = [ + ...Array.from({ length: 31 }, (_, index) => `Analyzer ${index}`), + "A".repeat(120), + ]; + it("accepts 32 analyzers and a 120-character name", () => { + expect(configured(namesAtLimit.join(","))).toEqual(namesAtLimit); }); it.each([ ["a leading flag name", "-Decompiler Switch Analysis"], ["a name with command metacharacters", "Analyzer(&calc)"], ["a non-ASCII name", "分析器"], - ["an over-long name", "A".repeat(121)], + ["a 121-character name", "A".repeat(121)], + ["33 analyzer names", [...namesAtLimit, "Extra Analyzer"].join(",")], ])("rejects %s", (_label, setting) => { - const config = parseConfig({ - GHIDRA_INSTALL_DIR: INSTALL, - REA_GHIDRA_DISABLED_ANALYZERS: setting, - }); - expect(config.ok).toBe(false); - if (!config.ok) - expect(config.error).toMatchObject({ + expect(parseSetting(setting)).toMatchObject({ + ok: false, + error: { settings: [ - expect.objectContaining({ - setting: "REA_GHIDRA_DISABLED_ANALYZERS", - }), + expect.objectContaining({ setting: "REA_GHIDRA_DISABLED_ANALYZERS" }), ], - }); - }); - - it("rejects more than the admitted analyzer count", () => { - const setting = Array.from( - { length: MAX_GHIDRA_DISABLED_ANALYZER_NAMES + 1 }, - (_, index) => `Analyzer ${index}`, - ).join(","); - const config = parseConfig({ - GHIDRA_INSTALL_DIR: INSTALL, - REA_GHIDRA_DISABLED_ANALYZERS: setting, + }, }); - expect(config.ok).toBe(false); - if (!config.ok) - expect(config.error).toMatchObject({ - settings: [ - expect.objectContaining({ - setting: "REA_GHIDRA_DISABLED_ANALYZERS", - }), - ], - }); }); }); diff --git a/src/ghidra/GhidraProvider.behavior.test.ts b/src/ghidra/GhidraProvider.behavior.test.ts index 977b9d2c3b..8bd6ddfdcd 100644 --- a/src/ghidra/GhidraProvider.behavior.test.ts +++ b/src/ghidra/GhidraProvider.behavior.test.ts @@ -5341,63 +5341,6 @@ const analyzerReport = ( }); describe("Ghidra disabled analyzer options", () => { - it("records the configured analyzer options in the analysis profile", async () => { - const ghidra = configuredProvider({ - REA_GHIDRA_DISABLED_ANALYZERS: - "Objective-C Message Analyzer,Decompiler Switch Analysis", - }); - const resolved = await ghidra.resolveAnalysisProfile( - executableTarget("elf", "x86_64"), - ); - if (!resolved.ok || resolved.value.profile === null) - throw new Error("expected a bound Ghidra profile"); - expect(resolved.value.profile.parameters).toMatchObject({ - analyzer_preset: - "ghidra-default+disabled:Objective-C Message Analyzer,Decompiler Switch Analysis", - analysis_options: { - disabled_analyzers: [ - "Objective-C Message Analyzer", - "Decompiler Switch Analysis", - ], - }, - }); - }); - - it("hands the configured analyzers to the launcher for a matching profile", async () => { - const launchers: GhidraHeadlessLauncher[] = []; - const ghidra = configuredProvider( - { REA_GHIDRA_DISABLED_ANALYZERS: "Objective-C Message Analyzer" }, - (options) => { - launchers.push(options.launcher as GhidraHeadlessLauncher); - return { - start: () => - Promise.resolve( - ok({ - ...sessionInfo(), - disabled_analyzers: analyzerReport([ - "Objective-C Message Analyzer", - "not_applicable", - ]), - }), - ), - callTool: () => Promise.resolve(ok(null)), - close: () => Promise.resolve(ok(null)), - }; - }, - ); - const target = executableTarget("elf", "x86_64"); - const resolved = await ghidra.resolveAnalysisProfile(target); - if (!resolved.ok || resolved.value.profile === null) - throw new Error("expected a bound Ghidra profile"); - const client = ghidra.createClient(target, resolved.value.profile); - const health = await client.execute("health", {}); - expect(health.ok).toBe(true); - expect(launchers).toHaveLength(1); - expect(launchers[0]?.options.disabledAnalyzers).toEqual([ - "Objective-C Message Analyzer", - ]); - }); - it("binds analyzer overrides and raw import settings in one profile", async () => { const launchers: GhidraHeadlessLauncher[] = []; const ghidra = configuredProvider( @@ -5440,6 +5383,9 @@ describe("Ghidra disabled analyzer options", () => { analysis_options: { disabled_analyzers: ["Decompiler Switch Analysis"] }, }); const client = ghidra.createClient(target, resolved.value.profile); + onTestFinished(async () => { + await client.close(); + }); expect(launchers).toHaveLength(0); expect((await client.execute("health", {})).ok).toBe(true); expect(launchers).toHaveLength(1); @@ -5451,7 +5397,6 @@ describe("Ghidra disabled analyzer options", () => { baseAddr: "0x1000", }, }); - await client.close(); }); it("refuses a profile resolved without or with other analyzer options", async () => { @@ -5500,39 +5445,45 @@ describe("Ghidra disabled analyzer applicability per program", () => { report: GhidraDisabledAnalyzerReport | undefined, ) => { const counts = { starts: 0, calls: 0, closes: 0 }; + const launches: GhidraHeadlessLauncher[] = []; const ghidra = configuredProvider( setting === undefined ? {} : { REA_GHIDRA_DISABLED_ANALYZERS: setting }, - () => ({ - start: () => { - counts.starts++; - return Promise.resolve( - ok({ - ...sessionInfo(), - ...(report === undefined ? {} : { disabled_analyzers: report }), - }), - ); - }, - callTool: () => { - counts.calls++; - return Promise.resolve( - ok([ - { - address: "0x1000", - value: "fixture_main", - procedure: { - external: false, - thunk: false, - thunk_target: null, + ({ launcher }) => { + if (!(launcher instanceof GhidraHeadlessLauncher)) + throw new Error("Expected the Ghidra launcher"); + launches.push(launcher); + return { + start: () => { + counts.starts++; + return Promise.resolve( + ok({ + ...sessionInfo(), + ...(report === undefined ? {} : { disabled_analyzers: report }), + }), + ); + }, + callTool: () => { + counts.calls++; + return Promise.resolve( + ok([ + { + address: "0x1000", + value: "fixture_main", + procedure: { + external: false, + thunk: false, + thunk_target: null, + }, }, - }, - ]), - ); - }, - close: () => { - counts.closes++; - return Promise.resolve(ok(null)); - }, - }), + ]), + ); + }, + close: () => { + counts.closes++; + return Promise.resolve(ok(null)); + }, + }; + }, ); const target = executableTarget("elf", "x86_64"); const resolved = await ghidra.resolveAnalysisProfile(target); @@ -5540,6 +5491,7 @@ describe("Ghidra disabled analyzer applicability per program", () => { throw new Error("expected a bound Ghidra profile"); return { counts, + launches, profile: resolved.value.profile, client: ghidra.createClient(target, resolved.value.profile), }; @@ -5559,7 +5511,7 @@ describe("Ghidra disabled analyzer applicability per program", () => { }); it("opens a program for which a configured analyzer is not applicable", async () => { - const { counts, profile, client } = await analyzerSession( + const { counts, launches, profile, client } = await analyzerSession( "Decompiler Switch Analysis,Objective-C Message Analyzer", analyzerReport( ["Decompiler Switch Analysis", "disabled"], @@ -5568,10 +5520,19 @@ describe("Ghidra disabled analyzer applicability per program", () => { ); const procedures = await client.execute("list_procedures", {}); expect(counts.calls).toBe(1); + expect( + launches.map((launcher) => launcher.options.disabledAnalyzers), + ).toEqual([["Decompiler Switch Analysis", "Objective-C Message Analyzer"]]); if (!procedures.ok) throw procedures.error; expect(procedures.value.analysisProfile?.parameters).toMatchObject({ analyzer_preset: "ghidra-default+disabled:Decompiler Switch Analysis,Objective-C Message Analyzer", + analysis_options: { + disabled_analyzers: [ + "Decompiler Switch Analysis", + "Objective-C Message Analyzer", + ], + }, }); expect(procedures.value.analysisProfile?.digest).toBe(profile.digest); expect(procedures.value.limitations).toEqual( @@ -5587,13 +5548,23 @@ describe("Ghidra disabled analyzer applicability per program", () => { it.each([ ["no analyzer report", undefined], [ - "a report for other analyzers", - analyzerReport(["Decompiler Switch Analysis", "not_applicable"]), + "a report replacing a configured analyzer", + analyzerReport( + ["Objective-C Message Analyzer", "not_applicable"], + ["Decompiler Switch Analysis", "disabled"], + ), ], [ "a report missing a configured analyzer", analyzerReport(["Objective-C Message Analyzer", "not_applicable"]), ], + [ + "a reordered report", + analyzerReport( + ["Demangler Swift", "disabled"], + ["Objective-C Message Analyzer", "not_applicable"], + ), + ], ])("fails closed on %s before any analysis call", async (_label, report) => { const { counts, client } = await analyzerSession( "Objective-C Message Analyzer,Demangler Swift", diff --git a/tests/boundary/providers/ghidra/ghidraLauncher.test.ts b/tests/boundary/providers/ghidra/ghidraLauncher.test.ts index 0ec344d020..7488406e39 100644 --- a/tests/boundary/providers/ghidra/ghidraLauncher.test.ts +++ b/tests/boundary/providers/ghidra/ghidraLauncher.test.ts @@ -657,36 +657,32 @@ describe("Ghidra disabled analyzers", () => { const seeds = arguments_.indexOf( posix.join("/package/bridge", "ReaGhidraApplySeeds.java"), ); + expect(prepare).toBeGreaterThanOrEqual(0); expect(prepare).toBeLessThan(seeds); expect(seeds).toBeLessThan(options); expect(options).toBeLessThan(arguments_.indexOf("-postScript")); }); - it.each(["darwin", "win32"] as const)( - "uses the target platform's script path for the options script on %s", - (platform) => { - const path = platform === "win32" ? win32 : posix; - const arguments_ = ghidraHeadlessArguments({ - ...base, - platform, - bridgeScriptPath: path.join( - "/package", - "bridge", - "ReaGhidraBridge.java", - ), - disabledAnalyzers: ["Objective-C Message Analyzer"], - }); - const options = arguments_.indexOf( - path.join("/package", "bridge", "ReaGhidraAnalysisOptions.java"), - ); - expect(arguments_[options - 1]).toBe("-preScript"); - expect(arguments_[options + 1]).toBe("Objective-C Message Analyzer"); - expect(options).toBeLessThan(arguments_.indexOf("-postScript")); - }, - ); + it("passes the Windows script path and one literal analyzer argument", () => { + const arguments_ = ghidraHeadlessArguments({ + ...base, + platform: "win32", + bridgeScriptPath: "C:\\package\\bridge\\ReaGhidraBridge.java", + disabledAnalyzers: ["Objective-C Message Analyzer"], + }); + const script = "C:\\package\\bridge\\ReaGhidraAnalysisOptions.java"; + const options = arguments_.indexOf(script); + expect(arguments_.slice(options - 1, options + 2)).toEqual([ + "-preScript", + script, + "Objective-C Message Analyzer", + ]); + expect(options).toBeLessThan(arguments_.indexOf("-postScript")); + }); - it("keeps the invocation byte-identical without an override", () => { - expect(ghidraHeadlessArguments(base)).toEqual([ + it("keeps the invocation byte-identical with no configured analyzer names", () => { + const arguments_ = ghidraHeadlessArguments(base); + expect(arguments_).toEqual([ "/tmp/project", "rea-project", "-import", @@ -705,13 +701,8 @@ describe("Ghidra disabled analyzers", () => { "/package/bridge/ReaGhidraBridge.java", "/tmp/session.json", ]); - }); - - it("adds no analyzer script for an empty override", () => { - expect( - ghidraHeadlessArguments({ ...base, disabledAnalyzers: [] }), - ).not.toContain( - posix.join("/package/bridge", "ReaGhidraAnalysisOptions.java"), + expect(ghidraHeadlessArguments({ ...base, disabledAnalyzers: [] })).toEqual( + arguments_, ); }); });