From 9e0d132ef3c16649333a9dfd8c43f5ddd335fa80 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:33:03 -0500 Subject: [PATCH] test(uefi): consolidate extraction publication contracts (#13627) --- src/uefi/UefiAnalysisProvider.test.ts | 231 ++++++++++++++------------ 1 file changed, 126 insertions(+), 105 deletions(-) diff --git a/src/uefi/UefiAnalysisProvider.test.ts b/src/uefi/UefiAnalysisProvider.test.ts index 64f933db0f..d2ceb7b464 100644 --- a/src/uefi/UefiAnalysisProvider.test.ts +++ b/src/uefi/UefiAnalysisProvider.test.ts @@ -1,11 +1,11 @@ import { createHash } from "node:crypto"; import { existsSync } from "node:fs"; -import { readFile, writeFile } from "node:fs/promises"; +import { readdir, readFile, writeFile } from "node:fs/promises"; import { createRequire } from "node:module"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { deflateSync } from "node:zlib"; -import { expect, it, vi } from "vitest"; +import { expect, it, onTestFinished, vi } from "vitest"; import { SafeOutputTree } from "../resources/SafeOutputTree.js"; import { SafeOutputTreeCreationFailure } from "../resources/SafeOutputTreeCreationFailure.js"; import { ProviderProcessSupervisor } from "../process/ProviderProcess.js"; @@ -115,6 +115,7 @@ const extractRequest = ( { operation: "extract_uefi_firmware" } >["input"]["target"]; readonly max_depth?: number; + readonly max_output_bytes?: number; readonly max_output_files?: number; readonly include_compressed?: boolean; }, @@ -137,122 +138,142 @@ const expectOk = < return executed as Extract; }; -it("maps a flash image into an inspect execution with volume and section coverage", async () => { - const { imagePath } = await writeTestImage(); +const countPublishedFiles = async (path: string): Promise => + (await readdir(path, { recursive: true, withFileTypes: true })).filter( + (entry) => entry.isFile(), + ).length; + +it("retains the unsupported guided observation with either carrier publication policy", async () => { + const { imagePath, directory } = await writeTestImage(); const provider = new UefiAnalysisProvider(); - const executed = expectOk( - await provider.execute(await inspectRequest(imagePath)), - ); - const result = executed.value.result as UefiInspectResult; + onTestFinished(() => provider.close()); + const inspected = expectOk(await provider.execute(inspectRequest(imagePath))); const image = await readFile(imagePath); - expect(result.parser.name).toBe("rea-uefi"); - expect(result.flash_image.sha256).toBe(sha256(image)); - expect(result.flash_image.entry_basis).toBe("fv-at-zero"); - expect(result.firmware_volumes).toHaveLength(1); - const volume0 = result.firmware_volumes[0]; - expect(volume0?.ffs_files.map((file) => file.type_name)).toContain("driver"); - expect(result.coverage).toBe("partial"); - expect(result.limitations.join(" ")).toContain("unknown-guid"); - expect(executed.value.subject?.sha256).toBe(sha256(image)); - await provider.close(); + const digest = sha256(image); + expect(inspected.value.subject).toMatchObject({ sha256: digest }); + expect(inspected.value.result).toMatchObject({ + parser: { name: "rea-uefi" }, + flash_image: { sha256: digest, entry_basis: "fv-at-zero" }, + firmware_volumes: [ + { + ffs_files: expect.arrayContaining([ + expect.objectContaining({ type_name: "driver" }), + ]), + }, + ], + coverage: "partial", + limitations: expect.arrayContaining([ + expect.stringContaining("unknown-guid"), + ]), + }); + + for (const [name, overrides, sources] of [ + [ + "default", + undefined, + ["raw-section", "compressed-passthrough", "uncompressed-payload"], + ], + [ + "without-carrier", + { include_compressed: false }, + ["raw-section", "uncompressed-payload"], + ], + ["file-limit", { max_output_files: 1 }, ["raw-section"]], + ] as const) { + const output = join(directory, name); + const extracted = expectOk( + await provider.execute(extractRequest(imagePath, output, overrides)), + ).value.result as UefiExtractResult; + expect(extracted.coverage).toBe("partial"); + expect(extracted.extraction_gaps.map(({ kind }) => kind)).toContain( + "unknown-guid-handler", + ); + expect(extracted.files.map(({ source }) => source)).toEqual(sources); + expect(await countPublishedFiles(output), name).toBe(sources.length); + if (name === "default") { + const carrier = extracted.files[1]; + if (carrier === undefined) throw new Error("Missing guided carrier"); + expect(carrier.size).toBe(GUIDED_BODY.length); + expect((await readFile(carrier.path)).equals(GUIDED_BODY)).toBe(true); + } else if (name === "file-limit") { + expect( + extracted.extraction_gaps.filter( + ({ kind }) => kind === "size-truncated", + ), + ).toEqual([ + expect.objectContaining({ + logical_path: expect.stringContaining(GUID_DRIVER), + }), + expect.objectContaining({ + logical_path: expect.stringContaining(GUID_RAW), + }), + ]); + } + } }); -it("extracts whole-image payloads with section provenance and published bytes", async () => { - // Without a guided section, everything requested is publishable: pad files - // stay in place by design, so they never downgrade coverage. +it("publishes exact whole-image payloads and honors the total byte bound", async () => { const { imagePath, directory } = await writeTestImage({ withGuided: false }); const provider = new UefiAnalysisProvider(); - const executed = expectOk( - await provider.execute( - await extractRequest(imagePath, join(directory, "out")), - ), - ); - const result = executed.value.result as UefiExtractResult; - expect(result.selection.target_kind).toBe("whole-image"); - expect(result.coverage).toBe("complete"); - expect(result.derivations).toHaveLength(0); - const rawSection = result.files.find((file) => file.source === "raw-section"); - expect(rawSection?.size).toBe(MODULE_A.length); - const rawBytes = await readFile(rawSection?.path ?? ""); - expect(rawBytes.equals(MODULE_A)).toBe(true); - const rawBody = result.files.find( - (file) => file.source === "uncompressed-payload", - ); - expect((await readFile(rawBody?.path ?? "")).equals(RAW_BODY)).toBe(true); - expect(rawBody?.relative_path).not.toContain(":"); - const padGap = result.extraction_gaps.find((gap) => gap.kind === "pad-file"); - expect(padGap).toBeDefined(); - expect(result.published_bytes).toBe( - result.files.reduce((total, file) => total + file.size, 0), - ); -}); + onTestFinished(() => provider.close()); -it("publishes the unknown-guid carrier and reports the decoding gap explicitly", async () => { - const { imagePath, directory } = await writeTestImage(); - const provider = new UefiAnalysisProvider(); - const executed = expectOk( - await provider.execute( - await extractRequest(imagePath, join(directory, "out")), - ), - ); - const result = executed.value.result as UefiExtractResult; - const guidedGap = result.extraction_gaps.find( - (gap) => gap.kind === "unknown-guid-handler", - ); - expect(guidedGap).toBeDefined(); - const carrier = result.files.find( - (file) => file.source === "compressed-passthrough", - ); - expect(carrier?.size).toBe(GUIDED_BODY.length); - expect((await readFile(carrier?.path ?? "")).equals(GUIDED_BODY)).toBe(true); -}); + const whole = expectOk( + await provider.execute(extractRequest(imagePath, join(directory, "whole"))), + ).value.result as UefiExtractResult; + expect(whole).toMatchObject({ + selection: { target_kind: "whole-image" }, + coverage: "complete", + derivations: [], + extraction_gaps: [expect.objectContaining({ kind: "pad-file" })], + files: [ + { source: "raw-section", size: MODULE_A.length }, + { source: "uncompressed-payload", size: RAW_BODY.length }, + ], + published_bytes: MODULE_A.length + RAW_BODY.length, + }); + const [section, raw] = whole.files; + if (section === undefined || raw === undefined) + throw new Error("Missing published payloads"); + expect(raw.relative_path).not.toContain(":"); + expect((await readFile(section.path)).equals(MODULE_A)).toBe(true); + expect((await readFile(raw.path)).equals(RAW_BODY)).toBe(true); -it("withholds carriers when include_compressed is disabled", async () => { - const { imagePath, directory } = await writeTestImage(); - const provider = new UefiAnalysisProvider(); - const executed = expectOk( + const output = join(directory, "byte-limit"); + const bounded = expectOk( await provider.execute( - await extractRequest(imagePath, join(directory, "out"), { - include_compressed: false, + extractRequest(imagePath, output, { + max_output_bytes: MODULE_A.length, }), ), - ); - const result = executed.value.result as UefiExtractResult; + ).value.result as UefiExtractResult; + expect(await countPublishedFiles(output), "byte-limit").toBe(1); + expect(bounded).toMatchObject({ + coverage: "partial", + files: [{ source: "raw-section", size: MODULE_A.length }], + published_bytes: MODULE_A.length, + }); expect( - result.files.find((file) => file.source === "compressed-passthrough"), - ).toBeUndefined(); - expect(result.coverage).toBe("partial"); -}); - -it("stops staging at the file budget and downgrades coverage", async () => { - const { imagePath, directory } = await writeTestImage(); - const provider = new UefiAnalysisProvider(); - const executed = expectOk( - await provider.execute( - await extractRequest(imagePath, join(directory, "out-tight"), { - max_output_files: 1, - }), - ), - ); - const result = executed.value.result as UefiExtractResult; - expect(result.files).toHaveLength(1); - expect(result.coverage).toBe("partial"); - expect( - result.extraction_gaps.some((gap) => gap.kind === "size-truncated"), - ).toBe(true); -}); - -it("rejects extraction scopes that do not match a scanned record", async () => { - const { imagePath, directory } = await writeTestImage(); - const provider = new UefiAnalysisProvider(); - const executed = await provider.execute( - await extractRequest(imagePath, join(directory, "out"), { - target: { kind: "volume", offset: 0xdeadbeef }, + bounded.extraction_gaps.filter(({ kind }) => kind === "size-truncated"), + ).toEqual([ + expect.objectContaining({ + logical_path: expect.stringContaining(GUID_RAW), }), - ); - expect(executed.ok).toBe(false); - if (executed.ok) return; - expect(executed.error._tag).toBe("AnalysisInputError"); + ]); +}); + +it("refuses an unmapped extraction scope and leaves no output", async () => { + const { imagePath, directory } = await writeTestImage(); + const provider = new UefiAnalysisProvider(); + onTestFinished(() => provider.close()); + const output = join(directory, "out"); + expect( + await provider.execute( + extractRequest(imagePath, output, { + target: { kind: "volume", offset: 0xdeadbeef }, + }), + ), + ).toMatchObject({ ok: false, error: { _tag: "AnalysisInputError" } }); + expect(existsSync(output)).toBe(false); }); const lzmaWorkerAvailable = existsSync(distUefiLzmaWorker);