mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
test(ghidra): consolidate checkpoint publication and restore coverage (#12930)
* test(ghidra): consolidate checkpoint publication and restore coverage * test(ghidra): preserve multi-buffer checkpoint hashing
This commit is contained in:
1 parent
f505759dd6
commit
a516a8533e
3 files changed
+53
-83
No files matched your search
@@ -20,6 +20,7 @@ import { acquireOwnedLease } from "../process/OwnedLease.js";
|
||||
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
||||
import {
|
||||
GhidraCheckpoint,
|
||||
hashGhidraPackedProgram,
|
||||
resolveGhidraCheckpoint,
|
||||
} from "./GhidraCheckpoint.js";
|
||||
|
||||
@@ -183,57 +184,17 @@ it.skipIf(process.platform === "win32").each(
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"publishes a verified state, reopens it, and separates target and analysis identities",
|
||||
async () => {
|
||||
const packed = Buffer.concat([
|
||||
Buffer.alloc(64 * 1024, 0x61),
|
||||
Buffer.from("packed bytes beyond the initial read"),
|
||||
]);
|
||||
const digest = createHash("sha256").update(packed).digest("hex");
|
||||
const f = await fixture(packed);
|
||||
expect(f.selection.state).toEqual({ kind: "initial-analysis" });
|
||||
expect(await f.owner.acquire("first")).toBeUndefined();
|
||||
await f.owner.publish(f.packed);
|
||||
await f.owner.close();
|
||||
const loaded = await resolveGhidraCheckpoint(
|
||||
f.root,
|
||||
target,
|
||||
profile,
|
||||
f.resources,
|
||||
);
|
||||
expect(loaded.state).toEqual(f.selection.state);
|
||||
const saved = f.owner.observation.source;
|
||||
if (saved === null) throw new Error("Expected a published checkpoint");
|
||||
expect(saved.sha256).toBe(digest);
|
||||
const resumed = new GhidraCheckpoint(loaded, f.resources);
|
||||
owners.push(resumed);
|
||||
expect(await resumed.acquire("second")).toEqual({
|
||||
path: join(loaded.directory, saved.file),
|
||||
sha256: digest,
|
||||
});
|
||||
expect(
|
||||
(
|
||||
await resolveGhidraCheckpoint(
|
||||
f.root,
|
||||
"2".repeat(64),
|
||||
profile,
|
||||
f.resources,
|
||||
)
|
||||
).key,
|
||||
).not.toBe(f.selection.key);
|
||||
expect(
|
||||
(
|
||||
await resolveGhidraCheckpoint(
|
||||
f.root,
|
||||
target,
|
||||
createAnalysisProfile(profile.provider, { import_mode: "other" }),
|
||||
f.resources,
|
||||
)
|
||||
).key,
|
||||
).not.toBe(f.selection.key);
|
||||
},
|
||||
);
|
||||
it("hashes packed checkpoint bytes beyond the first buffer", async () => {
|
||||
const bytes = Buffer.concat([
|
||||
Buffer.alloc(64 * 1024, 0x61),
|
||||
Buffer.from("packed bytes beyond the initial read"),
|
||||
]);
|
||||
const { packed, resources } = await fixture(bytes);
|
||||
const expected = createHash("sha256").update(bytes).digest("hex");
|
||||
await expect(hashGhidraPackedProgram(packed, resources)).resolves.toBe(
|
||||
expected,
|
||||
);
|
||||
});
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"refuses a concurrent owner and admits it after the original owner closes",
|
||||
@@ -279,23 +240,14 @@ it.skipIf(process.platform === "win32")(
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"keeps initial identity stable and rejects an unknown manifest version without rewriting it",
|
||||
"rejects an unknown manifest version without rewriting it",
|
||||
async () => {
|
||||
const f = await fixture();
|
||||
await f.owner.acquire("initial");
|
||||
await f.owner.publish(f.packed);
|
||||
await f.owner.close();
|
||||
const initial = await resolveGhidraCheckpoint(
|
||||
f.root,
|
||||
target,
|
||||
profile,
|
||||
f.resources,
|
||||
);
|
||||
expect(initial).toEqual(f.selection);
|
||||
const path = join(initial.directory, "current.json");
|
||||
const path = join(f.selection.directory, "current.json");
|
||||
const record = JSON.parse(await readFile(path, "utf8"));
|
||||
expect(record.version).toBe(2);
|
||||
expect(record.source.kind).toBe("initial-analysis");
|
||||
const unknown = JSON.stringify({ ...record, version: 1 });
|
||||
await writeFile(path, unknown);
|
||||
await expect(
|
||||
|
||||
@@ -102,18 +102,42 @@ it.skipIf(process.platform === "win32")(
|
||||
expect(f.client.checkpointRestore()).toBeUndefined();
|
||||
expect((await f.client.start()).ok).toBe(true);
|
||||
expect(f.client.checkpointRestore()).toBeNull();
|
||||
const path = join(f.selection.directory, "current.json"),
|
||||
before = await readFile(path, "utf8");
|
||||
const path = join(f.selection.directory, "current.json");
|
||||
const before = await readFile(path, "utf8");
|
||||
const manifest = JSON.parse(before);
|
||||
const sha256 = createHash("sha256")
|
||||
.update("fixture checkpoint initial")
|
||||
.digest("hex");
|
||||
expect(manifest).toMatchObject({
|
||||
version: 2,
|
||||
source: { kind: "initial-analysis", sha256 },
|
||||
});
|
||||
expect((await f.client.close()).ok).toBe(true);
|
||||
expect(f.client.checkpointRestore()).toBeUndefined();
|
||||
expect(await readFile(path, "utf8")).toEqual(before);
|
||||
expect(f.launcher.requests).toContain("checkpoint_program");
|
||||
expect(await readFile(path, "utf8")).toBe(before);
|
||||
const resources = new ArtifactResourceScope();
|
||||
const selection = await resolveGhidraCheckpoint(
|
||||
f.root,
|
||||
f.sha256,
|
||||
profile,
|
||||
new ArtifactResourceScope(),
|
||||
resources,
|
||||
);
|
||||
expect(selection).toEqual(f.selection);
|
||||
const otherProfile = createAnalysisProfile(profile.provider, {
|
||||
checkpoint_test: false,
|
||||
});
|
||||
for (const [target, baseProfile] of [
|
||||
["2".repeat(64), profile],
|
||||
[f.sha256, otherProfile],
|
||||
] as const) {
|
||||
const other = await resolveGhidraCheckpoint(
|
||||
f.root,
|
||||
target,
|
||||
baseProfile,
|
||||
resources,
|
||||
);
|
||||
expect(other.directory).not.toBe(selection.directory);
|
||||
}
|
||||
const resumed = new GhidraClient({
|
||||
launcher: f.launcher,
|
||||
targetPath: f.targetPath,
|
||||
@@ -125,12 +149,12 @@ it.skipIf(process.platform === "win32")(
|
||||
});
|
||||
clients.push(resumed);
|
||||
expect((await resumed.start()).ok).toBe(true);
|
||||
expect(resumed.checkpointRestore()).toMatchObject({
|
||||
sha256: JSON.parse(await readFile(path, "utf8")).source.sha256,
|
||||
});
|
||||
expect(f.launcher.packedPrograms.at(-1)?.sha256).toBe(
|
||||
JSON.parse(await readFile(path, "utf8")).source.sha256,
|
||||
);
|
||||
const packed = {
|
||||
path: join(selection.directory, manifest.source.file),
|
||||
sha256,
|
||||
};
|
||||
expect(resumed.checkpointRestore()).toEqual(packed);
|
||||
expect(f.launcher.packedPrograms.at(-1)).toEqual(packed);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { expect, it } from "vitest";
|
||||
import { assertCheckpointRefusalPreservesManifest } from "../../../../scripts/lib/ghidra-checkpoint-refusal.mjs";
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
@@ -19,13 +18,8 @@ const refusal = (code = "configuration_invalid", exitCode = 1) =>
|
||||
stderr: "controlled CLI diagnostic",
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "mismatched target identity",
|
||||
bytes: Buffer.from(JSON.stringify({ target_sha256: "0".repeat(64) })),
|
||||
},
|
||||
{ name: "malformed manifest", bytes: Buffer.from("{") },
|
||||
])("checks refusal-time manifest bytes for $name", async ({ bytes }) => {
|
||||
it("checks the original manifest bytes after the CLI refuses", async () => {
|
||||
const bytes = Buffer.from("{");
|
||||
const path = await checkpointFile(bytes);
|
||||
await expect(
|
||||
assertCheckpointRefusalPreservesManifest(
|
||||
|
||||
Reference in new issue
Block a user