|
| 1 | +import pytest |
| 2 | +from .iplist import IPList, get_ip_address_type |
| 3 | + |
| 4 | + |
| 5 | +def test_get_ip_address_type(): |
| 6 | + # Valid IPv4 addresses |
| 7 | + assert get_ip_address_type("192.168.1.1") == "ipv4" |
| 8 | + assert get_ip_address_type("255.255.255.255") == "ipv4" |
| 9 | + assert get_ip_address_type("0.0.0.0") == "ipv4" |
| 10 | + |
| 11 | + # Valid IPv6 addresses |
| 12 | + assert get_ip_address_type("::1") == "ipv6" |
| 13 | + assert get_ip_address_type("2001:0db8:85a3:0000:0000:8a2e:0370:7334") == "ipv6" |
| 14 | + assert get_ip_address_type("::") == "ipv6" |
| 15 | + |
| 16 | + # Invalid IP addresses |
| 17 | + assert get_ip_address_type("invalid_ip") is None |
| 18 | + assert get_ip_address_type("") is None |
| 19 | + assert get_ip_address_type("192.168.1.256") is None # Invalid octet |
| 20 | + assert ( |
| 21 | + get_ip_address_type("2001:0db8:85a3:0000:0000:8a2e:0370:7334:1234") is None |
| 22 | + ) # Too many segments |
| 23 | + |
| 24 | + |
| 25 | +def test_blocklist_add_address(): |
| 26 | + blocklist = IPList() |
| 27 | + assert blocklist.matches("192.168.1.1") is False |
| 28 | + blocklist.add_address("192.168.1.1", "ipv4") |
| 29 | + assert blocklist.matches("192.168.1.1") is True |
| 30 | + |
| 31 | + # Test adding the same address again |
| 32 | + blocklist.add_address("192.168.1.1", "ipv4") |
| 33 | + assert len(blocklist.blocked_addresses) == 1 # Should still be one entry |
| 34 | + |
| 35 | + |
| 36 | +def test_blocklist_add_subnet(): |
| 37 | + blocklist = IPList() |
| 38 | + assert blocklist.matches("10.0.0.1") is False |
| 39 | + blocklist.add_subnet("10.0.0.0", 8, "ipv4") |
| 40 | + assert blocklist.matches("10.0.0.1") is True |
| 41 | + assert blocklist.matches("10.1.1.1") is True |
| 42 | + assert blocklist.matches("192.168.1.1") is False |
| 43 | + |
| 44 | + |
| 45 | +def test_blocklist_multiple_addresses_and_subnets(): |
| 46 | + blocklist = IPList() |
| 47 | + blocklist.add_address("192.168.1.1", "ipv4") |
| 48 | + blocklist.add_subnet("10.0.0.0", 8, "ipv4") |
| 49 | + |
| 50 | + assert blocklist.matches("192.168.1.1") is True |
| 51 | + assert blocklist.matches("10.0.0.1") is True |
| 52 | + assert blocklist.matches("10.1.1.1") is True |
| 53 | + assert blocklist.matches("172.16.0.1") is False |
| 54 | + |
| 55 | + |
| 56 | +def test_blocklist_invalid_ip(): |
| 57 | + blocklist = IPList() |
| 58 | + blocklist.add_address("192.168.1.1", "ipv4") |
| 59 | + assert blocklist.matches("invalid_ip") is False |
| 60 | + |
| 61 | + |
| 62 | +def test_blocklist_subnet_edge_cases(): |
| 63 | + blocklist = IPList() |
| 64 | + blocklist.add_subnet("192.168.1.0", 24, "ipv4") |
| 65 | + |
| 66 | + assert blocklist.matches("192.168.1.255") is True # Last address in the subnet |
| 67 | + assert blocklist.matches("192.168.1.0") is True # First address in the subnet |
| 68 | + assert blocklist.matches("192.168.2.1") is False # Outside the subnet |
| 69 | + assert blocklist.matches("192.168.1.128") is True # Middle of the subnet |
| 70 | + |
| 71 | + |
| 72 | +def test_blocklist_ipv6(): |
| 73 | + blocklist = IPList() |
| 74 | + blocklist.add_address("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "ipv6") |
| 75 | + assert blocklist.matches("2001:0db8:85a3:0000:0000:8a2e:0370:7334") is True |
| 76 | + assert blocklist.matches("::1") is False # Not in the blocklist |
| 77 | + |
| 78 | + blocklist.add_subnet("2001:0db8::", 32, "ipv6") |
| 79 | + assert blocklist.matches("2001:0db8:abcd:0012:0000:0000:0000:0001") is True |
| 80 | + assert blocklist.matches("2001:0db9::") is False # Outside the subnet |
| 81 | + |
| 82 | + |
| 83 | +def test_blocklist_overlapping_subnets(): |
| 84 | + blocklist = IPList() |
| 85 | + blocklist.add_subnet( |
| 86 | + "192.168.1.0", 24, "ipv4" |
| 87 | + ) # Covers 192.168.1.0 to 192.168.1.255 |
| 88 | + blocklist.add_subnet( |
| 89 | + "192.168.1.128", 25, "ipv4" |
| 90 | + ) # Covers 192.168.1.128 to 192.168.1.255 |
| 91 | + |
| 92 | + assert blocklist.matches("192.168.1.130") is True # Inside both subnets |
| 93 | + assert blocklist.matches("192.168.1.0") is True # Inside first subnet |
| 94 | + assert ( |
| 95 | + blocklist.matches("192.168.1.127") is True |
| 96 | + ) # Inside first subnet, outside second |
| 97 | + assert blocklist.matches("192.168.1.255") is True # Last address in both subnets |
| 98 | + assert blocklist.matches("192.168.2.1") is False # Outside both subnets |
| 99 | + |
| 100 | + |
| 101 | +def test_blocklist_mixed_address_types(): |
| 102 | + blocklist = IPList() |
| 103 | + blocklist.add_address("192.168.1.1", "ipv4") |
| 104 | + blocklist.add_address("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "ipv6") |
| 105 | + |
| 106 | + assert blocklist.matches("192.168.1.1") is True |
| 107 | + assert blocklist.matches("2001:0db8:85a3:0000:0000:8a2e:0370:7334") is True |
| 108 | + assert blocklist.matches("192.168.1.2") is False # Different IPv4 |
| 109 | + assert ( |
| 110 | + blocklist.matches("2001:0db8:85a3:0000:0000:8a2e:0370:7335") is False |
| 111 | + ) # Different IPv6 |
| 112 | + |
| 113 | + |
| 114 | +def test_blocklist_subnet_with_single_ip(): |
| 115 | + blocklist = IPList() |
| 116 | + blocklist.add_subnet("192.168.1.1", 32, "ipv4") # Single IP subnet |
| 117 | + |
| 118 | + assert blocklist.matches("192.168.1.1") is True |
| 119 | + assert blocklist.matches("192.168.1.2") is False # Outside the subnet |
| 120 | + |
| 121 | + |
| 122 | +# Tests add function |
| 123 | +def test_valid_ips(): |
| 124 | + blocklist = IPList() |
| 125 | + |
| 126 | + blocklist.add("1.2.3.4") |
| 127 | + assert blocklist.matches("1.2.3.4") is True |
| 128 | + |
| 129 | + blocklist.add("fd00:ec2::254") |
| 130 | + assert blocklist.matches("fd00:ec2::254") is True |
| 131 | + |
| 132 | + blocklist.add("192.168.2.1/24") |
| 133 | + assert blocklist.matches("192.168.2.1") is True |
| 134 | + assert blocklist.matches("192.168.2.240") is True |
| 135 | + |
| 136 | + blocklist.add("fd00:124::1/64") |
| 137 | + assert blocklist.matches("fd00:124::1") is True |
| 138 | + assert blocklist.matches("fd00:124::f") is True |
| 139 | + assert blocklist.matches("fd00:124::ff13") is True |
| 140 | + |
| 141 | + blocklist.add("fd00:f123::1/128") |
| 142 | + assert blocklist.matches("fd00:f123::1") is True |
| 143 | + |
| 144 | + assert blocklist.matches("2.3.4.5") is False |
| 145 | + assert blocklist.matches("fd00:125::ff13") is False |
| 146 | + assert blocklist.matches("fd00:f123::2") is False |
| 147 | + |
| 148 | + |
| 149 | +def test_invalid_ips(): |
| 150 | + blocklist = IPList() |
| 151 | + |
| 152 | + blocklist.add("192.168.2.2.1/24") |
| 153 | + assert blocklist.matches("192.168.2.2.1") is False |
| 154 | + |
| 155 | + blocklist.add("test") |
| 156 | + assert blocklist.matches("test") is False |
| 157 | + |
| 158 | + blocklist.add("") |
| 159 | + assert blocklist.matches("") is False |
| 160 | + |
| 161 | + blocklist.add("fd00:124::1/test") |
| 162 | + assert blocklist.matches("fd00:124::1") is False |
0 commit comments