Skip to content

Commit bcb1342

Browse files
authored
Merge pull request #374 from grleblanc/fix-ipv4-private
fix(ip_address): properly handle private is false
2 parents d46cd58 + 0623b4a commit bcb1342

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/validators/ip_address.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
def _check_private_ip(value: str, is_private: Optional[bool]):
2020
if is_private is None:
2121
return True
22-
if is_private and (
22+
if (
2323
any(
2424
value.startswith(l_bit)
2525
for l_bit in {
@@ -33,8 +33,9 @@ def _check_private_ip(value: str, is_private: Optional[bool]):
3333
or re.match(r"^172\.(?:1[6-9]|2\d|3[0-1])\.", value) # private
3434
or re.match(r"^(?:22[4-9]|23[0-9]|24[0-9]|25[0-5])\.", value) # broadcast
3535
):
36-
return True
37-
return False
36+
return is_private
37+
38+
return not is_private
3839

3940

4041
@validator

tests/test_ip_address.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,59 @@ def test_returns_failed_validation_on_invalid_ipv6_cidr_address(
148148
):
149149
"""Test returns failed validation on invalid ipv6 CIDR address."""
150150
assert isinstance(ipv6(address, cidr=cidr, strict=strict, host_bit=host_bit), ValidationError)
151+
152+
153+
@pytest.mark.parametrize(
154+
("address", "private"),
155+
[
156+
("10.1.1.1", True),
157+
("192.168.1.1", True),
158+
("169.254.1.1", True),
159+
("127.0.0.1", True),
160+
("0.0.0.0", True),
161+
],
162+
)
163+
def test_returns_true_on_valid_private_ipv4_address(address: str, private: bool):
164+
"""Test returns true on private ipv4 address."""
165+
assert ipv4(address, private=private)
166+
167+
168+
@pytest.mark.parametrize(
169+
("address", "private"),
170+
[
171+
("1.1.1.1", True),
172+
("192.169.1.1", True),
173+
("7.53.12.1", True),
174+
],
175+
)
176+
def test_returns_failed_validation_on_invalid_private_ipv4_address(address: str, private: bool):
177+
"""Test returns failed validation on invalid private ipv4 address."""
178+
assert isinstance(ipv4(address, private=private), ValidationError)
179+
180+
181+
@pytest.mark.parametrize(
182+
("address", "private"),
183+
[
184+
("1.1.1.1", False),
185+
("192.169.1.1", False),
186+
("7.53.12.1", False),
187+
],
188+
)
189+
def test_returns_true_on_valid_public_ipv4_address(address: str, private: bool):
190+
"""Test returns true on valid public ipv4 address."""
191+
assert ipv4(address, private=private)
192+
193+
194+
@pytest.mark.parametrize(
195+
("address", "private"),
196+
[
197+
("10.1.1.1", False),
198+
("192.168.1.1", False),
199+
("169.254.1.1", False),
200+
("127.0.0.1", False),
201+
("0.0.0.0", False),
202+
],
203+
)
204+
def test_returns_failed_validation_on_invalid_public_ipv4_address(address: str, private: bool):
205+
"""Test returns failed validation on private ipv4 address."""
206+
assert isinstance(ipv4(address, private=private), ValidationError)

0 commit comments

Comments
 (0)