test(esp): share flash layout plan fixtures (#17319)

This commit is contained in:
Vincent Koc
2026-12-06 08:21:14 +08:00
committed by GitHub
parent 84f20aec9f
commit eabe6b80dc
+141 -162
View File
@@ -26,22 +26,23 @@ const table = (parts: readonly EspPartitionFixture[]): EspPartitionTable => {
const c3 = espChipForId(0x0005)!;
const input = (fields: Partial<EspFlashPlanInput> = {}): EspFlashPlanInput => ({
fileSize: 0x4_0000,
origin: { value: 0, source: "default" },
bootloader: { flashAddress: 0, fileOffset: 0, chip: c3 },
tableAddress: 0x8000,
tableMethod: "default",
table: table(TWO_OTA_PARTITIONS),
...fields,
});
const fixturePlan = (fields: Partial<EspFlashPlanInput> = {}) =>
planEspFlashLayout({
fileSize: 0x4_0000,
origin: { value: 0, source: "default" },
bootloader: { flashAddress: 0, fileOffset: 0, chip: c3 },
tableAddress: 0x8000,
tableMethod: "default",
table: table(TWO_OTA_PARTITIONS),
...fields,
});
const erasedGaps = (count: number) =>
Array.from({ length: count }, () => undefined);
describe("ESP flash layout plans", () => {
it("describes the bootloader region, the table sector and every partition", () => {
const plan = planEspFlashLayout(input());
const plan = fixturePlan();
expect(
plan.regions.map(({ path, flashAddress, length, available }) => [
path,
@@ -78,7 +79,7 @@ describe("ESP flash layout plans", () => {
{ label: "data", type: 1, subtype: 2, offset: 0xe000, size: 0x1000 },
];
expect(
planEspFlashLayout(input({ table: table(parts) }))
fixturePlan({ table: table(parts) })
.regions.map(({ path }) => path)
.slice(2),
).toEqual([
@@ -92,27 +93,25 @@ describe("ESP flash layout plans", () => {
});
it("accepts the primary bootloader and table entries ESP-IDF 5.3 declares", () => {
const plan = planEspFlashLayout(
input({
table: table([
{
label: "PrimaryBTLDR",
type: 2,
subtype: 0,
offset: 0,
size: 0x8000,
},
{
label: "PrimaryPrtTable",
type: 3,
subtype: 0,
offset: 0x8000,
size: 0x1000,
},
...TWO_OTA_PARTITIONS,
]),
}),
);
const plan = fixturePlan({
table: table([
{
label: "PrimaryBTLDR",
type: 2,
subtype: 0,
offset: 0,
size: 0x8000,
},
{
label: "PrimaryPrtTable",
type: 3,
subtype: 0,
offset: 0x8000,
size: 0x1000,
},
...TWO_OTA_PARTITIONS,
]),
});
expect(plan.limitations).toEqual([]);
expect(plan.regions.map(({ path }) => path).slice(0, 4)).toEqual([
"bootloader.bin",
@@ -123,37 +122,35 @@ describe("ESP flash layout plans", () => {
});
it("keeps a usable partition beside truncated, missing and early ones", () => {
const plan = planEspFlashLayout(
input({
fileSize: 0x3_8000,
origin: { value: 0x1000, source: "caller" },
bootloader: undefined,
table: table([
{ label: "early", type: 1, subtype: 2, offset: 0x0, size: 0x1000 },
{
label: "app",
type: 0,
subtype: 0,
offset: 0x1_0000,
size: 0x1_0000,
},
{
label: "cut",
type: 0,
subtype: 0x10,
offset: 0x3_0000,
size: 0x1_0000,
},
{
label: "gone",
type: 1,
subtype: 0x82,
offset: 0x10_0000,
size: 0x1000,
},
]),
}),
);
const plan = fixturePlan({
fileSize: 0x3_8000,
origin: { value: 0x1000, source: "caller" },
bootloader: undefined,
table: table([
{ label: "early", type: 1, subtype: 2, offset: 0x0, size: 0x1000 },
{
label: "app",
type: 0,
subtype: 0,
offset: 0x1_0000,
size: 0x1_0000,
},
{
label: "cut",
type: 0,
subtype: 0x10,
offset: 0x3_0000,
size: 0x1_0000,
},
{
label: "gone",
type: 1,
subtype: 0x82,
offset: 0x10_0000,
size: 0x1000,
},
]),
});
expect(
plan.regions.map(({ path, fileOffset, available }) => [
path,
@@ -185,20 +182,18 @@ describe("ESP flash layout overlaps", () => {
])(
"retains a zero-byte partition %s as an empty declaration",
(_, offset) => {
const plan = planEspFlashLayout(
input({
table: table([
{
label: "data",
type: 1,
subtype: 2,
offset: 0x9000,
size: 0x4000,
},
{ label: "empty", type: 1, subtype: 0x82, offset, size: 0 },
]),
}),
);
const plan = fixturePlan({
table: table([
{
label: "data",
type: 1,
subtype: 2,
offset: 0x9000,
size: 0x4000,
},
{ label: "empty", type: 1, subtype: 0x82, offset, size: 0 },
]),
});
expect(plan.regions.at(-1)).toMatchObject({
path: "partitions/empty.bin",
fileOffset: offset,
@@ -214,16 +209,14 @@ describe("ESP flash layout overlaps", () => {
it("retains a zero-byte partition declared before the selected flash origin", () => {
const origin = 0x1_0000;
const offset = 0x9000;
const plan = planEspFlashLayout(
input({
origin: { value: origin, source: "caller" },
tableAddress: origin,
bootloader: undefined,
table: table([
{ label: "empty", type: 1, subtype: 0x82, offset, size: 0 },
]),
}),
);
const plan = fixturePlan({
origin: { value: origin, source: "caller" },
tableAddress: origin,
bootloader: undefined,
table: table([
{ label: "empty", type: 1, subtype: 0x82, offset, size: 0 },
]),
});
expect(plan.regions).toContainEqual(
expect.objectContaining({
path: "partitions/empty.bin",
@@ -246,15 +239,13 @@ describe("ESP flash layout overlaps", () => {
});
it("reports every region inside a longer one", () => {
const plan = planEspFlashLayout(
input({
table: table([
{ label: "a", type: 1, subtype: 2, offset: 0x9000, size: 0x8000 },
{ label: "b", type: 1, subtype: 2, offset: 0xa000, size: 0x1000 },
{ label: "c", type: 1, subtype: 2, offset: 0xc000, size: 0x1000 },
]),
}),
);
const plan = fixturePlan({
table: table([
{ label: "a", type: 1, subtype: 2, offset: 0x9000, size: 0x8000 },
{ label: "b", type: 1, subtype: 2, offset: 0xa000, size: 0x1000 },
{ label: "c", type: 1, subtype: 2, offset: 0xc000, size: 0x1000 },
]),
});
expect(plan.limitations).toEqual([
expect.stringMatching(
/partitions\/b\.bin.*0xa000.*overlaps.*partitions\/a\.bin.*0x11000/iu,
@@ -287,9 +278,7 @@ describe("ESP flash layout partition paths", () => {
offset: 0x9000 + index * 0x1000,
size: 0x1000,
}));
const regions = planEspFlashLayout(
input({ table: table(parts) }),
).regions.slice(2);
const regions = fixturePlan({ table: table(parts) }).regions.slice(2);
expect(regions.map(({ path }) => path)).toEqual([
"partitions/0.bin",
"partitions/1.bin",
@@ -307,7 +296,7 @@ describe("ESP flash layout partition paths", () => {
describe("ESP flash layout coverage", () => {
it("keeps coverage complete for partitions the end of the file cuts short or leaves out", () => {
const cut = planEspFlashLayout(input({ fileSize: 0x2_8000 }));
const cut = fixturePlan({ fileSize: 0x2_8000 });
const reads = (plan: typeof cut) => ({
otadataRecords: [],
gapFirstUnerased: erasedGaps(plan.gaps.length),
@@ -322,21 +311,19 @@ describe("ESP flash layout coverage", () => {
expect(finishEspFlashLayout(cut, reads(cut)).coverage).toBe("complete");
// A partition before the dump's first byte has no entry, so coverage is
// unknown even with the bootloader located.
const later = planEspFlashLayout(
input({
fileSize: 0x3_f000,
origin: { value: 0x1000, source: "caller" },
bootloader: {
flashAddress: 0x1000,
fileOffset: 0,
chip: espChipForId(0x0000)!,
},
table: table([
{ label: "early", type: 1, subtype: 2, offset: 0x0, size: 0x1000 },
...TWO_OTA_PARTITIONS,
]),
}),
);
const later = fixturePlan({
fileSize: 0x3_f000,
origin: { value: 0x1000, source: "caller" },
bootloader: {
flashAddress: 0x1000,
fileOffset: 0,
chip: espChipForId(0x0000)!,
},
table: table([
{ label: "early", type: 1, subtype: 2, offset: 0x0, size: 0x1000 },
...TWO_OTA_PARTITIONS,
]),
});
expect(finishEspFlashLayout(later, reads(later)).coverage).toBe("unknown");
});
@@ -346,27 +333,25 @@ describe("ESP flash layout coverage", () => {
] as const)(
"marks coverage unknown when the partition table lies %s the bootloader and omits bootloader.bin",
(_relation, tableAddress, bootloaderAddress) => {
const plan = planEspFlashLayout(
input({
fileSize: 0x1_0000,
tableAddress,
tableMethod: "caller",
bootloader: {
flashAddress: bootloaderAddress,
fileOffset: bootloaderAddress,
chip: espChipForId(0x0000)!,
const plan = fixturePlan({
fileSize: 0x1_0000,
tableAddress,
tableMethod: "caller",
bootloader: {
flashAddress: bootloaderAddress,
fileOffset: bootloaderAddress,
chip: espChipForId(0x0000)!,
},
table: table([
{
label: "data",
type: 0x01,
subtype: 0x82,
offset: 0x1000,
size: 0x3000,
},
table: table([
{
label: "data",
type: 0x01,
subtype: 0x82,
offset: 0x1000,
size: 0x3000,
},
]),
}),
);
]),
});
expect(plan.regions.some(({ role }) => role === "bootloader")).toBe(
false,
);
@@ -387,15 +372,13 @@ describe("ESP flash layout otadata", () => {
const [, otadata] = TWO_OTA_PARTITIONS;
it("reads the otadata partition the bootloader uses: the last declared", () => {
const plan = planEspFlashLayout(
input({
table: table([
...TWO_OTA_PARTITIONS,
{ ...otadata!, label: "otadata2", offset: 0x3_0000 + 0x1_0000 },
]),
fileSize: 0x5_0000,
}),
);
const plan = fixturePlan({
table: table([
...TWO_OTA_PARTITIONS,
{ ...otadata!, label: "otadata2", offset: 0x3_0000 + 0x1_0000 },
]),
fileSize: 0x5_0000,
});
expect(plan.otadata?.partition.label).toBe("otadata2");
expect(plan.otadata?.records.map(({ fileOffset }) => fileOffset)).toEqual([
0x4_0000, 0x4_1000,
@@ -411,7 +394,7 @@ describe("ESP flash layout otadata", () => {
const short = TWO_OTA_PARTITIONS.map((part) =>
part.label === "otadata" ? { ...part, size: 0x1000 } : part,
);
const plan = planEspFlashLayout(input({ table: table(short) }));
const plan = fixturePlan({ table: table(short) });
expect(plan.otadata?.records).toEqual([
{ index: 0, fileOffset: 0xd000, available: true },
]);
@@ -441,7 +424,7 @@ describe("ESP flash layout otadata", () => {
expect(decoded.value.limitations).toEqual([
expect.stringMatching(/MD5.*0xc0.*does not match.*bootloader rejects/iu),
]);
const plan = planEspFlashLayout(input({ table: decoded.value }));
const plan = fixturePlan({ table: decoded.value });
const facts = finishEspFlashLayout(plan, {
otadataRecords: [otaRecord({ seq: 1 }), Buffer.alloc(32, 0xff)],
gapFirstUnerased: erasedGaps(plan.gaps.length),
@@ -457,7 +440,7 @@ describe("ESP flash layout otadata", () => {
});
it("keeps the record a dump holds when it ends between the two", () => {
const plan = planEspFlashLayout(input({ fileSize: 0xe000 }));
const plan = fixturePlan({ fileSize: 0xe000 });
expect(plan.otadata?.records).toEqual([
{ index: 0, fileOffset: 0xd000, available: true },
{ index: 1, fileOffset: 0xe000, available: false },
@@ -484,12 +467,10 @@ describe("ESP flash layout otadata", () => {
describe("ESP flash layout facts", () => {
it("describes erased gaps and reports the rest as undescribed", () => {
const plan = planEspFlashLayout(
input({
table: table(TWO_OTA_PARTITIONS.slice(0, 3)),
fileSize: 0x2_0000,
}),
);
const plan = fixturePlan({
table: table(TWO_OTA_PARTITIONS.slice(0, 3)),
fileSize: 0x2_0000,
});
expect(plan.gaps).toEqual([{ offset: 0x1_0000, length: 0x1_0000 }]);
const erased = finishEspFlashLayout(plan, {
otadataRecords: [],
@@ -508,7 +489,7 @@ describe("ESP flash layout facts", () => {
});
it("derives the selected app from the otadata records and leaves the running one unknown", () => {
const plan = planEspFlashLayout(input());
const plan = fixturePlan();
const facts = finishEspFlashLayout(plan, {
otadataRecords: [otaRecord({ seq: 2 }), Buffer.alloc(32, 0xff)],
gapFirstUnerased: [],
@@ -526,15 +507,13 @@ describe("ESP flash layout facts", () => {
});
expect(facts.coverage).toBe("complete");
// A factory-only table selects its factory app without otadata.
const withoutOtadata = planEspFlashLayout(
input({
table: table(
TWO_OTA_PARTITIONS.filter(({ label }) =>
["nvs", "phy_init", "factory"].includes(label),
),
const withoutOtadata = fixturePlan({
table: table(
TWO_OTA_PARTITIONS.filter(({ label }) =>
["nvs", "phy_init", "factory"].includes(label),
),
}),
);
),
});
expect(
finishEspFlashLayout(withoutOtadata, {
otadataRecords: [],