Files
reverse-engineering-anything/src/native/NativeMacosErrorProjection.test.ts
T
morluto a69837647b test: fix false-pass assertions, consolidate suites, and close coverage gaps (#15519)
* test: fix false-pass assertions, consolidate suites, and close coverage gaps

* test: finish signal-0, unchecked close_binary, and vacuous .every() audit

* test(registration): run shared capture inside test so child inherits scaled deadline

* test(artifacts): bind final verification fault to reader source and post-read phase
2026-12-01 18:10:12 +00:00

243 lines
7.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
import { InternalInvariantError } from "../domain/internalInvariant.js";
import { ParseFailureError } from "../domain/parseFailure.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
import type { NativeCommandCapture } from "../providers/NativeCommandRunner.js";
import {
codeSignArchitectures,
isNativeOperation,
isNonzeroUnsignedObservation,
nativeCommandCleanupFailure,
nativeOutputFailure,
parseEntitlements,
xmlSyntaxRefusal,
} from "./NativeMacosErrorProjection.js";
const makeCapture = (
overrides: Partial<NativeCommandCapture> = {},
): NativeCommandCapture => ({
tool: "codesign",
executable: "/usr/bin/codesign",
arguments: ["-dvv", "/tmp/snapshot.bin"],
toolVersion: null,
versionReason: null,
executableSha256: "a".repeat(64),
exitCode: 1,
signal: null,
stdout: "",
stderr: "/tmp/snapshot.bin: code object is not signed at all\n",
stdoutBytes: 0,
stderrBytes: 51,
...overrides,
});
describe("nativeOutputFailure", () => {
it("rethrows InternalInvariantError instead of wrapping it as output parsing", () => {
const invariant = new InternalInvariantError("broken assumption");
expect(() => nativeOutputFailure("inspect_macho", invariant)).toThrow(
invariant,
);
});
it("wraps ordinary producer errors in AnalysisOutputError with cause preserved", () => {
const cause = new SyntaxError("unexpected token");
const projected = nativeOutputFailure("inspect_macho", cause);
expect(projected).toBeInstanceOf(AnalysisOutputError);
if (!(projected instanceof AnalysisOutputError))
throw new Error("Expected AnalysisOutputError");
expect(projected.operation).toBe("inspect_macho");
expect(projected.cause).toBe(cause);
});
});
describe("isNonzeroUnsignedObservation", () => {
const snapshot = "/tmp/snapshot.bin";
it("accepts exact unsigned diagnostics with or without verified Executable prefix", () => {
expect(isNonzeroUnsignedObservation(makeCapture(), snapshot, [])).toBe(
true,
);
expect(
isNonzeroUnsignedObservation(
makeCapture({
stderr: `Executable=/app/bin\r\n${snapshot}: code object is not signed at all\r\n`,
}),
snapshot,
["/app/bin"],
),
).toBe(true);
});
it("rejects zero exit, signals, stdout noise, operand mismatch, and spoofed stderr", () => {
expect(
isNonzeroUnsignedObservation(makeCapture({ exitCode: 0 }), snapshot, []),
).toBe(false);
expect(
isNonzeroUnsignedObservation(
makeCapture({ exitCode: null }),
snapshot,
[],
),
).toBe(false);
expect(
isNonzeroUnsignedObservation(
makeCapture({ signal: "SIGTERM" }),
snapshot,
[],
),
).toBe(false);
expect(
isNonzeroUnsignedObservation(
makeCapture({ stdout: "unexpected" }),
snapshot,
[],
),
).toBe(false);
expect(
isNonzeroUnsignedObservation(
makeCapture({ arguments: ["-dvv", "/other/path"] }),
snapshot,
[],
),
).toBe(false);
expect(
isNonzeroUnsignedObservation(
makeCapture({
stderr: `Executable=/unverified/bin\n${snapshot}: code object is not signed at all\n`,
}),
snapshot,
["/app/bin"],
),
).toBe(false);
});
});
describe("codeSignArchitectures", () => {
const baseTarget = {
kind: "executable" as const,
path: "/tmp/app",
canonicalPath: "/tmp/app",
size: 1024,
sha256: "a".repeat(64),
format: "mach-o" as const,
architecture: "arm64" as const,
availableArchitectures: ["arm64", "x86"] as const,
};
it("reports unknownSlices when universal target lacks slice inventory", () => {
expect(
codeSignArchitectures(null, {
...baseTarget,
format: "mach-o-universal",
universalSlice: {
offset: 0,
size: 1024,
},
}),
).toEqual({ architectures: [], unknownSlices: true });
});
it("deduplicates architecture names from Mach-O universal format strings", () => {
expect(
codeSignArchitectures(
"Mach-O universal (x86_64 arm64 x86_64)",
baseTarget,
),
).toEqual({ architectures: ["x86_64", "arm64"], unknownSlices: false });
});
it("maps x86 to i386 for thin Mach-O targets without thin format prefix", () => {
expect(codeSignArchitectures(null, baseTarget)).toEqual({
architectures: ["arm64", "i386"],
unknownSlices: false,
});
});
it("returns empty architectures for Mach-O thin format strings", () => {
expect(codeSignArchitectures("Mach-O thin (arm64)", baseTarget)).toEqual({
architectures: [],
unknownSlices: false,
});
});
});
describe("parseEntitlements", () => {
it("returns null value with no limitations when no XML plist envelope exists", () => {
expect(parseEntitlements("Executable=/app/bin\n")).toEqual({
value: null,
limitations: [],
});
});
it("parses XML plist entitlements embedded in codesign output", () => {
const xml = [
"Executable=/app/bin",
'<?xml version="1.0" encoding="UTF-8"?>',
'<plist version="1.0"><dict>',
"<key>com.apple.security.app-sandbox</key><true/>",
"<key>com.apple.security.app-sandbox</key><false/>",
"</dict></plist>",
].join("\n");
const parsed = parseEntitlements(xml);
expect(parsed.value).toEqual({
"com.apple.security.app-sandbox": false,
});
expect(parsed.limitations.length).toBeGreaterThan(0);
});
});
describe("xmlSyntaxRefusal", () => {
it("extracts refusal message only for malformed xml ParseFailureError inside AnalysisOutputError", () => {
const malformedXml = new AnalysisOutputError("inspect_plist", "failed", {
cause: new ParseFailureError({
kind: "malformed",
format: "xml",
detail: "unclosed tag",
}),
});
expect(xmlSyntaxRefusal(malformedXml)).toContain("unclosed tag");
const nonXml = new AnalysisOutputError("inspect_plist", "failed", {
cause: new ParseFailureError({
kind: "malformed",
format: "plist",
detail: "bad dict",
}),
});
expect(xmlSyntaxRefusal(nonXml)).toBeUndefined();
expect(xmlSyntaxRefusal(new Error("plain"))).toBeUndefined();
});
});
describe("nativeCommandCleanupFailure & isNativeOperation", () => {
it("deduplicates cleanup resources across multiple failures", () => {
const owner = {} as ProviderProcessSupervisor;
const failure = nativeCommandCleanupFailure([
{
owner,
reason: "pid 1 stuck",
resources: ["process:1", "root:/tmp/a"],
},
{
owner,
reason: "pid 2 stuck",
resources: ["root:/tmp/a", "process:2"],
},
]);
expect(failure).toBeInstanceOf(ProviderCleanupError);
expect(failure.cleanupResources).toEqual([
"process:1",
"root:/tmp/a",
"process:2",
]);
});
it("distinguishes registered native operations from unrelated ones", () => {
expect(isNativeOperation("inspect_signature")).toBe(true);
expect(isNativeOperation("inspect_plist")).toBe(true);
expect(isNativeOperation("open_binary" as never)).toBe(false);
});
});