mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
test(ghidra): consolidate raw profile and packaged producer proof (#13379)
This commit is contained in:
1 parent
ac7ae19cb2
commit
64d42d2971
3 files changed
+68
-108
No files matched your search
@@ -1,26 +0,0 @@
|
||||
import { readdir } from "node:fs/promises";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { packagedSourcesDigest } from "../filesystem/ProducerIdentity.js";
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
import { GHIDRA_BRIDGE_SOURCES } from "./GhidraBridgeSources.js";
|
||||
import { ghidraProducerIdentity } from "./GhidraProducerIdentity.js";
|
||||
|
||||
const directory = new URL("../../bridge/ghidra/", import.meta.url);
|
||||
|
||||
it("lists every packaged Ghidra bridge source", async () => {
|
||||
const java = (await readdir(directory)).filter((name) =>
|
||||
name.endsWith(".java"),
|
||||
);
|
||||
expect(Object.values(GHIDRA_BRIDGE_SOURCES).sort()).toEqual(java.sort());
|
||||
});
|
||||
|
||||
it("identifies Ghidra results by the REA release and every bridge source", () => {
|
||||
expect(ghidraProducerIdentity()).toEqual({
|
||||
rea_version: PRODUCT_IDENTITY.packageVersion,
|
||||
sources_sha256: packagedSourcesDigest(
|
||||
directory,
|
||||
Object.values(GHIDRA_BRIDGE_SOURCES),
|
||||
),
|
||||
});
|
||||
});
|
||||
@@ -1,79 +1,75 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { parseConfig } from "../config/parseConfig.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
||||
import { silentLogger } from "../logger.js";
|
||||
import { ghidraProfileInstallationHost as host } from "./GhidraInstallation.fixture.js";
|
||||
import { GhidraProvider } from "./GhidraProvider.js";
|
||||
import { ghidraProducerIdentity } from "./GhidraProducerIdentity.js";
|
||||
import { createAnalysisProfile } from "../domain/analysisProfile.js";
|
||||
import { packagedSourcesDigest } from "../filesystem/ProducerIdentity.js";
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
import { GHIDRA_BRIDGE_SOURCES } from "./GhidraBridgeSources.js";
|
||||
|
||||
// A profile digest is the digest of its committed parameters; these tests pin
|
||||
// the parameters (runtime selections and the producer identity) rather than a
|
||||
// literal hash that every bridge or REA release would have to re-record.
|
||||
const expectCommittedProfile = (
|
||||
profile: ReturnType<typeof createAnalysisProfile> | null | undefined,
|
||||
): void => {
|
||||
if (profile == null) throw new Error("Expected an analysis profile");
|
||||
expect(profile.parameters).toMatchObject({
|
||||
producer: ghidraProducerIdentity(),
|
||||
const rawProfile = async (target: BinaryTarget, disabled?: string) => {
|
||||
const config = parseConfig({
|
||||
GHIDRA_INSTALL_DIR: "/ghidra",
|
||||
...(disabled === undefined
|
||||
? {}
|
||||
: { REA_GHIDRA_DISABLED_ANALYZERS: disabled }),
|
||||
});
|
||||
expect(profile.digest).toBe(
|
||||
createAnalysisProfile(profile.provider, profile.parameters).digest,
|
||||
);
|
||||
if (!config.ok) throw config.error;
|
||||
const provider = new GhidraProvider(config.value, silentLogger, {}, host);
|
||||
const resolved = await provider.resolveAnalysisProfile(target);
|
||||
if (!resolved.ok) throw resolved.error;
|
||||
if (resolved.value.profile === null)
|
||||
throw new Error("Missing raw analysis profile");
|
||||
return resolved.value.profile;
|
||||
};
|
||||
|
||||
const configFor = (environment: NodeJS.ProcessEnv) => {
|
||||
const parsed = parseConfig({ GHIDRA_INSTALL_DIR: "/ghidra", ...environment });
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
return parsed.value;
|
||||
};
|
||||
|
||||
const raw = (architecture: "6502" | "x86", entryPoint: string): BinaryTarget =>
|
||||
({
|
||||
it("commits the raw entry and analyzer settings independently of the carrier", async () => {
|
||||
const target = {
|
||||
path: "/tmp/raw.bin",
|
||||
sha256: "a".repeat(64),
|
||||
kind: "executable",
|
||||
format: "raw-binary",
|
||||
architecture,
|
||||
availableArchitectures: [architecture],
|
||||
entryPoint,
|
||||
entryPointSource: entryPoint,
|
||||
}) as unknown as BinaryTarget;
|
||||
|
||||
describe("raw-binary analysis profile commitments", () => {
|
||||
it("commits the 6502 raw profile to the current shared policy", async () => {
|
||||
const ghidra = new GhidraProvider(configFor({}), silentLogger, {}, host);
|
||||
const resolved = await ghidra.resolveAnalysisProfile(raw("6502", "0x8001"));
|
||||
if (!resolved.ok) throw resolved.error;
|
||||
expectCommittedProfile(resolved.value.profile);
|
||||
expect(resolved.value.profile?.parameters).toMatchObject({
|
||||
target_format: "raw-binary",
|
||||
loader: "BinaryLoader",
|
||||
language_id: "6502:LE:16:default",
|
||||
language_selection: "caller-supplied",
|
||||
entry_point: "0x8001",
|
||||
analyzer_preset: "ghidra-default",
|
||||
});
|
||||
architecture: "6502",
|
||||
availableArchitectures: ["6502"],
|
||||
entryPoint: "0x8001",
|
||||
entryPointSource: "0x8001",
|
||||
} as const satisfies BinaryTarget;
|
||||
const analyzer = "Aggressive Instruction Finder";
|
||||
const producer = {
|
||||
rea_version: PRODUCT_IDENTITY.packageVersion,
|
||||
sources_sha256: packagedSourcesDigest(
|
||||
new URL("../../bridge/ghidra/", import.meta.url),
|
||||
Object.values(GHIDRA_BRIDGE_SOURCES),
|
||||
),
|
||||
};
|
||||
const standard = await rawProfile(target);
|
||||
const shifted = await rawProfile({
|
||||
...target,
|
||||
entryPoint: "0x8002",
|
||||
entryPointSource: "0x8002",
|
||||
});
|
||||
|
||||
it("keeps the disabled-analyzer raw digest and its analysis options", async () => {
|
||||
const ghidra = new GhidraProvider(
|
||||
configFor({
|
||||
REA_GHIDRA_DISABLED_ANALYZERS: "Aggressive Instruction Finder",
|
||||
}),
|
||||
silentLogger,
|
||||
{},
|
||||
host,
|
||||
);
|
||||
const resolved = await ghidra.resolveAnalysisProfile(raw("x86", "0x1000"));
|
||||
if (!resolved.ok) throw resolved.error;
|
||||
expectCommittedProfile(resolved.value.profile);
|
||||
expect(resolved.value.profile?.parameters).toMatchObject({
|
||||
analyzer_preset: expect.any(String),
|
||||
analysis_options: {
|
||||
disabled_analyzers: ["Aggressive Instruction Finder"],
|
||||
},
|
||||
});
|
||||
const custom = await rawProfile(target, analyzer);
|
||||
expect(standard.parameters).toMatchObject({
|
||||
target_format: "raw-binary",
|
||||
loader: "BinaryLoader",
|
||||
language_id: "6502:LE:16:default",
|
||||
language_selection: "caller-supplied",
|
||||
entry_point: "0x8001",
|
||||
analyzer_preset: "ghidra-default",
|
||||
producer,
|
||||
});
|
||||
expect(shifted.parameters).toMatchObject({
|
||||
entry_point: "0x8002",
|
||||
analyzer_preset: "ghidra-default",
|
||||
});
|
||||
expect(custom.parameters).toMatchObject({
|
||||
entry_point: "0x8001",
|
||||
analyzer_preset: "ghidra-default+disabled:Aggressive Instruction Finder",
|
||||
analysis_options: { disabled_analyzers: [analyzer] },
|
||||
producer,
|
||||
});
|
||||
expect(shifted.digest).not.toBe(standard.digest);
|
||||
expect(custom.digest).not.toBe(standard.digest);
|
||||
});
|
||||
@@ -19,13 +19,18 @@ const sources = async (files: Readonly<Record<string, string>>) => {
|
||||
};
|
||||
|
||||
describe("packaged producer identity", () => {
|
||||
it("changes when a producer script changes, without a revision marker", async () => {
|
||||
it("binds the release and every source in canonical name order", async () => {
|
||||
const directory = await sources({ "a.java": "one", "b.py": "two" });
|
||||
const before = packagedSourcesDigest(directory, ["a.java", "b.py"]);
|
||||
const original = packagedProducerIdentity(directory, ["b.py", "a.java"])();
|
||||
expect(original).toEqual({
|
||||
rea_version: PRODUCT_IDENTITY.packageVersion,
|
||||
sources_sha256:
|
||||
"36a70f4afc72128b1276d83321de8fba924bd1b5988b0bf8944b7b8481093126",
|
||||
});
|
||||
await writeFile(new URL("b.py", directory), "two, edited");
|
||||
expect(packagedSourcesDigest(directory, ["a.java", "b.py"])).not.toBe(
|
||||
before,
|
||||
);
|
||||
expect(
|
||||
packagedProducerIdentity(directory, ["a.java", "b.py"])().sources_sha256,
|
||||
).not.toBe(original.sources_sha256);
|
||||
});
|
||||
|
||||
it("frames each source by name and length so bytes cannot move between files", async () => {
|
||||
@@ -36,25 +41,10 @@ describe("packaged producer identity", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("does not depend on the order sources are listed in", async () => {
|
||||
const directory = await sources({ "a.java": "one", "b.py": "two" });
|
||||
expect(packagedSourcesDigest(directory, ["b.py", "a.java"])).toBe(
|
||||
packagedSourcesDigest(directory, ["a.java", "b.py"]),
|
||||
);
|
||||
});
|
||||
|
||||
it("refuses an identity when a declared source is not packaged", async () => {
|
||||
const directory = await sources({ "a.java": "one" });
|
||||
expect(() =>
|
||||
packagedSourcesDigest(directory, ["a.java", "missing.py"]),
|
||||
).toThrow(/ENOENT/u);
|
||||
});
|
||||
|
||||
it("commits the REA release beside the source digest", async () => {
|
||||
const directory = await sources({ "a.java": "one" });
|
||||
expect(packagedProducerIdentity(directory, ["a.java"])()).toEqual({
|
||||
rea_version: PRODUCT_IDENTITY.packageVersion,
|
||||
sources_sha256: packagedSourcesDigest(directory, ["a.java"]),
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user