diff --git a/tests/boundary/filesystem/mipsAbiFlags.test.ts b/tests/boundary/filesystem/mipsAbiFlags.test.ts index 139bfd16ee6..da7e1cee92a 100644 --- a/tests/boundary/filesystem/mipsAbiFlags.test.ts +++ b/tests/boundary/filesystem/mipsAbiFlags.test.ts @@ -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. diff --git a/tests/boundary/filesystem/mipsSharedLoad.test.ts b/tests/boundary/filesystem/mipsSharedLoad.test.ts index aa0fd2f9be2..54aaeef2e37 100644 --- a/tests/boundary/filesystem/mipsSharedLoad.test.ts +++ b/tests/boundary/filesystem/mipsSharedLoad.test.ts @@ -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); diff --git a/tests/fixtures/mipsElfWriters.ts b/tests/fixtures/mipsElfWriters.ts new file mode 100644 index 00000000000..8afc4419a82 --- /dev/null +++ b/tests/fixtures/mipsElfWriters.ts @@ -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), +});