Wait for directory operations, close owned streams and traversal handles, and retain the mounted reader when cleanup fails so close can retry before detach. Consolidate stable-artifact filesystem regressions into the boundary suite. Validation: 35 artifact tests and temporary-directory hygiene, including real hdiutil diagnostics. Directory-close and traversal races use injected faults.
78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import {
|
|
lstat,
|
|
open,
|
|
rename,
|
|
symlink,
|
|
unlink,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect, it } from "vitest";
|
|
import { readStableArtifact } from "../../../src/artifacts/readStableArtifact.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
it("reads exact binary and empty bytes and rejects symlink/size/cancellation boundaries", async () => {
|
|
const root = await createTestTempDirectory("rea-stable-artifact-");
|
|
const path = join(root, "source.bin");
|
|
await writeFile(path, Buffer.from([0, 255, 254]));
|
|
expect((await readStableArtifact(path, 3)).bytes).toEqual(
|
|
Buffer.from([0, 255, 254]),
|
|
);
|
|
await expect(readStableArtifact(path, 2)).rejects.toMatchObject({
|
|
reason: "limit",
|
|
});
|
|
await writeFile(path, "");
|
|
expect((await readStableArtifact(path, 0)).bytes.length).toBe(0);
|
|
const real = join(root, "original.bin");
|
|
await rename(path, real);
|
|
await symlink(real, path);
|
|
await expect(readStableArtifact(path, 3)).rejects.toMatchObject({
|
|
reason: "path",
|
|
});
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
await expect(readStableArtifact(real, 3, controller.signal)).rejects.toBe(
|
|
controller.signal.reason,
|
|
);
|
|
});
|
|
|
|
const checkChange = async (
|
|
stage: "before-open" | "after-read" | "symlink-replacement",
|
|
): Promise<void> => {
|
|
const root = await createTestTempDirectory("rea-stable-read-");
|
|
const path = join(root, "selected");
|
|
await writeFile(path, "original evidence");
|
|
let inspected = 0;
|
|
const reading = readStableArtifact(path, 1024, undefined, {
|
|
async lstat(selected) {
|
|
inspected++;
|
|
if (stage === "after-read" && inspected === 2) await unlink(selected);
|
|
const metadata = await lstat(selected, { bigint: true });
|
|
if (inspected === 1 && stage !== "after-read") {
|
|
await unlink(selected);
|
|
if (stage === "symlink-replacement") await symlink("missing", selected);
|
|
}
|
|
return metadata;
|
|
},
|
|
open,
|
|
});
|
|
await expect(reading).rejects.toMatchObject({ reason: "integrity" });
|
|
};
|
|
|
|
it.each(["before-open", "after-read"] as const)(
|
|
"retains an actual post-validation %s change as integrity failure",
|
|
checkChange,
|
|
);
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"retains a post-validation symlink replacement as an integrity failure",
|
|
() => checkChange("symlink-replacement"),
|
|
);
|
|
|
|
it("keeps initially missing inputs distinguishable from later changes", async () => {
|
|
const root = await createTestTempDirectory("rea-stable-missing-");
|
|
await expect(
|
|
readStableArtifact(join(root, "missing"), 1024),
|
|
).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|