/** * Files sealed at arrival (e2e rulings R6 with R2): a file the object fetches * for a person (a cover, a poster, a show's artwork) is sealed to their * arrival key byte for byte as the core does (vectors/seal/arrival_file.json), * is never kept in the clear, and opens on a device holding the key. */ import { describe, expect, it } from "vitest"; import V from "../../../crates/cotton-conformance/vectors/seal/arrival_file.json"; import { keyFromText } from "../src/arrival.ts"; import { asArrived, NoArrivalKey, sealFileAtArrival, sealsAtArrival } from "../src/arrival_file.ts"; import { arrivalPub, openArrivalFile } from "./arrival_keys.ts"; const hex = (h: string): Uint8Array => Uint8Array.from(h.match(/../g) ?? [], (b) => parseInt(b, 16)); const toHex = (b: Uint8Array): string => [...b].map((x) => x.toString(16).padStart(2, "0")).join(""); const DB = "00000000-0000-4000-8000-0000000000a1"; const TYPE = "application/vnd.cotton.f1"; describe("files sealed at arrival", () => { it("seals the vector's file byte for byte as the core does", async () => { const stored = await sealFileAtArrival(keyFromText(V.arrival_pub)!, V.object_key, hex(V.plain_hex), { fileKey: hex(V.file_key_hex), prefix: hex(V.prefix_hex), ephemeral: hex(V.ephemeral_hex), wrapNonce: hex(V.wrap_nonce_hex), }); expect(toHex(stored)).toBe(V.stored_hex); }); it("keeps a person's picture sealed, its file key only wrapped, and a device opens it across parts", async () => { const pub = keyFromText(await arrivalPub())!; for (const len of [0, 1, 65_536, 65_537, 200_000]) { const plain = Uint8Array.from({ length: len }, (_, i) => (i * 13) % 251); const key = `${DB}/photos/${"ab".repeat(32)}.jpg`; const kept = await asArrived(DB, pub, key, plain, "image/jpeg", TYPE); expect(kept.contentType).toBe(TYPE); expect(new TextDecoder().decode(kept.bytes.subarray(0, 4))).toBe("CTA1"); expect(await openArrivalFile(kept.bytes, key)).toEqual(plain); await expect(openArrivalFile(kept.bytes, `${DB}/photos/${"cd".repeat(32)}.jpg`)).rejects.toThrow(); } }); it("never keeps a person's picture in the clear: with no arrival key held it is refused by name", async () => { await expect(asArrived(DB, null, `${DB}/photos/${"ab".repeat(32)}.png`, new Uint8Array([1, 2, 3]), "image/png", TYPE)).rejects.toBeInstanceOf(NoArrivalKey); }); it("seals the kinds a device seals and leaves the object's other files as they came", async () => { for (const kind of ["photos", "videos", "books", "music", "films"]) expect(sealsAtArrival(DB, `${DB}/${kind}/x.bin`)).toBe(true); expect(sealsAtArrival(DB, `${DB}/documents/x.opml`)).toBe(false); expect(sealsAtArrival(DB, `00000000-0000-4000-8000-0000000000a2/photos/x.jpg`)).toBe(false); const opml = new TextEncoder().encode(""); expect(await asArrived(DB, null, `${DB}/documents/x.opml`, opml, "text/x-opml", TYPE)).toEqual({ bytes: opml, contentType: "text/x-opml" }); }); });