From 82708786df88332a06ecdbf5f04afd476d76bb56 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 18 Nov 2026 06:39:40 +0800 Subject: [PATCH] test(dfuse): consolidate CRC inventory coverage in MCP (#11876) --- src/artifacts/DfuSeArtifactReader.test.ts | 54 ------------------- src/domain/firmware/dfuse.test.ts | 20 ++----- .../mcp/firmwareContainersMcp.test.ts | 46 +++++++++++++--- 3 files changed, 41 insertions(+), 79 deletions(-) diff --git a/src/artifacts/DfuSeArtifactReader.test.ts b/src/artifacts/DfuSeArtifactReader.test.ts index 765685bbac..ec0079f93f 100644 --- a/src/artifacts/DfuSeArtifactReader.test.ts +++ b/src/artifacts/DfuSeArtifactReader.test.ts @@ -108,56 +108,6 @@ it("inventories one region per element with its file range and target facts", as ]); }); -it("refuses a suffix CRC mismatch under the default integrity policy", async () => { - const path = await writeImage( - "bad-crc.dfu", - dfuseFile({ ...FIRMWARE, crc: 0x1234_5678 }), - ); - const expected = ( - await scanCanonicalArtifactInventory( - await writeImage("good.dfu", dfuseFile(FIRMWARE)), - ) - ).occurrences.find((row) => row.logical_path === ".")?.format_metadata; - if (expected?.kind !== "dfuse") throw new Error("Missing DfuSe metadata"); - const computed = expected.computed_crc.toString(16).padStart(8, "0"); - await expect(scanCanonicalArtifactInventory(path)).rejects.toMatchObject({ - reason: "integrity", - details: { - logicalPath: ".", - unpacked: false, - algorithm: "dfu-crc32", - declaredCrc32: "12345678", - calculatedCrc32: computed, - }, - }); - const recorded = await scanCanonicalArtifactInventory(path, { - integrity: "record-and-continue", - }); - const root = recorded.occurrences.find((row) => row.logical_path === "."); - expect(root).toMatchObject({ - hash_status: "mismatched", - limitations: ["Declared integrity metadata contradicts observed bytes."], - format_metadata: { - suffix_crc: 0x1234_5678, - computed_crc: expected.computed_crc, - }, - }); - expect(recorded.integrity_contradictions).toEqual([ - expect.objectContaining({ - occurrence_id: root?.occurrence_id, - logical_path: ".", - algorithm: "dfu-crc32", - declared_crc32: "12345678", - observed_crc32: computed, - trust: "observed-untrusted", - }), - ]); - expect(recorded.limitations).toContainEqual( - expect.stringMatching(/1.*integrity.*observed-untrusted/iu), - ); - expect(recorded.occurrences).toHaveLength(4); -}); - it("extracts elements and carries a root contradiction into the extraction", async () => { const directory = await createTestTempDirectory("rea-dfuse-extract-"); const path = join(directory, "firmware.dfu"); @@ -213,10 +163,6 @@ it("recognizes DfuSe only with its suffix, including inside a directory", async ["updates/image.bin", "container", "dfuse"], ["updates/no-suffix.dfu", "resource", "file"], ]); - const root = await scanCanonicalArtifactInventory( - await writeImage("no-suffix.dfu", bytes.subarray(0, bytes.length - 16)), - ); - expect(root.manifest.root_format).toBe("file"); }); it("reads the suffix of a DfuSe root longer than the classification prefix", async () => { diff --git a/src/domain/firmware/dfuse.test.ts b/src/domain/firmware/dfuse.test.ts index f47a20bc0a..e262a25d20 100644 --- a/src/domain/firmware/dfuse.test.ts +++ b/src/domain/firmware/dfuse.test.ts @@ -105,7 +105,7 @@ it("keeps each target's alternate setting and name on its elements", () => { targets: [ { alternateSetting: 0, - name: "Internal Flash", + name: Uint8Array.from([0x4f, 0xe9, 0, 0x58]), elements: [ { address: 0x0800_0000, data: [1, 2, 3, 4] }, { address: 0x0800_4000, data: [5, 6] }, @@ -117,7 +117,6 @@ it("keeps each target's alternate setting and name on its elements", () => { }, { alternateSetting: 2, - name: Uint8Array.from([0x4f, 0xe9, 0, 0x58]), elements: [{ address: 0x1fff_7800, data: [] }], }, ], @@ -133,8 +132,8 @@ it("keeps each target's alternate setting and name on its elements", () => { [...element.bytes], ]), ).toEqual([ - [0, 0, "Internal Flash", 0, 0x0800_0000, [1, 2, 3, 4]], - [0, 0, "Internal Flash", 1, 0x0800_4000, [5, 6]], + [0, 0, "Oé", 0, 0x0800_0000, [1, 2, 3, 4]], + [0, 0, "Oé", 1, 0x0800_4000, [5, 6]], [1, 1, null, 0, 0x1fff_c000, [0xaa, 0x55]], ]); expect(decoded.metadata).toMatchObject({ @@ -143,19 +142,6 @@ it("keeps each target's alternate setting and name on its elements", () => { empty_element_count: 1, data_bytes: 8, }); - // A named target's field is read to its first NUL, one character per byte. - const named = image( - dfuseFile({ - targets: [ - { - alternateSetting: 3, - name: Uint8Array.from([0x4f, 0xe9, 0, 0x58]), - elements: [{ address: 0, data: [1] }], - }, - ], - }), - ); - expect(named.elements[0]?.targetName).toBe("Oé"); }); it("reports a suffix CRC mismatch as facts without refusing the file", () => { diff --git a/tests/boundary/mcp/firmwareContainersMcp.test.ts b/tests/boundary/mcp/firmwareContainersMcp.test.ts index 8af3c06c12..f3e59b0a0e 100644 --- a/tests/boundary/mcp/firmwareContainersMcp.test.ts +++ b/tests/boundary/mcp/firmwareContainersMcp.test.ts @@ -201,10 +201,19 @@ it("delivers a DfuSe CRC contradiction under both integrity policies through MCP { alternateSetting: 0, name: "Internal Flash", - elements: [{ address: 0x0800_0000, data: [1, 2, 3, 4] }], + elements: [ + { address: 0x0800_0000, data: [1, 2, 3, 4] }, + { address: 0x0800_4000, data: [5, 6] }, + ], + }, + { + alternateSetting: 1, + elements: [{ address: 0x1fff_c000, data: [0xaa, 0x55] }], }, ], }); + const computedCrc = valid.readUInt32LE(valid.length - 4); + const computedHex = computedCrc.toString(16).padStart(8, "0"); const corrupt = Buffer.from(valid); corrupt.writeUInt32LE(0, corrupt.length - 4); await writeFile(path, corrupt); @@ -218,10 +227,7 @@ it("delivers a DfuSe CRC contradiction under both integrity policies through MCP logical_path: ".", algorithm: "dfu-crc32", declared_crc32: "00000000", - calculated_crc32: valid - .readUInt32LE(valid.length - 4) - .toString(16) - .padStart(8, "0"), + calculated_crc32: computedHex, }, }); const recorded = await call("inspect_artifact", { @@ -240,8 +246,32 @@ it("delivers a DfuSe CRC contradiction under both integrity policies through MCP const graph = artifactInventoryResultSchema.parse( inspection.substeps[0]?.evidence.normalized_result, ); - expect( - graph.occurrences.find((row) => row.logical_path === ".")?.hash_status, - ).toBe("mismatched"); + const rootOccurrence = graph.occurrences.find( + (row) => row.logical_path === ".", + ); + expect(rootOccurrence).toMatchObject({ + hash_status: "mismatched", + limitations: ["Declared integrity metadata contradicts observed bytes."], + format_metadata: { suffix_crc: 0, computed_crc: computedCrc }, + }); + expect(graph.integrity_contradictions).toEqual([ + expect.objectContaining({ + occurrence_id: rootOccurrence?.occurrence_id, + logical_path: ".", + algorithm: "dfu-crc32", + declared_crc32: "00000000", + observed_crc32: computedHex, + trust: "observed-untrusted", + }), + ]); + expect(graph.limitations).toContainEqual( + expect.stringMatching(/1.*integrity.*observed-untrusted/iu), + ); + expect(graph.occurrences.map(({ logical_path }) => logical_path)).toEqual([ + ".", + "target-0/element-0-0x08000000-0x08000003.bin", + "target-0/element-1-0x08004000-0x08004001.bin", + "target-1/element-0-0x1fffc000-0x1fffc001.bin", + ]); await call("close_binary", {}); });