Files
reverse-engineering-anything/src/domain/windowsNativeArtifact.test.ts
T
gensokyo-dev 740c0f5615 fix(windows): restore native verification fixture coverage (#7479)
Restore independent PE fixtures and dependency-complete standalone artifact verifier tests. Execute existing real Windows native behavior and inspection recovery proofs before unrelated curated tests.

Validation: 101 focused tests; full typecheck; scoped lint, module/discovery/format/docs checks; valid/corrupt real-image verifier controls; real Windows artifact, behavior, and inspection recovery proof steps passed. Greptile found no actionable issues. Codex review quota exhausted.

CI remains red from exact-base failures: Source 9, Windows 7 (base 15; loader 8 repaired), and built shard 4's 7 matching failures; 7 other shards cancelled by fail-fast. Later Windows package/Node 22/24 checks skipped. User explicitly authorized bypass of established main failures. No claim of complete CI or unavailable coverage.

Fixes #7474.
2026-10-27 18:50:40 +08:00

124 lines
4.0 KiB
TypeScript

import { createHash } from "node:crypto";
import { expect, it } from "vitest";
import { windowsNativeArtifactFixture } from "../../tests/fixtures/windowsNativeArtifact.js";
import {
createWindowsNativeManifest,
parseWindowsNativeManifest,
validateWindowsNativeArtifact,
} from "./windowsNativeArtifact.js";
it("produces and admits the exact manifest/byte identity", () => {
const { bytes, manifest } = windowsNativeArtifactFixture();
const created = createWindowsNativeManifest("1.0.0", bytes);
expect(created).toEqual({ ok: true, value: manifest });
const parsed = parseWindowsNativeManifest(manifest, "1.0.0");
expect(parsed).toEqual(created);
if (!parsed.ok) throw parsed.error;
expect(validateWindowsNativeArtifact(parsed.value, bytes)).toEqual({
ok: true,
value: undefined,
});
expect(parseWindowsNativeManifest(manifest, "2.0.0")).toMatchObject({
ok: false,
error: { message: expect.stringContaining("another REA version") },
});
expect(
validateWindowsNativeArtifact(parsed.value, Buffer.alloc(128)),
).toMatchObject({
ok: false,
error: { message: expect.stringContaining("SHA-256") },
});
});
it.each([null, [], {}, "manifest"])(
"rejects malformed manifest %j",
(value) => {
expect(parseWindowsNativeManifest(value, "1.0.0")).toMatchObject({
ok: false,
error: { _tag: "WindowsNativeArtifactError" },
});
},
);
it("rejects missing and unknown manifest fields", () => {
const { manifest } = windowsNativeArtifactFixture();
expect(
parseWindowsNativeManifest({ ...manifest, extra: true }, "1.0.0"),
).toMatchObject({ ok: false });
for (const key of Object.keys(manifest)) {
const incomplete: Record<string, unknown> = { ...manifest };
delete incomplete[key];
expect(parseWindowsNativeManifest(incomplete, "1.0.0")).toMatchObject({
ok: false,
});
expect(
parseWindowsNativeManifest({ ...manifest, [key]: null }, "1.0.0"),
).toMatchObject({ ok: false });
}
});
it.each([
"platform",
"architecture",
"abiVersion",
"nodeApiVersion",
"artifact",
"artifactSha256",
])("rejects unsupported %s", (key) => {
const { manifest } = windowsNativeArtifactFixture();
expect(
parseWindowsNativeManifest({ ...manifest, [key]: "unsupported" }, "1.0.0"),
).toMatchObject({ ok: false });
});
it.each([0, 63, 64, 89, 128])(
"rejects invalid PE content in %i bytes",
(length) => {
expect(
createWindowsNativeManifest("1.0.0", Buffer.alloc(length)),
).toMatchObject({ ok: false });
},
);
it.each(["offset", "signature", "machine", "optional magic"])(
"rejects invalid PE %s with a matching digest",
(field) => {
const { bytes } = windowsNativeArtifactFixture();
if (field === "offset") bytes.writeUInt32LE(0xffff_ffff, 0x3c);
if (field === "signature") bytes.write("BAD!", 64, "ascii");
if (field === "machine") bytes.writeUInt16LE(0x14c, 68);
if (field === "optional magic") bytes.writeUInt16LE(0x10b, 88);
const { manifest } = windowsNativeArtifactFixture();
manifest.artifactSha256 = createHash("sha256").update(bytes).digest("hex");
const parsed = parseWindowsNativeManifest(manifest, "1.0.0");
if (!parsed.ok) throw parsed.error;
expect(validateWindowsNativeArtifact(parsed.value, bytes)).toMatchObject({
ok: false,
error: { message: expect.stringContaining("PE32+") },
});
},
);
it.each([0, 1, 65, 0xffff])(
"rejects an unavailable declared optional header of %i bytes",
(length) => {
const { bytes } = windowsNativeArtifactFixture();
bytes.writeUInt16LE(length, 84);
expect(createWindowsNativeManifest("1.0.0", bytes)).toMatchObject({
ok: false,
error: { message: expect.stringContaining("PE32+") },
});
},
);
it.each([2, 32, 63, 64])(
"admits PE32+ identity in a present declared header of %i bytes",
(length) => {
const { bytes } = windowsNativeArtifactFixture();
bytes.writeUInt16LE(length, 84);
expect(createWindowsNativeManifest("1.0.0", bytes)).toMatchObject({
ok: true,
});
},
);