|
1 | 1 | """Test IP Address.""" |
2 | 2 |
|
| 3 | +# standard |
| 4 | +from typing import Any, Optional |
| 5 | + |
3 | 6 | # external |
4 | 7 | import pytest |
5 | 8 |
|
6 | 9 | # local |
7 | 10 | from validators import ValidationError, ipv4, ipv6 |
8 | 11 |
|
9 | 12 |
|
| 13 | +@pytest.mark.parametrize("address", [True, 3232235777, b"\x7f\x00\x00\x01", ("10.0.0.0", 8)]) |
| 14 | +@pytest.mark.parametrize("cidr", [True, False]) |
| 15 | +@pytest.mark.parametrize("private", [None, True, False]) |
| 16 | +def test_rejects_non_string_ipv4_address(address: Any, cidr: bool, private: Optional[bool]): |
| 17 | + """Reject non-string inputs even when ipaddress accepts their representation.""" |
| 18 | + assert isinstance(ipv4(address, cidr=cidr, private=private), ValidationError) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize("address", [True, 1, bytes(16), ("::", 0)]) |
| 22 | +@pytest.mark.parametrize("cidr", [True, False]) |
| 23 | +@pytest.mark.parametrize("strict", [True, False]) |
| 24 | +def test_rejects_non_string_ipv6_address(address: Any, cidr: bool, strict: bool): |
| 25 | + """Reject non-string addresses and avoid errors from CIDR string operations.""" |
| 26 | + assert isinstance(ipv6(address, cidr=cidr, strict=strict), ValidationError) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize("address", [True, 3232235777, b"\x7f\x00\x00\x01", ("10.0.0.0", 8)]) |
| 30 | +def test_rejects_non_string_strict_ipv4_address(address: Any): |
| 31 | + """Strict CIDR validation must reject non-string input without an AttributeError.""" |
| 32 | + assert isinstance(ipv4(address, strict=True), ValidationError) |
| 33 | + |
| 34 | + |
10 | 35 | @pytest.mark.parametrize( |
11 | 36 | ("address",), |
12 | 37 | [ |
|
0 commit comments