sync-padding.test.ts

5.7 KB · raw

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    // The point of the exercise: a relay must not be able to tell a HELLO from24    // a REQUEST from a DONE by looking at the frame it is forwarding.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    // Doubling a 12 MiB frame would exceed the 24 MiB transport cap for no49    // extra privacy, so buckets go linear above 64 KiB.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    // Every input comes from padSyncFrame, so this pins the header round-trip;68    // the KAT above pins the buckets.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    // The receiver reads this only once it holds all four header bytes.78    expect(() => declaredPaddedFrameBytes(new Uint8Array(0))).toThrow(79      /length header/,80    );81    expect(() => declaredPaddedFrameBytes(new Uint8Array(3))).toThrow(82      /length header/,83    );84  });85});8687/**88 * E7b — chunking invariants. The padded-frame ladder can produce frames up89 * to 24 MiB; Noise caps a single transport message at 65519 plaintext90 * bytes. SyncSession.send/receive chunks across that boundary transparently91 * to callers. These tests pin the invariants that the cross-platform92 * transport (and a future Android port) must respect.93 */94describe("sync frame chunking (E7b)", () => {95  it("MAX_NOISE_PLAINTEXT_BYTES matches what the crypto core allows", () => {96    // crypto-core/src/lib.rs: encrypt refuses > MAX_NOISE_MESSAGE - TAG_LEN,97    // which is 65535 - 16 = 65519. If either side drifts, the wire breaks98    // here first, before a real device.99    expect(MAX_NOISE_PLAINTEXT_BYTES).toBe(65519);100  });101102  it("the bucket ladder can produce frames above the Noise limit — the bug", () => {103    // A padded frame for a payload of 32 KiB rounds up to 64 KiB, which104    // exceeds the Noise transport limit (65535 bytes ciphertext, 65519105    // plaintext). Before chunking, the crypto core rejected these with106    // "Noise plaintext exceeds transport limit" and the peer waited for a107    // frame that never arrived. This test pins that the condition is108    // reachable so anyone tightening the ladder in future notices.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    // A byte that is never a plausible first byte of a real padded frame116    // (which begins with a 4-byte big-endian length, so byte 0 of a 3-byte117    // payload is 0). 0x01 is unambiguous and matches the KAT on Android.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});