offline-network-test.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # Round 190nx (issue #233 follow-up): proves that LYNX_OFFLINE=1 / --offline opens no network connection.
  3. # Usage: tests/offline-network-test.sh /path/to/LYNX [work folder]
  4. # Runs `LYNX --offlinetest` (the update check, going back, a crash-report upload, a live session start and join, the
  5. # relay's HTTP) inside a new network namespace (`unshare -rn`, no network at all) under strace, once with LYNX_OFFLINE=1 and once
  6. # with --offline, and counts the connect() calls to internet addresses (AF_INET / AF_INET6): there must be none. A control run
  7. # without offline mode (the same route, no flag) must make some, so the strace filter is proven to see them.
  8. # connect() to AF_UNIX (the display, the sound server) is not network access and is not counted.
  9. set -u
  10. LYNX="${1:?usage: offline-network-test.sh /path/to/LYNX [work folder]}"
  11. WORK="${2:-$(mktemp -d)}"
  12. mkdir -p "$WORK"
  13. STRACE="${STRACE:-strace}"
  14. command -v "$STRACE" >/dev/null || { echo "strace is needed (or set STRACE=/path/to/strace)"; exit 2; }
  15. unshare -rn true 2>/dev/null || { echo "unshare -rn is not allowed here"; exit 2; }
  16. fail=0
  17. run() { # name, env assignment (or ""), extra args
  18. local name="$1" envs="$2"; shift 2
  19. local profile="$WORK/$name-profile"
  20. mkdir -p "$profile"
  21. env $envs LYNX_TEST_MODE=1 LYNX_PROFILE_DIR="$profile" XDG_CONFIG_HOME="$profile/cfg" \
  22. unshare -rn "$STRACE" -f -qq -e trace=connect -o "$WORK/$name.strace" \
  23. "$LYNX" --offlinetest "$WORK/$name.txt" "$@" >"$WORK/$name.out" 2>&1
  24. echo "$? $(grep -cE 'connect\(.*sa_family=AF_INET6?,' "$WORK/$name.strace" 2>/dev/null)"
  25. }
  26. read -r rc1 n1 <<<"$(run env LYNX_OFFLINE=1)"
  27. read -r rc2 n2 <<<"$(run flag "" --offline)"
  28. read -r rc3 n3 <<<"$(run control "")"
  29. echo "control (no offline mode): internet connect() calls: $n3 (must be above 0: the filter sees them)"
  30. [ "${n3:-0}" -gt 0 ] || { echo "FAIL: the control run made no connect() call, the check proves nothing"; fail=1; }
  31. echo "LYNX_OFFLINE=1: exit $rc1, internet connect() calls: $n1"
  32. echo "--offline : exit $rc2, internet connect() calls: $n2"
  33. [ "$rc1" = 0 ] && [ "$n1" = 0 ] || { echo "FAIL (LYNX_OFFLINE=1)"; fail=1; }
  34. [ "$rc2" = 0 ] && [ "$n2" = 0 ] || { echo "FAIL (--offline)"; fail=1; }
  35. grep -q "RESULT OK" "$WORK/env.txt" 2>/dev/null || { echo "FAIL: no RESULT OK in env.txt"; fail=1; }
  36. grep -q "RESULT OK" "$WORK/flag.txt" 2>/dev/null || { echo "FAIL: no RESULT OK in flag.txt"; fail=1; }
  37. [ $fail = 0 ] && echo "PASS: zero network connect() calls with LYNX_OFFLINE=1 and with --offline"
  38. exit $fail