upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/helpers/network.mjs
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-15 17:03:40 +0530
committerYour Name <you@example.com>2026-05-15 17:03:40 +0530
commita7d0a672d59bf8985a6fc0e61b49015fabd96513 (patch)
tree46814d1757649a640f53805a8d9dfc1b0f354289 /tests/helpers/network.mjs
parent8a2307a5ced6da94cc674602219d5a68a1246264 (diff)
Phase 1 working: captive portal, DNS hijack, NAT-based access control
- Fix WiFi init order: netif creation before esp_wifi_init, set mode before set_config - Replace broken netif input filter with NAPT on/off per authentication state - NAPT disabled by default, enabled when client granted, disabled on revoke - Fix test helpers: use -I wlp59s0 for ping, handle nslookup exit code 1 - All 20 API tests pass, all 6 smoke tests pass
Diffstat (limited to 'tests/helpers/network.mjs')
-rw-r--r--tests/helpers/network.mjs89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/helpers/network.mjs b/tests/helpers/network.mjs
new file mode 100644
index 0000000..e4d5086
--- /dev/null
+++ b/tests/helpers/network.mjs
@@ -0,0 +1,89 @@
1import { execSync } from 'child_process';
2
3const ESP32_IP = process.env.TOLLGATE_IP || '192.168.4.1';
4const TIMEOUT = 5000;
5
6export function curl(args, expectStatus = null) {
7 const cmd = `curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 --max-time ${TIMEOUT/1000} ${args}`;
8 try {
9 const result = execSync(cmd, { encoding: 'utf8', timeout: TIMEOUT + 2000 }).trim();
10 if (expectStatus && result !== String(expectStatus)) {
11 throw new Error(`Expected HTTP ${expectStatus}, got ${result}`);
12 }
13 return result;
14 } catch (e) {
15 if (e.status === 'ETIMEDOUT' || e.killed) return 'TIMEOUT';
16 throw e;
17 }
18}
19
20export function curlBody(url) {
21 const cmd = `curl -s --connect-timeout 5 --max-time ${TIMEOUT/1000} "${url}"`;
22 try {
23 return execSync(cmd, { encoding: 'utf8', timeout: TIMEOUT + 2000 });
24 } catch {
25 return null;
26 }
27}
28
29export function getPortalIP() { return ESP32_IP; }
30
31export function canPing(host = '8.8.8.8', count = 2) {
32 try {
33 const result = execSync(`ping -c ${count} -W 2 -I wlp59s0 ${host}`, { encoding: 'utf8', timeout: 10000 });
34 return result.includes('0% packet loss') || result.includes('1 packets transmitted');
35 } catch {
36 return false;
37 }
38}
39
40export function canResolve(domain = 'google.com') {
41 try {
42 const result = execSync(`nslookup ${domain} ${ESP32_IP}`, { encoding: 'utf8', timeout: 10000 });
43 return result.includes('Address') && !result.includes('NXDOMAIN');
44 } catch (e) {
45 const result = e.stdout || '';
46 return result.includes('Address') && !result.includes('NXDOMAIN');
47 }
48}
49
50export function dnsResolvesToSelf(domain = 'google.com') {
51 try {
52 const result = execSync(`nslookup ${domain} ${ESP32_IP}`, { encoding: 'utf8', timeout: 10000 });
53 return result.includes(ESP32_IP);
54 } catch (e) {
55 return e.stdout && e.stdout.includes(ESP32_IP);
56 }
57}
58
59export function connectToAP(ssid, password = '') {
60 try {
61 if (password) {
62 execSync(`nmcli dev wifi connect "${ssid}" password "${password}" ifname wlan0`, { timeout: 30000 });
63 } else {
64 execSync(`nmcli dev wifi connect "${ssid}" ifname wlan0`, { timeout: 30000 });
65 }
66 return true;
67 } catch {
68 return false;
69 }
70}
71
72export function disconnectAP() {
73 try {
74 execSync('nmcli dev disconnect wlan0 2>/dev/null || true', { timeout: 10000 });
75 return true;
76 } catch {
77 return false;
78 }
79}
80
81export function getWifiInterface() {
82 try {
83 const result = execSync('nmcli -t -f DEVICE,TYPE dev status', { encoding: 'utf8' });
84 const line = result.split('\n').find(l => l.includes('wifi'));
85 return line ? line.split(':')[0] : null;
86 } catch {
87 return null;
88 }
89}