test(elf): consolidate debug-link candidate coverage (#12851)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 14:27:36 +00:00
1 parent 67a55ea8e1
commit 3dacda36f2
3 files changed
+119 -149

No files matched your search

+3 -53
View File
@@ -3,7 +3,6 @@ import { expect, it } from "vitest";
import { ParseFailureError } from "../../domain/parseFailure.js";
import {
ELF_DEBUG_LINK_CANDIDATE_UNREADABLE_LIMITATION,
ELF_DEBUG_LINK_CANNOT_VERIFY_LIMITATION,
ELF_DEBUG_LINK_MULTIPLE_BUILD_IDS_LIMITATION,
ELF_DEBUG_LINK_NAME_SEGMENT_LIMITATION,
@@ -13,11 +12,7 @@ import {
import { elfNote, gnuBuildIdNote } from "./ElfNotes.fixture.js";
import { parseElfNotes } from "./ElfNoteParser.js";
import { readElfImage } from "../../domain/native/elfImage.js";
import {
parseElfDebugLink,
unreadableElfDebugCandidate,
verifyElfDebugCandidate,
} from "./ElfDebugLink.js";
import { parseElfDebugLink, verifyElfDebugCandidate } from "./ElfDebugLink.js";
import {
debugaltlinkSection,
debuglinkSection,
@@ -84,14 +79,8 @@ it("reads a little-endian debuglink CRC, build-id, and lookup paths", () => {
});
it("keeps the CRC across padding, trailing bytes, and an overlong name", () => {
const padded = debuglinkSection("ab", 0x89abcdef, true);
expect(padded.length).toBe(8);
expect(padded.subarray(0, 4).equals(Buffer.from("ab\0\0", "latin1"))).toBe(
true,
);
expect(padded.readUInt32LE(4)).toBe(0x89abcdef);
const aligned = debuglinkSection("abc", 0x01020304, true);
expect(aligned.length).toBe(8);
const padded = Buffer.from([0x61, 0x62, 0, 0, 0xef, 0xcd, 0xab, 0x89]);
const aligned = Buffer.from([0x61, 0x62, 0x63, 0, 4, 3, 2, 1]);
expect(
parse(elfDebugLinkFixture({ debuglink: padded })).debuglink,
).toMatchObject({
@@ -458,42 +447,3 @@ it("cannot verify when neither CRC32 nor build-id is comparable", () => {
expect(report.verification?.crc32.observed_hex).toBe("314f0306");
expect(report.limitations).toContain(ELF_DEBUG_LINK_CANNOT_VERIFY_LIMITATION);
});
it("keeps the image linkage when the candidate cannot be read", () => {
const image = elfDebugLinkFixture({
debuglink: debuglinkSection(FIXTURE_DEBUG_NAME, 0x12345678, true),
});
const report = parseElfDebugLink(
image,
{ path: artifactPath, sha256: digest, bytes: image.length },
inputFor(),
);
const unreadable = elfDebugLinkSchema.parse(
unreadableElfDebugCandidate(report),
);
expect(unreadable.debuglink).toEqual(report.debuglink);
expect(unreadable.lookup_paths).toEqual(report.lookup_paths);
expect(unreadable.candidate).toBeNull();
expect(unreadable.verification).toEqual({
status: "cannot_verify",
reason: "candidate_unreadable",
crc32: {
status: "unavailable",
expected_hex: "12345678",
observed_hex: null,
reason: "candidate_unreadable",
},
build_id: {
status: "unavailable",
binary_hex: FIXTURE_BUILD_ID_HEX,
candidate_hex: null,
reason: "candidate_unreadable",
},
});
expect(unreadable.limitations).toEqual(
expect.arrayContaining([
ELF_DEBUG_LINK_CANDIDATE_UNREADABLE_LIMITATION,
ELF_DEBUG_LINK_CANNOT_VERIFY_LIMITATION,
]),
);
});
+44 -70
View File
@@ -1,4 +1,3 @@
import { scheduler } from "node:timers/promises";
import { primaryAnalysisError } from "../../domain/analysisErrorBase.js";
import { expect, it, vi } from "vitest";
@@ -12,7 +11,6 @@ import {
inspectElfDebugLinkInputSchema,
elfDebugLinkSchema,
} from "../../domain/native/elfDebugLink.js";
import { elfFrameAllowanceFixture } from "../../../tests/fixtures/binaryDiagnostics/elfFrameAllowance.js";
import { elfDebugLinkFixture } from "./ElfDebugLink.fixture.js";
const image = elfDebugLinkFixture();
@@ -52,36 +50,58 @@ it("degrades a changed candidate to candidate_unreadable", async () => {
expect(result.value.verification?.reason).toBe("candidate_unreadable");
});
it("keeps a failed candidate close visible after an integrity or limit refusal", async () => {
for (const reason of ["integrity", "limit"] as const) {
it.each([
{
refusal: new ArtifactReaderFailure(
"integrity",
"Candidate changed while read",
{
integrityCheck: "container-identity",
},
),
primary: {
_tag: "AnalysisArtifactChangedError",
path: "/artifacts/prog.debug",
integrityCheck: "container-identity",
},
},
{
refusal: new ArtifactReaderFailure(
"limit",
"Candidate exceeded its read budget",
),
primary: {
_tag: "AnalysisResourceConstraintError",
resource: "file-size",
reportedLimits: {
boundary: "candidate_bytes",
maximum: MAX_ELF_DEBUG_CANDIDATE_BYTES,
},
fixedAllowance: {
kind: "input",
limit: MAX_ELF_DEBUG_CANDIDATE_BYTES,
unit: "bytes",
},
},
},
])(
"keeps candidate cleanup and the $refusal.reason primary",
async ({ refusal, primary }) => {
const result = await inspect(
ArtifactReaderFailure.withCleanup(
reason === "integrity"
? new ArtifactReaderFailure(reason, "Candidate read refused", {
integrityCheck: "container-identity",
})
: new ArtifactReaderFailure(reason, "Candidate read refused"),
refusal,
ArtifactReaderFailure.cleanupObservation(
new Error("close failed"),
"/artifacts/prog.debug",
),
),
);
expect(result.ok).toBe(false);
if (!result.ok && reason === "limit")
expect(primaryAnalysisError(result.error)).toMatchObject({
reportedLimits: {
boundary: "candidate_bytes",
maximum: MAX_ELF_DEBUG_CANDIDATE_BYTES,
},
fixedAllowance: {
kind: "input",
limit: MAX_ELF_DEBUG_CANDIDATE_BYTES,
unit: "bytes",
},
});
}
});
if (result.ok) throw new Error("Expected a candidate cleanup failure");
expect(result.error.cleanupIncomplete).toBe(true);
expect(result.error.cleanupResources).toEqual(["/artifacts/prog.debug"]);
expect(primaryAnalysisError(result.error)).toMatchObject(primary);
},
);
it("degrades a candidate data-read EIO to candidate_unreadable", async () => {
const result = await inspect(
@@ -146,52 +166,6 @@ it("keeps a completed candidate stream's identity when cancellation arrives duri
);
});
it("preserves the completed candidate when its ELF comparison is cancelled", async () => {
const { streamStableArtifact } = await import("../../resources/index.js");
const prefix = elfFrameAllowanceFixture();
const sha256 = "cd".repeat(32);
vi.mocked(streamStableArtifact).mockResolvedValueOnce({
prefix,
sha256,
size: prefix.length,
});
const provider = new ElfDebugLinkProvider();
const controller = new AbortController();
const reason = new Error("candidate comparison cancelled");
const originalYield = scheduler.yield.bind(scheduler);
const pause = vi
.spyOn(scheduler, "yield")
.mockImplementationOnce(async () => {
await originalYield();
controller.abort(reason);
});
try {
const result = await provider.inspect(
inspectElfDebugLinkInputSchema.parse({
path: "/artifacts/prog",
candidate_path: "/artifacts/prog.debug",
}),
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected interrupted comparison");
expect(result.error).toMatchObject({
_tag: "AnalysisCancelledError",
cause: reason,
});
expect(retainedReport(result.error)).toMatchObject({
candidate: {
path: "/artifacts/prog.debug",
sha256,
bytes: prefix.length,
},
verification: null,
});
} finally {
pause.mockRestore();
await provider.close();
}
});
it("recovers a completed stream's whole-file size, not its prefix length", () => {
expect(
completedStableArtifactObservation(