import { ApktoolCommandCleanup } from "./ApktoolCommandCleanup.js"; import { afterEach, expect, it, vi } from "vitest"; import { AnalysisCancelledError, AnalysisTimeoutError, } from "../domain/analysisErrorCore.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { spawnFailure } from "../../tests/support/process/spawnFailure.js"; import { OwnedCommandFailure, runOwnedCommand, } from "../process/OwnedCommand.js"; import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js"; import { inspectApktoolClient } from "./ApktoolConfiguration.js"; import { ApktoolProvider } from "./ApktoolProvider.js"; vi.mock("../process/OwnedCommand.js", async (importOriginal) => ({ ...(await importOriginal()), runOwnedCommand: vi.fn(), prepareOwnedCommand: vi.fn(async () => {}), })); let commands = new ApktoolCommandCleanup(); afterEach(async () => { await commands.close(); commands = new ApktoolCommandCleanup(); vi.mocked(runOwnedCommand).mockReset(); }); const selection = { command: "/opt/apktool/apktool", commandSource: "environment", } as const; const owner = { stop: vi.fn(async () => ({ status: "verified-cleanup" as const })), } as unknown as ProviderProcessSupervisor; it("runs the version probe as an owned interpreter-script process group", async () => { vi.mocked(runOwnedCommand).mockImplementationOnce(async (_spawn, limits) => { const output = { stdout: { text: "2.9.3\n", bytes: 6, observedBytes: 6 }, stderr: { text: "", bytes: 0, observedBytes: 0 }, exitCode: 0, signal: null, }; limits.observeCompleted?.(output); return output; }); await expect( inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }), ).resolves.toEqual({ version: "2.9.3", supportsDocumentedVersion: true, limitations: [], }); expect(vi.mocked(runOwnedCommand).mock.lastCall?.[0]).toMatchObject({ command: "/opt/apktool/apktool", arguments: ["--version"], expectedCommand: null, }); expect(vi.mocked(runOwnedCommand).mock.lastCall?.[1]).toMatchObject({ timeoutMs: 20_000, diagnosticBytes: 64 * 1024, }); }); it("reports an expired version probe as a typed timeout", async () => { vi.mocked(runOwnedCommand).mockRejectedValueOnce( new OwnedCommandFailure("timeout", "Command deadline elapsed."), ); const failure = await inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }).catch((cause: unknown) => cause); expect(failure).toBeInstanceOf(AnalysisTimeoutError); const projected = projectAnalysisError(failure as AnalysisTimeoutError); expect(projected).toMatchObject({ code: "provider_timeout", category: "timeout", details: { timeout_ms: 20_000 }, }); expect(projected.details?.limit).toBeUndefined(); }); it("retains an unresolved probe cleanup owner and reports the cleanup", async () => { vi.mocked(runOwnedCommand).mockRejectedValueOnce( new OwnedCommandFailure( "timeout", "Command deadline elapsed.", null, "cleanup incomplete", undefined, ["pid:1"], owner, ), ); const failure = await inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }).catch((cause: unknown) => cause); expect((failure as AnalysisTimeoutError).cleanupPrimary).toBeInstanceOf( AnalysisTimeoutError, ); expect(projectAnalysisError(failure as AnalysisTimeoutError)).toMatchObject({ code: "cleanup_incomplete", }); expect(owner.stop).not.toHaveBeenCalled(); await commands.close(); expect((failure as AnalysisTimeoutError).cleanupIncomplete).toBe(false); }); it("reports a cancelled version probe with the exact recorded caller reason", async () => { const reason = { caller: "cancel selected version probe" }; vi.mocked(runOwnedCommand).mockRejectedValueOnce( new OwnedCommandFailure("cancelled", "Command cancelled.", null, null, { cancellation: { reason }, }), ); const failure: unknown = await inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }).catch((cause: unknown) => cause); expect(failure).toBeInstanceOf(AnalysisCancelledError); if (!(failure instanceof AnalysisCancelledError)) throw failure; expect(failure.cause).toBe(reason); expect(failure.cleanupResources).toEqual([]); }); it("keeps unresolved probe cleanup typed when the launcher overflows its output", async () => { const overflow = () => new OwnedCommandFailure( "output-limit", "Command diagnostic output exceeded its complete-output budget.", null, "process group still running", undefined, ["pid:7", "process-group:7"], owner, ); vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow()); const failure = await inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }).catch((cause: unknown) => cause); expect((failure as AnalysisTimeoutError).cleanupResources).toEqual([ "pid:7", "process-group:7", ]); // Both provider entry points report the unresolved processes as cleanup. const provider = new ApktoolProvider({ environment: { REA_APKTOOL_COMMAND: "/opt/apktool/apktool" }, }); vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow()); await expect(provider.inspectAvailability()).rejects.toMatchObject({ cleanupIncomplete: true, }); vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow()); const result = await provider.execute({ operation: "decode_android_resources", input: { path: "/fixture/input.apk", include_strings: false }, }); expect(result.ok ? null : result.error.cleanupIncomplete).toBe(true); expect(result.ok ? null : projectAnalysisError(result.error)).toMatchObject({ code: "cleanup_incomplete", details: { resources: ["pid:7", "process-group:7"] }, }); await provider.close(); expect(result.ok ? null : result.error.cleanupIncomplete).toBe(false); }); it.each(["linux", "win32"] as const)( "interprets advisory applicability using the selected %s host", async (platform) => { vi.mocked(runOwnedCommand).mockImplementationOnce( async (_spawn, limits) => { const output = { stdout: { text: "2.9.2\n", bytes: 6, observedBytes: 6 }, stderr: { text: "", bytes: 0, observedBytes: 0 }, exitCode: 0, signal: null, }; limits.observeCompleted?.(output); return output; }, ); const identity = await inspectApktoolClient(selection, commands, { environment: {}, platform, }); expect(identity.version).toBe("2.9.2"); expect( identity.limitations.some((warning) => warning.includes("GHSA-vgwr-4w3p-xmjv"), ), ).toBe(platform === "win32"); }, ); it.each([ ["EACCES", "command_not_executable"], ["EPERM", "command_not_executable"], ["ENOENT", "command_missing"], ])("distinguishes launcher spawn %s", async (code, expected) => { vi.mocked(runOwnedCommand).mockRejectedValueOnce( spawnFailure(code, selection.command), ); await expect( inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }), ).rejects.toMatchObject({ code: expected, diagnostics: { filesystem_error: code, selected_command: selection.command, }, }); }); it("does not read an errno from outside the launcher as a launch refusal", async () => { vi.mocked(runOwnedCommand).mockRejectedValueOnce( Object.assign(new Error("read failed"), { code: "ENOENT" }), ); await expect( inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }), ).rejects.toMatchObject({ code: "version_unresolved" }); }); it.each([ ["EAGAIN", true], ["E2BIG", false], ])( "reports a host launch constraint %s as a resource constraint, not a launcher fault", async (code, retryable) => { vi.mocked(runOwnedCommand).mockRejectedValueOnce( spawnFailure(code, selection.command), ); const failure = await inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }).catch((cause: unknown) => cause); expect(failure).toBeInstanceOf(AnalysisError); expect(projectAnalysisError(failure as AnalysisError)).toMatchObject({ code: "resource_constraint", retryable, details: { operation: "inspect_apktool_client", diagnostics: { system_code: code, command: selection.command, selected_command: selection.command, }, }, }); }, ); it("reports version capture overflow as a resource constraint with the selected launcher", async () => { vi.mocked(runOwnedCommand).mockRejectedValueOnce( new OwnedCommandFailure("output-limit", "too much output"), ); const failure = await inspectApktoolClient(selection, commands, { environment: {}, platform: "linux", }).catch((cause: unknown) => cause); expect(projectAnalysisError(failure as AnalysisTimeoutError)).toMatchObject({ code: "resource_constraint", details: { operation: "inspect_apktool_client", diagnostics: { selected_command: selection.command }, }, }); });