Files
HellCon/tests/codes.test.ts
T
SchluffeandClaude Opus 5.5 8bd37ac8b1 Use compressed connection codes in the transfer panel
Offers and answers now go through the HC1 codec: the sender creates a
ref per session and the answer must echo it, so pasting an offer as the
answer or an answer for another code is rejected with a specific message
while the live offer stays usable. Pasted input may be a full link.

webrtc.ts drops its Base64 JSON signaling and exchanges validated SDP
only; identity hellos travel in the code envelope. Tests, the browser
check, compare-ice and the docs follow the new format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 22:35:44 +02:00

80 lines
5.1 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { randomBytes } from 'node:crypto';
import { CODE_PREFIX, MAX_CODE_LENGTH, MAX_INPUT_LENGTH, codeFromText, codeLink, createRef, decodeCode, encodeCode, type Signal } from '../src/lib/codes.ts';
const fingerprint = Array.from({ length: 32 }, (_, i) => i.toString(16).padStart(2, '0')).join(':');
const sdp = `v=0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\na=fingerprint:sha-256 ${fingerprint}\r\n`;
const identity = { key: '04' + 'ab'.repeat(64), nonce: 'cd'.repeat(32) };
const ref = createRef();
const offer: Signal = { type: 'offer', ref, sdp };
const base64url = (bytes: Uint8Array) => Buffer.from(bytes).toString('base64url');
async function deflate(data: Uint8Array<ArrayBuffer> | string) {
return new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(new CompressionStream('deflate-raw'))).arrayBuffer());
}
const raw = async (payload: unknown) => CODE_PREFIX + base64url(await deflate(JSON.stringify(payload)));
test('codes round-trip both types, with and without identity', async () => {
assert.match(ref, /^[A-Za-z0-9_-]{22}$/);
assert.notEqual(createRef(), ref);
for (const signal of [offer, { ...offer, type: 'answer' as const }, { ...offer, identity }]) {
const code = await encodeCode(signal);
assert.ok(code.startsWith(CODE_PREFIX));
assert.match(code.slice(CODE_PREFIX.length), /^[A-Za-z0-9_-]+$/);
assert.deepEqual(await decodeCode(code), signal);
}
});
test('links, wrapped text and bare codes decode the same', async () => {
const code = await encodeCode(offer);
const link = codeLink(code, 'https://example.com/apps/hellcon/?x=1#old');
assert.equal(link, `https://example.com/apps/hellcon/?x=1#${code}`);
assert.equal(codeFromText(link), code);
assert.deepEqual(await decodeCode(link), offer);
assert.deepEqual(await decodeCode(` ${code.slice(0, 20)}\n${code.slice(20)} `), offer);
assert.deepEqual(await decodeCode('#' + code), offer);
});
test('expected type and ref are enforced', async () => {
const answer = await encodeCode({ ...offer, type: 'answer' });
await assert.rejects(decodeCode(await encodeCode(offer), { type: 'answer' }), /not an answer/);
await assert.rejects(decodeCode(answer, { type: 'offer' }), /not a connection code/);
await assert.rejects(decodeCode(answer, { type: 'answer', ref: createRef() }), /different connection code/);
assert.equal((await decodeCode(answer, { type: 'answer', ref })).type, 'answer');
});
test('descriptions are validated, including relay opt-in', async () => {
const relayed = await encodeCode({ ...offer, sdp: sdp + 'a=candidate:1 1 udp 10 192.0.2.1 5000 typ relay raddr 0.0.0.0 rport 0\r\n' });
await assert.rejects(decodeCode(relayed), /explicitly enable TURN/);
assert.equal((await decodeCode(relayed, { allowTurn: true })).type, 'offer');
await assert.rejects(encodeCode({ ...offer, identity: { key: '04', nonce: '' } }), /device identity/);
await assert.rejects(decodeCode(await raw({ version: 3, type: 'offer', ref, sdp: 'v=0' })), /data channel/);
});
test('malformed, old and unexpected payloads are rejected', async () => {
for (const text of ['', 'bad!', CODE_PREFIX, `${CODE_PREFIX}a`, `${CODE_PREFIX}not+base64`, `${CODE_PREFIX}AAAA`, CODE_PREFIX + base64url(new Uint8Array([0xff, 0xfe]))]) {
await assert.rejects(decodeCode(text), /Invalid connection code/, text);
}
await assert.rejects(decodeCode(btoa(JSON.stringify({ version: 4, sdp }))), /current HellCon version/);
await assert.rejects(decodeCode(await raw({ version: 2, type: 'offer', ref, sdp })), /current HellCon version/);
await assert.rejects(decodeCode(await raw([1])), /current HellCon version/);
await assert.rejects(decodeCode(await raw({ version: 3, type: 'pranswer', ref, sdp })), /Invalid connection code/);
await assert.rejects(decodeCode(await raw({ version: 3, type: 'offer', ref: 'short', sdp })), /Invalid connection code/);
await assert.rejects(decodeCode(await raw({ version: 3, type: 'offer', ref, sdp, identity })), /Invalid connection code/);
await assert.rejects(decodeCode(await raw({ version: 4, type: 'offer', ref, sdp })), /device identity/);
await assert.rejects(decodeCode(CODE_PREFIX + base64url(await deflate(new Uint8Array([0x7b, 0xff, 0x7d])))), /Invalid connection code/);
await assert.rejects(decodeCode(42), /too large/);
});
test('oversized codes and decompression bombs are rejected', async () => {
await assert.rejects(decodeCode(CODE_PREFIX + 'A'.repeat(MAX_CODE_LENGTH)), /too large/);
await assert.rejects(decodeCode('x'.repeat(MAX_INPUT_LENGTH + 1)), /too large/);
// 32 MiB of zeros deflate to about 32 KiB, which fits the code length limit.
const bomb = CODE_PREFIX + base64url(await deflate(new Uint8Array(32 * 1024 * 1024)));
assert.ok(bomb.length < MAX_CODE_LENGTH);
const started = Date.now();
await assert.rejects(decodeCode(bomb), /too large/);
assert.ok(Date.now() - started < 2000);
// Base64 text of random bytes stays incompressible enough to exceed the limit.
await assert.rejects(encodeCode({ ...offer, sdp: sdp + 'a=x:' + randomBytes(60000).toString('base64') }), /too large/);
});