test(elf): share binary record fixtures (#15486)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 14:32:09 +00:00
1 parent 3e565f4bf6
commit f2e7bb43d7
6 files changed
+57 -85

No files matched your search

+3 -8
View File
@@ -1,14 +1,9 @@
import { expect, it } from "vitest";
import { readElfDynamicEntry } from "./elfDynamicEntry.js";
const formats = [
{ bits: 32, littleEndian: false },
{ bits: 32, littleEndian: true },
{ bits: 64, littleEndian: false },
{ bits: 64, littleEndian: true },
] as const;
import { elfRecordFormats } from "./elfRecords.fixture.js";
it.each(formats)(
it.each(elfRecordFormats)(
"preserves signed ELF$bits tags and unsigned values (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const wide = bits === 64;
@@ -42,7 +37,7 @@ it.each(formats)(
},
);
it.each(formats)(
it.each(elfRecordFormats)(
"refuses incomplete ELF$bits dynamic entries (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const length = bits === 64 ? 16 : 8;
+8 -30
View File
@@ -4,42 +4,20 @@ import {
readElfProgramHeader,
} from "./elfProgramHeader.js";
const formats = [
{ bits: 32, littleEndian: false },
{ bits: 32, littleEndian: true },
{ bits: 64, littleEndian: false },
{ bits: 64, littleEndian: true },
] as const;
import {
elfHeaderRecordFixture,
elfRecordFormats,
} from "./elfRecords.fixture.js";
it.each(formats)(
it.each(elfRecordFormats)(
"preserves exact ELF$bits raw program fields (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const wide = bits === 64;
const length = wide ? 56 : 32;
const backing = Buffer.alloc(length + 19, 0xa5);
const bytes = new Uint8Array(
backing.buffer,
backing.byteOffset + 7,
const { backing, bytes, view, values } = elfHeaderRecordFixture(
length,
wide,
);
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const values = wide
? [
0xffffffffffffffffn,
0xfedcba9876543210n,
0x8000000000000001n,
0x123456789abcdef0n,
0x8888888888888888n,
0x7777777777777777n,
]
: [
0xffffffffn,
0xfedcba98n,
0x80000001n,
0x12345678n,
0x88888888n,
0x77777777n,
];
view.setUint32(0, 0xfedcba98, littleEndian);
view.setUint32(wide ? 4 : 24, 0x89abcdef, littleEndian);
const offsets = wide ? [8, 16, 24, 32, 40, 48] : [4, 8, 12, 16, 20, 28];
@@ -71,7 +49,7 @@ it.each(formats)(
},
);
it.each(formats)(
it.each(elfRecordFormats)(
"refuses each incomplete ELF$bits program record (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const length = bits === 64 ? 56 : 32;
+31
View File
@@ -0,0 +1,31 @@
export const elfRecordFormats = [
{ bits: 32, littleEndian: false },
{ bits: 32, littleEndian: true },
{ bits: 64, littleEndian: false },
{ bits: 64, littleEndian: true },
] as const;
/** Prepare a header view with padding on both sides and six unsigned values. */
export const elfHeaderRecordFixture = (length: number, wide: boolean) => {
const backing = Buffer.alloc(length + 19, 0xa5);
const bytes = new Uint8Array(backing.buffer, backing.byteOffset + 7, length);
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const values = wide
? [
0xffffffffffffffffn,
0xfedcba9876543210n,
0x8000000000000001n,
0x123456789abcdef0n,
0x8888888888888888n,
0x7777777777777777n,
]
: [
0xffffffffn,
0xfedcba98n,
0x80000001n,
0x12345678n,
0x88888888n,
0x77777777n,
];
return { backing, bytes, view, values };
};
+3 -8
View File
@@ -1,14 +1,9 @@
import { expect, it } from "vitest";
import { readElfRelocationEntryAt } from "./elfRelocation.js";
const formats = [
{ bits: 32, littleEndian: false },
{ bits: 32, littleEndian: true },
{ bits: 64, littleEndian: false },
{ bits: 64, littleEndian: true },
] as const;
import { elfRecordFormats } from "./elfRecords.fixture.js";
it.each(formats)(
it.each(elfRecordFormats)(
"preserves ELF$bits REL/RELA fields in a selected byte view (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const wordBytes = bits / 8;
@@ -89,7 +84,7 @@ it.each(formats)(
},
);
it.each(formats)(
it.each(elfRecordFormats)(
"refuses incomplete ELF$bits REL/RELA rows at the selected offset (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const wordBytes = bits / 8;
+8 -30
View File
@@ -1,42 +1,20 @@
import { expect, it } from "vitest";
import { readElfSectionHeader } from "./elfSectionHeader.js";
const formats = [
{ bits: 32, littleEndian: false },
{ bits: 32, littleEndian: true },
{ bits: 64, littleEndian: false },
{ bits: 64, littleEndian: true },
] as const;
import {
elfHeaderRecordFixture,
elfRecordFormats,
} from "./elfRecords.fixture.js";
it.each(formats)(
it.each(elfRecordFormats)(
"preserves exact ELF$bits section and section-zero fields (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const wide = bits === 64;
const length = wide ? 64 : 40;
const backing = Buffer.alloc(length + 19, 0xa5);
const bytes = new Uint8Array(
backing.buffer,
backing.byteOffset + 7,
const { backing, bytes, view, values } = elfHeaderRecordFixture(
length,
wide,
);
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const values = wide
? [
0xffffffffffffffffn,
0xfedcba9876543210n,
0x8000000000000001n,
0x123456789abcdef0n,
0x8888888888888888n,
0x7777777777777777n,
]
: [
0xffffffffn,
0xfedcba98n,
0x80000001n,
0x12345678n,
0x88888888n,
0x77777777n,
];
view.setUint32(0, 0xfedcba98, littleEndian);
view.setUint32(4, 0x89abcdef, littleEndian);
view.setUint32(wide ? 40 : 24, 0xffffffff, littleEndian);
@@ -73,7 +51,7 @@ it.each(formats)(
},
);
it.each(formats)(
it.each(elfRecordFormats)(
"refuses each incomplete ELF$bits section (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const length = bits === 64 ? 64 : 40;
+4 -9
View File
@@ -1,14 +1,9 @@
import { describe, expect, it } from "vitest";
import { readElfSymbol, readElfSymbolAt } from "./elfSymbol.js";
const formats = [
{ bits: 32, littleEndian: false },
{ bits: 32, littleEndian: true },
{ bits: 64, littleEndian: false },
{ bits: 64, littleEndian: true },
] as const;
import { elfRecordFormats } from "./elfRecords.fixture.js";
it.each(formats)(
it.each(elfRecordFormats)(
"preserves raw ELF$bits symbols (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const wide = bits === 64;
@@ -48,7 +43,7 @@ it.each(formats)(
},
);
it.each(formats)(
it.each(elfRecordFormats)(
"refuses each incomplete ELF$bits symbol (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const length = bits === 64 ? 24 : 16;
@@ -67,7 +62,7 @@ it.each(formats)(
);
describe("readElfSymbolAt", () => {
it.each(formats)(
it.each(elfRecordFormats)(
"decodes ELF$bits symbols at non-zero offsets without slicing (littleEndian=$littleEndian)",
({ bits, littleEndian }) => {
const length = bits === 64 ? 24 : 16;