120 lines
5.7 KiB
Python
120 lines
5.7 KiB
Python
"""Synthetic native reconnect regressions; no real tunnel, network or credentials."""
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
from network.reconnect import refresh
|
|
|
|
CONF = '''[Interface]
|
|
PrivateKey = AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=
|
|
Address = 192.0.2.10/32
|
|
[Peer]
|
|
PublicKey = AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI=
|
|
Endpoint = gateway.example.test:51820
|
|
AllowedIPs = 192.0.2.0/24
|
|
PersistentKeepalive = 25
|
|
'''
|
|
PEER = 'AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI='
|
|
STATUS = dict(backend='native-systemd', interface='wgdeck0', enabled=True, configured=True)
|
|
|
|
|
|
class ReconnectTests(unittest.TestCase):
|
|
def state(self, directory):
|
|
root = Path(directory)
|
|
(root / 'enabled.json').write_text('true')
|
|
(root / 'config.json').write_text(json.dumps(CONF))
|
|
return root
|
|
|
|
def test_disabled_does_not_read_config_or_call_helper(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
with patch('network.reconnect.native_status') as status, patch('network.reconnect.command') as command:
|
|
self.assertEqual(refresh(root)['state'], 'disabled')
|
|
status.assert_not_called()
|
|
command.assert_not_called()
|
|
|
|
def test_live_handshake_leaves_endpoint_untouched(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=STATUS), \
|
|
patch('network.reconnect.command', return_value=PEER + '\t900') as command:
|
|
result = refresh(root, now=1000)
|
|
self.assertEqual(result['state'], 'connected')
|
|
self.assertFalse(result['endpoint_refreshed'])
|
|
self.assertEqual(command.call_count, 1)
|
|
|
|
def test_stale_handshake_refreshes_hostname_without_link_reset(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
before = (root / 'config.json').read_bytes()
|
|
calls = []
|
|
def command(args):
|
|
calls.append(args)
|
|
return PEER + '\t700' if args[1] == 'show' else ''
|
|
with patch('network.reconnect.native_status', return_value=STATUS), \
|
|
patch('network.reconnect.command', side_effect=command):
|
|
result = refresh(root, now=1000)
|
|
self.assertTrue(result['endpoint_refreshed'])
|
|
self.assertEqual(calls[-1], ['wg', 'set', 'wgdeck0', 'peer', PEER, 'endpoint', 'gateway.example.test:51820'])
|
|
self.assertTrue(all(call[0] == 'wg' for call in calls))
|
|
self.assertNotIn('PrivateKey', json.dumps(result))
|
|
self.assertEqual((root / 'config.json').read_bytes(), before)
|
|
|
|
def test_first_handshake_can_be_retried(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=STATUS), \
|
|
patch('network.reconnect.command', side_effect=[PEER + '\t0', PEER + '\t0', '']):
|
|
self.assertTrue(refresh(root, now=1000)['endpoint_refreshed'])
|
|
|
|
def test_manual_disable_between_checks_prevents_update(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=STATUS), \
|
|
patch('network.reconnect.enabled', side_effect=[True, False]), \
|
|
patch('network.reconnect.command', return_value=PEER + '\t700') as command:
|
|
result = refresh(root, now=1000)
|
|
self.assertEqual(result['state'], 'configuration_changed')
|
|
self.assertEqual(command.call_count, 1)
|
|
|
|
def test_missing_peer_is_never_added(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=STATUS), \
|
|
patch('network.reconnect.command', return_value='') as command:
|
|
result = refresh(root, now=1000)
|
|
self.assertEqual(result['state'], 'peer_changed_or_missing')
|
|
self.assertEqual(command.call_count, 1)
|
|
|
|
def test_helper_disabled_is_respected(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=dict(STATUS, enabled=False)), \
|
|
patch('network.reconnect.command') as command:
|
|
self.assertFalse(refresh(root)['endpoint_refreshed'])
|
|
command.assert_not_called()
|
|
|
|
def test_dns_failure_does_not_fall_back_to_restart(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=STATUS), \
|
|
patch('network.reconnect.command', side_effect=[PEER + '\t700', PEER + '\t700', ValueError('DNS failure')]) as command:
|
|
with self.assertRaises(ValueError):
|
|
refresh(root, now=1000)
|
|
self.assertEqual(command.call_count, 3)
|
|
self.assertEqual(json.loads((root / 'enabled.json').read_text()), True)
|
|
|
|
def test_wrong_interface_is_refused(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.state(directory)
|
|
with patch('network.reconnect.native_status', return_value=dict(STATUS, interface='unrelated')), \
|
|
patch('network.reconnect.command') as command:
|
|
with self.assertRaises(ValueError):
|
|
refresh(root)
|
|
command.assert_not_called()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|