Files
reverse-engineering-anything/tests/boundary/filesystem/appleBundleTargetInvariant.test.ts
T
David Muñoz Rey 6aa3898317 fix(apple): rethrow internal invariant violations while resolving a bundle's program file (#11392)
* fix(apple): rethrow internal invariant violations while resolving a bundle's program file

Bundle target resolution decodes Info.plist with the shared binary plist decoder, which reports a broken internal assumption as InternalInvariantError. The resolver caught every decode failure and reported it as a BinaryTargetError ("Info.plist is malformed, unsupported, or lacks CFBundleExecutable"), and its two outer catches wrapped anything else the same way. A REA defect therefore reached the caller as a fact about the user's bundle.

#11029 made the other plist consumers rethrow the invariant. The resolver's decode site and both outer catches now do the same.

Tests: a boundary test opens a .app and a .bundle through parseBinaryTarget with the decoder replaced by one that reports an invariant violation. Before this change both resolved { ok: false } with a BinaryTargetError; now both reject with the InternalInvariantError. The same bundles open normally with the real decoder.

* test(apple): mock the binary plist decoder at its domain path

Main moved the binary plist materializer from src/artifacts/apple to
src/domain/apple, and AppBundleExecutable now imports it from there. The
invariant test still mocked the old path, which no longer exists, so tsc
failed and the mock would have intercepted nothing.

The test now mocks src/domain/apple/binaryPlistMaterializer.js. With the
rethrow removed from AppBundleExecutable both cases fail; with it they pass.
2026-11-21 20:56:47 +01:00

69 lines
2.2 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { buildBinary } from "plist";
import { afterEach, expect, it, vi } from "vitest";
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
import { thinMach } from "../../../src/domain/binaryTarget.fixture.js";
import {
InternalInvariantError,
invariantViolation,
} from "../../../src/domain/internalInvariant.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
/**
* No input reaches an invariant site today, so the shared binary plist
* decoder is replaced with one that reports a broken internal assumption.
*/
const decoder = vi.hoisted(() => ({ broken: false }));
vi.mock(
"../../../src/domain/apple/binaryPlistMaterializer.js",
async (importOriginal) => {
const actual =
await importOriginal<
typeof import("../../../src/domain/apple/binaryPlistMaterializer.js")
>();
return {
...actual,
decodeBinaryPropertyList: (
...args: Parameters<typeof actual.decodeBinaryPropertyList>
) => {
if (decoder.broken)
throw invariantViolation("Binary plist source plan is missing");
return actual.decodeBinaryPropertyList(...args);
},
};
},
);
afterEach(() => {
decoder.broken = false;
});
it.each(["Fixture.app", "Fixture.bundle"])(
"reports an invariant violation while reading %s's Info.plist as a REA defect, not a bundle failure",
async (name) => {
const root = await createTestTempDirectory("rea-bundle-invariant-");
const bundle = join(root, name);
await mkdir(join(bundle, "Contents", "MacOS"), { recursive: true });
await writeFile(
join(bundle, "Contents", "Info.plist"),
Buffer.from(buildBinary({ CFBundleExecutable: "Fixture" })),
);
await writeFile(
join(bundle, "Contents", "MacOS", "Fixture"),
thinMach(0xfeedfacf, 0x0100000c),
);
const open = () =>
parseBinaryTarget(bundle, { cwd: root, hostArchitecture: "arm64" });
await expect(open()).resolves.toMatchObject({ ok: true });
decoder.broken = true;
const rejected = await open().catch((cause: unknown) => cause);
expect(rejected).toBeInstanceOf(InternalInvariantError);
},
);