AtlatestRepositorysigil-nat
1
import assert from 'node:assert/strict';2
import { test } from 'node:test';3
import { mappedAddress, pcpAnnounce, pcpMap, decodePcp, natPmpAddress, natPmpMap, decodeNatPmp } from './codecs.mjs';4
import { parseSsdp } from './discovery.mjs';6
const hex = text => Buffer.from(text.replace(/\s/g, ''), 'hex');7
const nonce = hex('00112233445566778899aabb');8
const request = { clientAddress: '192.0.2.10', nonce, protocol: 17, internalPort: 40000, lifetime: 120 };9
const response = hex(`0281000000000078000004d200000000000000000000000010
00112233445566778899aabb110000009c40c35000000000000000000000ffffcb007109`);12
test('IPv4 PCP fields use the RFC IPv4-mapped representation', () => {13
assert.equal(mappedAddress('192.0.2.10').toString('hex'), '00000000000000000000ffffc000020a');14
assert.equal(mappedAddress('0.0.0.0').toString('hex'), '00000000000000000000ffff00000000');15
assert.throws(() => mappedAddress('router.invalid'), /IPv4/);16
});17
test('PCP ANNOUNCE is a zero-lifetime read-only common header', () => {18
assert.equal(pcpAnnounce('192.0.2.10').length, 24);19
assert.equal(pcpAnnounce('192.0.2.10').toString('hex'),20
'020000000000000000000000000000000000ffffc000020a');21
});22
test('PCP MAP wire positions match independently specified octets', () => {23
assert.equal(pcpMap(request).toString('hex'),24
'020100000000007800000000000000000000ffffc000020a' +25
'00112233445566778899aabb110000009c40000000000000000000000000ffff00000000');26
});27
test('PCP response matches nonce, transport and internal port before delivery', () => {28
const result = decodePcp(response, 1, request);29
assert.equal(result.result, 0); assert.equal(result.epoch, 1234); assert.equal(result.lifetime, 120);30
assert.equal(result.externalAddress, '203.0.113.9'); assert.equal(result.externalPort, 50000);31
assert.deepEqual(result.nonce, nonce);32
});33
test('PCP reserved bits are ignored on reception and zero in emitted requests', () => {34
const modified = Buffer.from(response); modified[2] = 255; modified[12] = 255; modified[37] = 255;35
assert.equal(decodePcp(modified, 1, request).externalPort, 50000);36
assert.equal(pcpMap(request)[37], 0);37
});38
test('PCP foreign nonce, opcode, direction and internal port are rejected', () => {39
for (const index of [0, 1, 24, 36, 40]) {40
const modified = Buffer.from(response); modified[index] ^= 1;41
assert.throws(() => decodePcp(modified, 1, request), /mismatch|Unmatched/);42
}43
const noResponseBit = Buffer.from(response); noResponseBit[1] = 1;44
assert.throws(() => decodePcp(noResponseBit, 1, request), /direction/);45
});46
test('PCP options are bounded and require their complete padded envelope', () => {47
const withOption = Buffer.concat([response, hex('80630001bc000000')]);48
assert.deepEqual(decodePcp(withOption, 1, request).options, [{ code: 128, bytes: hex('bc') }]);49
assert.throws(() => decodePcp(withOption.subarray(0, -1), 1, request), /Truncated/);50
assert.throws(() => decodePcp(response.subarray(0, 59), 1, request), /Truncated/);51
assert.throws(() => decodePcp(Buffer.alloc(1101), 0), /size/);52
});53
test('PCP server error header does not invent a successful mapping body', () => {54
const error = Buffer.from(response.subarray(0, 24)); error[3] = 2;55
const result = decodePcp(error, 1, request);56
assert.equal(result.result, 2); assert.equal(result.externalPort, undefined);57
});58
test('NAT-PMP address request and response match known octets', () => {59
assert.deepEqual(natPmpAddress(), hex('0000'));60
assert.deepEqual(decodeNatPmp(hex('00800000000004d2cb007109'), 0),61
{ protocol: 'nat-pmp', opcode: 0, result: 0, epoch: 1234, externalAddress: '203.0.113.9' });62
});63
test('NAT-PMP UDP/TCP map and exact-port delete never emit wildcard internal port', () => {64
assert.equal(natPmpMap(request).toString('hex'), '000100009c40000000000078');65
assert.equal(natPmpMap({ ...request, protocol: 6 }).toString('hex'), '000200009c40000000000078');66
assert.equal(natPmpMap({ ...request, lifetime: 0 }).toString('hex'), '000100009c40000000000000');67
assert.throws(() => natPmpMap({ ...request, lifetime: 0, externalPort: 50000 }), /delete/);68
});69
test('NAT-PMP mapping response and deletion match the requested internal port', () => {70
const result = decodeNatPmp(hex('00810000000004d29c40c35000000078'), 1, 40000);71
assert.equal(result.externalPort, 50000); assert.equal(result.lifetime, 120);72
assert.equal(decodeNatPmp(hex('00810000000004d29c40000000000000'), 1, 40000).lifetime, 0);73
assert.throws(() => decodeNatPmp(hex('00810000000004d29c41c35000000078'), 1, 40000), /Unmatched/);74
});75
test('NAT-PMP truncation, extra data and foreign opcodes do not pass as replies', () => {76
assert.throws(() => decodeNatPmp(hex('00800000000004d2cb0071'), 0), /Truncated/);77
assert.throws(() => decodeNatPmp(Buffer.alloc(17), 0), /size/);78
assert.throws(() => decodeNatPmp(hex('00820000000004d29c40c35000000078'), 1), /opcode/);79
assert.equal(decodeNatPmp(hex('00810002000004d2'), 1, 40000).result, 2);80
});81
test('mapping encoders reject wildcard protocols/ports and excessive leases', () => {82
for (const change of [{ internalPort: 0 }, { externalPort: 65536 }, { protocol: 0 }, { lifetime: -1 }, { lifetime: 7201 }]) {83
assert.throws(() => pcpMap({ ...request, ...change }));84
assert.throws(() => natPmpMap({ ...request, ...change }));85
}86
assert.throws(() => pcpMap({ ...request, nonce: Buffer.alloc(11) }), /nonce/);87
});88
test('SSDP accepts IGD descriptions only at the exact configured gateway', () => {89
const reply = location => Buffer.from(`HTTP/1.1 200 OK\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\nLOCATION: ${location}\r\nSERVER: unit-test\r\n\r\n`);90
assert.equal(parseSsdp(reply('http://192.0.2.1:5000/root.xml'), '192.0.2.1').location, 'http://192.0.2.1:5000/root.xml');91
assert.throws(() => parseSsdp(reply('http://203.0.113.1/root.xml'), '192.0.2.1'), /exact gateway/);92
assert.throws(() => parseSsdp(reply('file:///root.xml'), '192.0.2.1'), /exact gateway/);93
assert.throws(() => parseSsdp(Buffer.alloc(8193), '192.0.2.1'), /limit/);94
});