Files
reverse-engineering-anything/src/application/binaryDiagnostics/ElfNotesService.test.ts
T
Vincent Koc 4bcd0af58c test(diagnostics): share invalid inspection input assertions (#14501)
* test(diagnostics): share invalid inspection input assertions

* test(diagnostics): keep the PE import fixture independently mergeable
2026-11-25 08:34:03 -05:00

125 lines
4.2 KiB
TypeScript

import { expectInvalidInspectionInputs } from "../../../tests/support/expectInvalidInspectionInputs.js";
import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js";
import { expect, it } from "vitest";
import { ElfNotesService } from "./ElfNotesService.js";
import { ELF_NOTES_PROVIDER } from "../../domain/investigationProviders.js";
import { createHash } from "node:crypto";
import { inspectElfNotesInputSchema } from "../../domain/native/elfNotes.js";
import { elfNoteFixture } from "../../native/elf/ElfNotes.fixture.js";
import { parseElfNotesFromHandle } from "../../native/elf/ElfNoteParser.js";
import { ok } from "../../domain/result.js";
import {
AnalysisCancelledError,
AnalysisOutputError,
} from "../../domain/analysisErrorCore.js";
import { bufferByteSource } from "../../domain/byteSource.js";
const identity = ELF_NOTES_PROVIDER;
const path = "/artifacts/app";
const bytes = elfNoteFixture({ ownership: "none" });
const report = await parseElfNotesFromHandle(
bufferByteSource(bytes, 4096),
{
path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
},
inspectElfNotesInputSchema.parse({ path }),
async () => {},
);
it("rejects a relative path and extra arguments before reading", () =>
expectInvalidInspectionInputs(
report,
"app",
(inspect) => new ElfNotesService({ identity, inspect }),
));
it("records the selected ELF and an absent note table", async () => {
const result = await new ElfNotesService({
identity,
inspect: () => Promise.resolve(ok(report)),
}).inspect({ path: report.artifact.path });
if (!result.ok) throw result.error;
expect(result.value).toMatchObject({
confidence: "observed",
subject: { identity: { format: "elf", architecture: "x86_64" } },
normalized_result: report,
});
});
it.each(["examined_tables", "examined_entries"] as const)(
"rejects complete provider coverage with inconsistent %s",
async (count) => {
const result = await new ElfNotesService({
identity,
inspect: () =>
Promise.resolve(
ok({
...report,
coverage: { ...report.coverage, [count]: 1 },
}),
),
}).inspect({ path: report.artifact.path });
expect(result.ok).toBe(false);
if (!result.ok) expect(result.error).toBeInstanceOf(AnalysisOutputError);
},
);
it("retains an examined empty table and rejects an uncounted table", async () => {
const table = {
kind: "program" as const,
index: 0,
name: null,
location: { offset: 128, bytes: 0 },
alignment: 4,
};
for (const examinedTables of [0, 1]) {
const result = await new ElfNotesService({
identity,
inspect: () =>
Promise.resolve(
ok({
...report,
tables: [table],
coverage: { ...report.coverage, examined_tables: examinedTables },
}),
),
}).inspect({ path: report.artifact.path });
expect(result.ok).toBe(examinedTables === 1);
if (result.ok)
expect(result.value.normalized_result).toMatchObject({ tables: [table] });
else expect(result.error).toBeInstanceOf(AnalysisOutputError);
}
});
it("retains a completed observation when the caller cancels as the provider completes", async () => {
const evidence = await lateCancellationEvidence(
report,
"late ElfNotes cancellation",
(inspect) => new ElfNotesService({ identity, inspect }),
);
expect(evidence).toMatchObject({
operation: "inspect_elf_notes",
normalized_result: report,
});
});
it("keeps a failed report for a different artifact outside admitted Evidence", async () => {
const foreignReport = {
...report,
artifact: { ...report.artifact, path: "/artifacts/other" },
};
const failure = new AnalysisCancelledError("inspect_elf_notes", {
partialObservation: foreignReport,
});
const service = new ElfNotesService({
identity,
inspect: async () => ({ ok: false, error: failure }),
});
const result = await service.inspect({ path });
if (result.ok) throw new Error("Expected the original provider failure");
expect(result.error).toBe(failure);
expect(result.error.partialObservation).toBe(foreignReport);
});