| 1 |
/* |
| 2 |
Copyright 2025 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package net |
| 18 |
|
| 19 |
import ( |
| 20 |
"net" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
// testIP represents a set of equivalent IP address representations. |
| 25 |
type testIP struct { |
| 26 |
desc string |
| 27 |
family IPFamily |
| 28 |
strings []string |
| 29 |
ips []net.IP |
| 30 |
|
| 31 |
skipFamily bool |
| 32 |
skipParse bool |
| 33 |
} |
| 34 |
|
| 35 |
// goodTestIPs are "good" test IP values. For each item: |
| 36 |
// |
| 37 |
// Preconditions (not involving functions in netutils): |
| 38 |
// - Each element of .ips is the same (i.e., .Equal()). |
| 39 |
// - Each element of .ips stringifies to .strings[0]. |
| 40 |
// |
| 41 |
// IPFamily tests (unless `skipFamily: true`): |
| 42 |
// - Each element of .strings should be identified as .family. |
| 43 |
// - Each element of .ips should be identified as .family. |
| 44 |
// |
| 45 |
// Parsing tests (unless `skipParse: true`): |
| 46 |
// - Each element of .strings should parse to a value equal to .ips[0]. |
| 47 |
var goodTestIPs = []testIP{ |
| 48 |
{ |
| 49 |
desc: "IPv4", |
| 50 |
family: IPv4, |
| 51 |
strings: []string{ |
| 52 |
"192.168.0.5", |
| 53 |
"192.168.000.005", |
| 54 |
}, |
| 55 |
ips: []net.IP{ |
| 56 |
net.IPv4(192, 168, 0, 5), |
| 57 |
{192, 168, 0, 5}, |
| 58 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 5}, |
| 59 |
net.ParseIP("192.168.0.5"), |
| 60 |
func() net.IP { ip, _, _ := net.ParseCIDR("192.168.0.5/24"); return ip }(), |
| 61 |
func() net.IP { _, ipnet, _ := net.ParseCIDR("192.168.0.5/32"); return ipnet.IP }(), |
| 62 |
}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
desc: "IPv4 all-zeros", |
| 66 |
family: IPv4, |
| 67 |
strings: []string{ |
| 68 |
"0.0.0.0", |
| 69 |
"000.000.000.000", |
| 70 |
}, |
| 71 |
ips: []net.IP{ |
| 72 |
net.IPv4zero, |
| 73 |
net.IPv4(0, 0, 0, 0), |
| 74 |
{0, 0, 0, 0}, |
| 75 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0, 0, 0, 0}, |
| 76 |
net.ParseIP("0.0.0.0"), |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
desc: "IPv4 broadcast", |
| 81 |
family: IPv4, |
| 82 |
strings: []string{ |
| 83 |
"255.255.255.255", |
| 84 |
}, |
| 85 |
ips: []net.IP{ |
| 86 |
net.IPv4bcast, |
| 87 |
net.IPv4(255, 255, 255, 255), |
| 88 |
{255, 255, 255, 255}, |
| 89 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 255, 255, 255, 255}, |
| 90 |
net.ParseIP("255.255.255.255"), |
| 91 |
// A /32 IPMask is equivalent to 255.255.255.255 |
| 92 |
func() net.IP { _, ipnet, _ := net.ParseCIDR("1.2.3.4/32"); return net.IP(ipnet.Mask) }(), |
| 93 |
}, |
| 94 |
}, |
| 95 |
{ |
| 96 |
desc: "IPv6", |
| 97 |
family: IPv6, |
| 98 |
strings: []string{ |
| 99 |
"2001:db8::5", |
| 100 |
"2001:0db8::0005", |
| 101 |
"2001:DB8::5", |
| 102 |
}, |
| 103 |
ips: []net.IP{ |
| 104 |
{0x20, 0x01, 0x0D, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x05}, |
| 105 |
net.ParseIP("2001:db8::5"), |
| 106 |
func() net.IP { ip, _, _ := net.ParseCIDR("2001:db8::5/64"); return ip }(), |
| 107 |
func() net.IP { _, ipnet, _ := net.ParseCIDR("2001:db8::5/128"); return ipnet.IP }(), |
| 108 |
}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
desc: "IPv6 all-zeros", |
| 112 |
family: IPv6, |
| 113 |
strings: []string{ |
| 114 |
"::", |
| 115 |
"0:0:0:0:0:0:0:0", |
| 116 |
"0000:0000:0000:0000:0000:0000:0000:0000", |
| 117 |
"0::0", |
| 118 |
}, |
| 119 |
ips: []net.IP{ |
| 120 |
net.IPv6zero, |
| 121 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 122 |
net.ParseIP("::"), |
| 123 |
// ::/0 has an IP, network base IP, and Mask that are all |
| 124 |
// equivalent to :: |
| 125 |
func() net.IP { ip, _, _ := net.ParseCIDR("::/0"); return ip }(), |
| 126 |
func() net.IP { _, ipnet, _ := net.ParseCIDR("::/0"); return ipnet.IP }(), |
| 127 |
func() net.IP { _, ipnet, _ := net.ParseCIDR("::/0"); return net.IP(ipnet.Mask) }(), |
| 128 |
}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
desc: "IPv6 loopback", |
| 132 |
family: IPv6, |
| 133 |
strings: []string{ |
| 134 |
"::1", |
| 135 |
"0000:0000:0000:0000:0000:0000:0000:0001", |
| 136 |
}, |
| 137 |
ips: []net.IP{ |
| 138 |
net.IPv6loopback, |
| 139 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, |
| 140 |
net.ParseIP("::1"), |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
desc: "IPv4-mapped IPv6", |
| 145 |
// net.IP can represent an IPv4 address internally as either a 4-byte |
| 146 |
// value or a 16-byte value, but it treats the two forms as equivalent. |
| 147 |
// |
| 148 |
// This test case confirms that: |
| 149 |
// - The 4-byte and 16-byte forms of a given net.IP compare as .Equal(). |
| 150 |
// - Our parsers parse the plain IPv4 and IPv4-mapped IPv6 forms of an |
| 151 |
// IPv4 string to the same thing. |
| 152 |
// - The 4-byte and 16-byte forms of a given net.IP all stringify |
| 153 |
// to the plain IPv4 string form (i.e., .strings[0]). |
| 154 |
family: IPv4, |
| 155 |
strings: []string{ |
| 156 |
"192.168.0.5", |
| 157 |
"::ffff:192.168.0.5", |
| 158 |
"::ffff:0192.0168.0000.0005", |
| 159 |
}, |
| 160 |
ips: []net.IP{ |
| 161 |
{192, 168, 0, 5}, |
| 162 |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 5}, |
| 163 |
net.IPv4(192, 168, 0, 5).To4(), |
| 164 |
net.IPv4(192, 168, 0, 5).To16(), |
| 165 |
net.ParseIP("192.168.0.5").To4(), |
| 166 |
net.ParseIP("192.168.0.5").To16(), |
| 167 |
net.ParseIP("::ffff:192.168.0.5").To4(), |
| 168 |
net.ParseIP("::ffff:192.168.0.5").To16(), |
| 169 |
}, |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
// badTestIPs are bad test IP values. For each item: |
| 174 |
// |
| 175 |
// IPFamily tests (unless `skipFamily: true`): |
| 176 |
// - Each element of .strings should be identified as IPFamilyUnknown. |
| 177 |
// - Each element of .ips should be identified as IPFamilyUnknown. |
| 178 |
// |
| 179 |
// Parsing tests (unless `skipParse: true`): |
| 180 |
// - Each element of .strings should fail to parse. |
| 181 |
// - Each element of .ips should stringify to an error value that fails to parse. |
| 182 |
var badTestIPs = []testIP{ |
| 183 |
{ |
| 184 |
desc: "empty string is not an IP", |
| 185 |
strings: []string{ |
| 186 |
"", |
| 187 |
}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
desc: "random non-IP string is not an IP", |
| 191 |
strings: []string{ |
| 192 |
"bad ip", |
| 193 |
}, |
| 194 |
}, |
| 195 |
{ |
| 196 |
desc: "domain name is not an IP", |
| 197 |
strings: []string{ |
| 198 |
"www.example.com", |
| 199 |
}, |
| 200 |
}, |
| 201 |
{ |
| 202 |
desc: "mangled IPv4 addresses are invalid", |
| 203 |
strings: []string{ |
| 204 |
"1.2.3.400", |
| 205 |
"1.2..4", |
| 206 |
"1.2.3", |
| 207 |
"1.2.3.4.5", |
| 208 |
}, |
| 209 |
}, |
| 210 |
{ |
| 211 |
desc: "mangled IPv6 addresses are invalid", |
| 212 |
strings: []string{ |
| 213 |
"1:2::12345", |
| 214 |
"1::2::3", |
| 215 |
"1:2:::3", |
| 216 |
"1:2:3", |
| 217 |
"1:2:3:4:5:6:7:8:9", |
| 218 |
"1:2:3:4::6:7:8:9", |
| 219 |
}, |
| 220 |
}, |
| 221 |
{ |
| 222 |
desc: "IPs do not have ports or brackets", |
| 223 |
strings: []string{ |
| 224 |
"1.2.3.4:80", |
| 225 |
"[2001:db8::5]", |
| 226 |
"[2001:db8::5]:80", |
| 227 |
"www.example.com:80", |
| 228 |
}, |
| 229 |
}, |
| 230 |
{ |
| 231 |
desc: "IPs with zones are invalid", |
| 232 |
strings: []string{ |
| 233 |
"169.254.169.254%eth0", |
| 234 |
"fe80::1234%eth0", |
| 235 |
}, |
| 236 |
}, |
| 237 |
{ |
| 238 |
desc: "CIDR strings are not IPs", |
| 239 |
strings: []string{ |
| 240 |
"1.2.3.0/24", |
| 241 |
"2001:db8::/64", |
| 242 |
}, |
| 243 |
}, |
| 244 |
{ |
| 245 |
desc: "IPs with whitespace are invalid", |
| 246 |
strings: []string{ |
| 247 |
" 1.2.3.4", |
| 248 |
"1.2.3.4 ", |
| 249 |
" 2001:db8::5", |
| 250 |
"2001:db8::5 ", |
| 251 |
}, |
| 252 |
}, |
| 253 |
{ |
| 254 |
desc: "nil is an invalid net.IP", |
| 255 |
ips: []net.IP{ |
| 256 |
nil, |
| 257 |
}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
desc: "a byte slice of length other than 4 or 16 is an invalid net.IP", |
| 261 |
ips: []net.IP{ |
| 262 |
{}, |
| 263 |
{1, 2, 3}, |
| 264 |
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, |
| 265 |
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, |
| 266 |
}, |
| 267 |
}, |
| 268 |
} |
| 269 |
|
| 270 |
// testCIDR represents a set of equivalent CIDR representations. |
| 271 |
type testCIDR struct { |
| 272 |
desc string |
| 273 |
family IPFamily |
| 274 |
strings []string |
| 275 |
ipnets []*net.IPNet |
| 276 |
|
| 277 |
skipFamily bool |
| 278 |
skipParse bool |
| 279 |
} |
| 280 |
|
| 281 |
// goodTestCIDRs are "good" test CIDR values. For each item: |
| 282 |
// |
| 283 |
// Preconditions: |
| 284 |
// - Each element of .ipnets stringifies to .strings[0]. |
| 285 |
// |
| 286 |
// IPFamily tests (unless `skipFamily: true`): |
| 287 |
// - Each element of .strings should be identified as .family. |
| 288 |
// - Each element of .ipnets should be identified as .family. |
| 289 |
// |
| 290 |
// Parsing tests (unless `skipParse: true`): |
| 291 |
// - Each element of .strings should parse to a value "equal" to .ipnets[0]. |
| 292 |
// |
| 293 |
// (Unlike net.IP, *net.IPNet has no `.Equal()` method, and testing equality "by hand" is |
| 294 |
// complicated (there are 4 equivalent representations of every IPv4 CIDR value), so we |
| 295 |
// just consider two *net.IPNet values to be equal if they stringify to the same value.) |
| 296 |
var goodTestCIDRs = []testCIDR{ |
| 297 |
{ |
| 298 |
desc: "IPv4", |
| 299 |
family: IPv4, |
| 300 |
strings: []string{ |
| 301 |
"1.2.3.0/24", |
| 302 |
}, |
| 303 |
ipnets: []*net.IPNet{ |
| 304 |
{IP: net.IPv4(1, 2, 3, 0), Mask: net.CIDRMask(24, 32)}, |
| 305 |
{IP: net.ParseIP("1.2.3.0"), Mask: net.CIDRMask(24, 32)}, |
| 306 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("1.2.3.0/24"); return ipnet }(), |
| 307 |
}, |
| 308 |
}, |
| 309 |
{ |
| 310 |
desc: "IPv4, single IP", |
| 311 |
family: IPv4, |
| 312 |
strings: []string{ |
| 313 |
"1.1.1.1/32", |
| 314 |
}, |
| 315 |
ipnets: []*net.IPNet{ |
| 316 |
{IP: net.IPv4(1, 1, 1, 1), Mask: net.CIDRMask(32, 32)}, |
| 317 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("1.1.1.1/32"); return ipnet }(), |
| 318 |
}, |
| 319 |
}, |
| 320 |
{ |
| 321 |
desc: "IPv4, all IPs", |
| 322 |
family: IPv4, |
| 323 |
strings: []string{ |
| 324 |
"0.0.0.0/0", |
| 325 |
"000.000.000.000/000", |
| 326 |
}, |
| 327 |
ipnets: []*net.IPNet{ |
| 328 |
{IP: net.IPv4zero.To4(), Mask: net.IPMask(net.IPv4zero.To4())}, |
| 329 |
{IP: net.IPv4(0, 0, 0, 0), Mask: net.CIDRMask(0, 32)}, |
| 330 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("0.0.0.0/0"); return ipnet }(), |
| 331 |
}, |
| 332 |
}, |
| 333 |
{ |
| 334 |
desc: "IPv4 ifaddr (masked)", |
| 335 |
// This tests that if you try to parse an "ifaddr-style" CIDR string with |
| 336 |
// ParseCIDR/ParseCIDRSloppy, the *net.IPNet return value has the bits |
| 337 |
// beyond the prefix length masked out. |
| 338 |
family: IPv4, |
| 339 |
strings: []string{ |
| 340 |
"1.2.3.0/24", |
| 341 |
"1.2.3.4/24", |
| 342 |
"1.2.3.255/24", |
| 343 |
}, |
| 344 |
ipnets: []*net.IPNet{ |
| 345 |
{IP: net.IPv4(1, 2, 3, 0), Mask: net.CIDRMask(24, 32)}, |
| 346 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("1.2.3.0/24"); return ipnet }(), |
| 347 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("1.2.3.4/24"); return ipnet }(), |
| 348 |
}, |
| 349 |
}, |
| 350 |
{ |
| 351 |
desc: "IPv4 ifaddr", |
| 352 |
family: IPv4, |
| 353 |
strings: []string{ |
| 354 |
"1.2.3.4/24", |
| 355 |
}, |
| 356 |
ipnets: []*net.IPNet{ |
| 357 |
{IP: net.IPv4(1, 2, 3, 4), Mask: net.CIDRMask(24, 32)}, |
| 358 |
}, |
| 359 |
|
| 360 |
// The *net.IPNet return value of ParseCIDRSloppy() masks out the lower |
| 361 |
// bits, so the parsed version won't compare equal to .ipnets[0] |
| 362 |
skipParse: true, |
| 363 |
}, |
| 364 |
{ |
| 365 |
desc: "IPv6", |
| 366 |
family: IPv6, |
| 367 |
strings: []string{ |
| 368 |
"2001:db8::/64", |
| 369 |
"2001:db8:0:0:0:0:0:0/64", |
| 370 |
"2001:DB8::/64", |
| 371 |
}, |
| 372 |
ipnets: []*net.IPNet{ |
| 373 |
{IP: net.IP{0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Mask: net.IPMask{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, |
| 374 |
{IP: net.ParseIP("2001:db8::"), Mask: net.CIDRMask(64, 128)}, |
| 375 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("2001:db8::/64"); return ipnet }(), |
| 376 |
}, |
| 377 |
}, |
| 378 |
{ |
| 379 |
desc: "IPv6, all IPs", |
| 380 |
family: IPv6, |
| 381 |
strings: []string{ |
| 382 |
"::/0", |
| 383 |
}, |
| 384 |
ipnets: []*net.IPNet{ |
| 385 |
{IP: net.IPv6zero, Mask: net.IPMask(net.IPv6zero)}, |
| 386 |
{IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Mask: net.CIDRMask(0, 128)}, |
| 387 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("::/0"); return ipnet }(), |
| 388 |
}, |
| 389 |
}, |
| 390 |
{ |
| 391 |
desc: "IPv6, single IP", |
| 392 |
family: IPv6, |
| 393 |
strings: []string{ |
| 394 |
"::1/128", |
| 395 |
}, |
| 396 |
ipnets: []*net.IPNet{ |
| 397 |
{IP: net.IPv6loopback, Mask: net.CIDRMask(128, 128)}, |
| 398 |
{IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}, Mask: net.CIDRMask(128, 128)}, |
| 399 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("::1/128"); return ipnet }(), |
| 400 |
}, |
| 401 |
}, |
| 402 |
{ |
| 403 |
desc: "IPv6 ifaddr (masked)", |
| 404 |
// This tests that if you try to parse an "ifaddr-style" CIDR string with |
| 405 |
// ParseCIDRSloppy, the *net.IPNet return value has the bits beyond the |
| 406 |
// prefix length masked out. |
| 407 |
family: IPv6, |
| 408 |
strings: []string{ |
| 409 |
"2001:db8::/64", |
| 410 |
"2001:db8::1/64", |
| 411 |
"2001:db8::f00f:f0f0:0f0f:000f/64", |
| 412 |
}, |
| 413 |
ipnets: []*net.IPNet{ |
| 414 |
{IP: net.IP{0x20, 0x01, 0x0D, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Mask: net.CIDRMask(64, 128)}, |
| 415 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("2001:db8::/64"); return ipnet }(), |
| 416 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("2001:db8::1/64"); return ipnet }(), |
| 417 |
}, |
| 418 |
}, |
| 419 |
{ |
| 420 |
desc: "IPv6 ifaddr", |
| 421 |
family: IPv6, |
| 422 |
strings: []string{ |
| 423 |
"2001:db8::1/64", |
| 424 |
}, |
| 425 |
ipnets: []*net.IPNet{ |
| 426 |
{IP: net.IP{0x20, 0x01, 0x0D, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, Mask: net.CIDRMask(64, 128)}, |
| 427 |
}, |
| 428 |
|
| 429 |
// The *net.IPNet return value of ParseCIDRSloppy() masks out the lower |
| 430 |
// bits, so the parsed version won't compare equal to .ipnets[0] |
| 431 |
skipParse: true, |
| 432 |
}, |
| 433 |
{ |
| 434 |
desc: "IPv4-mapped IPv6", |
| 435 |
// As in the IP tests, confirm that plain IPv4 and IPv4-mapped IPv6 are |
| 436 |
// treated as equivalent. |
| 437 |
family: IPv4, |
| 438 |
strings: []string{ |
| 439 |
"1.1.1.0/24", |
| 440 |
"::ffff:1.1.1.0/120", |
| 441 |
"::ffff:01.01.01.00/0120", |
| 442 |
}, |
| 443 |
ipnets: []*net.IPNet{ |
| 444 |
{IP: net.IP{1, 1, 1, 0}, Mask: net.CIDRMask(24, 32)}, |
| 445 |
{IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 1, 1, 1, 0}, Mask: net.CIDRMask(120, 128)}, |
| 446 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("1.1.1.0/24"); return ipnet }(), |
| 447 |
func() *net.IPNet { _, ipnet, _ := net.ParseCIDR("::ffff:1.1.1.0/120"); return ipnet }(), |
| 448 |
|
| 449 |
// Explicitly test each of the 4 different combinations of 4-byte |
| 450 |
// or 16-byte IP and 4-byte or 16-byte Mask, all of which should |
| 451 |
// compare as equal and re-stringify to "1.1.1.0/24". |
| 452 |
{IP: net.IP{1, 1, 1, 0}, Mask: net.IPMask{255, 255, 255, 0}}, |
| 453 |
{IP: net.IP{1, 1, 1, 0}, Mask: net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0}}, |
| 454 |
{IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 1, 1, 1, 0}, Mask: net.IPMask{255, 255, 255, 0}}, |
| 455 |
{IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 1, 1, 1, 0}, Mask: net.IPMask{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0}}, |
| 456 |
}, |
| 457 |
}, |
| 458 |
} |
| 459 |
|
| 460 |
// badTestCIDRs are bad test CIDR values. For each item: |
| 461 |
// |
| 462 |
// IPFamily tests (unless `skipFamily: true`): |
| 463 |
// - Each element of .strings should be identified as IPFamilyUnknown. |
| 464 |
// - Each element of .ipnets should be identified as IPFamilyUnknown. |
| 465 |
// |
| 466 |
// Parsing tests (unless `skipParse: true`): |
| 467 |
// - Each element of .strings should fail to parse. |
| 468 |
// - Each element of .ipnets should stringify to some error value that fails to parse. |
| 469 |
var badTestCIDRs = []testCIDR{ |
| 470 |
{ |
| 471 |
desc: "empty string is not a CIDR", |
| 472 |
strings: []string{ |
| 473 |
"", |
| 474 |
}, |
| 475 |
}, |
| 476 |
{ |
| 477 |
desc: "random unparseable string is not a CIDR", |
| 478 |
strings: []string{ |
| 479 |
"bad cidr", |
| 480 |
}, |
| 481 |
}, |
| 482 |
{ |
| 483 |
desc: "CIDR with invalid IP is invalid", |
| 484 |
strings: []string{ |
| 485 |
"1.2.300.0/24", |
| 486 |
"2001:db8000::/64", |
| 487 |
}, |
| 488 |
}, |
| 489 |
{ |
| 490 |
desc: "CIDR with invalid prefix length is invalid", |
| 491 |
strings: []string{ |
| 492 |
"1.2.3.4/64", |
| 493 |
"2001:db8::5/192", |
| 494 |
"1.2.3.0/-8", |
| 495 |
"1.2.3.0/+24", |
| 496 |
}, |
| 497 |
}, |
| 498 |
{ |
| 499 |
desc: "URLs (that aren't also valid CIDRs) are invalid", |
| 500 |
strings: []string{ |
| 501 |
"www.example.com/24", |
| 502 |
"192.168.0.1/0/99", |
| 503 |
}, |
| 504 |
}, |
| 505 |
{ |
| 506 |
desc: "plain IP is not a CIDR", |
| 507 |
strings: []string{ |
| 508 |
"1.2.3.4", |
| 509 |
"2001:db8::1", |
| 510 |
}, |
| 511 |
}, |
| 512 |
{ |
| 513 |
desc: "CIDR with whitespace is invalid", |
| 514 |
strings: []string{ |
| 515 |
" 1.2.3.0/24", |
| 516 |
"1.2.3.0/24 ", |
| 517 |
}, |
| 518 |
}, |
| 519 |
{ |
| 520 |
desc: "nil is an invalid IPNet", |
| 521 |
ipnets: []*net.IPNet{ |
| 522 |
nil, |
| 523 |
}, |
| 524 |
}, |
| 525 |
{ |
| 526 |
desc: "IPNet containing invalid IP is invalid", |
| 527 |
ipnets: []*net.IPNet{ |
| 528 |
{IP: net.IP{0x1}, Mask: net.CIDRMask(24, 32)}, |
| 529 |
}, |
| 530 |
}, |
| 531 |
{ |
| 532 |
desc: "IPNet containing non-CIDR Mask is invalid", |
| 533 |
ipnets: []*net.IPNet{ |
| 534 |
{IP: net.IP{192, 168, 0, 0}, Mask: net.IPMask{255, 0, 255, 0}}, |
| 535 |
}, |
| 536 |
|
| 537 |
// IPFamilyOfCIDR only looks at IP and doesn't notice that Mask is invalid |
| 538 |
skipFamily: true, |
| 539 |
}, |
| 540 |
{ |
| 541 |
desc: "IPNet containing IPv6 IP and IPv4 Mask is invalid", |
| 542 |
ipnets: []*net.IPNet{ |
| 543 |
{IP: net.IP{0x20, 0x01, 0x0D, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Mask: net.CIDRMask(24, 32)}, |
| 544 |
}, |
| 545 |
|
| 546 |
// IPFamilyOfCIDR only looks at IP and doesn't notice that Mask is invalid |
| 547 |
skipFamily: true, |
| 548 |
}, |
| 549 |
} |
| 550 |
|
| 551 |
// TestGoodTestIPs confirms the Preconditions for goodTestIPs. |
| 552 |
func TestGoodTestIPs(t *testing.T) { |
| 553 |
for _, tc := range goodTestIPs { |
| 554 |
t.Run(tc.desc, func(t *testing.T) { |
| 555 |
for i, ip := range tc.ips { |
| 556 |
if !ip.Equal(tc.ips[0]) { |
| 557 |
t.Errorf("BAD TEST DATA: IP %d %#v %q does not equal %#v %q", i+1, ip, ip, tc.ips[0], tc.ips[0]) |
| 558 |
} |
| 559 |
str := ip.String() |
| 560 |
if str != tc.strings[0] { |
| 561 |
t.Errorf("BAD TEST DATA: IP %d %#v %q does not stringify to %q", i+1, ip, ip, tc.strings[0]) |
| 562 |
} |
| 563 |
} |
| 564 |
}) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
// TestGoodTestCIDRs confirms the Preconditions for goodTestCIDRs. |
| 569 |
func TestGoodTestCIDRs(t *testing.T) { |
| 570 |
for _, tc := range goodTestCIDRs { |
| 571 |
t.Run(tc.desc, func(t *testing.T) { |
| 572 |
for i, ipnet := range tc.ipnets { |
| 573 |
if ipnet.String() != tc.strings[0] { |
| 574 |
t.Errorf("BAD TEST DATA: IPNet %d %#v %q does not stringify to %q", i+1, ipnet, ipnet, tc.strings[0]) |
| 575 |
} |
| 576 |
} |
| 577 |
}) |
| 578 |
} |
| 579 |
} |