1
1
from twisted .trial import unittest
2
-
2
+ import ipaddress
3
3
from globaleaks .utils import ip
4
4
5
5
6
6
class TestIPUtils (unittest .TestCase ):
7
- def test_parse_csv_ip_ranges_to_ip_networks (self ):
7
+ def test_check_ip (self ):
8
8
ip_str = "192.168.1.1,10.0.0.0/8,::1,2001:db8::/32"
9
9
self .assertTrue (ip .check_ip ("192.168.1.1" , ip_str ))
10
10
@@ -16,3 +16,51 @@ def test_parse_csv_ip_ranges_to_ip_networks(self):
16
16
17
17
ip_str = "192.168.1.2, 10.0.0.0/8, ::1,2001:db8::/32"
18
18
self .assertTrue (ip .check_ip ("2001:db8::2" , ip_str ))
19
+
20
+ def test_check_ip_invalid_ip (self ):
21
+ """Test with an invalid IP address."""
22
+ ip_str = "192.168.1.1,10.0.0.0/8"
23
+ self .assertFalse (ip .check_ip ("999.999.999.999" , ip_str ))
24
+
25
+ def test_check_ip_invalid_filter (self ):
26
+ """Test with an invalid IP filter."""
27
+ ip_str = "invalid_data"
28
+ self .assertFalse (ip .check_ip ("10.0.0.1" , ip_str ))
29
+
30
+ def test_check_ip_empty_filter (self ):
31
+ """Test with an empty IP filter."""
32
+ ip_str = ""
33
+ self .assertFalse (ip .check_ip ("10.0.0.1" , ip_str ))
34
+
35
+ def test_check_ip_bytes_input (self ):
36
+ """Test with client IP as bytes."""
37
+ ip_str = "10.0.0.0/8"
38
+ self .assertTrue (ip .check_ip (b"10.0.0.1" , ip_str ))
39
+
40
+ def test_check_ip_ipv6 (self ):
41
+ """Test with IPv6 addresses."""
42
+ ip_str = "::1,2001:db8::/32"
43
+ self .assertTrue (ip .check_ip ("2001:db8::2" , ip_str ))
44
+ self .assertFalse (ip .check_ip ("2001:dead::1" , ip_str ))
45
+
46
+ def test_parse_csv_ip_ranges_to_ip_networks (self ):
47
+ """Test parsing valid IP and CIDR ranges."""
48
+ ip_str = "192.168.1.1,10.0.0.0/8,::1,2001:db8::/32"
49
+ expected = [
50
+ ipaddress .ip_network ("192.168.1.1/32" ),
51
+ ipaddress .ip_network ("10.0.0.0/8" ),
52
+ ipaddress .ip_network ("::1/128" ),
53
+ ipaddress .ip_network ("2001:db8::/32" )
54
+ ]
55
+
56
+ self .assertEqual (ip .parse_csv_ip_ranges_to_ip_networks (ip_str ), expected )
57
+
58
+ def test_parse_csv_ip_ranges_invalid_entries (self ):
59
+ """Test parsing invalid IPs and CIDR ranges."""
60
+ invalid_ip_str = "invalid,300.300.300.300,10.0.0.1/33"
61
+ with self .assertRaises (ValueError ):
62
+ ip .parse_csv_ip_ranges_to_ip_networks (invalid_ip_str )
63
+
64
+ def test_parse_csv_ip_ranges_empty (self ):
65
+ """Test parsing an empty input."""
66
+ self .assertEqual (ip .parse_csv_ip_ranges_to_ip_networks ("" ), [])
0 commit comments