mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
test(zip): consolidate archive coverage and name checks (#12857)
* test(zip): consolidate member extent and empty archive checks * test(zip): reuse Unicode path fixture framing
This commit is contained in:
1 parent
688d129aef
commit
ae449a3021
3 files changed
+126
-163
No files matched your search
@@ -25,7 +25,7 @@ const UTF8_NAME = 0x800;
|
||||
|
||||
/** A version-1 Info-ZIP Unicode Path claim bound to the stored filename bytes. */
|
||||
export const unicodePathExtra = (
|
||||
name: string,
|
||||
name: string | Uint8Array,
|
||||
path: string | Uint8Array,
|
||||
): Buffer => {
|
||||
const text = Buffer.from(path);
|
||||
@@ -33,7 +33,10 @@ export const unicodePathExtra = (
|
||||
field.writeUInt16LE(0x7075);
|
||||
field.writeUInt16LE(field.length - 4, 2);
|
||||
field[4] = 1;
|
||||
field.writeUInt32LE(crc32(Buffer.from(name)), 5);
|
||||
field.writeUInt32LE(
|
||||
crc32(typeof name === "string" ? Buffer.from(name) : name),
|
||||
5,
|
||||
);
|
||||
text.copy(field, 9);
|
||||
return field;
|
||||
};
|
||||
|
||||
@@ -9,128 +9,117 @@ import {
|
||||
artifactExtractionResultSchema,
|
||||
artifactInventoryResultSchema,
|
||||
} from "../../src/domain/artifactGraph.js";
|
||||
import { parseEvidence, type Evidence } from "../../src/domain/evidence.js";
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { zipArchive } from "../../src/domain/desktop/zipArchive.fixture.js";
|
||||
import { nugetNupkgSchema } from "../../src/domain/nuget/nupkg.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
|
||||
cliTest.for(["directory", "encrypted"] as const)(
|
||||
"keeps contradictory %s extents unknown through CLI and MCP",
|
||||
async (kind, { cli }) => {
|
||||
cliTest.for([
|
||||
{ claim: "directory size", name: "dir/", flags: 0 },
|
||||
{ claim: "directory descriptor", name: "dir/", flags: 0x8 },
|
||||
{ claim: "encrypted member size", name: "encrypted.bin", flags: 1 },
|
||||
])(
|
||||
"keeps an unverified ZIP $claim unknown through CLI and MCP",
|
||||
async ({ claim, name, flags }, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-zip-extent-coverage-");
|
||||
const path = join(root, "fixture.zip");
|
||||
const hidden = {
|
||||
name: "hidden.txt",
|
||||
data: "secret",
|
||||
method: 0,
|
||||
listed: false,
|
||||
};
|
||||
const hiddenBytes =
|
||||
30 + Buffer.byteLength(hidden.name) + hidden.data.length;
|
||||
const first = {
|
||||
name: kind === "directory" ? "dir/" : "encrypted.bin",
|
||||
data: "",
|
||||
method: 0,
|
||||
flags: kind === "encrypted" ? 1 : 0,
|
||||
};
|
||||
const hidden = { name: "hidden.txt", data: "secret", method: 0 };
|
||||
const bytes = zipArchive([
|
||||
{
|
||||
name,
|
||||
flags,
|
||||
method: 0,
|
||||
centralCompressedSize:
|
||||
30 + Buffer.byteLength(hidden.name) + hidden.data.length,
|
||||
},
|
||||
{ ...hidden, listed: false },
|
||||
]);
|
||||
await writeFile(path, bytes);
|
||||
const inspected = await cli.run({
|
||||
arguments: ["inspect-artifact", path, "--json"],
|
||||
});
|
||||
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
|
||||
const inspection = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(inspected.json).normalized_result,
|
||||
);
|
||||
const evidence = inspection.substeps[0]?.evidence;
|
||||
const inventory = artifactInventoryResultSchema.parse(
|
||||
evidence?.normalized_result,
|
||||
);
|
||||
expect(inventory.coverage.status).toBe("unknown");
|
||||
expect(
|
||||
inventory.occurrences.map((item) => item.logical_path),
|
||||
).not.toContain(hidden.name);
|
||||
expect(inventory.manifest.root_sha256).toBe(
|
||||
createHash("sha256").update(bytes).digest("hex"),
|
||||
);
|
||||
|
||||
const call = await cli.sharedMcpCall?.();
|
||||
if (call === undefined)
|
||||
throw new Error("Expected the compiled MCP fixture");
|
||||
let seed: Evidence | undefined;
|
||||
if (kind === "directory") {
|
||||
const seedPath = join(root, "seed.zip");
|
||||
await writeFile(
|
||||
seedPath,
|
||||
zipArchive([first, { ...hidden, listed: true }]),
|
||||
const opened = await call("open_binary", { path });
|
||||
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
|
||||
try {
|
||||
const response = await call("inspect_artifact", {});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
const mcpInspection = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(response.structuredContent).normalized_result,
|
||||
);
|
||||
const result = await cli.run({
|
||||
expect(
|
||||
artifactInventoryResultSchema.parse(
|
||||
mcpInspection.substeps[0]?.evidence.normalized_result,
|
||||
),
|
||||
).toEqual(inventory);
|
||||
} finally {
|
||||
const closed = await call("close_binary", {});
|
||||
expect(closed.isError, JSON.stringify(closed)).not.toBe(true);
|
||||
}
|
||||
|
||||
if (claim === "directory size") {
|
||||
const seedPath = join(root, "seed.zip");
|
||||
await writeFile(seedPath, zipArchive([{ name, method: 0 }, hidden]));
|
||||
const seedResult = await cli.run({
|
||||
arguments: ["inspect-artifact", seedPath, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stdout + result.stderr).toBe(0);
|
||||
seed = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(result.json).normalized_result,
|
||||
expect(seedResult.exitCode, seedResult.stdout + seedResult.stderr).toBe(
|
||||
0,
|
||||
);
|
||||
const seed = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(seedResult.json).normalized_result,
|
||||
).substeps[0]?.evidence;
|
||||
const seedInventory = artifactInventoryResultSchema.parse(
|
||||
seed?.normalized_result,
|
||||
);
|
||||
expect(seedInventory.coverage.status).toBe("complete");
|
||||
expect(
|
||||
artifactInventoryResultSchema.parse(seed?.normalized_result).coverage
|
||||
.status,
|
||||
).toBe("complete");
|
||||
}
|
||||
for (const deferred of [false, true]) {
|
||||
for (const contradictory of [false, true]) {
|
||||
const bytes = zipArchive([
|
||||
{
|
||||
...first,
|
||||
flags: first.flags | (deferred ? 0x8 : 0),
|
||||
centralCompressedSize: contradictory ? hiddenBytes : 0,
|
||||
},
|
||||
hidden,
|
||||
]);
|
||||
await writeFile(path, bytes);
|
||||
const inspected = await cli.run({
|
||||
arguments: ["inspect-artifact", path, "--json"],
|
||||
});
|
||||
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
|
||||
const inspection = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(inspected.json).normalized_result,
|
||||
);
|
||||
const inventory = artifactInventoryResultSchema.parse(
|
||||
inspection.substeps[0]?.evidence.normalized_result,
|
||||
);
|
||||
if (seed !== undefined) {
|
||||
const compared = await call("compare_artifacts", {
|
||||
left: seed,
|
||||
right: inspection.substeps[0]?.evidence,
|
||||
});
|
||||
expect(compared.isError, JSON.stringify(compared)).not.toBe(true);
|
||||
const comparison = artifactComparisonResultSchema.parse(
|
||||
parseEvidence(compared.structuredContent).normalized_result,
|
||||
);
|
||||
expect(
|
||||
comparison.changes.find(
|
||||
(change) => change.logical_path === hidden.name,
|
||||
)?.classification,
|
||||
).toBe("unknown");
|
||||
}
|
||||
expect(inventory.coverage.status).toBe("unknown");
|
||||
expect(
|
||||
inventory.occurrences.map((item) => item.logical_path),
|
||||
).not.toContain(hidden.name);
|
||||
expect(inventory.manifest.root_sha256).toBe(
|
||||
createHash("sha256").update(bytes).digest("hex"),
|
||||
);
|
||||
const opened = await call("open_binary", { path });
|
||||
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
|
||||
try {
|
||||
const response = await call("inspect_artifact", {});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
const mcpInspection = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(response.structuredContent).normalized_result,
|
||||
);
|
||||
const mcpInventory = artifactInventoryResultSchema.parse(
|
||||
mcpInspection.substeps[0]?.evidence.normalized_result,
|
||||
);
|
||||
expect(mcpInventory).toEqual(inventory);
|
||||
} finally {
|
||||
const closed = await call("close_binary", {});
|
||||
expect(closed.isError, JSON.stringify(closed)).not.toBe(true);
|
||||
}
|
||||
if (kind === "directory") {
|
||||
const extracted = await cli.run({
|
||||
arguments: ["extract-artifact", path, "--json"],
|
||||
environment: { TMPDIR: root, TMP: root, TEMP: root },
|
||||
});
|
||||
expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(
|
||||
0,
|
||||
);
|
||||
const result = artifactExtractionResultSchema.parse(
|
||||
parseEvidence(extracted.json).normalized_result,
|
||||
);
|
||||
expect(result.coverage.status).toBe("unknown");
|
||||
}
|
||||
expect((await readFile(path)).equals(bytes)).toBe(true);
|
||||
}
|
||||
seedInventory.occurrences.map((item) => item.logical_path),
|
||||
).toContain(hidden.name);
|
||||
|
||||
const compared = await call("compare_artifacts", {
|
||||
left: seed,
|
||||
right: evidence,
|
||||
});
|
||||
expect(compared.isError, JSON.stringify(compared)).not.toBe(true);
|
||||
const comparison = artifactComparisonResultSchema.parse(
|
||||
parseEvidence(compared.structuredContent).normalized_result,
|
||||
);
|
||||
expect(
|
||||
comparison.changes.find((change) => change.logical_path === hidden.name)
|
||||
?.classification,
|
||||
).toBe("unknown");
|
||||
|
||||
const extracted = await cli.run({
|
||||
arguments: ["extract-artifact", path, "--json"],
|
||||
environment: { TMPDIR: root, TMP: root, TEMP: root },
|
||||
});
|
||||
expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
|
||||
const result = artifactExtractionResultSchema.parse(
|
||||
parseEvidence(extracted.json).normalized_result,
|
||||
);
|
||||
expect(result.coverage.status).toBe("unknown");
|
||||
}
|
||||
expect((await readFile(path)).equals(bytes)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@ import {
|
||||
} from "../../fixtures/zipNames.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
import {
|
||||
unicodePathExtra,
|
||||
zipArchive,
|
||||
} from "../../../src/domain/desktop/zipArchive.fixture.js";
|
||||
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
|
||||
import { withArtifactClient } from "../../support/artifactClientScope.js";
|
||||
import {
|
||||
@@ -130,15 +134,6 @@ for (const zip64 of [false, true]) {
|
||||
const bytes = Buffer.from(await writer.close());
|
||||
await writeFile(archive, bytes);
|
||||
const digest = createHash("sha256").update(bytes).digest("hex");
|
||||
const inventory = await inventoryArtifact(archive);
|
||||
expect(inventory.manifest).toMatchObject({
|
||||
root_sha256: digest,
|
||||
root_format: "zip",
|
||||
});
|
||||
expect(inventory.coverage).toEqual({
|
||||
scope: "container-entries",
|
||||
status: "complete",
|
||||
});
|
||||
const inspected = await cli.run({
|
||||
arguments: ["inspect-artifact", archive, "--json"],
|
||||
});
|
||||
@@ -153,6 +148,10 @@ for (const zip64 of [false, true]) {
|
||||
root_sha256: digest,
|
||||
root_format: "zip",
|
||||
});
|
||||
expect(retained.coverage).toEqual({
|
||||
scope: "container-entries",
|
||||
status: "complete",
|
||||
});
|
||||
const extracted = await cli.run({
|
||||
arguments: ["extract-artifact", archive, "--json"],
|
||||
});
|
||||
@@ -308,19 +307,9 @@ it("preserves valid Info-ZIP Unicode Path overrides", async () => {
|
||||
const root = await createTestTempDirectory("rea-zip-unicode-path-");
|
||||
const path = join(root, "unicode-path.zip");
|
||||
const rawName = Buffer.from([0xc7, 0xd1, 0xb1, 0xdb, 0x2e, 0x74, 0x78, 0x74]);
|
||||
const unicodeName = Buffer.from("café.txt", "utf8");
|
||||
const field = Buffer.alloc(5 + unicodeName.length);
|
||||
field[0] = 1;
|
||||
field.writeUInt32LE(zipCrc32(rawName), 1);
|
||||
unicodeName.copy(field, 5);
|
||||
const writer = new ZipWriter(new Uint8ArrayWriter());
|
||||
await writer.add("name.txt", new TextReader("unicode path"), {
|
||||
extraField: new Map([[0x7075, field]]),
|
||||
});
|
||||
const archive = Buffer.from(await writer.close());
|
||||
await writeFile(
|
||||
path,
|
||||
rewriteSingleEntryZipName(archive, "name.txt", rawName, false),
|
||||
zipWithUnicodePath(rawName, unicodePathExtra(rawName, "café.txt")),
|
||||
);
|
||||
|
||||
const inventory = await inventoryArtifact(path);
|
||||
@@ -339,20 +328,9 @@ it("falls back to the stored name and reports bytes when a Unicode Path field CR
|
||||
);
|
||||
const path = join(root, "invalid-unicode-extra-crc.zip");
|
||||
const rawName = Buffer.from([0x82, 0x2e, 0x74, 0x78, 0x74]);
|
||||
const unicodeName = Buffer.from("café.txt", "utf8");
|
||||
const field = Buffer.alloc(5 + unicodeName.length);
|
||||
field[0] = 1;
|
||||
field.writeUInt32LE((zipCrc32(rawName) ^ 1) >>> 0, 1);
|
||||
unicodeName.copy(field, 5);
|
||||
const writer = new ZipWriter(new Uint8ArrayWriter());
|
||||
await writer.add("x.txt", new TextReader("malformed extra"), {
|
||||
extraField: new Map([[0x7075, field]]),
|
||||
});
|
||||
const archive = Buffer.from(await writer.close());
|
||||
await writeFile(
|
||||
path,
|
||||
rewriteSingleEntryZipName(archive, "x.txt", rawName, false),
|
||||
);
|
||||
const field = unicodePathExtra(rawName, "café.txt");
|
||||
field.writeUInt8(field.readUInt8(5) ^ 1, 5);
|
||||
await writeFile(path, zipWithUnicodePath(rawName, field));
|
||||
|
||||
const inventory = await inventoryArtifact(path);
|
||||
expect(inventory.occurrences).toContainEqual(
|
||||
@@ -363,12 +341,13 @@ it("falls back to the stored name and reports bytes when a Unicode Path field CR
|
||||
);
|
||||
});
|
||||
|
||||
const malformedName = Buffer.from([0xc3, 0x28, 0x2e, 0x74, 0x78]);
|
||||
|
||||
it("rejects malformed UTF-8 in flagged ZIP member names", async () => {
|
||||
const root = await createTestTempDirectory("rea-zip-invalid-utf8-name-");
|
||||
const path = join(root, "invalid-utf8.zip");
|
||||
await writeSingleEntryZip(path, "x.txt");
|
||||
const archive = await readFile(path);
|
||||
const malformedName = Buffer.from([0xc3, 0x28, 0x2e, 0x74, 0x78]);
|
||||
await writeFile(
|
||||
path,
|
||||
rewriteSingleEntryZipName(archive, "x.txt", malformedName, true),
|
||||
@@ -385,19 +364,9 @@ it("rejects malformed UTF-8 in a CRC-valid Unicode Path extra field", async () =
|
||||
const root = await createTestTempDirectory("rea-zip-invalid-unicode-extra-");
|
||||
const path = join(root, "invalid-unicode-extra.zip");
|
||||
const rawName = Buffer.from("legacy.txt");
|
||||
const malformedName = Buffer.from([0xc3, 0x28, 0x2e, 0x74, 0x78]);
|
||||
const field = Buffer.alloc(5 + malformedName.length);
|
||||
field[0] = 1;
|
||||
field.writeUInt32LE(zipCrc32(rawName), 1);
|
||||
malformedName.copy(field, 5);
|
||||
const writer = new ZipWriter(new Uint8ArrayWriter());
|
||||
await writer.add("legacy.txt", new TextReader("malformed extra"), {
|
||||
extraField: new Map([[0x7075, field]]),
|
||||
});
|
||||
const archive = Buffer.from(await writer.close());
|
||||
await writeFile(
|
||||
path,
|
||||
rewriteSingleEntryZipName(archive, "legacy.txt", rawName, false),
|
||||
zipWithUnicodePath(rawName, unicodePathExtra(rawName, malformedName)),
|
||||
);
|
||||
|
||||
await expect(inventoryArtifact(path)).rejects.toMatchObject({
|
||||
@@ -806,13 +775,15 @@ const writeSingleEntryZip = async (
|
||||
await writeFile(path, await createSingleEntryZip(name));
|
||||
};
|
||||
|
||||
const zipCrc32 = (bytes: Uint8Array): number => {
|
||||
let checksum = 0xffffffff;
|
||||
for (const byte of bytes) {
|
||||
checksum ^= byte;
|
||||
for (let bit = 0; bit < 8; bit += 1)
|
||||
checksum =
|
||||
(checksum & 1) === 1 ? (checksum >>> 1) ^ 0xedb88320 : checksum >>> 1;
|
||||
}
|
||||
return (checksum ^ 0xffffffff) >>> 0;
|
||||
const zipWithUnicodePath = (
|
||||
rawName: Buffer,
|
||||
centralExtra: Uint8Array,
|
||||
): Buffer => {
|
||||
const placeholder = "x".repeat(rawName.length);
|
||||
return rewriteSingleEntryZipName(
|
||||
zipArchive([{ name: placeholder, data: "member bytes", centralExtra }]),
|
||||
placeholder,
|
||||
rawName,
|
||||
false,
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user