Skip to content

Commit 7c0e605

Browse files
author
inspxctelement
committed
fix(ip_address): properly handle private is false
1 parent d46cd58 commit 7c0e605

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 bool(is_private)
37+
else:
38+
return not bool(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", False),
157+
("192.168.1.1", False),
158+
("169.254.1.1", False),
159+
("127.0.0.1", False),
160+
("0.0.0.0", False),
161+
],
162+
)
163+
def test_returns_failed_validation_on_private_ipv4_address(address: str, private: bool):
164+
"""Test returns failed validation on invalid ipv4 CIDR address."""
165+
assert isinstance(ipv4(address, private=private), ValidationError)
166+
167+
168+
@pytest.mark.parametrize(
169+
("address", "private"),
170+
[
171+
("10.1.1.1", True),
172+
("192.168.1.1", True),
173+
("169.254.1.1", True),
174+
("127.0.0.1", True),
175+
("0.0.0.0", True),
176+
],
177+
)
178+
def test_returns_valid_on_private_ipv4_address(address: str, private: bool):
179+
"""Test returns failed validation on invalid ipv4 CIDR address."""
180+
assert ipv4(address, private=private)
181+
182+
183+
@pytest.mark.parametrize(
184+
("address", "private"),
185+
[
186+
("1.1.1.1", True),
187+
("192.169.1.1", True),
188+
("7.53.12.1", True),
189+
],
190+
)
191+
def test_returns_failed_validation_on_not_private_ipv4_address(address: str, private: bool):
192+
"""Test returns failed validation on invalid ipv4 CIDR address."""
193+
assert isinstance(ipv4(address, private=private), ValidationError)
194+
195+
196+
@pytest.mark.parametrize(
197+
("address", "private"),
198+
[
199+
("1.1.1.1", False),
200+
("192.169.1.1", False),
201+
("7.53.12.1", False),
202+
],
203+
)
204+
def test_returns_valid_on_private_ipv4_address(address: str, private: bool):
205+
"""Test returns failed validation on invalid ipv4 CIDR address."""
206+
assert ipv4(address, private=private)

0 commit comments

Comments
 (0)