Files
reverse-engineering-anything/src/domain/artifactFormatMetadata.test.ts
T
ebd709cef5 fix(esp): disclose partition entries outside the table checksum (#12384)
* fix(esp): disclose partition entries outside the table checksum

* fix(esp): label the partition checksum offset origin

* fix(esp): count partition entries outside the table MD5

The coverage warning was only prose, and it presented the MD5 entry's
table-relative offset without a structured fact. Record
entriesAfterChecksum on the MD5 result, project it as
entries_after_checksum, and keep the warning as presentation.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(esp): accept saved flash inventories without the checksum count

A required entries_after_checksum rejected ESP flash Evidence written
before the field existed. Keep the count exactOptional so omission
stays unknown, and still emit it for new inventories.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
Co-authored-by: ShuaixinHuang <ShuaixinHuang@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-19 11:59:02 -05:00

131 lines
3.4 KiB
TypeScript

import { expect, it } from "vitest";
import { artifactInventoryResultSchema } from "./artifactGraph.js";
const digest = (character: string) => character.repeat(64);
const MD5 = "ab".repeat(16);
/** The MD5 object Evidence stored before `entries_after_checksum` existed. */
const historicalMd5 = {
status: "valid" as const,
stored: MD5,
computed: MD5,
};
const flashMetadata = (md5: {
readonly status: "valid";
readonly stored: string;
readonly computed: string;
readonly entries_after_checksum?: number | undefined;
}) => ({
kind: "esp-flash-image" as const,
flash_offset: { value: 0, source: "default" as const },
chip: null,
bootloader: null,
partition_table: {
flash_address: 0x8000,
method: "default" as const,
partition_count: 1,
md5,
},
partitions_outside_dump: [],
apps: [],
otadata: null,
boot_selection: {
basis: "none" as const,
active_record: null,
slot: null,
partition_index: null,
assumptions: [],
running_partition: "unknown" as const,
},
undescribed: [],
});
/** One inventory page, the payload `inspect_analysis_view` parses. */
const inventory = (
metadata: ReturnType<typeof flashMetadata>,
): Record<string, unknown> => ({
manifest: {
manifest_id: `agm_${digest("b")}`,
root_artifact_id: `art_${digest("a")}`,
root_sha256: digest("c"),
root_format: "esp-flash-image",
graph_sha256: digest("d"),
node_count: 1,
occurrence_count: 1,
edge_count: 0,
},
nodes: [
{
artifact_id: `art_${digest("a")}`,
format: "file",
sha256: digest("c"),
size: 0,
media_type: null,
architecture: null,
content_state: "materialized",
limitations: [],
},
],
occurrences: [
{
occurrence_id: `occ_${digest("e")}`,
artifact_id: `art_${digest("a")}`,
parent_occurrence_id: null,
logical_path: ".",
entry_kind: "file",
artifact_kind: "container",
artifact_format: "esp-flash-image",
declared_size: 0,
compressed_size: 0,
executable: false,
encrypted: false,
hash_status: "not-hashed",
source_location: null,
format_metadata: metadata,
limitations: [],
},
],
edges: [],
provenance: [],
coverage: { scope: "container-entries", status: "complete" },
integrity_contradictions: [],
limitations: [],
});
const parsedMd5 = (input: Record<string, unknown>) => {
const metadata =
artifactInventoryResultSchema.parse(input).occurrences[0]?.format_metadata;
if (metadata?.kind !== "esp-flash-image")
throw new Error("expected ESP flash metadata");
return metadata.partition_table.md5;
};
it("parses ESP flash Evidence saved before the checksum entry count", () => {
const md5 = parsedMd5(inventory(flashMetadata(historicalMd5)));
expect(md5).toEqual(historicalMd5);
expect(md5).not.toHaveProperty("entries_after_checksum");
});
it("keeps a recorded checksum entry count on current ESP flash Evidence", () => {
expect(
parsedMd5(
inventory(flashMetadata({ ...historicalMd5, entries_after_checksum: 2 })),
),
).toEqual({ ...historicalMd5, entries_after_checksum: 2 });
});
it("rejects an explicit undefined checksum entry count", () => {
expect(
artifactInventoryResultSchema.safeParse(
inventory(
flashMetadata({
...historicalMd5,
entries_after_checksum: undefined,
}),
),
).success,
).toBe(false);
});