AtlatestRepositorysigil-nat

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

1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { parseIgdDescription, upnpUdpCandidate, UpnpIgdClient } from './upnp.mjs';
4
5
6const gateway = '192.0.2.1';
7const location = new URL(`http://${gateway}:52869/gatedesc.xml`);
8const description = Buffer.from(`<?xml version="1.0"?>
9<root><URLBase>http://${gateway}:52869/</URLBase><device><serviceList>
10<service><serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>
11<controlURL>/upnp/control/WANIPConn1</controlURL></service>
12</serviceList></device></root>`);
14test('IGD service description resolves a same-gateway control URL', () => {
15 assert.deepEqual(parseIgdDescription(description, location, gateway), {
16 protocol: 'upnp-igd', serviceType: 'urn:schemas-upnp-org:service:WANIPConnection:1',
17 controlUrl: `http://${gateway}:52869/upnp/control/WANIPConn1`,
18 });
19});
21test('IGD description refuses external control hosts and XML entities', () => {
22 const offGateway = Buffer.from(description.toString().replace('/upnp/control/WANIPConn1',
23 'http://192.0.2.99/control'));
24 assert.throws(() => parseIgdDescription(offGateway, location, gateway), /escaped/);
25 assert.throws(() => parseIgdDescription(Buffer.from('<!DOCTYPE root [<!ENTITY x SYSTEM "file:///etc/passwd">]>'),
26 location, gateway), /entities/);
27});
29test('UPnP mapping candidate probes only free ports and verifies its owner before deletion', async () => {
30 const entries = new Map();
31 const calls = [];
32 const client = {
33 async getMapping(port) { calls.push(['get', port]); return entries.get(port); },
34 async addMapping(entry) {
35 calls.push(['add', entry.externalPort, entry.lifetime]);
36 entries.set(entry.externalPort, { internalPort: entry.internalPort, internalClient: entry.internalClient,
37 enabled: '1', description: entry.description, lifetime: entry.lifetime });
38 },
39 async deleteMapping(port) { calls.push(['delete', port]); entries.delete(port); },
40 };
41 const candidate = upnpUdpCandidate(client, { internalPort: 43123, internalClient: '192.0.2.44',
42 externalAddress: '198.51.100.4', randomPort: () => 51000 });
43 const active = await candidate.exchange(120);
44 assert.equal(active.externalPort, 43123);
45 assert.equal(active.lifetime, 120);
46 const deleted = await candidate.exchange(0, active);
47 assert.equal(deleted.lifetime, 0);
48 assert.equal(entries.size, 0);
49 assert.deepEqual(calls.map(call => call[0]), ['get', 'add', 'get', 'get', 'delete', 'get']);
50});
52test('UPnP mapping candidate refuses an occupied port and excessive leases', async () => {
53 const client = { async getMapping() { return { description: 'someone else' }; } };
54 const candidate = upnpUdpCandidate(client, { internalPort: 43123, internalClient: '192.0.2.44',
55 externalAddress: '198.51.100.4', randomPort: () => 51000 });
56 await assert.rejects(candidate.exchange(120), /unoccupied/);
57 await assert.rejects(candidate.exchange(1801), /30 minutes/);
58});
60function fakeGateway({ granted = 120, ignoredDelete = false, lostDeleteReply = false } = {}) {
61 const entries = new Map();
62 let deletes = 0;
63 const client = {
64 async getMapping(port) { return entries.get(port); },
65 async addMapping(entry) {
66 entries.set(entry.externalPort, { ...entry, enabled: '1', lifetime: granted });
67 },
68 async deleteMapping(port) {
69 deletes += 1;
70 if (!ignoredDelete) entries.delete(port);
71 if (lostDeleteReply) throw new Error('lost delete reply');
72 },
73 };
74 const candidate = upnpUdpCandidate(client, { internalPort: 43123,
75 internalClient: '192.0.2.44', externalAddress: '198.51.100.4' });
76 return { candidate, entries, deletes: () => deletes };
79test('delete success without actual absence is rejected', async () => {
80 const gateway = fakeGateway({ ignoredDelete: true });
81 const active = await gateway.candidate.exchange(120);
82 await assert.rejects(gateway.candidate.exchange(0, active), /still exists/);
83 assert.equal(gateway.entries.size, 1);
84});
86test('foreign owner installed before cleanup is never deleted', async () => {
87 const gateway = fakeGateway();
88 const active = await gateway.candidate.exchange(120);
89 gateway.entries.get(active.externalPort).description = 'foreign';
90 await assert.rejects(gateway.candidate.exchange(0, active), /does not own/);
91 assert.equal(gateway.deletes(), 0);
92});
94test('owned disabled mappings remain eligible for verified cleanup', async () => {
95 const gateway = fakeGateway();
96 const active = await gateway.candidate.exchange(120);
97 gateway.entries.get(active.externalPort).enabled = '0';
98 const result = await gateway.candidate.exchange(0, active);
99 assert.equal(result.absenceCode, 714);
100});
102test('grant above the requested 120 seconds is refused and can be cleaned up', async () => {
103 const gateway = fakeGateway({ granted: 121 });
104 await assert.rejects(gateway.candidate.exchange(120), /bounded leased/);
105 const result = await gateway.candidate.exchange(0);
106 assert.equal(result.absenceCode, 714);
107 assert.equal(gateway.deletes(), 1);
108});
110test('lost delete replies are reconciled by explicit absence lookup', async () => {
111 const gateway = fakeGateway({ lostDeleteReply: true });
112 const active = await gateway.candidate.exchange(120);
113 const result = await gateway.candidate.exchange(0, active);
114 assert.equal(result.absenceCode, 714);
115 assert.equal(gateway.entries.size, 0);
116});
118test('only GetSpecificPortMappingEntry fault 714 counts as absence', async () => {
119 const client = new UpnpIgdClient({ protocol: 'upnp-igd',
120 serviceType: 'urn:schemas-upnp-org:service:WANIPConnection:1',
121 controlUrl: `http://${gateway}:52869/control` }, { gateway, localAddress: '192.0.2.44' });
122 let code = 714;
123 client.action = async action => {
124 assert.equal(action, 'GetSpecificPortMappingEntry');
125 const error = new Error('SOAP failure');
126 error.upnpCode = code;
127 throw error;
128 };
129 assert.equal(await client.getMapping(43123), undefined);
130 code = 501;
131 await assert.rejects(client.getMapping(43123), /SOAP failure/);
132});