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