mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 14:31:22 +00:00
75 lines
2.2 KiB
TypeScript
75 lines
2.2 KiB
TypeScript
import { expect, it } from "vitest";
|
|
import { readElfSectionHeader } from "./elfSectionHeader.js";
|
|
|
|
import {
|
|
elfHeaderRecordFixture,
|
|
elfRecordFormats,
|
|
} from "./elfRecords.fixture.js";
|
|
|
|
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, bytes, view, values } = elfHeaderRecordFixture(
|
|
length,
|
|
wide,
|
|
);
|
|
view.setUint32(0, 0xfedcba98, littleEndian);
|
|
view.setUint32(4, 0x89abcdef, littleEndian);
|
|
view.setUint32(wide ? 40 : 24, 0xffffffff, littleEndian);
|
|
view.setUint32(wide ? 44 : 28, 0x80000001, littleEndian);
|
|
const offsets = wide ? [8, 16, 24, 32, 48, 56] : [8, 12, 16, 20, 32, 36];
|
|
values.forEach((value, index) => {
|
|
if (wide) view.setBigUint64(offsets[index]!, value, littleEndian);
|
|
else view.setUint32(offsets[index]!, Number(value), littleEndian);
|
|
});
|
|
const before = Buffer.from(backing);
|
|
const parsed = readElfSectionHeader(
|
|
new Uint8Array(backing.buffer, bytes.byteOffset, length + 5),
|
|
bits,
|
|
littleEndian,
|
|
);
|
|
expect(backing.equals(before)).toBe(true);
|
|
backing.fill(0);
|
|
expect(parsed).toEqual({
|
|
ok: true,
|
|
value: {
|
|
nameOffset: 0xfedcba98,
|
|
type: 0x89abcdef,
|
|
flags: values[0],
|
|
compressed: true,
|
|
address: values[1],
|
|
offset: values[2],
|
|
size: values[3],
|
|
link: 0xffffffff,
|
|
info: 0x80000001,
|
|
alignment: values[4],
|
|
entrySize: values[5],
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it.each(elfRecordFormats)(
|
|
"refuses each incomplete ELF$bits section (littleEndian=$littleEndian)",
|
|
({ bits, littleEndian }) => {
|
|
const length = bits === 64 ? 64 : 40;
|
|
const backing = Buffer.alloc(length + 7);
|
|
for (let observed = 0; observed < length; observed++)
|
|
expect(
|
|
readElfSectionHeader(
|
|
backing.subarray(7, 7 + observed),
|
|
bits,
|
|
littleEndian,
|
|
),
|
|
).toEqual({
|
|
ok: false,
|
|
error: {
|
|
kind: "malformed",
|
|
message: expect.stringContaining("truncated"),
|
|
},
|
|
});
|
|
},
|
|
);
|