| 1 |
import ipaddress |
| 2 |
import unittest |
| 3 |
from unittest.mock import Mock |
| 4 |
from govee_local_api.controller import GoveeController |
| 5 |
|
| 6 |
|
| 7 |
class TestNetworkMaskFunctionality(unittest.TestCase): |
| 8 |
"""Test the network mask functionality via listening_addresses with CIDR/netmask notation.""" |
| 9 |
|
| 10 |
def test_address_with_cidr_mask(self): |
| 11 |
"""Test initialization with CIDR notation in listening_addresses.""" |
| 12 |
controller = GoveeController(listening_addresses="192.168.1.100/24") |
| 13 |
self.assertEqual(controller.listening_addresses, ["192.168.1.100"]) |
| 14 |
self.assertEqual( |
| 15 |
controller.networks, |
| 16 |
[ipaddress.ip_network("192.168.1.0/24", strict=False)], |
| 17 |
) |
| 18 |
|
| 19 |
def test_address_with_netmask(self): |
| 20 |
"""Test initialization with dotted netmask notation.""" |
| 21 |
controller = GoveeController(listening_addresses="192.168.1.100/255.255.255.0") |
| 22 |
self.assertEqual(controller.listening_addresses, ["192.168.1.100"]) |
| 23 |
self.assertEqual( |
| 24 |
controller.networks, |
| 25 |
[ipaddress.ip_network("192.168.1.0/24", strict=False)], |
| 26 |
) |
| 27 |
|
| 28 |
def test_address_without_mask(self): |
| 29 |
"""Test initialization without a mask (heuristic fallback).""" |
| 30 |
controller = GoveeController(listening_addresses="192.168.1.100") |
| 31 |
self.assertEqual(controller.listening_addresses, ["192.168.1.100"]) |
| 32 |
self.assertEqual(controller.networks, [None]) |
| 33 |
|
| 34 |
def test_wildcard_address(self): |
| 35 |
"""Test that 0.0.0.0 has no network even with a mask.""" |
| 36 |
controller = GoveeController(listening_addresses="0.0.0.0/24") |
| 37 |
self.assertEqual(controller.listening_addresses, ["0.0.0.0"]) |
| 38 |
self.assertEqual(controller.networks, [None]) |
| 39 |
|
| 40 |
def test_multiple_addresses_mixed(self): |
| 41 |
"""Test multiple addresses with mixed mask formats.""" |
| 42 |
addresses = ["192.168.1.100/24", "10.0.0.100/8", "172.16.1.100"] |
| 43 |
controller = GoveeController(listening_addresses=addresses) |
| 44 |
self.assertEqual( |
| 45 |
controller.listening_addresses, |
| 46 |
["192.168.1.100", "10.0.0.100", "172.16.1.100"], |
| 47 |
) |
| 48 |
self.assertEqual( |
| 49 |
controller.networks, |
| 50 |
[ |
| 51 |
ipaddress.ip_network("192.168.1.0/24", strict=False), |
| 52 |
ipaddress.ip_network("10.0.0.0/8", strict=False), |
| 53 |
None, |
| 54 |
], |
| 55 |
) |
| 56 |
|
| 57 |
def test_precise_subnet_matching_cidr(self): |
| 58 |
"""Test precise subnet matching using CIDR notation.""" |
| 59 |
controller = GoveeController( |
| 60 |
listening_addresses=["192.168.1.100/24", "10.0.0.100/8"], |
| 61 |
) |
| 62 |
|
| 63 |
transport1 = Mock() |
| 64 |
transport2 = Mock() |
| 65 |
controller._transports = [transport1, transport2] |
| 66 |
|
| 67 |
selected = controller._get_best_transport_for_ip("192.168.1.200") |
| 68 |
self.assertEqual(selected, transport1) |
| 69 |
|
| 70 |
selected = controller._get_best_transport_for_ip("10.50.100.200") |
| 71 |
self.assertEqual(selected, transport2) |
| 72 |
|
| 73 |
|
| 74 |
selected = controller._get_best_transport_for_ip("172.16.1.100") |
| 75 |
self.assertEqual(selected, transport1) |
| 76 |
|
| 77 |
def test_precise_subnet_matching_netmask(self): |
| 78 |
"""Test precise subnet matching using netmask notation.""" |
| 79 |
controller = GoveeController( |
| 80 |
listening_addresses=[ |
| 81 |
"192.168.1.100/255.255.255.0", |
| 82 |
"192.168.2.100/255.255.254.0", |
| 83 |
], |
| 84 |
) |
| 85 |
|
| 86 |
transport1 = Mock() |
| 87 |
transport2 = Mock() |
| 88 |
controller._transports = [transport1, transport2] |
| 89 |
|
| 90 |
selected = controller._get_best_transport_for_ip("192.168.1.200") |
| 91 |
self.assertEqual(selected, transport1) |
| 92 |
|
| 93 |
selected = controller._get_best_transport_for_ip("192.168.2.200") |
| 94 |
self.assertEqual(selected, transport2) |
| 95 |
|
| 96 |
|
| 97 |
selected = controller._get_best_transport_for_ip("192.168.3.100") |
| 98 |
self.assertEqual(selected, transport2) |
| 99 |
|
| 100 |
def test_mixed_notation_support(self): |
| 101 |
"""Test support for mixed CIDR and netmask notation.""" |
| 102 |
controller = GoveeController( |
| 103 |
listening_addresses=[ |
| 104 |
"192.168.1.100/24", |
| 105 |
"10.0.0.100/255.0.0.0", |
| 106 |
"172.16.1.100/20", |
| 107 |
], |
| 108 |
) |
| 109 |
|
| 110 |
t1, t2, t3 = Mock(), Mock(), Mock() |
| 111 |
controller._transports = [t1, t2, t3] |
| 112 |
|
| 113 |
selected = controller._get_best_transport_for_ip("192.168.1.200") |
| 114 |
self.assertEqual(selected, t1) |
| 115 |
|
| 116 |
selected = controller._get_best_transport_for_ip("10.50.100.200") |
| 117 |
self.assertEqual(selected, t2) |
| 118 |
|
| 119 |
selected = controller._get_best_transport_for_ip("172.16.5.100") |
| 120 |
self.assertEqual(selected, t3) |
| 121 |
|
| 122 |
def test_wildcard_address_handling_with_masks(self): |
| 123 |
"""Test that wildcard address is dropped when mixed with specific addresses.""" |
| 124 |
controller = GoveeController( |
| 125 |
listening_addresses=["0.0.0.0/24", "192.168.1.100/24"], |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
self.assertEqual(controller.listening_addresses, ["192.168.1.100"]) |
| 130 |
|
| 131 |
transport1 = Mock() |
| 132 |
controller._transports = [transport1] |
| 133 |
|
| 134 |
|
| 135 |
selected = controller._get_best_transport_for_ip("192.168.1.200") |
| 136 |
self.assertEqual(selected, transport1) |
| 137 |
|
| 138 |
selected = controller._get_best_transport_for_ip("10.0.0.100") |
| 139 |
self.assertEqual(selected, transport1) |
| 140 |
|
| 141 |
def test_fallback_to_heuristic_without_masks(self): |
| 142 |
"""Test fallback to heuristic matching when no masks provided.""" |
| 143 |
controller = GoveeController( |
| 144 |
listening_addresses=["192.168.1.100", "10.0.0.100"], |
| 145 |
) |
| 146 |
|
| 147 |
transport1 = Mock() |
| 148 |
transport2 = Mock() |
| 149 |
controller._transports = [transport1, transport2] |
| 150 |
|
| 151 |
selected = controller._get_best_transport_for_ip("192.168.1.200") |
| 152 |
self.assertEqual(selected, transport1) |
| 153 |
|
| 154 |
selected = controller._get_best_transport_for_ip("10.50.100.200") |
| 155 |
self.assertEqual(selected, transport2) |
| 156 |
|
| 157 |
def test_subnet_edge_cases(self): |
| 158 |
"""Test subnet matching edge cases with small subnets.""" |
| 159 |
controller = GoveeController( |
| 160 |
listening_addresses=["192.168.1.100/30"], |
| 161 |
) |
| 162 |
|
| 163 |
transport1 = Mock() |
| 164 |
controller._transports = [transport1] |
| 165 |
|
| 166 |
|
| 167 |
selected = controller._get_best_transport_for_ip("192.168.1.101") |
| 168 |
self.assertEqual(selected, transport1) |
| 169 |
|
| 170 |
selected = controller._get_best_transport_for_ip("192.168.1.103") |
| 171 |
self.assertEqual(selected, transport1) |
| 172 |
|
| 173 |
def test_large_subnet_matching(self): |
| 174 |
"""Test matching in large subnets.""" |
| 175 |
controller = GoveeController( |
| 176 |
listening_addresses=["10.0.0.100/8"], |
| 177 |
) |
| 178 |
|
| 179 |
transport1 = Mock() |
| 180 |
controller._transports = [transport1] |
| 181 |
|
| 182 |
test_ips = ["10.0.0.1", "10.255.255.255", "10.123.45.67"] |
| 183 |
for ip in test_ips: |
| 184 |
selected = controller._get_best_transport_for_ip(ip) |
| 185 |
self.assertEqual(selected, transport1) |
| 186 |
|
| 187 |
def test_invalid_cidr_prefix_rejected(self): |
| 188 |
"""A mask that can't be parsed must fail loudly, not silently |
| 189 |
degrade to heuristic matching.""" |
| 190 |
with self.assertRaises(ValueError): |
| 191 |
GoveeController(listening_addresses="192.168.1.100/33") |
| 192 |
|
| 193 |
def test_garbage_mask_rejected(self): |
| 194 |
with self.assertRaises(ValueError): |
| 195 |
GoveeController(listening_addresses="192.168.1.100/banana") |
| 196 |
|
| 197 |
def test_non_contiguous_netmask_rejected(self): |
| 198 |
with self.assertRaises(ValueError): |
| 199 |
GoveeController(listening_addresses="192.168.1.100/255.255.0.255") |
| 200 |
|
| 201 |
def test_ipv6_handling(self): |
| 202 |
"""Test that IPv6 addresses are handled gracefully.""" |
| 203 |
controller = GoveeController(listening_addresses=["192.168.1.100/24"]) |
| 204 |
|
| 205 |
transport1 = Mock() |
| 206 |
controller._transports = [transport1] |
| 207 |
|
| 208 |
selected = controller._get_best_transport_for_ip("2001:db8::1") |
| 209 |
self.assertEqual(selected, transport1) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
unittest.main() |