Files
reverse-engineering-anything/tests/boundary/providers/artifactReadCleanupRetry.test.ts
T
94c3077355 test(providers): own temporary directories through createTestTempDirectory (#12722)
* test(providers): own temporary directories through createTestTempDirectory

Route provider-boundary fixtures through createTestTempDirectory so each
case removes its canonical directory after the test. Process shutdown stays
in afterEach, which still runs before that removal. Skip the two Windows
Cutter checks with it.skipIf, and compare unknown plugin recovery with
cutterBridgeSourceRecovery.

* test(providers): keep gated fixture cleanup with its owner

createTestTempDirectory always deletes after the test, including when
GDB or inferior cleanup fails and when the Rizin MCP harness already
owns the directory. Restore mkdtemp removal behind confirmed process
cleanup, and leave decoder inputs owned by the harness.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(cutter): keep an independent unknown-source recovery check

Comparing discovery recovery with cutterBridgeSourceRecovery follows the
formatter, so dropping the unknown-source explanation still passes. Assert
that explanation directly, beside bridge_source and the plugin path.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-20 04:06:31 +00:00

111 lines
4.2 KiB
TypeScript

import type { FileHandle } from "node:fs/promises";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const cleanupState = vi.hoisted(() => ({
failFirstClose: false,
abortController: undefined as AbortController | undefined,
firstHandle: undefined as FileHandle | undefined,
firstHandleCloseAttempts: 0,
}));
const openedFirstHandle = (): FileHandle => {
const handle = cleanupState.firstHandle;
if (handle === undefined) throw new Error("Missing admitted descriptor");
return handle;
};
vi.mock("node:fs/promises", async (importOriginal) => {
const fs = await importOriginal<typeof import("node:fs/promises")>();
return {
...fs,
open: async (...args: Parameters<typeof fs.open>) => {
const handle = await fs.open(...args);
const close = handle.close.bind(handle);
// Establish the owner at admission and count its first close as well as
// retries; stream teardown must not supply an implicit extra attempt.
cleanupState.firstHandle ??= handle;
if (cleanupState.abortController !== undefined)
setImmediate(() => {
cleanupState.abortController?.abort();
cleanupState.abortController = undefined;
});
handle.close = async () => {
if (handle === cleanupState.firstHandle)
cleanupState.firstHandleCloseAttempts += 1;
if (
cleanupState.failFirstClose &&
handle === cleanupState.firstHandle
) {
cleanupState.failFirstClose = false;
throw new Error("injected descriptor close failure");
}
return close();
};
return handle;
},
};
});
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { GoBinaryProvider } from "../../../src/go/GoBinaryProvider.js";
import { PeResourcesProvider } from "../../../src/native/pe/PeResourcesProvider.js";
import { createGoBinaryFixture } from "../../fixtures/go/image.js";
import { peResourceFixture } from "../../../src/domain/pe/peResources.fixture.js";
it.each(["go", "pe"] as const)(
"%s provider retains a failed descriptor close and retries that owner before the next read",
async (kind) => {
const root = await createTestTempDirectory("rea-read-cleanup-");
const path = join(root, kind === "go" ? "program.elf" : "program.exe");
const bytes =
kind === "go" ? createGoBinaryFixture().bytes : peResourceFixture().bytes;
await writeFile(path, bytes);
const provider =
kind === "go" ? new GoBinaryProvider() : new PeResourcesProvider();
cleanupState.failFirstClose = true;
const controller = new AbortController();
cleanupState.abortController = controller;
cleanupState.firstHandle = undefined;
cleanupState.firstHandleCloseAttempts = 0;
try {
const input = {
path,
max_file_bytes: 8 * 1024 * 1024,
max_entries: 4096,
};
const first = await provider.inspect(input, {
signal: controller.signal,
});
if (first.ok) throw new Error("Injected close failure was not returned");
expect(projectAnalysisError(first.error)).toMatchObject({
code: "cleanup_incomplete",
details: { diagnostics: { primary_error: { code: "cancelled" } } },
});
expect(first.error.cause).toBeInstanceOf(AggregateError);
if (!(first.error.cause instanceof AggregateError))
throw first.error.cause;
expect(first.error.cause.errors[1]).toMatchObject({
cleanupResources: [path],
});
expect(cleanupState.firstHandleCloseAttempts).toBe(1);
const retainedHandle = openedFirstHandle();
expect(retainedHandle.fd).toBeGreaterThanOrEqual(0);
const retry = await provider.inspect(input);
expect(retry.ok).toBe(true);
expect(cleanupState.firstHandleCloseAttempts).toBe(2);
expect(retainedHandle.fd).toBe(-1);
} finally {
await provider.close();
cleanupState.failFirstClose = false;
cleanupState.abortController = undefined;
cleanupState.firstHandle = undefined;
cleanupState.firstHandleCloseAttempts = 0;
}
},
);