From 1e5cdbc7ee10118dde862e904b2b537e886bf35a Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 25 Nov 2026 15:44:55 +0800 Subject: [PATCH] test(fixtures): share little-endian integer buffers (#14432) --- src/artifacts/deno/DenoStandalone.fixture.ts | 19 ++++++------------- src/artifacts/dotnet/DotnetBundle.fixture.ts | 14 ++++---------- src/artifacts/nodeSea/NodeSea.fixture.ts | 12 +++++------- src/artifacts/pyinstaller/PyzArchive.test.ts | 7 +------ src/dotnet/ManagedFieldConstants.test.ts | 7 +------ src/dotnet/ManagedPe.fixture.ts | 7 +------ src/native/BinaryInteger.fixture.ts | 18 ++++++++++++++++++ src/python/PythonPyc.fixture.ts | 14 ++++---------- src/python/PythonPycParser.test.ts | 14 ++++---------- tests/acceptance/pyinstallerArchive.test.ts | 7 +------ tests/fixtures/apkSignature/signingScheme.ts | 9 +++------ tests/fixtures/godotPck.ts | 16 ++++++---------- 12 files changed, 54 insertions(+), 90 deletions(-) diff --git a/src/artifacts/deno/DenoStandalone.fixture.ts b/src/artifacts/deno/DenoStandalone.fixture.ts index 7f437718df..a0ef1e20ae 100644 --- a/src/artifacts/deno/DenoStandalone.fixture.ts +++ b/src/artifacts/deno/DenoStandalone.fixture.ts @@ -1,4 +1,9 @@ -import { write32, write64 } from "../../native/BinaryInteger.fixture.js"; +import { + uint32LE as u32, + uint64LE as u64, + write32, + write64, +} from "../../native/BinaryInteger.fixture.js"; import { elfNote, elfNoteFixture } from "../../native/elf/ElfNotes.fixture.js"; import { peResourceFixture } from "../../domain/pe/peResources.fixture.js"; @@ -46,18 +51,6 @@ export interface DenoSectionFixture { const u8 = (value: number): Buffer => Buffer.from([value]); -const u32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeUInt32LE(value); - return bytes; -}; - -const u64 = (value: number | bigint): Buffer => { - const bytes = Buffer.alloc(8); - bytes.writeBigUInt64LE(BigInt(value)); - return bytes; -}; - const asBytes = (value: Buffer | string): Buffer => typeof value === "string" ? Buffer.from(value, "utf8") : value; diff --git a/src/artifacts/dotnet/DotnetBundle.fixture.ts b/src/artifacts/dotnet/DotnetBundle.fixture.ts index 85af9ab3db..7370abe300 100644 --- a/src/artifacts/dotnet/DotnetBundle.fixture.ts +++ b/src/artifacts/dotnet/DotnetBundle.fixture.ts @@ -1,3 +1,7 @@ +import { + uint32LE as u32, + uint64LE as u64, +} from "../../native/BinaryInteger.fixture.js"; import { deflateRawSync } from "node:zlib"; /** Source-owned Microsoft bundle-layout fixture; no target executable is run. */ @@ -101,16 +105,6 @@ export const dotnetBundleFixture = ( }; }; -const u32 = (value: number): Buffer => { - const out = Buffer.alloc(4); - out.writeUInt32LE(value); - return out; -}; -const u64 = (value: number): Buffer => { - const out = Buffer.alloc(8); - out.writeBigUInt64LE(BigInt(value)); - return out; -}; const text = (value: string): Buffer => { const bytes = Buffer.from(value); const length: number[] = []; diff --git a/src/artifacts/nodeSea/NodeSea.fixture.ts b/src/artifacts/nodeSea/NodeSea.fixture.ts index 091969655b..b3b9ca7d51 100644 --- a/src/artifacts/nodeSea/NodeSea.fixture.ts +++ b/src/artifacts/nodeSea/NodeSea.fixture.ts @@ -1,4 +1,8 @@ -import { write32, write64 } from "../../native/BinaryInteger.fixture.js"; +import { + uint64LE as u64, + write32, + write64, +} from "../../native/BinaryInteger.fixture.js"; import { elfNote, elfNoteFixture } from "../../native/elf/ElfNotes.fixture.js"; import { peResourceFixture } from "../../domain/pe/peResources.fixture.js"; @@ -38,12 +42,6 @@ export interface NodeSeaCarrierOptions extends NodeSeaBlobFixtureOptions { const asBytes = (value: Buffer | string): Buffer => typeof value === "string" ? Buffer.from(value, "utf8") : value; -const u64 = (value: number): Buffer => { - const bytes = Buffer.alloc(8); - bytes.writeBigUInt64LE(BigInt(value)); - return bytes; -}; - const sized = (value: Buffer | string): Buffer => { const body = asBytes(value); return Buffer.concat([u64(body.length), body]); diff --git a/src/artifacts/pyinstaller/PyzArchive.test.ts b/src/artifacts/pyinstaller/PyzArchive.test.ts index fda19ad583..eb175a3f88 100644 --- a/src/artifacts/pyinstaller/PyzArchive.test.ts +++ b/src/artifacts/pyinstaller/PyzArchive.test.ts @@ -1,3 +1,4 @@ +import { int32LE as int32 } from "../../native/BinaryInteger.fixture.js"; import { pythonPycFixture } from "../../python/PythonPyc.fixture.js"; import { parsePythonPyc } from "../../python/PythonPycParser.js"; import { pythonPycLimitsSchema } from "../../domain/python/pythonPyc.js"; @@ -180,12 +181,6 @@ it.each([0x61, 0x41, 0x7a, 0x5a, 0x75])( }, ); -const int32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeInt32LE(value); - return bytes; -}; - const readImage = async (bytes: Buffer, declaredSize = bytes.length) => { const root = await createTestTempDirectory("rea-pyz-file-"); const path = join(root, "image.pyz"); diff --git a/src/dotnet/ManagedFieldConstants.test.ts b/src/dotnet/ManagedFieldConstants.test.ts index 2c7e2008f3..a58ebb2a58 100644 --- a/src/dotnet/ManagedFieldConstants.test.ts +++ b/src/dotnet/ManagedFieldConstants.test.ts @@ -1,3 +1,4 @@ +import { int32LE as i4Value } from "../native/BinaryInteger.fixture.js"; import { describe, expect, it } from "vitest"; import { buildManagedPeFixture } from "../../scripts/fixtures/managed/pe.mjs"; import { managedPeFixtureTarget } from "./ManagedPe.fixture.js"; @@ -222,12 +223,6 @@ it.each([ }, ); -const i4Value = (n: number): Buffer => { - const buf = Buffer.alloc(4); - buf.writeInt32LE(n, 0); - return buf; -}; - it("binds unique and duplicate defaults to their selected fields in a non-monotonic table", () => { const bytes = buildManagedPeFixture({ fields: ["F1", "F2", "F3", "F4"].map((name) => ({ name, flags: 0x8056 })), diff --git a/src/dotnet/ManagedPe.fixture.ts b/src/dotnet/ManagedPe.fixture.ts index 1280366e6b..323ae005a7 100644 --- a/src/dotnet/ManagedPe.fixture.ts +++ b/src/dotnet/ManagedPe.fixture.ts @@ -1,3 +1,4 @@ +import { uint32LE as u32 } from "../native/BinaryInteger.fixture.js"; import { createHash } from "node:crypto"; import type { BinaryTarget } from "../domain/binaryTargetTypes.js"; @@ -184,12 +185,6 @@ const u16 = (value: number): Buffer => { return bytes; }; -const u32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeUInt32LE(value, 0); - return bytes; -}; - type MetadataIndexSize = 2 | 4; const indexSize = (rows: number, codedTagBits = 0): MetadataIndexSize => diff --git a/src/native/BinaryInteger.fixture.ts b/src/native/BinaryInteger.fixture.ts index a333ca73c2..2c790475bc 100644 --- a/src/native/BinaryInteger.fixture.ts +++ b/src/native/BinaryInteger.fixture.ts @@ -38,3 +38,21 @@ export const writeWord = ( if (bits === 64) write64(bytes, offset, value, little); else write32(bytes, offset, Number(value), little); }; + +export const uint32LE = (value: number): Buffer => { + const bytes = Buffer.alloc(4); + bytes.writeUInt32LE(value); + return bytes; +}; + +export const int32LE = (value: number): Buffer => { + const bytes = Buffer.alloc(4); + bytes.writeInt32LE(value); + return bytes; +}; + +export const uint64LE = (value: number | bigint): Buffer => { + const bytes = Buffer.alloc(8); + bytes.writeBigUInt64LE(BigInt(value)); + return bytes; +}; diff --git a/src/python/PythonPyc.fixture.ts b/src/python/PythonPyc.fixture.ts index 370b8dfda5..72686de88e 100644 --- a/src/python/PythonPyc.fixture.ts +++ b/src/python/PythonPyc.fixture.ts @@ -1,18 +1,12 @@ +import { + uint32LE as u32, + int32LE as i32, +} from "../native/BinaryInteger.fixture.js"; import type { PythonPycCodeLayout } from "../domain/python/pythonPyc.js"; const FLAG_REF = 0x80; const u8 = (value: number): Buffer => Buffer.from([value]); -const u32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeUInt32LE(value); - return bytes; -}; -const i32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeInt32LE(value); - return bytes; -}; const concat = (...parts: Buffer[]): Buffer => Buffer.concat(parts); /** 16-byte CPython 3.7+ pyc header. Hash-based pycs store an 8-byte source hash. */ diff --git a/src/python/PythonPycParser.test.ts b/src/python/PythonPycParser.test.ts index 05a108555d..49b056e92b 100644 --- a/src/python/PythonPycParser.test.ts +++ b/src/python/PythonPycParser.test.ts @@ -1,3 +1,7 @@ +import { + uint32LE as u32, + int32LE as i32, +} from "../native/BinaryInteger.fixture.js"; import { createHash } from "node:crypto"; import { expect, it } from "vitest"; import { @@ -484,16 +488,6 @@ it("classifies the 12-byte 3.7 alpha header as unsupported", () => { } }); -const u32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeUInt32LE(value); - return bytes; -}; -const i32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeInt32LE(value); - return bytes; -}; const f64 = (value: number): Buffer => { const bytes = Buffer.alloc(8); bytes.writeDoubleLE(value); diff --git a/tests/acceptance/pyinstallerArchive.test.ts b/tests/acceptance/pyinstallerArchive.test.ts index 80ec219ff5..54f724ca70 100644 --- a/tests/acceptance/pyinstallerArchive.test.ts +++ b/tests/acceptance/pyinstallerArchive.test.ts @@ -1,3 +1,4 @@ +import { int32LE as int32 } from "../../src/native/BinaryInteger.fixture.js"; import { createHash } from "node:crypto"; import { readFile, readdir, rm, writeFile } from "node:fs/promises"; import { dirname, join } from "node:path"; @@ -93,12 +94,6 @@ const onefile = async ( return path; }; -const int32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeInt32LE(value); - return bytes; -}; - const marshalInt = (value: number): Buffer => Buffer.concat([Buffer.from([0x69]), int32(value)]); diff --git a/tests/fixtures/apkSignature/signingScheme.ts b/tests/fixtures/apkSignature/signingScheme.ts index 9781911001..1949e1d911 100644 --- a/tests/fixtures/apkSignature/signingScheme.ts +++ b/tests/fixtures/apkSignature/signingScheme.ts @@ -1,12 +1,9 @@ -/** Full v2/v3 signer framing with opaque, deliberately unverified signatures. */ -const u32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeUInt32LE(value); - return bytes; -}; +import { uint32LE as u32 } from "../../../src/native/BinaryInteger.fixture.js"; + const lp = (bytes: Uint8Array): Buffer => Buffer.concat([u32(bytes.length), bytes]); +/** Full v2/v3 signer framing with opaque, deliberately unverified signatures. */ export const apkSchemeValue = ( signedData: Uint8Array, sdkRange?: { readonly min: number; readonly max: number }, diff --git a/tests/fixtures/godotPck.ts b/tests/fixtures/godotPck.ts index a560a24e05..7f517b042f 100644 --- a/tests/fixtures/godotPck.ts +++ b/tests/fixtures/godotPck.ts @@ -1,3 +1,9 @@ +import { + uint32LE as pckU32, + uint64LE as pckU64, +} from "../../src/native/BinaryInteger.fixture.js"; + +export { pckU32, pckU64 }; import { createHash } from "node:crypto"; export interface GodotPckMember { @@ -9,16 +15,6 @@ export interface GodotPckMember { readonly offset?: bigint; } -export const pckU32 = (value: number): Buffer => { - const bytes = Buffer.alloc(4); - bytes.writeUInt32LE(value); - return bytes; -}; -export const pckU64 = (value: bigint | number): Buffer => { - const bytes = Buffer.alloc(8); - bytes.writeBigUInt64LE(BigInt(value)); - return bytes; -}; const pad4 = (bytes: Buffer): Buffer => Buffer.concat([bytes, Buffer.alloc((4 - (bytes.length % 4)) % 4)]);