1import { describe, expect, it } from "vitest";2import {3 padSyncFrame,4 declaredPaddedFrameBytes,5 syncFrameBucket,6 unpadSyncFrame,7 MAX_NOISE_PLAINTEXT_BYTES,8 FRAME_CONTINUATION_MARKER,9 chunkPaddedSyncFrame,10} from "./sync-padding";1112describe("sync frame padding (E7)", () => {13 it("round-trips payloads of every shape, including empty", () => {14 for (const size of [0, 1, 5, 251, 252, 253, 1000, 70_000]) {15 const payload = Uint8Array.from({ length: size }, (_, i) => (i * 31) & 0xff);16 expect(Array.from(unpadSyncFrame(padSyncFrame(payload)))).toEqual(17 Array.from(payload),18 );19 }20 });2122 it("collapses every small control frame to one indistinguishable size", () => {23 24 25 const hello = new TextEncoder().encode('{"type":"SYNC_HELLO","vaultId":"' + "a".repeat(64) + '"}');26 const done = new TextEncoder().encode('{"type":"SYNC_DONE"}');27 expect(padSyncFrame(hello).length).toBe(padSyncFrame(done).length);28 expect(padSyncFrame(done).length).toBe(256);29 });3031 it("pins the bucket ladder — cross-platform KAT, must match peach-android", () => {32 const cases: Array<[number, number]> = [33 [0, 256],34 [252, 256],35 [253, 512],36 [1024, 2048],37 [65_532, 65_536],38 [65_533, 131_072],39 [200_000, 262_144],40 ];41 for (const [payload, expected] of cases) {42 expect(syncFrameBucket(4 + payload)).toBe(expected);43 expect(padSyncFrame(new Uint8Array(payload)).length).toBe(expected);44 }45 });4647 it("keeps large-frame overhead bounded rather than doubling", () => {48 49 50 const twelveMiB = 12 * 1024 * 1024;51 const padded = syncFrameBucket(twelveMiB);52 expect(padded).toBeGreaterThanOrEqual(twelveMiB);53 expect(padded - twelveMiB).toBeLessThan(64 * 1024);54 });5556 it("rejects a frame whose declared length exceeds what it carries", () => {57 const forged = new Uint8Array(256);58 new DataView(forged.buffer).setUint32(0, 9999, false);59 expect(() => unpadSyncFrame(forged)).toThrow(/more payload than it carries/);60 });6162 it("rejects a frame shorter than its length header", () => {63 expect(() => unpadSyncFrame(new Uint8Array(3))).toThrow(/length header/);64 });6566 it("declares the padded size the frame's own length header implies", () => {67 68 69 for (const size of [0, 251, 70_000, 1_000_000]) {70 const payload = new Uint8Array(size);71 const padded = padSyncFrame(payload);72 expect(declaredPaddedFrameBytes(padded)).toBe(padded.byteLength);73 }74 });7576 it("refuses a prefix shorter than the length header", () => {77 78 expect(() => declaredPaddedFrameBytes(new Uint8Array(0))).toThrow(79 /length header/,80 );81 expect(() => declaredPaddedFrameBytes(new Uint8Array(3))).toThrow(82 /length header/,83 );84 });85});868788899091929394describe("sync frame chunking (E7b)", () => {95 it("MAX_NOISE_PLAINTEXT_BYTES matches what the crypto core allows", () => {96 97 98 99 expect(MAX_NOISE_PLAINTEXT_BYTES).toBe(65519);100 });101102 it("the bucket ladder can produce frames above the Noise limit — the bug", () => {103 104 105 106 107 108 109 const payload = Uint8Array.from({ length: 32 * 1024 }, () => 0);110 const padded = padSyncFrame(payload);111 expect(padded.byteLength).toBeGreaterThan(MAX_NOISE_PLAINTEXT_BYTES);112 });113114 it("the continuation marker is the byte the chunking protocol uses", () => {115 116 117 118 expect(FRAME_CONTINUATION_MARKER).toBe(0x01);119 });120121 it("production chunking reserves marker space at the Noise boundary", () => {122 const payload = Uint8Array.from(123 { length: 32 * 1024 },124 (_, index) => index & 0xff,125 );126 const padded = padSyncFrame(payload);127 const chunks = chunkPaddedSyncFrame(padded);128129 expect(chunks).toHaveLength(2);130 expect(chunks[0]![0]).toBe(FRAME_CONTINUATION_MARKER);131 expect(chunks[0]!.byteLength).toBe(MAX_NOISE_PLAINTEXT_BYTES);132 expect(chunks[1]![0]).not.toBe(FRAME_CONTINUATION_MARKER);133134 const reassembled = new Uint8Array(135 chunks[0]!.byteLength - 1 + chunks[1]!.byteLength,136 );137 reassembled.set(chunks[0]!.subarray(1), 0);138 reassembled.set(chunks[1]!, chunks[0]!.byteLength - 1);139 expect(unpadSyncFrame(reassembled)).toEqual(payload);140 });141});