122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import textwrap
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
WATCH_SCRIPT = ROOT / "scripts" / "linux" / "wireguard_watch.sh"
|
|
|
|
|
|
class WireGuardWatchTests(unittest.TestCase):
|
|
def test_stale_keepalive_peer_restarts_iface_dump_shape(self) -> None:
|
|
bash = shutil.which("bash")
|
|
if not bash:
|
|
self.skipTest("bash is required for wireguard_watch.sh tests")
|
|
probe = subprocess.run([bash, "--version"], text=True, capture_output=True, timeout=10)
|
|
if probe.returncode != 0:
|
|
self.skipTest("bash is not usable in this environment")
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
bindir = Path(tmp) / "bin"
|
|
bindir.mkdir()
|
|
calls = Path(tmp) / "calls.log"
|
|
last_restart = Path(tmp) / "live-wireguard-watch-wgeasy.last"
|
|
fake_now = 1_700_000_000
|
|
stale_handshake = fake_now - 900
|
|
|
|
(bindir / "date").write_text(
|
|
textwrap.dedent(
|
|
f"""\
|
|
#!/usr/bin/env bash
|
|
if [ "${{1:-}}" = "+%s" ]; then
|
|
echo {fake_now}
|
|
else
|
|
/bin/date "$@"
|
|
fi
|
|
"""
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(bindir / "wg").write_text(
|
|
textwrap.dedent(
|
|
f"""\
|
|
#!/usr/bin/env bash
|
|
if [ "$1 $2" = "show interfaces" ]; then
|
|
echo wgeasy
|
|
exit 0
|
|
fi
|
|
if [ "$1 $2 $3" = "show wgeasy dump" ]; then
|
|
printf '%s\\n' 'privkey pubkey 51820 off'
|
|
printf '%s\\n' 'peerkey psk 129.204.21.129:51820 10.88.0.0/24 {stale_handshake} 1024 2048 25'
|
|
exit 0
|
|
fi
|
|
if [ "$1 $2 $3" = "show wgeasy latest-handshakes" ]; then
|
|
printf '%s\\t%s\\n' peerkey {stale_handshake}
|
|
exit 0
|
|
fi
|
|
exit 1
|
|
"""
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
(bindir / "systemctl").write_text(
|
|
textwrap.dedent(
|
|
f"""\
|
|
#!/usr/bin/env bash
|
|
printf '%s\\n' "$*" >> {calls}
|
|
case "$1" in
|
|
list-unit-files) echo 'wg-quick@wgeasy.service enabled'; exit 0 ;;
|
|
restart) exit 0 ;;
|
|
*) exit 0 ;;
|
|
esac
|
|
"""
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
for path in bindir.iterdir():
|
|
path.chmod(0o755)
|
|
|
|
harness = Path(tmp) / "harness.sh"
|
|
harness.write_text(
|
|
textwrap.dedent(
|
|
f"""\
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
export PATH="{bindir}:$PATH"
|
|
export WIREGUARD_STALE_SECONDS=300
|
|
export WIREGUARD_RESTART_COOLDOWN=300
|
|
export WIREGUARD_WATCH_INTERVAL=30
|
|
source "{WATCH_SCRIPT}"
|
|
watch_once
|
|
"""
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
harness.chmod(0o755)
|
|
|
|
env = os.environ.copy()
|
|
env["PATH"] = f"{bindir}{os.pathsep}{env['PATH']}"
|
|
env["WIREGUARD_WATCH_RUN_FOREVER"] = "0"
|
|
env["WIREGUARD_WATCH_STATE_DIR"] = tmp
|
|
result = subprocess.run(
|
|
[bash, str(harness)],
|
|
cwd=str(ROOT),
|
|
env=env,
|
|
text=True,
|
|
capture_output=True,
|
|
timeout=20,
|
|
)
|
|
|
|
self.assertEqual(result.returncode, 0, result.stderr + result.stdout)
|
|
self.assertIn("restart wg-quick@wgeasy.service", calls.read_text(encoding="utf-8"))
|
|
self.assertTrue(last_restart.exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|