test(mips): share endian writers for ELF fixtures (#16942)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 12:55:20 +08:00
1 parent f97e823e7d
commit 2ce38c0cd0
3 files changed
+16 -32

No files matched your search

+4 -24
View File
@@ -1,3 +1,4 @@
import { mipsElfWriters } from "../../fixtures/mipsElfWriters.js";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { open, readFile, realpath, writeFile } from "node:fs/promises";
@@ -34,14 +35,7 @@ const abiOffset = 8192; // Deliberately outside BinaryTargetResolver's 4 KiB pro
const fixture = (little = true): Buffer => {
const bytes = Buffer.alloc(abiOffset + 48);
bytes.set([0x7f, 0x45, 0x4c, 0x46, 1, little ? 1 : 2, 1]);
const u16 = (value: number, offset: number) =>
little
? bytes.writeUInt16LE(value, offset)
: bytes.writeUInt16BE(value, offset);
const u32 = (value: number, offset: number) =>
little
? bytes.writeUInt32LE(value, offset)
: bytes.writeUInt32BE(value, offset);
const { u16, u32 } = mipsElfWriters(bytes, little);
u16(2, 16);
u16(8, 18);
u32(1, 20);
@@ -99,14 +93,7 @@ const withGnuAttributes = (
);
source.copy(bytes);
let payloadOffset = payloadStart;
const write16 = (value: number, offset: number) =>
little
? bytes.writeUInt16LE(value, offset)
: bytes.writeUInt16BE(value, offset);
const write32 = (value: number, offset: number) =>
little
? bytes.writeUInt32LE(value, offset)
: bytes.writeUInt32BE(value, offset);
const { u16: write16, u32: write32 } = mipsElfWriters(bytes, little);
write16(2 + payloads.length, 48);
write16(0, 50); // Section names are not needed for numeric-type detection.
payloads.forEach((payload, index) => {
@@ -714,14 +701,7 @@ it("preserves cancellation instead of turning it into malformed target data", as
const legacyFixture = (little = true, flags = 0x50000001, type = 2): Buffer => {
const bytes = fixture(little);
const u16 = (value: number, offset: number) =>
little
? bytes.writeUInt16LE(value, offset)
: bytes.writeUInt16BE(value, offset);
const u32 = (value: number, offset: number) =>
little
? bytes.writeUInt32LE(value, offset)
: bytes.writeUInt32BE(value, offset);
const { u16, u32 } = mipsElfWriters(bytes, little);
u16(type, 16);
u32(flags, 36);
u32(0, 52); // Clear PT_MIPS_ABIFLAGS so the image is recordless.
@@ -1,3 +1,4 @@
import { mipsElfWriters } from "../../fixtures/mipsElfWriters.js";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { open, readFile, writeFile } from "node:fs/promises";
@@ -18,14 +19,7 @@ const relOffset = 4096;
const fixture = (little: boolean, count = 2): Buffer => {
const bytes = Buffer.alloc(relOffset + count * 8 + 16);
bytes.set([0x7f, 0x45, 0x4c, 0x46, 1, little ? 1 : 2, 1]);
const u16 = (value: number, offset: number) =>
little
? bytes.writeUInt16LE(value, offset)
: bytes.writeUInt16BE(value, offset);
const u32 = (value: number, offset: number) =>
little
? bytes.writeUInt32LE(value, offset)
: bytes.writeUInt32BE(value, offset);
const { u16, u32 } = mipsElfWriters(bytes, little);
u16(3, 16);
u16(8, 18);
u32(1, 20);
+10
View File
@@ -0,0 +1,10 @@
export const mipsElfWriters = (bytes: Buffer, little: boolean) => ({
u16: (value: number, offset: number) =>
little
? bytes.writeUInt16LE(value, offset)
: bytes.writeUInt16BE(value, offset),
u32: (value: number, offset: number) =>
little
? bytes.writeUInt32LE(value, offset)
: bytes.writeUInt32BE(value, offset),
});