From e9dead75ea7efd3567e80cf84a79b40fe3883c51 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 04:44:13 -0500 Subject: [PATCH] test(firmware): consolidate UF2 numbering and shared reader identity (#13372) --- src/artifacts/HexRecordArtifactReader.test.ts | 17 +- src/artifacts/Uf2ArtifactReader.test.ts | 176 +++++++----------- src/domain/firmware/uf2.test.ts | 38 ++-- 3 files changed, 100 insertions(+), 131 deletions(-) diff --git a/src/artifacts/HexRecordArtifactReader.test.ts b/src/artifacts/HexRecordArtifactReader.test.ts index 44d937f67d..6e5ec7d621 100644 --- a/src/artifacts/HexRecordArtifactReader.test.ts +++ b/src/artifacts/HexRecordArtifactReader.test.ts @@ -1,6 +1,6 @@ import { artifactPreparationRecoveryCases } from "../../tests/fixtures/artifactReaderLifecycle.js"; import { openRegularFile } from "../filesystem/RegularFile.js"; -import { writeFile } from "node:fs/promises"; +import { stat, utimes, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it, vi } from "vitest"; @@ -152,7 +152,7 @@ it("rejects forged entries and changed carrier identity, then closes idempotentl try { const entries = []; for await (const entry of reader.entries()) entries.push(entry); - const entry = entries[0]; + const entry = entries[1]; if (entry === undefined) throw new Error("Missing region"); await expect( reader.open({ ...entry, declaredSize: 1 }), @@ -160,9 +160,20 @@ it("rejects forged entries and changed carrier identity, then closes idempotentl await expect( reader.open({ ...entry, adapterKey: "hex-records:9" }), ).rejects.toMatchObject({ reason: "integrity" }); - await writeFile(path, INTEL_HEX_I32_GAP.replace(":00000001FF\n", "")); + expect(entry.path).toBe("0x08000100-0x08000103.bin"); + const replacement = INTEL_HEX_I32_GAP.replace( + ":04010000DEADBEEFC3", + ":04020000DEADBEEFC2", + ); + expect(replacement).not.toBe(INTEL_HEX_I32_GAP); + const { size } = await stat(path); + await writeFile(path, replacement); + // Stabilize the distinct timestamp even on a coarse-resolution volume. + await utimes(path, 1_000_000_000, 1_000_000_000); + expect((await stat(path)).size).toBe(size); await expect(reader.open(entry)).rejects.toMatchObject({ reason: "integrity", + integrityCheck: "container-identity", }); } finally { await reader.close(); diff --git a/src/artifacts/Uf2ArtifactReader.test.ts b/src/artifacts/Uf2ArtifactReader.test.ts index 1634ce11e9..77427b6748 100644 --- a/src/artifacts/Uf2ArtifactReader.test.ts +++ b/src/artifacts/Uf2ArtifactReader.test.ts @@ -3,7 +3,6 @@ import { join } from "node:path"; import { expect, it } from "vitest"; -import { Uf2ArtifactReader } from "./Uf2ArtifactReader.js"; import { RP2040_FAMILY, RP2XXX_ABSOLUTE_FAMILY, @@ -113,112 +112,94 @@ it("inventories each family's regions with family and block facts", async () => ]); }); -it.each([ - [[0x1000], [0, 0xffff_ffff]], - [[0x1000, 0x2000], [0]], -] as const)( - "keeps addressless UF2 comments out of copy counting (%j, %j)", - async (flash, comments) => { - const blocks = [ - ...flash.map((address) => - uf2Block({ address, payload: [1, 2, 3, 4], blockNo: 0, numBlocks: 2 }), - ), - ...comments.map((address) => - uf2Block({ - address, - payload: [9, 9], - blockNo: 1, - numBlocks: 2, - flags: UF2_FLAGS.notMainFlash, - }), - ), - ]; - const snapshot = await scanCanonicalArtifactInventory( - await writeImage("comments.uf2", uf2File(blocks)), - ); - expect(snapshot.coverage).toEqual({ - scope: "container-entries", - status: "complete", - }); - expect( - snapshot.occurrences.find((row) => row.logical_path === ".") - ?.format_metadata, - ).toMatchObject({ - kind: "uf2", - ambiguous_numbering_group_count: 0, - missing_block_count: 0, - repeated_block_count: 1, - not_main_flash_block_count: comments.length, - flash_block_count: flash.length, - region_count: flash.length, - data_bytes: 4 * flash.length, - }); - }, -); +type Block = readonly [blockNo: number, address: number, kind?: "comment"]; +const flash = (blockNo: number, address: number): Block => [blockNo, address]; +const comment = (blockNo: number, address: number): Block => [ + blockNo, + address, + "comment", +]; + +const first = flash(0, 0x1000); +const firstComment = comment(0, 0xffff_ffff); +const secondCopies = [flash(1, 0x3000), flash(1, 0x4000)]; +const twice = flash(0, 0x2000); it.each([ { - order: "first", - secondAddress: 0x2000, - coverage: "complete", - regionCount: 4, + label: "one flash address and repeated comments", + input: [first, comment(1, 0), comment(1, 0xffff_ffff)], + regions: 1, + ambiguous: 0, + repeats: 1, }, { - order: "second", - secondAddress: 0x2000, - coverage: "complete", - regionCount: 4, + label: "two flash addresses and one comment", + input: [first, twice, comment(1, 0)], + regions: 2, + ambiguous: 0, + repeats: 1, }, { - order: "first", - secondAddress: 0x1000, - coverage: "unknown", - regionCount: 3, + label: "leading shared-number comment and two copies", + input: [firstComment, first, twice, ...secondCopies], + regions: 4, + ambiguous: 0, + repeats: 3, }, { - order: "second", - secondAddress: 0x1000, - coverage: "unknown", - regionCount: 3, + label: "trailing shared-number comment and two copies", + input: [first, firstComment, twice, ...secondCopies], + regions: 4, + ambiguous: 0, + repeats: 3, }, -] as const)( - "reports $coverage UF2 coverage when the shared-number comment comes $order", - async ({ order, secondAddress, coverage, regionCount }) => { - const flash = (blockNo: number, address: number) => - uf2Block({ address, payload: [1, 2, 3, 4], blockNo, numBlocks: 2 }); - const comment = uf2Block({ - address: 0xffff_ffff, - payload: [9, 9], - blockNo: 0, - numBlocks: 2, - flags: UF2_FLAGS.notMainFlash, - }); - const first = flash(0, 0x1000); - const blocks = [ - ...(order === "first" ? [comment, first] : [first, comment]), - flash(0, secondAddress), - flash(1, 0x3000), - flash(1, 0x4000), - ]; + { + label: "leading shared-number comment and missing copy", + input: [firstComment, first, first, ...secondCopies], + regions: 3, + ambiguous: 1, + repeats: 3, + }, + { + label: "trailing shared-number comment and missing copy", + input: [first, firstComment, first, ...secondCopies], + regions: 3, + ambiguous: 1, + repeats: 3, + }, +])( + "reports exact physical coverage with $label", + async ({ input, regions, ambiguous, repeats }) => { + const blocks = input.map(([blockNo, address, kind]) => + uf2Block({ + address, + blockNo, + numBlocks: 2, + payload: kind === "comment" ? [9, 9] : [1, 2, 3, 4], + flags: kind === "comment" ? UF2_FLAGS.notMainFlash : 0, + }), + ); const snapshot = await scanCanonicalArtifactInventory( - await writeImage("shared-number.uf2", uf2File(blocks)), + await writeImage("numbered.uf2", uf2File(blocks)), ); expect(snapshot.coverage).toEqual({ scope: "container-entries", - status: coverage, + status: ambiguous === 0 ? "complete" : "unknown", }); + const comments = input.filter(([, , kind]) => kind === "comment").length; expect( snapshot.occurrences.find((row) => row.logical_path === ".") ?.format_metadata, ).toMatchObject({ kind: "uf2", - ambiguous_numbering_group_count: coverage === "complete" ? 0 : 1, + ambiguous_numbering_group_count: ambiguous, missing_block_count: 0, - repeated_block_count: 3, - not_main_flash_block_count: 1, - flash_block_count: 4, - region_count: regionCount, - data_bytes: 4 * regionCount, + repeated_block_count: repeats, + not_main_flash_block_count: comments, + flash_block_count: input.length - comments, + region_count: regions, + data_bytes: 4 * regions, }); }, ); @@ -229,26 +210,3 @@ it("leaves short files unrecognized", async () => { ); expect(truncated.manifest.root_format).toBe("file"); }); - -it("rejects a forged region entry and a changed carrier", async () => { - const path = await writeImage( - "identity.uf2", - uf2File(uf2Image(RP2040_FAMILY, 0x1000_0000, APP)), - ); - const reader = new Uf2ArtifactReader(path); - try { - const entries = []; - for await (const entry of reader.entries()) entries.push(entry); - const entry = entries[0]; - if (entry === undefined) throw new Error("Missing region"); - await expect( - reader.open({ ...entry, adapterKey: "uf2:1" }), - ).rejects.toMatchObject({ reason: "integrity" }); - await writeFile(path, uf2File(uf2Image(RP2040_FAMILY, 0, APP))); - await expect(reader.open(entry)).rejects.toMatchObject({ - reason: "integrity", - }); - } finally { - await reader.close(); - } -}); diff --git a/src/domain/firmware/uf2.test.ts b/src/domain/firmware/uf2.test.ts index 26c949e6b5..a27128b3d3 100644 --- a/src/domain/firmware/uf2.test.ts +++ b/src/domain/firmware/uf2.test.ts @@ -68,25 +68,25 @@ it("recognizes only a complete first block with all three magic numbers", () => it("joins one family's numbered blocks into a region across chunk boundaries", () => { const bytes = patternBytes(1000); const file = uf2File(uf2Image(RP2040_FAMILY, 0x1000_0000, bytes)); - for (const chunkSize of [file.length, 512, 100, 7]) { - const decoded = image(file, chunkSize); - expect(decoded.complete).toBe(true); - expect(decoded.limitations).toEqual([]); - expect(regionFacts(decoded)).toEqual([ - { - familyId: RP2040_FAMILY, - familyName: "RP2040", - start: 0x1000_0000, - end: 0x1000_03e7, - length: 1000, - blockCount: 4, - firstFileBlock: 0, - lastFileBlock: 3, - }, - ]); - expect(Buffer.compare(decoded.regions[0]!.bytes, bytes)).toBe(0); - } - expect(image(file).metadata).toEqual({ + const decoded = image(file, 7); + expect(decoded.complete).toBe(true); + expect(decoded.limitations).toEqual([]); + expect(regionFacts(decoded)).toEqual([ + { + familyId: RP2040_FAMILY, + familyName: "RP2040", + start: 0x1000_0000, + end: 0x1000_03e7, + length: 1000, + blockCount: 4, + firstFileBlock: 0, + lastFileBlock: 3, + }, + ]); + const [region] = decoded.regions; + if (region === undefined) throw new Error("Expected the complete UF2 region"); + expect(Buffer.compare(region.bytes, bytes)).toBe(0); + expect(decoded.metadata).toEqual({ kind: "uf2", block_count: 4, trailing_bytes: 0,