1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Unit tests: VLESS header helpers (pure). Run: node --test test/vless.test.js */
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { bytesToUuid, uuidToBytes, isValidUuid, newUuid, parseVlessHeader, rewriteUuid, buildVlessHeader } from '../src/vless.js';
test('uuid roundtrip', () => {
const u = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890';
assert.equal(bytesToUuid(uuidToBytes(u)), u);
});
test('isValidUuid', () => {
assert.ok(isValidUuid('a1b2c3d4-e5f6-7890-abcd-ef1234567890'));
assert.ok(!isValidUuid('not-a-uuid'));
assert.ok(!isValidUuid('a1b2c3d4e5f67890abcdef1234567890')); // no dashes
});
test('newUuid format + uniqueness', () => {
const a = newUuid(), b = newUuid();
assert.ok(isValidUuid(a)); assert.ok(isValidUuid(b)); assert.notEqual(a, b);
});
test('build -> parse roundtrip', () => {
const u = newUuid();
const hdr = buildVlessHeader(u, { port: 443, addr: [8, 8, 8, 8] });
const p = parseVlessHeader(hdr);
assert.equal(p.uuid, u);
});
test('parse needs 18 bytes', () => {
assert.throws(() => parseVlessHeader(new Uint8Array(17)), /18 bytes/);
});
test('parse rejects bad version', () => {
const b = new Uint8Array(18); b[0] = 0x01;
assert.throws(() => parseVlessHeader(b), /version/);
});
test('rewriteUuid swaps bytes 1..16, keeps rest', () => {
const u1 = newUuid(), u2 = newUuid();
const hdr = buildVlessHeader(u1);
const rw = rewriteUuid(hdr, uuidToBytes(u2));
assert.equal(parseVlessHeader(rw).uuid, u2);
assert.deepEqual(rw.slice(17), hdr.slice(17)); // everything after UUID untouched
assert.equal(rw[0], hdr[0]);
});
test('domain address header parses', () => {
const u = newUuid();
const hdr = buildVlessHeader(u, { addr: 'example.com', port: 443 });
assert.equal(parseVlessHeader(hdr).uuid, u);
});