fix(elf): align metadata and admission fixture error types

This commit is contained in:
morluto
2026-10-24 05:05:41 +08:00
parent 651afa4455
commit e8a44f8679
4 changed files with 35 additions and 11 deletions
@@ -220,7 +220,7 @@ export function* readElfSectionDirectory(
}
return {
format: "elf",
elf_os_abi: parsed.value.osAbi,
elf_os_abi: header.osAbi,
bits,
little,
machine,
@@ -4,6 +4,7 @@ import { join } from "node:path";
import { expect, it } from "vitest";
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
import { elf } from "../../../src/domain/binaryTarget.fixture.js";
import { expectBinaryTargetError } from "../../fixtures/binaryTargetAdmission.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const image = (bits: 32 | 64, little: boolean) => {
@@ -85,8 +86,9 @@ for (const bits of [32, 64] as const) {
reason: expect.stringContaining(`truncated ELF ${declaration}`),
},
});
if (result.ok) throw new Error("Expected a declared range refusal");
expect(result.error.reason).toContain(`${length}-byte file`);
expect(expectBinaryTargetError(result).reason).toContain(
`${length}-byte file`,
);
}
});
@@ -117,8 +119,9 @@ for (const bits of [32, 64] as const) {
fixture.word(fixture.program + (bits === 64 ? 8 : 4), maximum);
const result = await admit(fixture.bytes);
expect(result).toMatchObject({ ok: false });
if (result.ok) throw new Error("Expected an out-of-file range");
expect(result.error.reason).toContain(`${maximum}..${maximum + 16n}`);
expect(expectBinaryTargetError(result).reason).toContain(
`${maximum}..${maximum + 16n}`,
);
});
}
}
@@ -153,7 +156,8 @@ it.each(["sections", "programs", "section names"] as const)(
expect((await admit(bytes)).ok).toBe(true);
const truncated = await admit(bytes.subarray(0, bytes.length - 1));
expect(truncated).toMatchObject({ ok: false });
if (truncated.ok) throw new Error("Expected an extended table refusal");
expect(truncated.error.reason).toContain("truncated ELF");
expect(expectBinaryTargetError(truncated).reason).toContain(
"truncated ELF",
);
},
);
@@ -6,6 +6,7 @@ import { it } from "vitest";
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
import { readMipsElfMetadata } from "../../../src/application/MipsElfMetadata.js";
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
import { expectBinaryTargetError } from "../../fixtures/binaryTargetAdmission.js";
import {
ghidraMipsProfileParameters,
ghidraMipsUnsupportedReason,
@@ -224,8 +225,10 @@ it("does not treat uncaptured ABI declaration headers as an inspected absence",
bytes.writeUInt16LE(1, 48);
const path = await writeFixture(bytes);
const admitted = await parseBinaryTarget(path);
assert.ok(!admitted.ok);
assert.match(admitted.error.reason, /truncated ELF section header table/u);
assert.match(
expectBinaryTargetError(admitted).reason,
/truncated ELF section header table/u,
);
// The specialized inspector still retains unknown source facts if used on
// damaged evidence; normal target admission refuses its impossible extent.
const declarations = await inspectSource(path);
@@ -304,8 +307,10 @@ it.each(malformed)(
if (rejectsExtent) {
const path = await writeFixture(bytes);
const admitted = await parseBinaryTarget(path);
assert.ok(!admitted.ok);
assert.match(admitted.error.reason, /truncated ELF program \d/u);
assert.match(
expectBinaryTargetError(admitted).reason,
/truncated ELF program \d/u,
);
const declarations = await inspectSource(path);
assert.ok(declarations.ok);
assert.equal(declarations.value.sharedLoad?.status, "unavailable");
+15
View File
@@ -0,0 +1,15 @@
import assert from "node:assert/strict";
import type { parseBinaryTarget } from "../../src/application/BinaryTargetResolver.js";
import { BinaryTargetError } from "../../src/domain/configurationErrors.js";
/** Require target admission failure rather than an unrelated cancellation. */
export const expectBinaryTargetError = (
result: Awaited<ReturnType<typeof parseBinaryTarget>>,
): BinaryTargetError => {
assert.ok(!result.ok, "Expected target admission to fail");
assert.ok(
result.error instanceof BinaryTargetError,
"Expected a target admission error",
);
return result.error;
};