forked from mtusi2026/mtusi2026lab7
113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
import { test, expect } from "bun:test";
|
|
import { createHash } from "node:crypto";
|
|
import {
|
|
assertSameSnapshot,
|
|
createEvidencePreserver,
|
|
pinHandover,
|
|
assertAcceptedGeneration,
|
|
} from "../scripts/checkpoint-guards";
|
|
const hash = (value: Uint8Array) =>
|
|
createHash("sha256").update(value).digest("hex");
|
|
test("checkpoint second snapshot rejects changed bytes, additions and removals", () => {
|
|
const first = { a: "a".repeat(64) },
|
|
mutations: Record<string, string>[] = [
|
|
{ a: "b".repeat(64) },
|
|
{ ...first, b: "b".repeat(64) },
|
|
{},
|
|
];
|
|
for (const second of mutations)
|
|
expect(() => assertSameSnapshot(first, second)).toThrow(
|
|
"SOURCE_SNAPSHOT_CHANGED",
|
|
);
|
|
expect(() => assertSameSnapshot(first, { ...first })).not.toThrow();
|
|
});
|
|
test("historical evidence never replaces existing different bytes and permits exact identical bytes", async () => {
|
|
let existing = Buffer.from("old"),
|
|
writes = 0;
|
|
const preserve = createEvidencePreserver({
|
|
writeExclusive: async () => {
|
|
writes++;
|
|
throw Object.assign(new Error("exists"), { code: "EEXIST" });
|
|
},
|
|
read: async () => existing,
|
|
});
|
|
await expect(preserve("fixture", Buffer.from("new"))).rejects.toThrow(
|
|
"HISTORICAL_EVIDENCE_CONFLICT",
|
|
);
|
|
expect(existing.toString()).toBe("old");
|
|
await preserve("fixture", Buffer.from("old"));
|
|
expect(writes).toBe(2);
|
|
});
|
|
test("handover lease rejects a different valid source generation after initial verification", async () => {
|
|
let source = Buffer.from("A");
|
|
const receipt = () =>
|
|
Buffer.from(
|
|
JSON.stringify({
|
|
checkedAt: "2026-11-15T17:47:34Z",
|
|
runtimeTelemetry: false,
|
|
runtimeSecurity: false,
|
|
files: { "main.go": hash(source) },
|
|
preservedFreshness: {},
|
|
}),
|
|
);
|
|
let current = receipt();
|
|
const lease = await pinHandover({
|
|
readReceipt: async () => current,
|
|
readSource: async () => source,
|
|
});
|
|
expect(lease.count).toBe(1);
|
|
source = Buffer.from("B");
|
|
current = receipt();
|
|
await expect(lease.verify()).rejects.toThrow("HANDOVER_GENERATION_CHANGED");
|
|
});
|
|
test("handover lease rejects source mutation even when receipt is unchanged", async () => {
|
|
let source = Buffer.from("A");
|
|
const current = Buffer.from(
|
|
JSON.stringify({
|
|
checkedAt: "2026-11-15T17:47:34Z",
|
|
runtimeTelemetry: false,
|
|
runtimeSecurity: false,
|
|
files: { "main.go": hash(source) },
|
|
preservedFreshness: {},
|
|
}),
|
|
);
|
|
const lease = await pinHandover({
|
|
readReceipt: async () => current,
|
|
readSource: async () => source,
|
|
});
|
|
source = Buffer.from("B");
|
|
await expect(lease.verify()).rejects.toThrow("HANDOVER_SOURCE_CHANGED");
|
|
});
|
|
test("historical finalization requires its accepted source and build generation binding", () => {
|
|
const source = Buffer.from(
|
|
JSON.stringify({
|
|
checkedAt: "A",
|
|
files: { a: "a" },
|
|
preservedFreshness: {},
|
|
}),
|
|
),
|
|
build = {
|
|
backend_checked_at: "A",
|
|
source_handover_sha256: hash(source),
|
|
source_files_verified: 1,
|
|
binaries: { app: "a", worker: "w" },
|
|
};
|
|
const accepted = {
|
|
backend_checked_at: "A",
|
|
source_handover_sha256: hash(source),
|
|
source_files_verified: 1,
|
|
binaries: { app: "a", worker: "w" },
|
|
};
|
|
expect(() => assertAcceptedGeneration(accepted, source, build)).not.toThrow();
|
|
expect(() =>
|
|
assertAcceptedGeneration(
|
|
{ ...accepted, backend_checked_at: "B" },
|
|
source,
|
|
build,
|
|
),
|
|
).toThrow("ACCEPTED_GENERATION_MISMATCH");
|
|
expect(() => assertAcceptedGeneration(undefined, source, build)).toThrow(
|
|
"ACCEPTED_GENERATION_MISMATCH",
|
|
);
|
|
});
|