diff options
| author | Your Name <you@example.com> | 2026-05-17 17:18:43 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 17:18:43 +0530 |
| commit | 8071741815f0b0938701e80a63e80b0ec94b2778 (patch) | |
| tree | 2a1511480e0b58f4efb144aa9d10c9fba5eed034 /tests/integration | |
| parent | 0c2c67b463d6a90aaa0bb69bf3c91dba1d9ec3ec (diff) | |
refactor: reorganize test suite, add integration tests for NAT filter
- Move integration tests (api, network, phase2, smoke) to tests/integration/
- Move Playwright specs (captive-portal, interop-happy-path) to tests/e2e/
- Move playwright.config.mjs to tests/e2e/
- Fix hardcoded IP fallbacks: 192.168.4.1 → 10.192.45.1
- Add test-reset-auth.mjs: reset→pay→allow→revoke→block cycle
- Add test-session-expiry.mjs: pay→wait 65s→verify blocked (slow test)
- Add test-dns-firewall.mjs: DNS hijack/forward + per-client NAT filter
- Update Makefile with test-unit, test-integration, test-e2e, test-all targets
- Update package.json scripts for new paths
- Fix Playwright video: retain-on-failure instead of always-on
- Update AGENTS.md: per-client NAT filter description
- Update CHECKLIST.md: mark completed items, add Board B identity
- Board B nsec: 9af47906... → SSID TollGate-b96d80, AP IP 10.185.47.1
- 186 unit tests passing
Diffstat (limited to 'tests/integration')
| -rw-r--r-- | tests/integration/api.mjs | 79 | ||||
| -rw-r--r-- | tests/integration/network.mjs | 66 | ||||
| -rw-r--r-- | tests/integration/phase2.mjs | 151 | ||||
| -rw-r--r-- | tests/integration/smoke.mjs | 52 | ||||
| -rw-r--r-- | tests/integration/test-dns-firewall.mjs | 123 | ||||
| -rw-r--r-- | tests/integration/test-reset-auth.mjs | 101 | ||||
| -rw-r--r-- | tests/integration/test-session-expiry.mjs | 103 |
7 files changed, 675 insertions, 0 deletions
diff --git a/tests/integration/api.mjs b/tests/integration/api.mjs new file mode 100644 index 0000000..5218d7b --- /dev/null +++ b/tests/integration/api.mjs | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | import { curl, curlBody, getPortalIP, canPing, canResolve, dnsResolvesToSelf } from './helpers/network.mjs'; | ||
| 2 | |||
| 3 | const IP = getPortalIP(); | ||
| 4 | let passed = 0, failed = 0; | ||
| 5 | |||
| 6 | function assert(condition, test) { | ||
| 7 | if (condition) { console.log(` ✓ ${test}`); passed++; } | ||
| 8 | else { console.log(` ✗ ${test}`); failed++; } | ||
| 9 | } | ||
| 10 | |||
| 11 | async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 12 | |||
| 13 | console.log(`\n=== API Tests (target: ${IP}) ===\n`); | ||
| 14 | |||
| 15 | // Test 3: Captive portal serves HTML | ||
| 16 | console.log('Test 3: GET / returns portal HTML'); | ||
| 17 | const body3 = curlBody(`http://${IP}/`); | ||
| 18 | assert(body3 && body3.includes('TollGate'), 'Portal HTML contains "TollGate"'); | ||
| 19 | assert(body3 && body3.includes('Grant Free Access'), 'Portal has Grant Access button'); | ||
| 20 | |||
| 21 | // Test 4: Captive detection URIs | ||
| 22 | console.log('\nTest 4: Captive detection URIs'); | ||
| 23 | for (const uri of ['/generate_204', '/hotspot-detect.html', '/canonical.html', '/success.txt', '/ncsi.txt', '/connecttest.txt', '/wpad.dat', '/redirect']) { | ||
| 24 | const code = curl(`http://${IP}${uri}`); | ||
| 25 | assert(code === '200', `${uri} → 200`); | ||
| 26 | } | ||
| 27 | |||
| 28 | // Test 7: /whoami returns MAC | ||
| 29 | console.log('\nTest 7: GET /whoami'); | ||
| 30 | const body7 = curlBody(`http://${IP}/whoami`); | ||
| 31 | assert(body7 && body7.startsWith('mac='), '/whoami returns mac=...'); | ||
| 32 | |||
| 33 | // Test 8: /usage returns no session | ||
| 34 | console.log('\nTest 8: GET /usage'); | ||
| 35 | const body8 = curlBody(`http://${IP}/usage`); | ||
| 36 | assert(body8 && body8.includes('-1/-1'), '/usage returns -1/-1 before auth'); | ||
| 37 | |||
| 38 | // Test 5: DNS hijack before auth | ||
| 39 | console.log('\nTest 5: DNS hijack before auth'); | ||
| 40 | assert(dnsResolvesToSelf('google.com'), 'DNS resolves google.com to AP IP'); | ||
| 41 | |||
| 42 | // Test 6: No internet before auth | ||
| 43 | console.log('\nTest 6: No internet before auth'); | ||
| 44 | assert(!canPing('8.8.8.8', 1), 'ping 8.8.8.8 fails before auth'); | ||
| 45 | |||
| 46 | // Test 9: Grant access | ||
| 47 | console.log('\nTest 9: GET /grant_access'); | ||
| 48 | const body9 = curlBody(`http://${IP}/grant_access`); | ||
| 49 | assert(body9 && body9.includes('"granted"'), 'Grant access returns {"status":"granted"}'); | ||
| 50 | |||
| 51 | await sleep(2000); | ||
| 52 | |||
| 53 | // Test 10: DNS forward after auth | ||
| 54 | console.log('\nTest 10: DNS forward after auth'); | ||
| 55 | assert(canResolve('google.com'), 'DNS resolves normally after auth'); | ||
| 56 | |||
| 57 | // Test 11: Internet after auth | ||
| 58 | console.log('\nTest 11: Internet after auth'); | ||
| 59 | assert(canPing('8.8.8.8'), 'ping 8.8.8.8 succeeds after auth'); | ||
| 60 | |||
| 61 | // Test 12: HTTP browsing works | ||
| 62 | console.log('\nTest 12: HTTP browsing'); | ||
| 63 | const body12 = curlBody('http://example.com/'); | ||
| 64 | assert(body12 && (body12.includes('Example Domain') || body12.includes('example')), 'HTTP page loads'); | ||
| 65 | |||
| 66 | // Test 13: Reset auth | ||
| 67 | console.log('\nTest 13: GET /reset_authentication'); | ||
| 68 | const body13 = curlBody(`http://${IP}/reset_authentication`); | ||
| 69 | assert(body13 && body13.includes('"reset"'), 'Reset returns {"status":"reset"}'); | ||
| 70 | |||
| 71 | await sleep(2000); | ||
| 72 | |||
| 73 | // Test 14: Internet blocked after reset | ||
| 74 | console.log('\nTest 14: Internet blocked after reset'); | ||
| 75 | assert(!canPing('8.8.8.8', 1), 'ping fails after auth reset'); | ||
| 76 | |||
| 77 | // Summary | ||
| 78 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 79 | process.exit(failed > 0 ? 1 : 0); | ||
diff --git a/tests/integration/network.mjs b/tests/integration/network.mjs new file mode 100644 index 0000000..dcd7a9a --- /dev/null +++ b/tests/integration/network.mjs | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | let passed = 0, failed = 0; | ||
| 5 | |||
| 6 | function assert(condition, test) { | ||
| 7 | if (condition) { console.log(` ✓ ${test}`); passed++; } | ||
| 8 | else { console.log(` ✗ ${test}`); failed++; } | ||
| 9 | } | ||
| 10 | |||
| 11 | function run(cmd) { | ||
| 12 | try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); } | ||
| 13 | catch { return null; } | ||
| 14 | } | ||
| 15 | |||
| 16 | console.log(`\n=== Network Tests (target: ${IP}) ===\n`); | ||
| 17 | |||
| 18 | // Test 1: AP visible in scan | ||
| 19 | console.log('Test 1: AP visible in scan'); | ||
| 20 | const scan = run('nmcli -t -f SSID dev wifi list 2>/dev/null'); | ||
| 21 | assert(scan && scan.includes('TollGate'), 'TollGate SSID visible in WiFi scan'); | ||
| 22 | |||
| 23 | // Test 2: DHCP lease | ||
| 24 | console.log('\nTest 2: DHCP lease / connectivity'); | ||
| 25 | const ip_show = run(`ip addr show | grep "inet ${IP.split('.').slice(0,3).join('.')}"`); | ||
| 26 | assert(ip_show !== null, `Has IP in ${IP.split('.').slice(0,3).join('.')}.* subnet`); | ||
| 27 | |||
| 28 | // Test 5: DNS hijack | ||
| 29 | console.log('\nTest 5: DNS hijack before auth'); | ||
| 30 | const ns1 = run(`nslookup random-test.example.com ${IP} 2>/dev/null`); | ||
| 31 | assert(ns1 && ns1.includes(IP), 'DNS resolves arbitrary domain to AP IP'); | ||
| 32 | |||
| 33 | // Test 6: No internet | ||
| 34 | console.log('\nTest 6: No internet before auth'); | ||
| 35 | const ping1 = run('ping -c 1 -W 3 1.1.1.1 2>/dev/null'); | ||
| 36 | assert(ping1 === null || ping1.includes('100% packet loss'), 'Internet blocked before auth'); | ||
| 37 | |||
| 38 | // Grant access for further tests | ||
| 39 | console.log('\nGranting access...'); | ||
| 40 | run(`curl -s http://${IP}/grant_access`); | ||
| 41 | |||
| 42 | import { execSync as exec } from 'child_process'; | ||
| 43 | await new Promise(r => setTimeout(r, 2000)); | ||
| 44 | |||
| 45 | // Test 10: DNS forward | ||
| 46 | console.log('Test 10: DNS forward after auth'); | ||
| 47 | const ns2 = run(`nslookup google.com ${IP} 2>/dev/null`); | ||
| 48 | assert(ns2 && !ns2.includes(IP) && ns2.includes('Address'), 'DNS resolves to real IPs'); | ||
| 49 | |||
| 50 | // Test 11: Internet | ||
| 51 | console.log('\nTest 11: Internet after auth'); | ||
| 52 | const ping2 = run('ping -c 2 -W 3 8.8.8.8'); | ||
| 53 | assert(ping2 && !ping2.includes('100% packet loss'), 'ping succeeds after auth'); | ||
| 54 | |||
| 55 | // Reset | ||
| 56 | console.log('\nResetting auth...'); | ||
| 57 | run(`curl -s http://${IP}/reset_authentication`); | ||
| 58 | await new Promise(r => setTimeout(r, 2000)); | ||
| 59 | |||
| 60 | // Test 14 | ||
| 61 | console.log('Test 14: Internet blocked after reset'); | ||
| 62 | const ping3 = run('ping -c 1 -W 3 8.8.8.8 2>/dev/null'); | ||
| 63 | assert(ping3 === null || ping3.includes('100% packet loss'), 'Internet blocked after reset'); | ||
| 64 | |||
| 65 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 66 | process.exit(failed > 0 ? 1 : 0); | ||
diff --git a/tests/integration/phase2.mjs b/tests/integration/phase2.mjs new file mode 100644 index 0000000..9eaa7d7 --- /dev/null +++ b/tests/integration/phase2.mjs | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | const API = `http://${IP}:2121`; | ||
| 5 | let passed = 0, failed = 0; | ||
| 6 | |||
| 7 | function assert(condition, test) { | ||
| 8 | if (condition) { console.log(` ✓ ${test}`); passed++; } | ||
| 9 | else { console.log(` ✗ ${test}`); failed++; } | ||
| 10 | } | ||
| 11 | |||
| 12 | function curlBody(url, options = {}) { | ||
| 13 | const cmd = options.method | ||
| 14 | ? `curl -s --connect-timeout 5 --max-time 10 -X ${options.method} ${options.data ? `-d '${options.data.replace(/'/g, "'\\''")}'` : ''} "${url}"` | ||
| 15 | : `curl -s --connect-timeout 5 --max-time 10 "${url}"`; | ||
| 16 | try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); } | ||
| 17 | catch { return null; } | ||
| 18 | } | ||
| 19 | |||
| 20 | function curlStatus(url, options = {}) { | ||
| 21 | const cmd = `curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 --max-time 10 ${options.method ? `-X ${options.method}` : ''} ${options.data ? `-d '${options.data.replace(/'/g, "'\\''")}'` : ''} "${url}"`; | ||
| 22 | try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }).trim(); } | ||
| 23 | catch { return null; } | ||
| 24 | } | ||
| 25 | |||
| 26 | async function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 27 | |||
| 28 | console.log(`\n=== Phase 2 Tests (target: ${API}) ===\n`); | ||
| 29 | |||
| 30 | // Test 15: Advertisement valid | ||
| 31 | console.log('Test 15: GET :2121/ returns kind=10021 advertisement'); | ||
| 32 | const body15 = curlBody(`${API}/`); | ||
| 33 | const json15 = body15 ? JSON.parse(body15) : null; | ||
| 34 | assert(json15 && json15.kind === 10021, 'kind=10021'); | ||
| 35 | assert(json15 && json15.tags && json15.tags.some(t => t[0] === 'price_per_step'), 'Has price_per_step tag'); | ||
| 36 | assert(json15 && json15.tags && json15.tags.some(t => t[0] === 'step_size'), 'Has step_size tag'); | ||
| 37 | assert(json15 && json15.tags && json15.tags.some(t => t[0] === 'metric'), 'Has metric tag'); | ||
| 38 | |||
| 39 | // Test 19: Invalid token | ||
| 40 | console.log('\nTest 19: POST :2121/ with invalid token'); | ||
| 41 | const body19 = curlBody(`${API}/`, { method: 'POST', data: 'garbage_not_a_token' }); | ||
| 42 | const json19 = body19 ? JSON.parse(body19) : null; | ||
| 43 | assert(json19 && json19.kind === 21023, 'Returns kind=21023 notice'); | ||
| 44 | assert(json19 && json19.tags && json19.tags.some(t => t[0] === 'code'), 'Has error code tag'); | ||
| 45 | const status19 = curlStatus(`${API}/`, { method: 'POST', data: 'garbage_not_a_token' }); | ||
| 46 | assert(status19 === '400', 'Returns HTTP 400'); | ||
| 47 | |||
| 48 | // Test 21: Wrong mint (token from wrong mint) | ||
| 49 | console.log('\nTest 21: POST :2121/ with wrong mint token'); | ||
| 50 | const wrongMintToken = 'cashuA' + Buffer.from(JSON.stringify({ | ||
| 51 | token: [{ mint: 'https://wrong.mint.example.com', proofs: [{ amount: 21, secret: 'test', id: '00'.repeat(8), C: '02'.repeat(33) }] }] | ||
| 52 | })).toString('base64url'); | ||
| 53 | const body21 = curlBody(`${API}/`, { method: 'POST', data: wrongMintToken }); | ||
| 54 | const json21 = body21 ? JSON.parse(body21) : null; | ||
| 55 | assert(json21 && json21.kind === 21023, 'Returns kind=21023'); | ||
| 56 | const codeTag21 = json21 && json21.tags && json21.tags.find(t => t[0] === 'code'); | ||
| 57 | assert(codeTag21 && codeTag21[1] === 'payment-error-mint-not-accepted', 'Error code: mint-not-accepted'); | ||
| 58 | |||
| 59 | // Test valid token (if provided) | ||
| 60 | const TEST_TOKEN = process.env.TEST_TOKEN; | ||
| 61 | if (TEST_TOKEN) { | ||
| 62 | console.log('\nTest 16: POST :2121/ with valid token'); | ||
| 63 | const body16 = curlBody(`${API}/`, { method: 'POST', data: TEST_TOKEN }); | ||
| 64 | const json16 = body16 ? JSON.parse(body16) : null; | ||
| 65 | assert(json16 && json16.kind === 1022, 'Returns kind=1022 session'); | ||
| 66 | assert(json16 && json16.tags && json16.tags.some(t => t[0] === 'allotment'), 'Has allotment tag'); | ||
| 67 | |||
| 68 | // Test 17: Usage tracking | ||
| 69 | console.log('\nTest 17: GET :2121/usage after payment'); | ||
| 70 | const body17 = curlBody(`${API}/usage`); | ||
| 71 | assert(body17 && !body17.includes('-1/-1'), 'Returns active usage'); | ||
| 72 | |||
| 73 | // Test 18: Internet after payment | ||
| 74 | console.log('\nTest 18: Internet works after payment'); | ||
| 75 | await sleep(1500); | ||
| 76 | const sudoPw = process.env.SUDO_PW || 'c03rad0r123'; | ||
| 77 | try { | ||
| 78 | execSync(`echo '${sudoPw}' | sudo -S ip route add default via ${IP} dev wlp59s0 metric 50 2>/dev/null`, { encoding: 'utf8', timeout: 5000 }); | ||
| 79 | } catch {} | ||
| 80 | let pingOk = false; | ||
| 81 | try { | ||
| 82 | const ping18 = execSync('ping -c 3 -W 3 8.8.8.8', { encoding: 'utf8', timeout: 15000 }); | ||
| 83 | pingOk = ping18 && !ping18.includes('100% packet loss'); | ||
| 84 | } catch { | ||
| 85 | pingOk = false; | ||
| 86 | } | ||
| 87 | assert(pingOk, 'Internet works'); | ||
| 88 | |||
| 89 | // Test 20: Spent token | ||
| 90 | console.log('\nTest 20: Reuse token (should fail)'); | ||
| 91 | const body20 = curlBody(`${API}/`, { method: 'POST', data: TEST_TOKEN }); | ||
| 92 | const json20 = body20 ? JSON.parse(body20) : null; | ||
| 93 | assert(json20 && json20.kind === 21023, 'Returns kind=21023 for spent token'); | ||
| 94 | |||
| 95 | // Test 22: Session expiry | ||
| 96 | console.log('\nTest 22: Session expiry (waiting 65s for allotment to expire)...'); | ||
| 97 | try { | ||
| 98 | execSync(`echo '${sudoPw}' | sudo -S ip route add default via ${IP} dev wlp59s0 metric 50 2>/dev/null`, { encoding: 'utf8', timeout: 5000 }); | ||
| 99 | } catch {} | ||
| 100 | await sleep(65000); | ||
| 101 | let expiredPingOk = true; | ||
| 102 | try { | ||
| 103 | const ping22 = execSync('ping -c 2 -W 2 8.8.8.8', { encoding: 'utf8', timeout: 10000 }); | ||
| 104 | expiredPingOk = !ping22.includes('100% packet loss'); | ||
| 105 | } catch { | ||
| 106 | expiredPingOk = false; | ||
| 107 | } | ||
| 108 | assert(!expiredPingOk, 'Internet blocked after session expiry'); | ||
| 109 | const body22 = curlBody(`${API}/usage`); | ||
| 110 | assert(body22 && body22.includes('-1/-1'), 'Usage returns -1/-1 after expiry'); | ||
| 111 | |||
| 112 | // Test 23: Session renewal | ||
| 113 | const TEST_TOKEN2 = process.env.TEST_TOKEN2; | ||
| 114 | if (TEST_TOKEN2) { | ||
| 115 | console.log('\nTest 23: Session renewal with second token'); | ||
| 116 | const body23 = curlBody(`${API}/`, { method: 'POST', data: TEST_TOKEN2 }); | ||
| 117 | const json23 = body23 ? JSON.parse(body23) : null; | ||
| 118 | assert(json23 && json23.kind === 1022, 'Returns kind=1022 for renewal'); | ||
| 119 | await sleep(1500); | ||
| 120 | let renewPingOk = false; | ||
| 121 | try { | ||
| 122 | const ping23 = execSync('ping -c 2 -W 2 8.8.8.8', { encoding: 'utf8', timeout: 10000 }); | ||
| 123 | renewPingOk = !ping23.includes('100% packet loss'); | ||
| 124 | } catch { | ||
| 125 | renewPingOk = false; | ||
| 126 | } | ||
| 127 | assert(renewPingOk, 'Internet works after renewal'); | ||
| 128 | } else { | ||
| 129 | console.log('\n ⚠ Skipping test 23: Set TEST_TOKEN2 env var for renewal test'); | ||
| 130 | } | ||
| 131 | try { | ||
| 132 | execSync(`echo '${sudoPw}' | sudo -S ip route del default via ${IP} dev wlp59s0 metric 50 2>/dev/null`, { encoding: 'utf8', timeout: 5000 }); | ||
| 133 | } catch {} | ||
| 134 | } else { | ||
| 135 | console.log('\n ⚠ Skipping tests 16-20: Set TEST_TOKEN env var with a valid Cashu token'); | ||
| 136 | } | ||
| 137 | |||
| 138 | // Test: whoami on :2121 | ||
| 139 | console.log('\nTest: GET :2121/whoami'); | ||
| 140 | const bodyWhoami = curlBody(`${API}/whoami`); | ||
| 141 | assert(bodyWhoami && bodyWhoami.includes('mac='), '/whoami returns mac=...'); | ||
| 142 | |||
| 143 | // Test: Portal has payment form | ||
| 144 | console.log('\nTest: Portal has payment form'); | ||
| 145 | const bodyPortal = curlBody(`http://${IP}/`); | ||
| 146 | assert(bodyPortal && bodyPortal.includes('cashuA'), 'Portal has Cashu token input'); | ||
| 147 | assert(bodyPortal && bodyPortal.includes('Pay & Connect') || bodyPortal && bodyPortal.includes('Pay'), 'Portal has Pay button'); | ||
| 148 | |||
| 149 | // Summary | ||
| 150 | console.log(`\n=== Phase 2 Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 151 | process.exit(failed > 0 ? 1 : 0); | ||
diff --git a/tests/integration/smoke.mjs b/tests/integration/smoke.mjs new file mode 100644 index 0000000..f89eeac --- /dev/null +++ b/tests/integration/smoke.mjs | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const PORT = process.argv[2] || '/dev/ttyACM0'; | ||
| 4 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 5 | const SSID = process.env.AP_SSID || 'TollGate'; | ||
| 6 | |||
| 7 | console.log(`\n=== Smoke Test (30s) ===`); | ||
| 8 | console.log(`Port: ${PORT}, Portal IP: ${IP}, SSID: ${SSID}\n`); | ||
| 9 | |||
| 10 | let passed = 0, failed = 0; | ||
| 11 | function assert(cond, msg) { | ||
| 12 | if (cond) { console.log(` ✓ ${msg}`); passed++; } | ||
| 13 | else { console.log(` ✗ ${msg}`); failed++; } | ||
| 14 | } | ||
| 15 | |||
| 16 | function run(cmd) { | ||
| 17 | try { return execSync(cmd, { encoding: 'utf8', timeout: 10000 }); } | ||
| 18 | catch { return null; } | ||
| 19 | } | ||
| 20 | |||
| 21 | // 1. Check AP visible | ||
| 22 | const scan = run('nmcli -t -f SSID dev wifi list 2>/dev/null'); | ||
| 23 | assert(scan && scan.includes(SSID), `SSID "${SSID}" visible`); | ||
| 24 | |||
| 25 | // 2. Check we can reach portal | ||
| 26 | const portal = run(`curl -s --connect-timeout 5 http://${IP}/`); | ||
| 27 | assert(portal && portal.includes('TollGate'), 'Portal HTML loads'); | ||
| 28 | |||
| 29 | // 3. Grant access | ||
| 30 | const grant = run(`curl -s http://${IP}/grant_access`); | ||
| 31 | assert(grant && grant.includes('granted'), 'Grant access works'); | ||
| 32 | |||
| 33 | // Wait for DNS | ||
| 34 | const sleep = ms => new Promise(r => setTimeout(r, ms)); | ||
| 35 | await sleep(2000); | ||
| 36 | |||
| 37 | // 4. Internet works | ||
| 38 | const ping = run('ping -c 1 -W 3 -I wlp59s0 1.1.1.1 2>/dev/null'); | ||
| 39 | assert(ping && !ping.includes('100% packet loss'), 'Internet works after grant'); | ||
| 40 | |||
| 41 | // 5. Reset | ||
| 42 | const reset = run(`curl -s http://${IP}/reset_authentication`); | ||
| 43 | assert(reset && reset.includes('reset'), 'Reset auth works'); | ||
| 44 | |||
| 45 | await sleep(2000); | ||
| 46 | |||
| 47 | // 6. Internet blocked | ||
| 48 | const ping2 = run('ping -c 1 -W 3 -I wlp59s0 1.1.1.1 2>/dev/null'); | ||
| 49 | assert(!ping2 || ping2.includes('100% packet loss'), 'Internet blocked after reset'); | ||
| 50 | |||
| 51 | console.log(`\n=== Smoke: ${passed} passed, ${failed} failed ===\n`); | ||
| 52 | process.exit(failed > 0 ? 1 : 0); | ||
diff --git a/tests/integration/test-dns-firewall.mjs b/tests/integration/test-dns-firewall.mjs new file mode 100644 index 0000000..b69b524 --- /dev/null +++ b/tests/integration/test-dns-firewall.mjs | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | const API = `http://${IP}:2121`; | ||
| 5 | let passed = 0, failed = 0; | ||
| 6 | |||
| 7 | function assert(cond, msg) { | ||
| 8 | if (cond) { console.log(` ✓ ${msg}`); passed++; } | ||
| 9 | else { console.log(` ✗ ${msg}`); failed++; } | ||
| 10 | } | ||
| 11 | |||
| 12 | function run(cmd) { | ||
| 13 | try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); } | ||
| 14 | catch { return null; } | ||
| 15 | } | ||
| 16 | |||
| 17 | function runJson(cmd) { | ||
| 18 | const out = run(cmd); | ||
| 19 | try { return out ? JSON.parse(out) : null; } | ||
| 20 | catch { return null; } | ||
| 21 | } | ||
| 22 | |||
| 23 | function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 24 | |||
| 25 | function mintToken(amount = 21) { | ||
| 26 | run('cashu -h https://testnut.cashu.space invoice ' + amount + ' 2>&1'); | ||
| 27 | const out = run('cashu -h https://testnut.cashu.space send --legacy ' + amount + ' 2>&1'); | ||
| 28 | const match = out && out.match(/cashuA[a-zA-Z0-9_-]+/); | ||
| 29 | return match ? match[0] : null; | ||
| 30 | } | ||
| 31 | |||
| 32 | function dnsResolves(domain, server) { | ||
| 33 | const result = run(`nslookup -timeout=3 ${domain} ${server} 2>&1`); | ||
| 34 | return result && result.includes('Address') && !result.includes('NXDOMAIN'); | ||
| 35 | } | ||
| 36 | |||
| 37 | function dnsResolvesToSelf(domain) { | ||
| 38 | try { | ||
| 39 | const result = run(`nslookup ${domain} ${IP} 2>&1`); | ||
| 40 | return result && result.includes(IP); | ||
| 41 | } catch { | ||
| 42 | return false; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | function canPing(host = '8.8.8.8') { | ||
| 47 | const result = run(`ping -c 1 -W 2 -I wlp59s0 ${host} 2>/dev/null`); | ||
| 48 | return result && !result.includes('100% packet loss'); | ||
| 49 | } | ||
| 50 | |||
| 51 | console.log(`\n=== DNS + Firewall Integration Test (target: ${IP}) ===\n`); | ||
| 52 | |||
| 53 | console.log('--- Part 1: Before Authentication ---\n'); | ||
| 54 | |||
| 55 | console.log('1. DNS hijack: resolves to ESP32 AP IP'); | ||
| 56 | assert(dnsResolvesToSelf('google.com'), 'google.com resolves to AP IP'); | ||
| 57 | assert(dnsResolvesToSelf('random-test.example.com'), 'random domain resolves to AP IP'); | ||
| 58 | |||
| 59 | console.log('\n2. DNS hijack: upstream DNS not reachable'); | ||
| 60 | const upstreamResolve = run(`nslookup -timeout=3 google.com 8.8.8.8 2>&1`); | ||
| 61 | assert(!upstreamResolve || upstreamResolve.includes('connection timed out') || upstreamResolve.includes('no servers'), 'Upstream DNS unreachable before auth'); | ||
| 62 | |||
| 63 | console.log('\n3. Per-client NAT filter: ping blocked'); | ||
| 64 | assert(!canPing(), 'Ping to 8.8.8.8 blocked by NAT filter'); | ||
| 65 | |||
| 66 | console.log('\n4. Per-client NAT filter: HTTP blocked'); | ||
| 67 | const httpBefore = run(`curl -s --connect-timeout 5 -m 5 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`); | ||
| 68 | assert(!httpBefore || httpBefore.length === 0, 'HTTP blocked before auth'); | ||
| 69 | |||
| 70 | console.log('\n5. Captive portal and API still accessible'); | ||
| 71 | const portal = run(`curl -s --connect-timeout 5 http://${IP}/`); | ||
| 72 | assert(portal && portal.includes('TollGate'), 'Portal HTML accessible'); | ||
| 73 | const apiDisc = runJson(`curl -s --connect-timeout 5 ${API}/`); | ||
| 74 | assert(apiDisc && apiDisc.kind === 10021, 'API discovery accessible'); | ||
| 75 | |||
| 76 | console.log('\n--- Part 2: After Authentication ---\n'); | ||
| 77 | |||
| 78 | console.log('6. Reset + Pay'); | ||
| 79 | run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 80 | await sleep(1000); | ||
| 81 | |||
| 82 | const token = mintToken(21); | ||
| 83 | assert(token !== null, 'Token generated'); | ||
| 84 | if (token) { | ||
| 85 | const payResult = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token}' -H "Content-Type: application/cashu" ${API}/`); | ||
| 86 | assert(payResult && payResult.kind === 1022, 'Payment accepted'); | ||
| 87 | } | ||
| 88 | |||
| 89 | await sleep(1000); | ||
| 90 | |||
| 91 | console.log('\n7. DNS now forwards to upstream'); | ||
| 92 | assert(dnsResolveWorks('google.com'), 'DNS resolves to real IPs after auth'); | ||
| 93 | |||
| 94 | console.log('\n8. Per-client NAT filter: ping allowed'); | ||
| 95 | assert(canPing(), 'Ping to 8.8.8.8 allowed after auth'); | ||
| 96 | |||
| 97 | console.log('\n9. Per-client NAT filter: HTTP allowed'); | ||
| 98 | const httpAfter = run(`curl -s --connect-timeout 10 -m 10 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`); | ||
| 99 | assert(httpAfter && httpAfter.length > 0, 'HTTP allowed after auth'); | ||
| 100 | |||
| 101 | console.log('\n--- Part 3: After Revocation ---\n'); | ||
| 102 | |||
| 103 | console.log('10. Reset auth'); | ||
| 104 | run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 105 | await sleep(1000); | ||
| 106 | |||
| 107 | console.log('\n11. DNS goes back to hijack'); | ||
| 108 | assert(dnsResolvesToSelf('google.com'), 'DNS hijack restored after revoke'); | ||
| 109 | |||
| 110 | console.log('\n12. Per-client NAT filter: ping blocked again'); | ||
| 111 | assert(!canPing(), 'Ping blocked after revoke'); | ||
| 112 | |||
| 113 | console.log('\n13. Per-client NAT filter: HTTP blocked again'); | ||
| 114 | const httpRevoke = run(`curl -s --connect-timeout 5 -m 5 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`); | ||
| 115 | assert(!httpRevoke || httpRevoke.length === 0, 'HTTP blocked after revoke'); | ||
| 116 | |||
| 117 | function dnsResolveWorks(domain) { | ||
| 118 | const result = run(`nslookup -timeout=3 ${domain} 2>&1`); | ||
| 119 | return result && result.includes('Address') && !result.includes(IP) && !result.includes('NXDOMAIN'); | ||
| 120 | } | ||
| 121 | |||
| 122 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 123 | process.exit(failed > 0 ? 1 : 0); | ||
diff --git a/tests/integration/test-reset-auth.mjs b/tests/integration/test-reset-auth.mjs new file mode 100644 index 0000000..279b2f9 --- /dev/null +++ b/tests/integration/test-reset-auth.mjs | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | const API = `http://${IP}:2121`; | ||
| 5 | const SUDO_PW = process.env.SUDO_PW || 'c03rad0r123'; | ||
| 6 | let passed = 0, failed = 0; | ||
| 7 | |||
| 8 | function assert(cond, msg) { | ||
| 9 | if (cond) { console.log(` ✓ ${msg}`); passed++; } | ||
| 10 | else { console.log(` ✗ ${msg}`); failed++; } | ||
| 11 | } | ||
| 12 | |||
| 13 | function run(cmd) { | ||
| 14 | try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); } | ||
| 15 | catch { return null; } | ||
| 16 | } | ||
| 17 | |||
| 18 | function runJson(cmd) { | ||
| 19 | const out = run(cmd); | ||
| 20 | try { return out ? JSON.parse(out) : null; } | ||
| 21 | catch { return null; } | ||
| 22 | } | ||
| 23 | |||
| 24 | function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 25 | |||
| 26 | function mintToken(amount = 21) { | ||
| 27 | run('cashu -h https://testnut.cashu.space invoice ' + amount + ' 2>&1'); | ||
| 28 | const out = run('cashu -h https://testnut.cashu.space send --legacy ' + amount + ' 2>&1'); | ||
| 29 | const match = out && out.match(/cashuA[a-zA-Z0-9_-]+/); | ||
| 30 | return match ? match[0] : null; | ||
| 31 | } | ||
| 32 | |||
| 33 | function canPing(host = '8.8.8.8') { | ||
| 34 | const result = run(`ping -c 1 -W 2 -I wlp59s0 ${host} 2>/dev/null`); | ||
| 35 | return result && !result.includes('100% packet loss'); | ||
| 36 | } | ||
| 37 | |||
| 38 | console.log(`\n=== Reset Auth Integration Test (target: ${IP}) ===\n`); | ||
| 39 | |||
| 40 | console.log('1. Reset auth to clear state'); | ||
| 41 | const reset1 = run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 42 | assert(reset1 && reset1.includes('reset'), 'Reset returns {"status":"reset"}'); | ||
| 43 | |||
| 44 | await sleep(1000); | ||
| 45 | |||
| 46 | console.log('\n2. Verify no session'); | ||
| 47 | const usage1 = run(`curl -s --connect-timeout 10 ${API}/usage`); | ||
| 48 | assert(usage1 && usage1.includes('-1/-1'), 'Usage is -1/-1 before payment'); | ||
| 49 | |||
| 50 | console.log('\n3. Verify internet blocked'); | ||
| 51 | assert(!canPing(), 'Ping blocked before payment'); | ||
| 52 | |||
| 53 | console.log('\n4. Pay with valid token'); | ||
| 54 | const token = mintToken(21); | ||
| 55 | assert(token !== null, 'Token generated'); | ||
| 56 | if (token) { | ||
| 57 | const payResult = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token}' -H "Content-Type: application/cashu" ${API}/`); | ||
| 58 | assert(payResult && payResult.kind === 1022, 'Payment accepted (kind=1022)'); | ||
| 59 | const allotment = payResult && payResult.tags && payResult.tags.find(t => t[0] === 'allotment'); | ||
| 60 | assert(allotment && parseInt(allotment[1]) > 0, `Allotment: ${allotment ? allotment[1] : 'N/A'}ms`); | ||
| 61 | } | ||
| 62 | |||
| 63 | await sleep(1000); | ||
| 64 | |||
| 65 | console.log('\n5. Verify session active'); | ||
| 66 | const usage2 = run(`curl -s --connect-timeout 10 ${API}/usage`); | ||
| 67 | assert(usage2 && !usage2.includes('-1/-1'), `Usage: ${usage2}`); | ||
| 68 | |||
| 69 | console.log('\n6. Verify internet allowed'); | ||
| 70 | assert(canPing(), 'Ping works with active session'); | ||
| 71 | |||
| 72 | console.log('\n7. Reset auth while session active'); | ||
| 73 | const reset2 = run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 74 | assert(reset2 && reset2.includes('reset'), 'Reset returns {"status":"reset"}'); | ||
| 75 | |||
| 76 | await sleep(1000); | ||
| 77 | |||
| 78 | console.log('\n8. Verify session cleared'); | ||
| 79 | const usage3 = run(`curl -s --connect-timeout 10 ${API}/usage`); | ||
| 80 | assert(usage3 && usage3.includes('-1/-1'), 'Usage is -1/-1 after reset'); | ||
| 81 | |||
| 82 | console.log('\n9. Verify internet blocked again'); | ||
| 83 | assert(!canPing(), 'Ping blocked after reset'); | ||
| 84 | |||
| 85 | console.log('\n10. Pay again (new token)'); | ||
| 86 | const token2 = mintToken(21); | ||
| 87 | if (token2) { | ||
| 88 | const pay2 = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token2}' -H "Content-Type: application/cashu" ${API}/`); | ||
| 89 | assert(pay2 && pay2.kind === 1022, 'Second payment accepted'); | ||
| 90 | } | ||
| 91 | |||
| 92 | await sleep(1000); | ||
| 93 | |||
| 94 | console.log('\n11. Verify internet works again'); | ||
| 95 | assert(canPing(), 'Ping works with new session'); | ||
| 96 | |||
| 97 | console.log('\n12. Final reset'); | ||
| 98 | run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 99 | |||
| 100 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 101 | process.exit(failed > 0 ? 1 : 0); | ||
diff --git a/tests/integration/test-session-expiry.mjs b/tests/integration/test-session-expiry.mjs new file mode 100644 index 0000000..c8334ab --- /dev/null +++ b/tests/integration/test-session-expiry.mjs | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | const API = `http://${IP}:2121`; | ||
| 5 | let passed = 0, failed = 0; | ||
| 6 | |||
| 7 | function assert(cond, msg) { | ||
| 8 | if (cond) { console.log(` ✓ ${msg}`); passed++; } | ||
| 9 | else { console.log(` ✗ ${msg}`); failed++; } | ||
| 10 | } | ||
| 11 | |||
| 12 | function run(cmd) { | ||
| 13 | try { return execSync(cmd, { encoding: 'utf8', timeout: 15000 }); } | ||
| 14 | catch { return null; } | ||
| 15 | } | ||
| 16 | |||
| 17 | function runJson(cmd) { | ||
| 18 | const out = run(cmd); | ||
| 19 | try { return out ? JSON.parse(out) : null; } | ||
| 20 | catch { return null; } | ||
| 21 | } | ||
| 22 | |||
| 23 | function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 24 | |||
| 25 | function mintToken(amount = 21) { | ||
| 26 | run('cashu -h https://testnut.cashu.space invoice ' + amount + ' 2>&1'); | ||
| 27 | const out = run('cashu -h https://testnut.cashu.space send --legacy ' + amount + ' 2>&1'); | ||
| 28 | const match = out && out.match(/cashuA[a-zA-Z0-9_-]+/); | ||
| 29 | return match ? match[0] : null; | ||
| 30 | } | ||
| 31 | |||
| 32 | function canPing(host = '8.8.8.8') { | ||
| 33 | const result = run(`ping -c 1 -W 2 -I wlp59s0 ${host} 2>/dev/null`); | ||
| 34 | return result && !result.includes('100% packet loss'); | ||
| 35 | } | ||
| 36 | |||
| 37 | console.log(`\n=== Session Expiry Integration Test (target: ${IP}) ===`); | ||
| 38 | console.log(`NOTE: This test waits 65s for session expiry. Total runtime ~80s.\n`); | ||
| 39 | |||
| 40 | console.log('1. Reset auth'); | ||
| 41 | run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 42 | |||
| 43 | await sleep(1000); | ||
| 44 | |||
| 45 | console.log('\n2. Verify blocked before payment'); | ||
| 46 | assert(!canPing(), 'Ping blocked before payment'); | ||
| 47 | |||
| 48 | const usage0 = run(`curl -s --connect-timeout 10 ${API}/usage`); | ||
| 49 | assert(usage0 && usage0.includes('-1/-1'), 'Usage is -1/-1'); | ||
| 50 | |||
| 51 | console.log('\n3. Pay with valid token (21 sats = 60000ms)'); | ||
| 52 | const token = mintToken(21); | ||
| 53 | assert(token !== null, 'Token generated'); | ||
| 54 | if (token) { | ||
| 55 | const payResult = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token}' -H "Content-Type: application/cashu" ${API}/`); | ||
| 56 | assert(payResult && payResult.kind === 1022, 'Payment accepted'); | ||
| 57 | } | ||
| 58 | |||
| 59 | await sleep(1000); | ||
| 60 | |||
| 61 | console.log('\n4. Verify session active'); | ||
| 62 | const usage1 = run(`curl -s --connect-timeout 10 ${API}/usage`); | ||
| 63 | assert(usage1 && !usage1.includes('-1/-1'), `Usage: ${usage1}`); | ||
| 64 | |||
| 65 | console.log('\n5. Verify internet works'); | ||
| 66 | assert(canPing(), 'Ping works with active session'); | ||
| 67 | |||
| 68 | const httpResult = run(`curl -s --connect-timeout 10 -m 10 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`); | ||
| 69 | assert(httpResult && httpResult.length > 0, 'HTTP request reaches internet'); | ||
| 70 | |||
| 71 | console.log('\n6. Waiting 65s for session expiry (allotment=60000ms)...'); | ||
| 72 | for (let i = 65; i > 0; i -= 5) { | ||
| 73 | process.stdout.write(`\r ${i}s remaining...`); | ||
| 74 | await sleep(Math.min(5000, i * 1000)); | ||
| 75 | } | ||
| 76 | console.log('\r Session should be expired now. '); | ||
| 77 | |||
| 78 | console.log('\n7. Verify session expired'); | ||
| 79 | const usage2 = run(`curl -s --connect-timeout 10 ${API}/usage`); | ||
| 80 | assert(usage2 && usage2.includes('-1/-1'), `Usage after expiry: ${usage2}`); | ||
| 81 | |||
| 82 | console.log('\n8. Verify internet blocked after expiry'); | ||
| 83 | assert(!canPing(), 'Ping blocked after session expiry'); | ||
| 84 | |||
| 85 | const httpResult2 = run(`curl -s --connect-timeout 5 -m 5 --interface wlp59s0 http://1.1.1.1/ 2>/dev/null`); | ||
| 86 | assert(!httpResult2 || httpResult2.length === 0, 'HTTP blocked after expiry'); | ||
| 87 | |||
| 88 | console.log('\n9. Pay again to verify renewal works'); | ||
| 89 | const token2 = mintToken(21); | ||
| 90 | if (token2) { | ||
| 91 | const pay2 = runJson(`curl -s --connect-timeout 20 -X POST --data-binary '${token2}' -H "Content-Type: application/cashu" ${API}/`); | ||
| 92 | assert(pay2 && pay2.kind === 1022, 'Renewal payment accepted'); | ||
| 93 | } | ||
| 94 | |||
| 95 | await sleep(1000); | ||
| 96 | |||
| 97 | console.log('\n10. Verify internet works after renewal'); | ||
| 98 | assert(canPing(), 'Ping works after renewal'); | ||
| 99 | |||
| 100 | run(`curl -s --connect-timeout 10 http://${IP}/reset_authentication`); | ||
| 101 | |||
| 102 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 103 | process.exit(failed > 0 ? 1 : 0); | ||