AtlatestRepositorysigil-nat

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

1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { encodeProbeMessage, decodeProbeMessage, validateProbeReflectors,
4 probeOperations as op } from './nat-probe-wire.mjs';
5import { classifyProbeMapping, classifyProbeFiltering, summarizeProbeLifetime,
6 summarizeNatProbe, summarizeSingleHostNatProbe } from './nat-probe-analysis.mjs';
7
8const token = Buffer.alloc(32, 0x11), transaction = Buffer.alloc(12, 0x22);
9const observed = port => ({ sent: true, matched: true, observed: { address: '198.51.100.1', port } });
10const baseline = (localPort = 45000) => ({ localPort, a1: observed(localPort), a2: observed(localPort),
11 b: observed(localPort), repeatedA1: observed(localPort) });
12const filter = received => ({ sent: true, ackMatched: true, received });
13const controls = { primary: true, alternateAddress: true, alternatePort: true, recoveredPrimary: true };
14const idle = (seconds, port) => ({ idleMs: seconds * 1000, localPort: port,
15 outboundPackets: 1, established: true, probeSent: true, ackMatched: true,
16 controlHealthy: true, received: true });
18test('probe wire has exact independent header/tuple positions and no amplification', () => {
19 const bytes = encodeProbeMessage({ token, transaction, operation: op.observed,
20 endpoint: { address: '203.0.113.7', port: 50000 } });
21 assert.equal(bytes.length, 64);
22 assert.equal(bytes.subarray(0, 4).toString(), 'SNP1');
23 assert.equal(bytes[48], 2);
24 assert.equal(bytes.subarray(49, 55).toString('hex'), 'cb007107c350');
25 const decoded = decodeProbeMessage(bytes, token);
26 assert.deepEqual(decoded.endpoint, { address: '203.0.113.7', port: 50000 });
27 assert.deepEqual(decoded.transaction, transaction);
28});
30test('wrong run tokens, truncated/oversized frames and reserved bits fail closed', () => {
31 const bytes = encodeProbeMessage({ token, transaction, operation: op.lookup });
32 assert.throws(() => decodeProbeMessage(bytes, Buffer.alloc(32)), /token/);
33 for (const bad of [bytes.subarray(0, 63), Buffer.concat([bytes, Buffer.alloc(1)])])
34 assert.throws(() => decodeProbeMessage(bad, token), /size/);
35 const reserved = Buffer.from(bytes); reserved[63] = 1;
36 assert.throws(() => decodeProbeMessage(reserved, token), /reserved/);
37});
39test('reflector topology rejects same outside IP and identical A ports', () => {
40 const topology = { a1: { address: '198.51.100.1', port: 41000 },
41 a2: { address: '198.51.100.1', port: 41001 }, b: { address: '203.0.113.2', port: 41000 } };
42 assert.deepEqual(validateProbeReflectors(topology), topology);
43 assert.throws(() => validateProbeReflectors({ ...topology, b: topology.a1 }), /different IPv4/);
44 assert.throws(() => validateProbeReflectors({ ...topology, a2: topology.a1 }), /two ports/);
45});
47test('mapping categories depend on the observed full tuple, including public IP', () => {
48 assert.equal(classifyProbeMapping(baseline()), 'endpoint-independent');
49 assert.equal(classifyProbeMapping({ ...baseline(), b: observed(50001) }), 'address-dependent');
50 assert.equal(classifyProbeMapping({ ...baseline(), a2: observed(50001) }), 'address-and-port-dependent');
51 const alternateAddress = observed(45000); alternateAddress.observed.address = '203.0.113.9';
52 assert.equal(classifyProbeMapping({ ...baseline(), b: alternateAddress }), 'address-dependent');
53});
55test('loss or changing baseline is inconclusive, never invented restrictive mapping', () => {
56 assert.equal(classifyProbeMapping({ ...baseline(), b: undefined }), 'inconclusive');
57 assert.equal(classifyProbeMapping({ ...baseline(), a2: { ...observed(50001), matched: false } }), 'inconclusive');
58 assert.equal(classifyProbeMapping({ ...baseline(), repeatedA1: observed(50001) }), 'inconclusive');
59});
61test('filtering distinguishes alternate address from alternate port responses', () => {
62 assert.equal(classifyProbeFiltering({ controls, alternateAddress: filter(true) }), 'endpoint-independent');
63 assert.equal(classifyProbeFiltering({ controls, alternateAddress: filter(false), alternatePort: filter(true) }), 'address-dependent');
64 assert.equal(classifyProbeFiltering({ controls, alternateAddress: filter(false), alternatePort: filter(false) }), 'address-and-port-dependent');
65});
67test('missing send acknowledgement or failed positive control cannot prove filtering', () => {
68 assert.equal(classifyProbeFiltering({ controls, alternateAddress: { ...filter(false), sent: false },
69 alternatePort: filter(false) }), 'inconclusive');
70 assert.equal(classifyProbeFiltering({ controls: { ...controls, alternateAddress: false },
71 alternateAddress: filter(false), alternatePort: filter(false) }), 'inconclusive');
72});
74test('independent idle sockets report lower bounds without converting UDP loss to expiry', () => {
75 const samples = [idle(15, 45001), idle(30, 45002), idle(60, 45003), { ...idle(120, 45004), received: false }];
76 const result = summarizeProbeLifetime(samples);
77 assert.equal(result.lowerBoundSeconds, 60);
78 assert.equal(result.failureCount, 1);
79 assert.equal(result.trials[3].result, 'not-observed');
80 assert.throws(() => summarizeProbeLifetime([samples[0], samples[0]]), /separate/);
81 assert.equal(summarizeProbeLifetime([{ ...samples[0], outboundPackets: 2 }]).inconclusiveCount, 1);
82});
84test('public summary exposes counts/classifications/timings without addresses or local ports', () => {
85 const summary = summarizeNatProbe({ mapping: [baseline(45000), baseline(45010), baseline(45020)],
86 filtering: [{ controls, alternateAddress: filter(true) }], lifetime: [idle(15, 45001)] });
87 assert.deepEqual(summary.mapping, { 'endpoint-independent': 3 });
88 assert.deepEqual(summary.portPreservation, { compared: 3, preserved: 3 });
89 assert.doesNotMatch(JSON.stringify(summary), /198\.51|203\.0|45000|45001/);
90 assert.throws(() => summarizeNatProbe({ mapping: [baseline(), baseline(), baseline()],
91 filtering: [{ controls, alternateAddress: filter(true) }], lifetime: [] }), /different local/);
92});
94test('single-host evidence never invents an address-dependence classification', () => {
95 const summary = summarizeSingleHostNatProbe({ mapping: [baseline(45000),
96 { ...baseline(45010), a2: observed(50000) }, baseline(45020)],
97 filtering: [{ controls, alternatePort: filter(true) },
98 { controls, alternatePort: filter(false) },
99 { controls, alternatePort: { ...filter(false), ackMatched: false } }], lifetime: [] });
100 assert.deepEqual(summary.mapping, { 'port-independent-for-tested-address': 2, 'port-dependent-for-tested-address': 1 });
101 assert.deepEqual(summary.filtering, { 'alternate-port-allowed': 1, 'alternate-port-not-observed': 1, inconclusive: 1 });
102 assert.equal(summary.addressDependence, 'unmeasured-single-reflector-IP');
103});