AtlatestRepositorysigil-nat
1
import assert from 'node:assert/strict';2
import { test } from 'node:test';3
import { createSocket } from 'node:dgram';4
import { once } from 'node:events';5
import { SingleUdpLease, MappingRejected } from './lease.mjs';6
import { closeUdpSocket } from './socket-cleanup.mjs';8
const mapping = { lifetime: 120, externalPort: 43123, externalAddress: '198.51.100.4' };9
const options = candidates => ({ candidates, deadline: 150000, now: () => 0 });10
const candidate = exchange => ({ protocol: 'fixture', identity: { owner: 'fixture' }, exchange });12
test('an excessive grant is deleted before any fallback', async () => {13
const calls = [];14
const lease = new SingleUdpLease(options([candidate(async lifetime => {15
calls.push(lifetime);16
return { ...mapping, lifetime: lifetime ? 121 : 0 };17
})]));18
await assert.rejects(lease.create(), /over-cap/);19
assert.deepEqual(calls, [120, 0]);20
assert.equal(lease.state.phase, 'deleted');21
});23
test('a lost create reply must be deleted before the next protocol', async () => {24
const calls = [];25
const first = candidate(async lifetime => {26
calls.push(['first', lifetime]);27
if (lifetime) throw new Error('lost reply');28
return { lifetime: 0 };29
});30
const second = candidate(async lifetime => {31
calls.push(['second', lifetime]);32
return { ...mapping, lifetime };33
});34
const lease = new SingleUdpLease(options([first, second]));35
await lease.create();36
await lease.close();37
assert.deepEqual(calls, [['first', 120], ['first', 0], ['second', 120], ['second', 0]]);38
});40
test('unconfirmed deletion blocks fallback and remains uncertain', async () => {41
let fallback = false;42
const lease = new SingleUdpLease(options([43
candidate(async () => { throw new Error('network lost'); }),44
candidate(async () => { fallback = true; return mapping; }),45
]));46
await assert.rejects(lease.create(), /Deletion unconfirmed/);47
assert.equal(lease.state.phase, 'uncertain');48
assert.equal(fallback, false);49
});51
test('a rejected request may fall back without pretending it created a lease', async () => {52
const calls = [];53
const lease = new SingleUdpLease(options([54
candidate(async lifetime => { calls.push(lifetime); throw new MappingRejected('unsupported'); }),55
candidate(async lifetime => ({ ...mapping, lifetime })),56
]));57
await lease.create();58
await lease.close();59
assert.deepEqual(calls, [120]);60
});62
test('close during creation serializes deletion and repeated close is harmless', async () => {63
let release;64
let started;65
const beginning = new Promise(done => { started = done; });66
const calls = [];67
const lease = new SingleUdpLease(options([candidate(async lifetime => {68
calls.push(lifetime);69
if (!lifetime) return { lifetime: 0 };70
started();71
await new Promise(done => { release = done; });72
return mapping;73
})]));74
const creating = lease.create();75
const rejected = assert.rejects(creating, /stopped during creation/);76
await beginning;77
const closing = lease.close();78
release();79
await rejected;80
await closing;81
await lease.close();82
assert.deepEqual(calls, [120, 0]);83
assert.equal(lease.state.phase, 'deleted');84
});86
test('renewal failure deletes the old owned mapping and cancels maintenance', async () => {87
const calls = [];88
const tasks = new Map();89
let id = 0;90
const lease = new SingleUdpLease({ ...options([candidate(async lifetime => {91
calls.push(lifetime);92
if (calls.length === 2) throw new Error('renewal reply lost');93
return { ...mapping, lifetime };94
})]), schedule: (callback, delay) => { tasks.set(++id, { callback, delay }); return id; },95
cancel: timer => tasks.delete(timer) });96
await lease.create();97
lease.maintain();98
assert.deepEqual([...tasks.values()].map(task => task.delay), [150000, 60000]);99
await assert.rejects(lease.renew(), /renewal reply lost/);100
await lease.close();101
assert.deepEqual(calls, [120, 120, 0]);102
assert.equal(tasks.size, 0);103
});105
test('journal failure on teardown cannot suppress the delete request', async () => {106
let deleting = false;107
const lease = new SingleUdpLease({ ...options([candidate(async lifetime => {108
if (!lifetime) deleting = true;109
return { ...mapping, lifetime };110
})]), journal: state => { if (state.phase === 'deleting') throw new Error('disk full'); } });111
await lease.create();112
await lease.close();113
assert.equal(deleting, true);114
assert.equal(lease.state.phase, 'deleted');115
});117
test('concurrent UDP cleanup waits for one close event and permits retry after close', async () => {118
const socket = createSocket('udp4');119
socket.bind(0, '127.0.0.1');120
await once(socket, 'listening');121
let closes = 0;122
socket.on('close', () => { closes += 1; });123
const first = closeUdpSocket(socket);124
const second = closeUdpSocket(socket);125
assert.equal(first, second);126
await Promise.all([first, second]);127
await closeUdpSocket(socket);128
assert.equal(closes, 1);129
});131
test('cleanup of an unbound socket does not crash the error path', async () => {132
const socket = createSocket('udp4');133
await closeUdpSocket(socket);134
await closeUdpSocket(socket);135
});