upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/integration/helpers/network.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/helpers/network.mjs')
-rw-r--r--tests/integration/helpers/network.mjs67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/integration/helpers/network.mjs b/tests/integration/helpers/network.mjs
new file mode 100644
index 0000000..0d6013d
--- /dev/null
+++ b/tests/integration/helpers/network.mjs
@@ -0,0 +1,67 @@
1import { execSync } from 'child_process';
2
3const DEFAULT_IP = '10.192.45.1';
4const WIFI_IFACE = 'wlp59s0';
5
6export function getPortalIP() {
7 return process.env.TOLLGATE_IP || DEFAULT_IP;
8}
9
10export function curl(url, timeout = 30) {
11 try {
12 return execSync(
13 `curl -s -o /dev/null -w "%{http_code}" --connect-timeout ${timeout} --max-time ${timeout + 10} "${url}"`,
14 { encoding: 'utf8', timeout: (timeout + 10) * 1000 }
15 ).trim();
16 } catch {
17 return null;
18 }
19}
20
21export function curlBody(url, timeout = 30) {
22 try {
23 return execSync(
24 `curl -s --connect-timeout ${timeout} --max-time ${timeout + 10} "${url}"`,
25 { encoding: 'utf8', timeout: (timeout + 10) * 1000 }
26 );
27 } catch {
28 return null;
29 }
30}
31
32export function canPing(host = '8.8.8.8', count = 1) {
33 try {
34 const result = execSync(
35 `ping -c ${count} -W 3 -I ${WIFI_IFACE} ${host} 2>/dev/null`,
36 { encoding: 'utf8', timeout: 10000 }
37 );
38 return result && !result.includes('100% packet loss');
39 } catch {
40 return false;
41 }
42}
43
44export function canResolve(domain, timeout = 5) {
45 try {
46 const result = execSync(
47 `dig +short +timeout=${timeout} +tries=1 ${domain} 2>&1`,
48 { encoding: 'utf8', timeout: (timeout + 2) * 1000 }
49 ).trim();
50 return result.length > 0 && !result.includes('NXDOMAIN');
51 } catch {
52 return false;
53 }
54}
55
56export function dnsResolvesToSelf(domain) {
57 const ip = getPortalIP();
58 try {
59 const result = execSync(
60 `dig +short +timeout=5 ${domain} @{ip} 2>&1`,
61 { encoding: 'utf8', timeout: 10000 }
62 ).trim();
63 return result === ip;
64 } catch {
65 return false;
66 }
67}