Own ELF machine and canonical family in the fixed-header parser. Fuse inspection consumes validated ELF facts and complete COFF machine values while retaining null for unknown or incomplete headers. Binary target, MIPS and Go consumers reuse the parsed facts. Adapt public CLI controls and fixture builders from #4788; extend x86, ELF byte order, malformed and unknown headers, MCP projection and byte preservation coverage. 430 focused tests passed, one Windows-only skip. Five authored inert fixtures have full actual CLI/stdio MCP Evidence parity; real Linux/Windows Electron apps remain unverified. Fixes #4786 Co-authored-by: Kaoru0822 <[email protected]>
71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { expect, it } from "vitest";
|
|
import { readElfFixedHeader } from "./elfFixedHeader.js";
|
|
import { elf } from "./binaryTarget.fixture.js";
|
|
|
|
it.each([
|
|
["ELF32 little endian", 1, 1],
|
|
["ELF32 big endian", 1, 2],
|
|
["ELF64 little endian", 2, 1],
|
|
["ELF64 big endian", 2, 2],
|
|
] as const)(
|
|
"reads common fixed header commitments for %s",
|
|
(_name, cls, data) => {
|
|
const bytes = elf(cls, data, cls === 1 ? 3 : 62);
|
|
expect(readElfFixedHeader(bytes)).toEqual({
|
|
ok: true,
|
|
value: {
|
|
elfClass: cls === 1 ? 32 : 64,
|
|
byteOrder: data === 1 ? "little" : "big",
|
|
headerSize: cls === 1 ? 52 : 64,
|
|
machine: cls === 1 ? 3 : 62,
|
|
architecture: cls === 1 ? "x86" : "x86_64",
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
[
|
|
"identification version",
|
|
(bytes: Buffer) => {
|
|
bytes[6] = 0;
|
|
},
|
|
],
|
|
[
|
|
"header version",
|
|
(bytes: Buffer) => {
|
|
bytes.writeUInt32LE(0, 20);
|
|
},
|
|
],
|
|
[
|
|
"undersized declared header",
|
|
(bytes: Buffer) => {
|
|
bytes.writeUInt16LE(51, 40);
|
|
},
|
|
],
|
|
[
|
|
"declared header beyond file",
|
|
(bytes: Buffer) => {
|
|
bytes.writeUInt16LE(53, 40);
|
|
},
|
|
],
|
|
])("rejects an invalid %s", (_name, corrupt) => {
|
|
const bytes = elf(1, 1, 3);
|
|
corrupt(bytes);
|
|
expect(readElfFixedHeader(bytes)).toMatchObject({ ok: false });
|
|
});
|
|
|
|
it("checks declared header size against the whole file, not the prefix", () => {
|
|
const bytes = elf(2, 1, 62);
|
|
bytes.writeUInt16LE(128, 52);
|
|
expect(readElfFixedHeader(bytes, 128)).toMatchObject({ ok: true });
|
|
expect(readElfFixedHeader(bytes, 127)).toMatchObject({ ok: false });
|
|
});
|
|
|
|
it("preserves an unknown ELF machine as a fact without rejecting the header", () => {
|
|
expect(readElfFixedHeader(elf(2, 2, 0xfffe))).toMatchObject({
|
|
ok: true,
|
|
value: { machine: 0xfffe, architecture: null },
|
|
});
|
|
});
|