Files
reverse-engineering-anything/src/domain/binaryTarget.mips.test.ts
T

68 lines
2.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { parseExecutableHeader } from "./binaryTarget.js";
import { elf } from "./binaryTarget.fixture.js";
const header = (little: boolean, bits: 32 | 64 = 32): Buffer =>
elf(bits === 32 ? 1 : 2, little ? 1 : 2, 8, { type: 2, flags: 0x70001001 });
describe("MIPS ELF header boundaries", () => {
it.each([true, false])(
"retains source endian and ABI/ISA flags (little=%s)",
(little) => {
const parsed = parseExecutableHeader(header(little), "x64");
if (!parsed.ok) throw new Error(parsed.error);
expect(parsed.value).toEqual({
format: "elf",
architecture: "mips",
availableArchitectures: ["mips"],
mips: {
elfClass: 32,
byteOrder: little ? "little" : "big",
type: 2,
flags: 0x70001001,
},
});
},
);
it("reads ELF64 flags at their own offset without claiming o32 support", () => {
const bytes = header(false, 64);
bytes.writeUInt32BE(0xfeedface, 36);
bytes.writeUInt32BE(0x80000001, 48);
const parsed = parseExecutableHeader(bytes, "x64");
if (!parsed.ok) throw new Error(parsed.error);
expect(parsed.value).toMatchObject({
architecture: "mips",
mips: { elfClass: 64, byteOrder: "big", flags: 0x80000001 },
});
});
it("retains a PSP declaration without claiming Allegrex provider support", () => {
const bytes = header(true);
bytes.writeUInt32LE(0x10a23001, 36);
const parsed = parseExecutableHeader(bytes, "x64");
if (!parsed.ok) throw new Error(parsed.error);
expect(parsed.value).toMatchObject({
architecture: "mips",
mips: { flags: 0x10a23001 },
});
});
it("rejects a truncated header before reading flags", () => {
const parsed = parseExecutableHeader(header(true).subarray(0, 39), "x64");
expect(parsed).toEqual({
ok: false,
error: expect.stringMatching(/truncated ELF header.*52 bytes.*39/iu),
});
});
it("does not turn invalid byte order into little-endian MIPS", () => {
const bytes = header(true);
bytes[5] = 0;
expect(parseExecutableHeader(bytes, "x64")).toEqual({
ok: false,
error: expect.stringMatching(/unsupported ELF endianness/iu),
});
});
});