37 lines
1.8 KiB
Python
37 lines
1.8 KiB
Python
|
|
import importlib.util
|
||
|
|
from pathlib import Path
|
||
|
|
import unittest
|
||
|
|
|
||
|
|
MODULE = Path(__file__).resolve().parents[1] / 'deploy/lan_config.py'
|
||
|
|
|
||
|
|
|
||
|
|
class LanConfigurationTests(unittest.TestCase):
|
||
|
|
def validator(self):
|
||
|
|
self.assertTrue(MODULE.is_file(), 'LAN boundary configuration is not implemented')
|
||
|
|
spec = importlib.util.spec_from_file_location('lan_config', MODULE)
|
||
|
|
module = importlib.util.module_from_spec(spec)
|
||
|
|
spec.loader.exec_module(module)
|
||
|
|
return module.validate_settings
|
||
|
|
|
||
|
|
def test_explicit_private_interface_and_matching_subnet_are_accepted(self):
|
||
|
|
result = self.validator()({'address':'10.88.118.188','allowed_cidrs':['10.88.0.0/17','10.20.0.0/21'],'port':14440})
|
||
|
|
self.assertEqual(result['address'], '10.88.118.188')
|
||
|
|
self.assertEqual(result['allowed_cidrs'], ['10.88.0.0/17','10.20.0.0/21'])
|
||
|
|
|
||
|
|
def test_wildcard_public_loopback_and_nonmatching_network_are_rejected(self):
|
||
|
|
validate = self.validator()
|
||
|
|
for address, cidr in [('0.0.0.0','0.0.0.0/0'), ('8.8.8.8','8.8.8.0/24'),
|
||
|
|
('127.0.0.1','127.0.0.0/8'), ('10.88.118.188','10.89.0.0/17'),
|
||
|
|
('10.88.118.188','0.0.0.0/0'), ('10.88.118.188; admin','10.88.0.0/17')]:
|
||
|
|
with self.subTest(address=address,cidr=cidr), self.assertRaises(ValueError):
|
||
|
|
validate({'address':address,'allowed_cidrs':[cidr],'port':14440})
|
||
|
|
|
||
|
|
def test_invalid_port_and_extra_fields_are_rejected(self):
|
||
|
|
validate = self.validator()
|
||
|
|
base = {'address':'10.88.118.188','allowed_cidrs':['10.88.0.0/17','10.20.0.0/21'],'port':14440}
|
||
|
|
for port in (0, 80, 65536, True, '14440'):
|
||
|
|
with self.subTest(port=port), self.assertRaises(ValueError):
|
||
|
|
validate(dict(base, port=port))
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
validate(dict(base, public=True))
|