AtlatestRepositorysigil-nat

sigil-nat / tree / tools / nattest-nat-probe-runtime.mjs

1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { randomBytes } from 'node:crypto';
4import { createSocket } from 'node:dgram';
5import { once } from 'node:events';
6import { openNatProbeReflector } from './nat-probe-reflector.mjs';
7import { openNatProbeSocket } from './nat-probe-socket.mjs';
8import { runNatProbe } from './nat-probe.mjs';
9import { closeUdpSocket } from './socket-cleanup.mjs';
10import { encodeProbeMessage, probeOperations as op } from './nat-probe-wire.mjs';
12async function reflectors(t, { lifetimeMs = 5000, wrongPeer = false } = {}) {
13 const token = randomBytes(32);
14 const a = await openNatProbeReflector({ role: 'a', bindAddress: '127.0.0.1', ports: [0, 0], token,
15 clientAddresses: ['127.0.0.3'], lifetimeMs });
16 t.after(() => a.close());
17 const b = await openNatProbeReflector({ role: 'b', bindAddress: '127.0.0.2', ports: [0], token,
18 clientAddresses: ['127.0.0.3'], peer: wrongPeer ? a.endpoints[1] : a.endpoints[0], lifetimeMs });
19 t.after(() => b.close());
20 a.setPeer(b.endpoints[0]);
21 return { a, b, token, topology: { a1: a.endpoints[0], a2: a.endpoints[1], b: b.endpoints[0] } };
24test('real UDP client measures all mapping/filtering channels and isolated idle sockets', async t => {
25 const hosts = await reflectors(t);
26 const result = await runNatProbe({ localAddress: '127.0.0.3', token: hosts.token,
27 reflectors: hosts.topology, idleMs: [20, 40, 80, 120], timeoutMs: 250, deadlineMs: 4000 });
28 assert.deepEqual(result.summary.mapping, { 'endpoint-independent': 3 });
29 assert.deepEqual(result.summary.filtering, { 'endpoint-independent': 3 });
30 assert.equal(result.summary.portPreservation.preserved, 3);
31 assert.equal(result.summary.lifetime.trials.length, 4);
32 assert.equal(result.summary.lifetime.lowerBoundSeconds, 0.12);
33 assert.equal(result.summary.lifetime.inconclusiveCount, 0);
34 assert.ok(result.details.lifetime.every(sample => sample.outboundPackets === 1 && sample.received && sample.ackMatched));
35 assert.ok(result.details.filtering.every(sample =>
36 JSON.stringify(sample.outboundDestinationsBeforeFiltering) === '["a1"]'));
37 assert.equal(new Set(result.details.sourcePorts).size, 8);
38 assert.ok(result.summary.packetCounts.outbound > 0);
39 assert.doesNotMatch(JSON.stringify(result.summary), /127\.0\.0/);
40 assert.equal(hosts.a.errors.length, 0);
41 assert.equal(hosts.b.errors.length, 0);
42 await hosts.a.close(); await hosts.b.close();
43 assert.ok(hosts.a.counts.sent > 0 && hosts.b.counts.sent > 0);
44});
46test('missing alternate-host forwarding acknowledgement stays inconclusive', async t => {
47 const hosts = await reflectors(t, { wrongPeer: true });
48 const result = await runNatProbe({ localAddress: '127.0.0.3', token: hosts.token,
49 reflectors: hosts.topology, idleMs: [20], timeoutMs: 40, deadlineMs: 4000 });
50 // Direct connectivity to B succeeds; B rejects only the unadmitted forwarder.
51 assert.deepEqual(result.summary.mapping, { 'endpoint-independent': 3 });
52 assert.deepEqual(result.summary.filtering, { inconclusive: 3 });
53 assert.ok(result.details.filtering.every(sample => !sample.alternateAddress.ackMatched));
54});
56test('foreign run tokens receive no reply; the correct token remains a positive control', async t => {
57 const hosts = await reflectors(t);
58 const socket = createSocket('udp4');
59 t.after(() => closeUdpSocket(socket));
60 socket.bind(0, '127.0.0.3');
61 await once(socket, 'listening');
62 let replies = 0;
63 socket.on('message', () => { replies += 1; });
64 const packet = encodeProbeMessage({ token: Buffer.alloc(32), transaction: Buffer.alloc(12), operation: op.lookup });
65 await new Promise((done, reject) => socket.send(packet, hosts.topology.a1.port, hosts.topology.a1.address,
66 error => error ? reject(error) : done()));
67 await new Promise(done => setTimeout(done, 30));
68 assert.equal(replies, 0);
69 const channel = await openNatProbeSocket({ localAddress: '127.0.0.3', token: hosts.token });
70 t.after(() => channel.close());
71 const transaction = randomBytes(12);
72 const response = channel.watch({ transaction, expected: { [op.observed]: hosts.topology.a1 },
73 required: [op.observed], timeoutMs: 250 });
74 await channel.send(op.lookup, hosts.topology.a1, transaction);
75 assert.ok((await response).messages[op.observed]);
76});
78test('overall deadline aborts idle waits and closes client sockets', async t => {
79 const hosts = await reflectors(t);
80 await assert.rejects(runNatProbe({ localAddress: '127.0.0.3', token: hosts.token,
81 reflectors: hosts.topology, idleMs: [1000], timeoutMs: 100, deadlineMs: 100 }), /abort|deadline/i);
82 assert.equal(hosts.a.errors.length, 0);
83});
85test('reflectors expire automatically and repeated close is safe', async t => {
86 const hosts = await reflectors(t, { lifetimeMs: 100 });
87 await Promise.all([hosts.a.closed, hosts.b.closed]);
88 await Promise.all([hosts.a.close(), hosts.a.close(), hosts.b.close()]);
89 assert.equal(hosts.a.errors.length, 0);
90 assert.equal(hosts.b.errors.length, 0);
91});