69 lines
3.2 KiB
Python
69 lines
3.2 KiB
Python
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
|
|
path = Path(__file__).resolve().parents[2] / 'scripts/network-sandbox-wan-host.py'
|
|
spec = importlib.util.spec_from_file_location('sandbox_wan_host', path)
|
|
wan = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(wan)
|
|
|
|
|
|
class WanGuardTest(unittest.TestCase):
|
|
def test_guard_blocks_private_ranges_before_permitting_ipv4(self):
|
|
with patch.object(wan, 'run') as run:
|
|
wan.install_guard()
|
|
calls = [call.args for call in run.call_args_list]
|
|
for cidr in wan.BLOCKED:
|
|
self.assertTrue(any('dst_ip' in c and cidr in c and 'drop' in c for c in calls))
|
|
self.assertEqual(calls[-1][-2:], ('action', 'drop'))
|
|
self.assertIn('all', calls[-1])
|
|
|
|
def test_cleanup_disables_tap_before_qmp(self):
|
|
with patch.object(wan, 'run') as run, patch.object(wan, 'qmp') as qmp:
|
|
wan.isolate()
|
|
run.assert_called_once_with('ip', 'link', 'set', 'dev', wan.TAP, 'down')
|
|
qmp.assert_called_once_with(False)
|
|
|
|
def test_bridge_guard_attaches_only_after_all_rules(self):
|
|
with tempfile.TemporaryDirectory() as directory, \
|
|
patch.object(wan, 'MARKER', Path(directory) / 'marker'), \
|
|
patch.object(wan.secrets, 'token_hex', return_value='1234abcd'), \
|
|
patch.object(wan, 'ebt') as ebt:
|
|
self.assertEqual(wan.install_bridge_guard(), 'ebtables')
|
|
state = json.loads(wan.MARKER.read_text())
|
|
self.assertEqual(state['chains'], ['JSI1234abcd', 'JSF1234abcd'])
|
|
calls = [call.args for call in ebt.call_args_list]
|
|
self.assertEqual(calls[-2:], [('-I', 'INPUT', '1', '-i', wan.TAP, '-j', 'JSI1234abcd'),
|
|
('-I', 'FORWARD', '1', '-i', wan.TAP, '-j', 'JSF1234abcd')])
|
|
for cidr in wan.BLOCKED:
|
|
self.assertIn(('-A', 'JSF1234abcd', '-p', 'IPv4', '--ip-destination', cidr, '-j', 'DROP'), calls)
|
|
self.assertIn(('-A', 'JSI1234abcd', '-j', 'DROP'), calls)
|
|
self.assertIn(('-A', 'JSF1234abcd', '-j', 'DROP'), calls)
|
|
|
|
def test_cleanup_rejects_foreign_chain_names(self):
|
|
with self.assertRaises(RuntimeError):
|
|
wan.remove_bridge_guard({'chains': ['FORWARD']})
|
|
|
|
def test_cleanup_removes_exact_legacy_bridge_hook(self):
|
|
listing = 'ebtables -t filter -A FORWARD -i tap9100i0 -j JSF1234abcd\n'
|
|
with patch.object(wan, 'ebt', side_effect=[listing, '', '', '']) as ebt:
|
|
wan.remove_bridge_guard({'chains': ['JSF1234abcd']})
|
|
self.assertEqual([call.args for call in ebt.call_args_list], [
|
|
('-L', 'FORWARD', '--Lx'),
|
|
('-D', 'FORWARD', '-i', wan.TAP, '-j', 'JSF1234abcd'),
|
|
('-F', 'JSF1234abcd'), ('-X', 'JSF1234abcd')])
|
|
|
|
def test_cleanup_handles_chain_without_attached_hook(self):
|
|
with patch.object(wan, 'ebt', side_effect=['', '', '']) as ebt:
|
|
wan.remove_bridge_guard({'chains': ['JSI1234abcd']})
|
|
self.assertEqual([call.args for call in ebt.call_args_list], [
|
|
('-L', 'INPUT', '--Lx'), ('-F', 'JSI1234abcd'), ('-X', 'JSI1234abcd')])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|