Files
reverse-engineering-anything/src/android/JadxJavaProbe.test.ts
T
morluto ab84ba6839 fix(process)!: retain inspection cancellation and cleanup ownership (#10113)
* fix(process): preserve producer-owned inspection cancellation

Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration.

* fix(process): type inspection ownership handoffs precisely

* fix(process): retain plain inspection preparation cancellation

* fix(process): retain explicit inspection cleanup and cancellation facts

* fix(process): require explicit inspection facts and cleanup scope

* refactor(process): remove redundant cleanup forwarding wrapper

* fix(firmware): retain inspection cancellation and cleanup ownership

* fix(wakaru): retain typed inspection outcomes and caller scope

* fix(ghidra): preserve inspection facts and launch cleanup scope

* fix(providers): preserve typed process inspection outcomes

* refactor(process): remove obsolete inspection cancellation re-export

* fix(native): preserve inspection preparation cleanup and cancellation

* fix(registration): retain typed inspection cleanup during Git discovery

* fix(process): preserve inspection deadline origin in adapter handoff

* fix(process): consume the canonical inspection selector in binding

* fix(native): preserve inspection deadlines and cleanup retry completion

* test(process): narrow the retained Darwin preparation owner

* fix(providers): preserve preparation ownership and timeout policy

Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy.

* refactor(process): use one inspection failure type owner

* fix(providers): retain recorded secondary failures when rebinding

* fix(ghidra): preserve normalized inspection facts through public projection

* fix(process): normalize native process listing cancellation

* docs(development): clarify failure provenance and cleanup verification

* fix(process): preserve direct inspection cancellation facts

* fix(process): retain inspection owners through termination

* fix(process): preserve command inspection cleanup provenance

* fix(desktop): preserve inspection facts after cleanup retry

* refactor(process)!: migrate desktop capture with inspection producers

* fix(desktop)!: preserve capture failure origins and cleanup owners (#10115)

* fix(desktop): preserve capture failure origins and cleanup

* fix(desktop): type keyring cleanup cancellation

* fix(kuna)!: retain copy cleanup before retiring workspaces (#10114)

* fix(kuna): own copy cancellation and descriptor cleanup

* fix(kuna): close copy descriptors before workspace removal

* test(kuna): type copy lifecycle gates for project library

* docs(kuna): explain retained copy-file cleanup and staging ownership

* docs(kuna): separate cache publication, integrity and cleanup guidance

* fix(desktop): retain sampling inspection owners before release

* fix(process): preserve capture file read cancellation origin

* fix(process): retain capture inspection cancellation and cleanup owners

* fix(process): retain sampling inspection failures in capture scope

* fix(process): retain inspection failures through mandatory cleanup

* fix(process): distinguish sampler shutdown from capture cancellation

* fix(browser): retain startup EOF before later cancellation

* fix(process): bind capture inspection cleanup to the owning run

* test(process): migrate cancellation fixtures to typed owners

Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification.

* fix(process): own sampling reads and settle inspection peers

* fix(errors): retain authored recovery through cleanup composition

* fix(process): retain all sampling inspection leaves for final cleanup

* test(browser): report recording cleanup signal outcome

* fix(process): keep inspection error context out of implicit JSON

* fix(desktop): retain normalized sampling cleanup ownership

* test(process): assert tagged startup cancellation
2026-11-11 16:37:48 -05:00

213 lines
7.9 KiB
TypeScript

import { validateJadxConfiguration } from "./JadxConfiguration.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { ArtifactResourceRetryFailure } from "../artifacts/ArtifactResourceScope.js";
import { expect, it, vi } from "vitest";
import { inspectJadxJava, type JadxJavaProbeOptions } from "./JadxJavaProbe.js";
import {
AnalysisError,
primaryAnalysisError,
} from "../domain/analysisErrorBase.js";
import {
ProcessOwnershipIdentityBatchFailure,
ProcessOwnershipInspectionCancelled,
} from "../process/ProcessInspectionCancellation.js";
import {
prepareProcessOwnershipInspection,
systemProcessOwnershipHost,
} from "../process/ProcessOwnershipObservation.js";
import type { OwnedProviderProcessSpawnOptions } from "../process/ProviderProcess.js";
import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js";
import { createInspectionResources } from "../../tests/fixtures/inspectionResources.js";
it.each([false, true])(
"retains physical preparation instead of reporting a Java refusal (batch: %s)",
async (batch) => {
const descriptor = await failedInspectionDescriptor();
const resources = createInspectionResources();
const reason = { source: "inspection caller" };
const cause = batch
? new ProcessOwnershipIdentityBatchFailure(
[
{ pid: 41, requestIndex: 0, cause: descriptor.failure },
{
pid: 42,
requestIndex: 1,
cause: new ProcessOwnershipInspectionCancelled(reason),
},
],
new Map(),
)
: descriptor.failure;
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockRejectedValue(cause);
// macOS prepares before launcher admission; other hosts prepare inside it.
// Keep that real admission step while recording that Java never starts.
const startJava = vi.fn(() => {
throw new Error("Java must not start");
});
const launcher = async (input: OwnedProviderProcessSpawnOptions) => {
await prepareProcessOwnershipInspection(input.signal);
return startJava();
};
try {
const result: unknown = await inspectJadxJava(
"/selected/java",
{},
{ resourceOwner: resources.owner, launcher },
).catch((cause: unknown) => cause);
expect(result).toBeInstanceOf(AnalysisError);
if (!(result instanceof AnalysisError)) throw result;
const primary = primaryAnalysisError(result);
expect(primary).toMatchObject({
_tag: "ProviderAdapterError",
diagnostics: { stage: "ownership_preparation" },
});
expect(primary.cause).toBe(cause);
if (batch)
expect(primary.secondaryFailures).toContainEqual(
expect.objectContaining({
_tag: "AnalysisCancelledError",
cause: reason,
}),
);
expect(primary.capturedOutput).toBeUndefined();
expect(primary.diagnostics).not.toHaveProperty("compiler_outcome");
expect(startJava).not.toHaveBeenCalled();
expect(result.cleanupResources).toEqual([descriptor.failure.path]);
await expect(resources.close()).rejects.toBeInstanceOf(
ArtifactResourceRetryFailure,
);
expect(result.cleanupIncomplete).toBe(true);
descriptor.allow();
await resources.close();
expect(result.cleanupIncomplete).toBe(false);
expect(descriptor.failure.owner.handle.fd).toBe(-1);
} finally {
prepare.mockRestore();
descriptor.allow();
await resources.close();
await descriptor.failure.owner.close();
}
},
);
it("preserves the actual inspection cancellation record and opaque caller reason", async () => {
const resources = createInspectionResources();
const reason = new ProviderCleanupError("foreign", ["/foreign/resource"], {});
const inspection = new ProcessOwnershipInspectionCancelled(reason);
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockRejectedValue(inspection);
const startJava = vi.fn(() => {
throw new Error("Java must not start");
});
const launcher = async (input: OwnedProviderProcessSpawnOptions) => {
await prepareProcessOwnershipInspection(input.signal);
return startJava();
};
try {
const result: unknown = await inspectJadxJava(
"/selected/java",
{},
{ resourceOwner: resources.owner, launcher },
).catch((cause: unknown) => cause);
expect(result).toMatchObject({ _tag: "AnalysisCancelledError" });
if (!(result instanceof AnalysisError)) throw result;
expect(result.cause).toBe(reason);
expect(Reflect.get(result, "inspectionFailure")).toBe(inspection);
expect(result.cleanupResources).toEqual([]);
expect(result.secondaryFailures).toEqual([]);
expect(reason.cleanupIncomplete).toBe(true);
expect(startJava).not.toHaveBeenCalled();
} finally {
prepare.mockRestore();
await resources.close();
}
});
it.each(["probe", "configuration"] as const)(
"keeps owned deadline policy and exact inspection facts through %s",
async (boundary) => {
const resources = createInspectionResources();
const prepare = vi
.spyOn(systemProcessOwnershipHost, "prepare")
.mockResolvedValue(undefined);
const caller = new AbortController();
let stopped: ProcessOwnershipInspectionCancelled | undefined;
const options: JadxJavaProbeOptions = {
resourceOwner: resources.owner,
signal: caller.signal,
timeoutMs: 20,
launcher: async (input) => {
const signal = input.signal;
if (signal === undefined)
throw new Error("Expected owned deadline signal");
await new Promise<void>((resolve) => {
if (signal.aborted) resolve();
else
signal.addEventListener("abort", () => resolve(), { once: true });
});
stopped = new ProcessOwnershipInspectionCancelled(signal.reason);
throw stopped;
},
};
try {
const result: unknown = await (
boundary === "probe"
? inspectJadxJava("/selected/java", {}, options)
: validateJadxConfiguration(
{
jar: "/unused/engine.jar",
java: "/selected/java",
jvmArguments: [],
},
{},
"inspect_android_package",
options,
)
).catch((cause: unknown) => cause);
expect(stopped).toBeInstanceOf(ProcessOwnershipInspectionCancelled);
if (boundary === "probe") {
expect(result).toMatchObject({
ok: false,
error: {
code: "version_unresolved",
kind: "java_probe",
diagnostics: { failure: "probe_terminated", deadline_ms: 20 },
inspectionFailure: stopped,
},
});
if (
typeof result !== "object" ||
result === null ||
!("error" in result)
)
throw new Error("Expected runtime refusal");
const refusal = result.error;
if (typeof refusal !== "object" || refusal === null)
throw new Error("Expected refusal facts");
expect(Reflect.get(refusal, "inspectionFailure")).toBe(stopped);
expect(Object.keys(refusal)).not.toContain("inspectionFailure");
expect(refusal).not.toHaveProperty("cancellation");
} else {
expect(result).toMatchObject({
_tag: "AnalysisCapabilityUnavailableError",
diagnostics: { failure_code: "version_unresolved" },
});
if (
!(result instanceof AnalysisError) ||
!(result.cause instanceof Error)
)
throw new Error("Expected projected configuration refusal");
expect(result.cause.cause).toBe(stopped);
expect(result.secondaryFailures).toEqual([]);
}
expect(caller.signal.aborted).toBe(false);
} finally {
prepare.mockRestore();
await resources.close();
}
},
);