| 1 |
from django.core.exceptions import ValidationError |
| 2 |
from django.forms import GenericIPAddressField |
| 3 |
from django.test import SimpleTestCase |
| 4 |
from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH |
| 5 |
|
| 6 |
|
| 7 |
class GenericIPAddressFieldTest(SimpleTestCase): |
| 8 |
def test_generic_ipaddress_invalid_arguments(self): |
| 9 |
with self.assertRaises(ValueError): |
| 10 |
GenericIPAddressField(protocol="hamster") |
| 11 |
with self.assertRaises(ValueError): |
| 12 |
GenericIPAddressField(protocol="ipv4", unpack_ipv4=True) |
| 13 |
|
| 14 |
def test_generic_ipaddress_as_generic(self): |
| 15 |
# The edge cases of the IPv6 validation code are not deeply tested |
| 16 |
# here, they are covered in the tests for django.utils.ipv6 |
| 17 |
f = GenericIPAddressField() |
| 18 |
with self.assertRaisesMessage(ValidationError, "'This field is required.'"): |
| 19 |
f.clean("") |
| 20 |
with self.assertRaisesMessage(ValidationError, "'This field is required.'"): |
| 21 |
f.clean(None) |
| 22 |
self.assertEqual(f.clean(" 127.0.0.1 "), "127.0.0.1") |
| 23 |
with self.assertRaisesMessage( |
| 24 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 25 |
): |
| 26 |
f.clean("foo") |
| 27 |
with self.assertRaisesMessage( |
| 28 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 29 |
): |
| 30 |
f.clean("127.0.0.") |
| 31 |
with self.assertRaisesMessage( |
| 32 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 33 |
): |
| 34 |
f.clean("1.2.3.4.5") |
| 35 |
with self.assertRaisesMessage( |
| 36 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 37 |
): |
| 38 |
f.clean("256.125.1.5") |
| 39 |
self.assertEqual( |
| 40 |
f.clean(" fe80::223:6cff:fe8a:2e8a "), "fe80::223:6cff:fe8a:2e8a" |
| 41 |
) |
| 42 |
self.assertEqual( |
| 43 |
f.clean(" 2a02::223:6cff:fe8a:2e8a "), "2a02::223:6cff:fe8a:2e8a" |
| 44 |
) |
| 45 |
with self.assertRaisesMessage( |
| 46 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 47 |
): |
| 48 |
f.clean("12345:2:3:4") |
| 49 |
with self.assertRaisesMessage( |
| 50 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 51 |
): |
| 52 |
f.clean("1::2:3::4") |
| 53 |
with self.assertRaisesMessage( |
| 54 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 55 |
): |
| 56 |
f.clean("foo::223:6cff:fe8a:2e8a") |
| 57 |
with self.assertRaisesMessage( |
| 58 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 59 |
): |
| 60 |
f.clean("1::2:3:4:5:6:7:8") |
| 61 |
with self.assertRaisesMessage( |
| 62 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 63 |
): |
| 64 |
f.clean("1:2") |
| 65 |
|
| 66 |
def test_generic_ipaddress_as_ipv4_only(self): |
| 67 |
f = GenericIPAddressField(protocol="IPv4") |
| 68 |
with self.assertRaisesMessage(ValidationError, "'This field is required.'"): |
| 69 |
f.clean("") |
| 70 |
with self.assertRaisesMessage(ValidationError, "'This field is required.'"): |
| 71 |
f.clean(None) |
| 72 |
self.assertEqual(f.clean(" 127.0.0.1 "), "127.0.0.1") |
| 73 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"): |
| 74 |
f.clean("foo") |
| 75 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"): |
| 76 |
f.clean("127.0.0.") |
| 77 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"): |
| 78 |
f.clean("1.2.3.4.5") |
| 79 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"): |
| 80 |
f.clean("256.125.1.5") |
| 81 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"): |
| 82 |
f.clean("fe80::223:6cff:fe8a:2e8a") |
| 83 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv4 address.'"): |
| 84 |
f.clean("2a02::223:6cff:fe8a:2e8a") |
| 85 |
|
| 86 |
def test_generic_ipaddress_as_ipv6_only(self): |
| 87 |
f = GenericIPAddressField(protocol="IPv6") |
| 88 |
with self.assertRaisesMessage(ValidationError, "'This field is required.'"): |
| 89 |
f.clean("") |
| 90 |
with self.assertRaisesMessage(ValidationError, "'This field is required.'"): |
| 91 |
f.clean(None) |
| 92 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv6 address.'"): |
| 93 |
f.clean("127.0.0.1") |
| 94 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv6 address.'"): |
| 95 |
f.clean("foo") |
| 96 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv6 address.'"): |
| 97 |
f.clean("127.0.0.") |
| 98 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv6 address.'"): |
| 99 |
f.clean("1.2.3.4.5") |
| 100 |
with self.assertRaisesMessage(ValidationError, "'Enter a valid IPv6 address.'"): |
| 101 |
f.clean("256.125.1.5") |
| 102 |
self.assertEqual( |
| 103 |
f.clean(" fe80::223:6cff:fe8a:2e8a "), "fe80::223:6cff:fe8a:2e8a" |
| 104 |
) |
| 105 |
self.assertEqual( |
| 106 |
f.clean(" 2a02::223:6cff:fe8a:2e8a "), "2a02::223:6cff:fe8a:2e8a" |
| 107 |
) |
| 108 |
with self.assertRaisesMessage( |
| 109 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 110 |
): |
| 111 |
f.clean("12345:2:3:4") |
| 112 |
with self.assertRaisesMessage( |
| 113 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 114 |
): |
| 115 |
f.clean("1::2:3::4") |
| 116 |
with self.assertRaisesMessage( |
| 117 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 118 |
): |
| 119 |
f.clean("foo::223:6cff:fe8a:2e8a") |
| 120 |
with self.assertRaisesMessage( |
| 121 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 122 |
): |
| 123 |
f.clean("1::2:3:4:5:6:7:8") |
| 124 |
with self.assertRaisesMessage( |
| 125 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 126 |
): |
| 127 |
f.clean("1:2") |
| 128 |
|
| 129 |
def test_generic_ipaddress_max_length_custom(self): |
| 130 |
# Valid IPv4-mapped IPv6 address, len 45. |
| 131 |
addr = "0000:0000:0000:0000:0000:ffff:192.168.100.228" |
| 132 |
f = GenericIPAddressField(max_length=len(addr)) |
| 133 |
f.clean(addr) |
| 134 |
|
| 135 |
def test_generic_ipaddress_max_length_validation_error(self): |
| 136 |
# Valid IPv4-mapped IPv6 address, len 45. |
| 137 |
addr = "0000:0000:0000:0000:0000:ffff:192.168.100.228" |
| 138 |
|
| 139 |
cases = [ |
| 140 |
({}, MAX_IPV6_ADDRESS_LENGTH), # Default value. |
| 141 |
({"max_length": len(addr) - 1}, len(addr) - 1), |
| 142 |
] |
| 143 |
for kwargs, max_length in cases: |
| 144 |
max_length_plus_one = max_length + 1 |
| 145 |
msg = ( |
| 146 |
f"Ensure this value has at most {max_length} characters (it has " |
| 147 |
f"{max_length_plus_one}).'" |
| 148 |
) |
| 149 |
with self.subTest(max_length=max_length): |
| 150 |
f = GenericIPAddressField(**kwargs) |
| 151 |
with self.assertRaisesMessage(ValidationError, msg): |
| 152 |
f.clean("x" * max_length_plus_one) |
| 153 |
with self.assertRaisesMessage( |
| 154 |
ValidationError, "This is not a valid IPv6 address." |
| 155 |
): |
| 156 |
f.clean(addr) |
| 157 |
|
| 158 |
def test_generic_ipaddress_as_generic_not_required(self): |
| 159 |
f = GenericIPAddressField(required=False) |
| 160 |
self.assertEqual(f.clean(""), "") |
| 161 |
self.assertEqual(f.clean(None), "") |
| 162 |
self.assertEqual(f.clean("127.0.0.1"), "127.0.0.1") |
| 163 |
with self.assertRaisesMessage( |
| 164 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 165 |
): |
| 166 |
f.clean("foo") |
| 167 |
with self.assertRaisesMessage( |
| 168 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 169 |
): |
| 170 |
f.clean("127.0.0.") |
| 171 |
with self.assertRaisesMessage( |
| 172 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 173 |
): |
| 174 |
f.clean("1.2.3.4.5") |
| 175 |
with self.assertRaisesMessage( |
| 176 |
ValidationError, "'Enter a valid IPv4 or IPv6 address.'" |
| 177 |
): |
| 178 |
f.clean("256.125.1.5") |
| 179 |
self.assertEqual( |
| 180 |
f.clean(" fe80::223:6cff:fe8a:2e8a "), "fe80::223:6cff:fe8a:2e8a" |
| 181 |
) |
| 182 |
self.assertEqual( |
| 183 |
f.clean(" " * MAX_IPV6_ADDRESS_LENGTH + " 2a02::223:6cff:fe8a:2e8a "), |
| 184 |
"2a02::223:6cff:fe8a:2e8a", |
| 185 |
) |
| 186 |
with self.assertRaisesMessage( |
| 187 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 188 |
): |
| 189 |
f.clean("12345:2:3:4") |
| 190 |
with self.assertRaisesMessage( |
| 191 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 192 |
): |
| 193 |
f.clean("1::2:3::4") |
| 194 |
with self.assertRaisesMessage( |
| 195 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 196 |
): |
| 197 |
f.clean("foo::223:6cff:fe8a:2e8a") |
| 198 |
with self.assertRaisesMessage( |
| 199 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 200 |
): |
| 201 |
f.clean("1::2:3:4:5:6:7:8") |
| 202 |
with self.assertRaisesMessage( |
| 203 |
ValidationError, "'This is not a valid IPv6 address.'" |
| 204 |
): |
| 205 |
f.clean("1:2") |
| 206 |
|
| 207 |
def test_generic_ipaddress_normalization(self): |
| 208 |
# Test the normalizing code |
| 209 |
f = GenericIPAddressField() |
| 210 |
self.assertEqual(f.clean(" ::ffff:0a0a:0a0a "), "::ffff:10.10.10.10") |
| 211 |
self.assertEqual(f.clean(" ::ffff:10.10.10.10 "), "::ffff:10.10.10.10") |
| 212 |
self.assertEqual( |
| 213 |
f.clean(" 2001:000:a:0000:0:fe:fe:beef "), "2001:0:a::fe:fe:beef" |
| 214 |
) |
| 215 |
self.assertEqual( |
| 216 |
f.clean(" 2001::a:0000:0:fe:fe:beef "), "2001:0:a::fe:fe:beef" |
| 217 |
) |
| 218 |
|
| 219 |
f = GenericIPAddressField(unpack_ipv4=True) |
| 220 |
self.assertEqual(f.clean(" ::ffff:0a0a:0a0a"), "10.10.10.10") |