AtlatestRepositorysigil-http3
sigil-http3 / tree / spikes / webrtcwindow-check.mjs
1
// Exercise the real controller without opening a firewall or arming a peer.2
import assert from 'node:assert/strict';3
import { spawn, execFileSync } from 'node:child_process';4
import { existsSync, readFileSync, writeFileSync } from 'node:fs';5
import { join, resolve } from 'node:path';6
import { hostname } from 'node:os';7
import { identity, sameIdentity, stopRun } from '../../scripts/echo-lifecycle.mjs';8
import { requestArm, validOpenProof, validateArm } from './window.mjs';10
const directory = resolve('build/webrtc-phone');11
const now = Date.now();12
const sample = { bindAddress: '2001:db8:1::1', supervisor: { pid: 123, start: '456' }, startedAt: new Date(now - 1000).toISOString() };13
const proof = { schema: 1, address: sample.bindAddress, minPort: 44000, maxPort: 44009,14
supervisorPID: 123, supervisorStart: '456', openedEpoch: Math.floor(now / 1000), expiresEpoch: Math.floor(now / 1000) + 4500 };15
assert.equal(validOpenProof(proof, sample, now), true);16
for (const patch of [{ maxPort: 44010 }, { minPort: 43443 }, { supervisorStart: '457' },17
{ address: '2001:db8:2::1' }, { expiresEpoch: Math.floor(now / 1000) - 1 }, { expiresEpoch: proof.expiresEpoch + 1 }])18
assert.equal(validOpenProof({ ...proof, ...patch }, sample, now), false);19
assert.throws(() => validateArm({ schema: 1, arm: 'cellular', rendezvous: {20
cellularOnly: true, phoneIPv6: '2a02:1388:1::2', observedAt: new Date(now - 600001).toISOString() } }, '2a14:6700:1::1', now));21
assert.throws(() => validateArm({ schema: 1, arm: 'cellular', rendezvous: {22
cellularOnly: true, phoneIPv6: '2a14:6700:1::2', observedAt: new Date(now).toISOString() } }, '2a14:6700:1::1', now));23
assert.equal(validateArm({ schema: 1, arm: 'wifi', rendezvous: {24
wifiOnly: true, phoneIPv6: '2a14:6700:1::2', observedAt: new Date(now).toISOString() } }, '2a14:6700:1::1', now), '2a14:6700:1::2');25
const child = spawn(process.execPath, ['spikes/webrtc/window.mjs', 'check-run'], { stdio: ['ignore', 'pipe', 'pipe'] });26
let output = '', closed = false;27
child.stdout.on('data', bytes => { output += bytes; }); child.stderr.on('data', bytes => { output += bytes; });28
const admitted = identity(child.pid);29
assert.equal(admitted?.uid, process.getuid());30
const done = new Promise((resolve, reject) => {31
child.once('error', reject); child.once('close', (code, signal) => { closed = true; resolve({ code, signal }); });32
});33
const deadline = setTimeout(() => {34
if (!closed && sameIdentity(admitted, identity(child.pid))) child.kill('SIGTERM');35
}, 15000);36
try {37
const until = Date.now() + 8000;38
let state;39
while (!closed && Date.now() < until) {40
if (existsSync(join(directory, 'state.json'))) {41
state = JSON.parse(readFileSync(join(directory, 'state.json'), 'utf8'));42
if (state.phase === 'awaiting-firewall') break;43
}44
await new Promise(resolve => setTimeout(resolve, 50));45
}46
assert.equal(state?.phase, 'awaiting-firewall', output);47
assert.equal(sameIdentity(admitted, state.supervisor), true);48
const script = readFileSync(join(directory, 'firewall/open.sh'), 'utf8');49
assert.ok(script.includes('--dport 44000:44009'));50
assert.ok(script.includes(state.bindAddress + '/128'));51
assert.ok(script.includes('sigil-http3-webrtc-phone-999'));52
for (const name of ['open.sh', 'close.sh']) execFileSync('bash', ['-n', join(directory, 'firewall', name)]);53
const response = await fetch(`http://127.0.0.1:${state.signalPort}/wt-spike/signal`, { method: 'POST',54
headers: { Origin: 'https://example.com', 'Content-Type': 'application/json' }, body: '{}' });55
assert.equal(response.status, 503);56
const rendezvous = join(directory, 'test-rendezvous.json');57
writeFileSync(rendezvous, JSON.stringify({ cellularOnly: true, phoneIPv6: '2a02:1388:1::2', observedAt: new Date().toISOString() }), { mode: 0o600 });58
assert.throws(() => requestArm(directory, rendezvous, 'cellular'), /Controller not ready/);59
assert.equal(existsSync(join(directory, 'session.json')), false);60
assert.equal(existsSync(join(directory, 'firewall/opened.json')), false);61
assert.equal(stopRun(directory).stopRequested, true);62
assert.deepEqual(await done, { code: 0, signal: null });63
const finished = JSON.parse(readFileSync(join(directory, 'state.json'), 'utf8'));64
assert.equal(finished.phase, 'finished'); assert.equal(finished.socketsClosed, true);65
assert.equal(finished.peer.counters.txChecks, 0); assert.equal(finished.peer.counters.udpReceived, 0);66
assert.equal(identity(child.pid), null);67
writeFileSync('build/webrtc-window-check.json', JSON.stringify({ passed: true, controllerReaped: true,68
noRootRule: true, noToken: true, noUdp: true, allowanceAbsentRefused: true, scriptsParsed: true }, null, 2) + '\n', { mode: 0o600 });69
console.log('RESULT webrtc-window-check passed=true controller-reaped=true root-opened=false udp=0 token=false');70
} finally {71
if (!closed && sameIdentity(admitted, identity(child.pid))) child.kill('SIGTERM');72
await done; clearTimeout(deadline);73
}