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(
+72 -26
View File
@@ -1,6 +1,6 @@
import * as zlib from "node:zlib";
import { createHash } from "node:crypto";
import { mkdir, symlink, writeFile } from "node:fs/promises";
import { mkdir, open, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { scheduler } from "node:timers/promises";
import { describe, expect, it, vi } from "vitest";
@@ -9,6 +9,8 @@ import { createElfDebugLinkService } from "../../../src/composition/binaryDiagno
import {
ELF_DEBUG_LINK_CANDIDATE_BUDGET_LIMITATION,
ELF_DEBUG_LINK_CANDIDATE_PREFIX_LIMITATION,
ELF_DEBUG_LINK_CANDIDATE_UNREADABLE_LIMITATION,
ELF_DEBUG_LINK_CANNOT_VERIFY_LIMITATION,
ELF_DEBUG_LINK_UNAVAILABLE_CHECK_LIMITATION,
elfDebugLinkSchema,
} from "../../../src/domain/native/elfDebugLink.js";
@@ -27,6 +29,7 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { elfFrameAllowanceFixture } from "../../fixtures/binaryDiagnostics/elfFrameAllowance.js";
vi.mock(import("node:zlib"), { spy: true });
vi.mock(import("node:fs/promises"), { spy: true });
it("inflates section names once per artifact while preserving build-ID comparison", async () => {
const root = await createTestTempDirectory("rea-elf-debug-link-name-reuse-");
@@ -97,7 +100,8 @@ describe("ELF debug-link stable filesystem boundary", () => {
const root = await createTestTempDirectory("rea-elf-debug-link-candidate-");
const path = join(root, "prog");
const alias = join(root, "alias.debug");
await writeFile(path, elfDebugLinkFixture());
const image = elfDebugLinkFixture();
await writeFile(path, image);
await symlink(path, alias);
const service = createElfDebugLinkService();
try {
@@ -107,15 +111,44 @@ describe("ELF debug-link stable filesystem boundary", () => {
});
if (!missing.ok) throw missing.error;
const report = elfDebugLinkSchema.parse(missing.value.normalized_result);
expect(report.debuglink.status).toBe("present");
expect(report.artifact).toEqual({
path,
sha256: createHash("sha256").update(image).digest("hex"),
bytes: image.length,
});
expect(report.debuglink).toMatchObject({
status: "present",
filename: { text: FIXTURE_DEBUG_NAME },
crc32_hex: "12345678",
});
expect(report.build_id.hex).toBe(FIXTURE_BUILD_ID_HEX);
expect(report.lookup_paths.length).toBeGreaterThan(0);
expect(report.lookup_paths).toContainEqual({
kind: "debug_subdirectory",
path: join(root, ".debug", FIXTURE_DEBUG_NAME),
trust: "observed-untrusted",
});
expect(report.limitations).toEqual(
expect.arrayContaining([
ELF_DEBUG_LINK_CANDIDATE_UNREADABLE_LIMITATION,
ELF_DEBUG_LINK_CANNOT_VERIFY_LIMITATION,
]),
);
expect(report.candidate).toBeNull();
expect(report.verification).toMatchObject({
expect(report.verification).toEqual({
status: "cannot_verify",
reason: "candidate_unreadable",
crc32: { status: "unavailable", reason: "candidate_unreadable" },
build_id: { status: "unavailable", 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",
},
});
const linked = await service.inspect({ path, candidate_path: alias });
const relative = await service.inspect({
@@ -126,7 +159,7 @@ describe("ELF debug-link stable filesystem boundary", () => {
throw new Error("Expected a symlinked candidate rejection");
expect(linked.error).toMatchObject({
_tag: "AnalysisInputError",
issues: [{ path: ["candidate_path"] }],
issues: [{ path: ["candidate_path"], reason: "invalid_format" }],
});
if (relative.ok)
throw new Error("Expected a relative candidate rejection");
@@ -167,25 +200,38 @@ describe("ELF debug-link stable filesystem boundary", () => {
await service.close();
}
});
});
it("does not open a conventional lookup path when no candidate is selected", async () => {
const root = await createTestTempDirectory("rea-elf-debug-link-decoy-");
const path = join(root, "prog");
await writeFile(path, elfDebugLinkFixture());
await mkdir(join(root, ".debug"));
await mkdir(join(root, ".debug", "prog.debug"));
const service = createElfDebugLinkService();
try {
const result = await service.inspect({ path });
if (!result.ok) throw result.error;
const report = elfDebugLinkSchema.parse(result.value.normalized_result);
expect(report.verification).toBeNull();
expect(report.candidate).toBeNull();
expect(report.debuglink.status).toBe("present");
} finally {
await service.close();
}
});
it("does not open a conventional lookup path when no candidate is selected", async () => {
const root = await createTestTempDirectory("rea-elf-debug-link-decoy-");
const path = join(root, "prog");
const decoy = join(root, ".debug", FIXTURE_DEBUG_NAME);
await writeFile(path, elfDebugLinkFixture());
await mkdir(join(root, ".debug"));
await writeFile(decoy, elfDebugCandidateFixture());
const service = createElfDebugLinkService();
try {
vi.mocked(open).mockClear();
const result = await service.inspect({ path });
if (!result.ok) throw result.error;
const selectedPaths = vi
.mocked(open)
.mock.calls.map(([selected]) => selected);
expect(selectedPaths).toContain(path);
expect(selectedPaths).not.toContain(decoy);
const report = elfDebugLinkSchema.parse(result.value.normalized_result);
expect(report.lookup_paths).toContainEqual({
kind: "debug_subdirectory",
path: decoy,
trust: "observed-untrusted",
});
expect(report.verification).toBeNull();
expect(report.candidate).toBeNull();
expect(report.debuglink.status).toBe("present");
} finally {
await service.close();
}
});
describe("ELF debug-link streamed candidate boundary", () => {