mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 23:15:58 +00:00
test(artifacts): consolidate failure projection and marking coverage (#13473)
This commit is contained in:
1 parent
a3e7ed6d3b
commit
2b4847b951
3 files changed
+122
-147
No files matched your search
@@ -1,10 +1,13 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { expect, it, onTestFinished, vi } from "vitest";
|
||||
import { ZstandardFrameAllowanceError } from "../domain/compression/zstandardFrameAllowance.js";
|
||||
import {
|
||||
MAXIMUM_ZSTD_MAX_FRAMES,
|
||||
MINIMUM_ZSTD_MAX_FRAMES,
|
||||
} from "../domain/analysisResourceLimits.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { open, writeFile } from "node:fs/promises";
|
||||
import { sep } from "node:path";
|
||||
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
|
||||
|
||||
import {
|
||||
ArtifactReaderFailure,
|
||||
@@ -21,12 +24,11 @@ import {
|
||||
} from "./StableArtifactFailure.js";
|
||||
import { artifactIntegrityDetails } from "../domain/artifactIntegrity.js";
|
||||
import {
|
||||
ArtifactFileCleanupFailure,
|
||||
artifactFilePrimaryFacts,
|
||||
artifactParseFailure,
|
||||
withArtifactFileHandle,
|
||||
} from "./ArtifactFileOperation.js";
|
||||
import { AnalysisCleanupLease } from "./AnalysisCleanupLease.js";
|
||||
import { RegularFileCleanupFailure } from "../filesystem/OwnedRegularFile.js";
|
||||
import { ArtifactResourceScope } from "./ArtifactResourceScope.js";
|
||||
import { artifactReaderLimitFailure } from "./index.js";
|
||||
import {
|
||||
malformedInput,
|
||||
@@ -54,30 +56,62 @@ it("projects a Zstandard frame-cap as an adjustable artifact limit", () => {
|
||||
});
|
||||
});
|
||||
|
||||
const failWithDescriptorCleanup = async (
|
||||
bytes: Buffer,
|
||||
failure: unknown,
|
||||
retryFailure?: Error,
|
||||
) => {
|
||||
const root = await createTestTempDirectory("rea-artifact-projection-");
|
||||
const path = `${root}${sep}.${sep}artifact`;
|
||||
await writeFile(path, bytes);
|
||||
const descriptor = await open(path);
|
||||
const nativeClose = descriptor.close.bind(descriptor);
|
||||
onTestFinished(async () => {
|
||||
if (descriptor.fd >= 0) await nativeClose();
|
||||
});
|
||||
const close = vi
|
||||
.spyOn(descriptor, "close")
|
||||
.mockRejectedValueOnce(new Error("close failed"));
|
||||
if (retryFailure !== undefined) close.mockRejectedValueOnce(retryFailure);
|
||||
const scope = new ArtifactResourceScope();
|
||||
const wrapped = await withArtifactFileHandle(
|
||||
path,
|
||||
descriptor,
|
||||
scope,
|
||||
undefined,
|
||||
async () => {
|
||||
throw failure;
|
||||
},
|
||||
).then(
|
||||
() => {
|
||||
throw new Error("Expected artifact operation and close failures");
|
||||
},
|
||||
(cause: unknown) => cause,
|
||||
);
|
||||
return { path, scope, descriptor, wrapped };
|
||||
};
|
||||
|
||||
it.each([
|
||||
malformedInput("pe", "bad field"),
|
||||
truncatedInput("pe", "short field"),
|
||||
unsupportedInput("pe", "unsupported image"),
|
||||
{ cause: malformedInput("pe", "bad field"), genericCode: "invalid_request" },
|
||||
{ cause: truncatedInput("pe", "short field"), genericCode: "truncated" },
|
||||
{
|
||||
cause: unsupportedInput("pe", "unsupported image"),
|
||||
genericCode: "unsupported_target",
|
||||
},
|
||||
])(
|
||||
"preserves selected input projection and partial identity through cleanup retry: $kind",
|
||||
async (cause) => {
|
||||
const path = "/selected/./program.exe";
|
||||
"preserves $cause.kind projection and selected identity through descriptor cleanup",
|
||||
async ({ cause, genericCode }) => {
|
||||
const operation = "inspect_pe_resources";
|
||||
const bytes = Buffer.from("inert snapshot");
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
const failure = artifactParseFailure(cause, undefined);
|
||||
expect(failure).toMatchObject({ cause });
|
||||
let attempts = 0;
|
||||
const lease = new AnalysisCleanupLease(async () => {
|
||||
if (attempts++ === 0) throw new Error("cleanup retry failed");
|
||||
}, [path]);
|
||||
const wrapped = new ArtifactFileCleanupFailure(
|
||||
new RegularFileCleanupFailure({
|
||||
path,
|
||||
owner: { close: async () => undefined } as never,
|
||||
outcome: { kind: "failed", cause: failure },
|
||||
cleanupCause: new Error("close failed"),
|
||||
}),
|
||||
lease,
|
||||
);
|
||||
const { path, scope, descriptor, wrapped } =
|
||||
await failWithDescriptorCleanup(
|
||||
bytes,
|
||||
failure,
|
||||
new Error("cleanup retry failed"),
|
||||
);
|
||||
const primary = projectOwnedArtifactInputFailure(
|
||||
artifactFilePrimaryFacts(wrapped),
|
||||
operation,
|
||||
@@ -86,9 +120,7 @@ it.each([
|
||||
if (primary === undefined)
|
||||
throw new Error("Expected a selected-input projection");
|
||||
expect(primary.cause).toBe(failure);
|
||||
const bytes = Buffer.from("inert snapshot");
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
const projected = projectStableArtifactFailure(
|
||||
const selected = projectStableArtifactFailure(
|
||||
wrapped,
|
||||
primary,
|
||||
path,
|
||||
@@ -96,19 +128,42 @@ it.each([
|
||||
operation,
|
||||
{ bytes, sha256 },
|
||||
);
|
||||
expect(projectAnalysisError(projected)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
const generic = translateArtifactFailure("inspect_artifact", wrapped);
|
||||
const observation = {
|
||||
kind: "file-snapshot-hash",
|
||||
path,
|
||||
sha256,
|
||||
bytes: bytes.length,
|
||||
};
|
||||
const expectPending = () => {
|
||||
expect(projectAnalysisError(selected)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: { resources: [path], partial_observation: observation },
|
||||
});
|
||||
expect(projectAnalysisError(generic)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
reason: cause.kind,
|
||||
detail: cause.message,
|
||||
cleanup: "incomplete",
|
||||
resources: [path],
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
expectPending();
|
||||
await expect(scope.retryCleanup()).rejects.toMatchObject({
|
||||
cleanup: { reason: "cleanup retry failed", resources: [path] },
|
||||
});
|
||||
await expect(lease.release()).rejects.toThrow("cleanup retry failed");
|
||||
expect(projectAnalysisError(projected)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
});
|
||||
await lease.release();
|
||||
expect(projectAnalysisError(projected)).toMatchObject({
|
||||
expect(descriptor.fd).toBeGreaterThanOrEqual(0);
|
||||
expectPending();
|
||||
await scope.close();
|
||||
expect(descriptor.fd).toBe(-1);
|
||||
expect(projectAnalysisError(selected)).toMatchObject({
|
||||
code:
|
||||
cause.kind === "unsupported" ? "unsupported_target" : "invalid_request",
|
||||
details:
|
||||
cause.kind === "unsupported"
|
||||
details: {
|
||||
...(cause.kind === "unsupported"
|
||||
? { operation, path, reason: cause.message }
|
||||
: {
|
||||
operation,
|
||||
@@ -119,13 +174,15 @@ it.each([
|
||||
message: cause.message,
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
partial_observation: observation,
|
||||
},
|
||||
});
|
||||
expect(projected.partialObservation).toEqual({
|
||||
kind: "file-snapshot-hash",
|
||||
path,
|
||||
sha256,
|
||||
bytes: bytes.length,
|
||||
expect(projectAnalysisError(generic)).toMatchObject({
|
||||
code: genericCode,
|
||||
category:
|
||||
genericCode === "invalid_request" ? "invalid_input" : genericCode,
|
||||
details: { reason: cause.kind, detail: cause.message },
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -154,61 +211,6 @@ it("leaves opaque operation errors and decode limits with their owning projectio
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
cause: malformedInput("fixture", "bad field"),
|
||||
code: "invalid_request",
|
||||
category: "invalid_input",
|
||||
},
|
||||
{
|
||||
cause: truncatedInput("fixture", "short field"),
|
||||
code: "truncated",
|
||||
category: "truncated",
|
||||
},
|
||||
{
|
||||
cause: unsupportedInput("fixture", "new revision"),
|
||||
code: "unsupported_target",
|
||||
category: "unsupported_target",
|
||||
},
|
||||
])(
|
||||
"preserves $cause.kind parser facts after cleanup retry",
|
||||
async ({ cause, code, category }) => {
|
||||
const readerFailure = artifactParseFailure(cause, undefined);
|
||||
if (!(readerFailure instanceof ArtifactReaderFailure))
|
||||
throw new Error(
|
||||
"Parser failure did not become an artifact reader failure",
|
||||
);
|
||||
expect(readerFailure).toMatchObject({ reason: cause.kind, cause });
|
||||
const lease = new AnalysisCleanupLease(async () => {}, [
|
||||
"fixture descriptor",
|
||||
]);
|
||||
const wrapped = ArtifactReaderFailure.withCleanup(
|
||||
readerFailure,
|
||||
{ reason: "close failed", resources: ["fixture descriptor"] },
|
||||
undefined,
|
||||
[lease],
|
||||
);
|
||||
const projectedOwner = translateArtifactFailure(
|
||||
"inspect_artifact",
|
||||
wrapped,
|
||||
);
|
||||
expect(projectAnalysisError(projectedOwner)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
reason: cause.kind,
|
||||
detail: cause.message,
|
||||
cleanup: "incomplete",
|
||||
},
|
||||
});
|
||||
await lease.release();
|
||||
expect(projectAnalysisError(projectedOwner)).toMatchObject({
|
||||
code,
|
||||
category,
|
||||
details: { reason: cause.kind, detail: cause.message },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// The provider ceiling test uses a sparse file and is skipped on Windows.
|
||||
it.skipIf(process.platform !== "win32")(
|
||||
"names the fixed image ceiling when the selected budget is already maximal",
|
||||
@@ -373,7 +375,6 @@ it("keeps a fixed reader ceiling's refusal through cleanup wrapping and public p
|
||||
reason: "close failed",
|
||||
resources: ["handle"],
|
||||
});
|
||||
expect(wrapped.fixedAllowance).toBe(failure.fixedAllowance);
|
||||
const projected = projectAnalysisError(
|
||||
translateArtifactFailure("inspect_artifact", wrapped),
|
||||
);
|
||||
@@ -459,30 +460,24 @@ it("names the failed integrity check through cleanup wrapping and public project
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the named integrity check when a descriptor close failure rebuilds the reader failure", () => {
|
||||
const failure = new ArtifactFileCleanupFailure(
|
||||
new RegularFileCleanupFailure({
|
||||
path: "/artifact.asar",
|
||||
owner: { close: async () => undefined } as never,
|
||||
outcome: {
|
||||
kind: "failed",
|
||||
cause: new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
"ASAR read coverage was short: main.js",
|
||||
{ integrityCheck: "read-coverage" },
|
||||
),
|
||||
it("keeps the named integrity check through descriptor cleanup", async () => {
|
||||
const { path, scope, wrapped } = await failWithDescriptorCleanup(
|
||||
Buffer.from("fixture"),
|
||||
new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
"ASAR read coverage was short: main.js",
|
||||
{
|
||||
integrityCheck: "read-coverage",
|
||||
},
|
||||
cleanupCause: new Error("close failed"),
|
||||
}),
|
||||
new AnalysisCleanupLease(async () => undefined),
|
||||
),
|
||||
);
|
||||
expect(failure.integrityCheck).toBe("read-coverage");
|
||||
expect(
|
||||
projectAnalysisError(translateArtifactFailure("extract_artifact", failure)),
|
||||
projectAnalysisError(translateArtifactFailure("extract_artifact", wrapped)),
|
||||
).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: { integrity_check: "read-coverage", cleanup: "incomplete" },
|
||||
details: { integrity_check: "read-coverage", resources: [path] },
|
||||
});
|
||||
await scope.close();
|
||||
});
|
||||
|
||||
it("keeps a reader's named check when a provider supplies its own cause and options", () => {
|
||||
|
||||
@@ -11,22 +11,10 @@ const directory = (mode: number) => ({
|
||||
isFile: () => false,
|
||||
isSymbolicLink: () => false,
|
||||
});
|
||||
const symlink = (mode: number) => ({
|
||||
mode,
|
||||
isFile: () => false,
|
||||
isSymbolicLink: () => true,
|
||||
});
|
||||
|
||||
describe("hostExecutableMarking", () => {
|
||||
it("marks Windows launchable files by extension because NTFS has no execute bit", () => {
|
||||
expect(
|
||||
hostExecutableMarking("C:\\scratch\\whoami.exe", file(0o666), "win32"),
|
||||
).toBe(true);
|
||||
expect(
|
||||
hostExecutableMarking("C:\\scratch\\SETUP.CMD", file(0o444), "win32"),
|
||||
).toBe(true);
|
||||
for (const path of [
|
||||
"tools/SETUP.EXE",
|
||||
"C:\\scratch\\SETUP.EXE",
|
||||
"run.Cmd",
|
||||
"build.bat",
|
||||
"legacy.com",
|
||||
@@ -35,9 +23,6 @@ describe("hostExecutableMarking", () => {
|
||||
expect(hostExecutableMarking("readonly.exe", file(0o444), "win32")).toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
hostExecutableMarking("C:\\scratch\\library.dll", file(0o666), "win32"),
|
||||
).toBe(false);
|
||||
expect(
|
||||
hostExecutableMarking(
|
||||
"C:\\scratch\\tools.exe",
|
||||
@@ -48,7 +33,7 @@ describe("hostExecutableMarking", () => {
|
||||
});
|
||||
|
||||
it("does not treat other Windows filenames as executable even with execute bits", () => {
|
||||
for (const path of ["notes.txt", "README", "archive.exe.bak"])
|
||||
for (const path of ["library.dll", "README", "archive.exe.bak"])
|
||||
expect(hostExecutableMarking(path, file(0o777), "win32")).toBe(false);
|
||||
});
|
||||
|
||||
@@ -60,15 +45,4 @@ describe("hostExecutableMarking", () => {
|
||||
hostExecutableMarking("/scratch/whoami.exe", file(0o644), "darwin"),
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("never marks a symlink, whose target decides what runs", () => {
|
||||
for (const [path, platform] of [
|
||||
["/scratch/current", "linux"],
|
||||
["/scratch/current", "darwin"],
|
||||
["C:\\scratch\\tool.exe", "win32"],
|
||||
] as const)
|
||||
expect(hostExecutableMarking(path, symlink(0o120777), platform)).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -37,7 +37,9 @@ describe("artifact directory inventory", () => {
|
||||
);
|
||||
await writeFile(join(app, "Contents", "Resources", "main.js"), "main();\n");
|
||||
await writeFile(join(app, "Contents", "Resources", "main.js.MAP"), "{}\n");
|
||||
await symlink("/etc/passwd", join(app, "Contents", "Resources", "outside"));
|
||||
const outside = join(root, "outside.exe");
|
||||
await writeFile(outside, "unselected fixture", { mode: 0o755 });
|
||||
await symlink(outside, join(app, "Contents", "Resources", "outside.exe"));
|
||||
const client = createOwnedArtifactClient(await target(app));
|
||||
const input = {};
|
||||
const first = await client.execute("inventory_artifact", input);
|
||||
@@ -58,9 +60,13 @@ describe("artifact directory inventory", () => {
|
||||
);
|
||||
expect(
|
||||
left.occurrences.find(({ logical_path: path }) =>
|
||||
path.endsWith("outside"),
|
||||
path.endsWith("outside.exe"),
|
||||
),
|
||||
).toMatchObject({ artifact_id: null, entry_kind: "symlink" });
|
||||
).toMatchObject({
|
||||
artifact_id: null,
|
||||
entry_kind: "symlink",
|
||||
executable: false,
|
||||
});
|
||||
const script = left.occurrences.find(({ logical_path: path }) =>
|
||||
path.endsWith("main.js"),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user