46 lines
1.9 KiB
Python
Executable File
46 lines
1.9 KiB
Python
Executable File
"""Standard-library regression tests for the role's CIDR predicates."""
|
|
|
|
import unittest
|
|
|
|
from filter_plugins.luanti_wasm_network import valid_cidr, valid_ip_block
|
|
|
|
|
|
class NetworkValidationTests(unittest.TestCase):
|
|
def test_canonical_ipv4_and_ipv6(self):
|
|
for cidr in ("192.0.2.0/24", "192.0.2.1/32", "2001:db8::/64", "2001:db8::1/128"):
|
|
with self.subTest(cidr=cidr):
|
|
self.assertTrue(valid_cidr(cidr))
|
|
|
|
def test_rejects_malformed_or_unbounded_cidrs(self):
|
|
for value in (
|
|
None, True, 123, [], {}, "", "192.0.2.1", "192.0.2.1/24",
|
|
"999.0.0.0/8", "192.0.2.0/33", "0.0.0.0/0", "::/0",
|
|
"2001:db8:::/64", "2001:db8::/129", "fe80::%eth0/64",
|
|
"2001:DB8::/64", "host.example/32", "192.0.2.0/255.255.255.0",
|
|
):
|
|
with self.subTest(value=value):
|
|
self.assertFalse(valid_cidr(value))
|
|
|
|
def test_contained_exceptions(self):
|
|
self.assertTrue(valid_ip_block({"cidr": "192.0.2.0/24"}))
|
|
self.assertTrue(valid_ip_block({"cidr": "192.0.2.0/24", "except": ["192.0.2.1/32"]}))
|
|
self.assertTrue(valid_ip_block({"cidr": "2001:db8::/64", "except": ["2001:db8::1/128"]}))
|
|
|
|
def test_rejects_invalid_ip_blocks(self):
|
|
for value in (
|
|
None, [], {}, {"cidr": "::/0"}, {"cidr": "192.0.2.0/24", "extra": True},
|
|
{"cidr": "192.0.2.0/24", "except": "192.0.2.1/32"},
|
|
{"cidr": "192.0.2.0/24", "except": ["192.0.2.0/24"]},
|
|
{"cidr": "192.0.2.0/24", "except": ["198.51.100.0/24"]},
|
|
{"cidr": "192.0.2.0/24", "except": ["2001:db8::/64"]},
|
|
{"cidr": "2001:db8::/64", "except": ["2001:db8::/48"]},
|
|
{"cidr": "2001:db8::/64", "except": ["2001:db8:1::/64"]},
|
|
{"cidr": "2001:db8::/64", "except": ["192.0.2.1/32"]},
|
|
):
|
|
with self.subTest(value=value):
|
|
self.assertFalse(valid_ip_block(value))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|