k8s.io/apimachinery@v0.29.2/pkg/util/validation/validation_test.go (about) 1 /* 2 Copyright 2014 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 validation 18 19 import ( 20 "strings" 21 "testing" 22 23 "k8s.io/apimachinery/pkg/util/validation/field" 24 ) 25 26 func TestIsDNS1123Label(t *testing.T) { 27 goodValues := []string{ 28 "a", "ab", "abc", "a1", "a-1", "a--1--2--b", 29 "0", "01", "012", "1a", "1-a", "1--a--b--2", 30 strings.Repeat("a", 63), 31 } 32 for _, val := range goodValues { 33 if msgs := IsDNS1123Label(val); len(msgs) != 0 { 34 t.Errorf("expected true for '%s': %v", val, msgs) 35 } 36 } 37 38 badValues := []string{ 39 "", "A", "ABC", "aBc", "A1", "A-1", "1-A", 40 "-", "a-", "-a", "1-", "-1", 41 "_", "a_", "_a", "a_b", "1_", "_1", "1_2", 42 ".", "a.", ".a", "a.b", "1.", ".1", "1.2", 43 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 44 strings.Repeat("a", 64), 45 } 46 for _, val := range badValues { 47 if msgs := IsDNS1123Label(val); len(msgs) == 0 { 48 t.Errorf("expected false for '%s'", val) 49 } 50 } 51 } 52 53 func TestIsDNS1123Subdomain(t *testing.T) { 54 goodValues := []string{ 55 "a", "ab", "abc", "a1", "a-1", "a--1--2--b", 56 "0", "01", "012", "1a", "1-a", "1--a--b--2", 57 "a.a", "ab.a", "abc.a", "a1.a", "a-1.a", "a--1--2--b.a", 58 "a.1", "ab.1", "abc.1", "a1.1", "a-1.1", "a--1--2--b.1", 59 "0.a", "01.a", "012.a", "1a.a", "1-a.a", "1--a--b--2", 60 "0.1", "01.1", "012.1", "1a.1", "1-a.1", "1--a--b--2.1", 61 "a.b.c.d.e", "aa.bb.cc.dd.ee", "1.2.3.4.5", "11.22.33.44.55", 62 strings.Repeat("a", 253), 63 } 64 for _, val := range goodValues { 65 if msgs := IsDNS1123Subdomain(val); len(msgs) != 0 { 66 t.Errorf("expected true for '%s': %v", val, msgs) 67 } 68 } 69 70 badValues := []string{ 71 "", "A", "ABC", "aBc", "A1", "A-1", "1-A", 72 "-", "a-", "-a", "1-", "-1", 73 "_", "a_", "_a", "a_b", "1_", "_1", "1_2", 74 ".", "a.", ".a", "a..b", "1.", ".1", "1..2", 75 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 76 "A.a", "aB.a", "ab.A", "A1.a", "a1.A", 77 "A.1", "aB.1", "A1.1", "1A.1", 78 "0.A", "01.A", "012.A", "1A.a", "1a.A", 79 "A.B.C.D.E", "AA.BB.CC.DD.EE", "a.B.c.d.e", "aa.bB.cc.dd.ee", 80 "a@b", "a,b", "a_b", "a;b", 81 "a:b", "a%b", "a?b", "a$b", 82 strings.Repeat("a", 254), 83 } 84 for _, val := range badValues { 85 if msgs := IsDNS1123Subdomain(val); len(msgs) == 0 { 86 t.Errorf("expected false for '%s'", val) 87 } 88 } 89 } 90 91 func TestIsDNS1035Label(t *testing.T) { 92 goodValues := []string{ 93 "a", "ab", "abc", "a1", "a-1", "a--1--2--b", 94 strings.Repeat("a", 63), 95 } 96 for _, val := range goodValues { 97 if msgs := IsDNS1035Label(val); len(msgs) != 0 { 98 t.Errorf("expected true for '%s': %v", val, msgs) 99 } 100 } 101 102 badValues := []string{ 103 "0", "01", "012", "1a", "1-a", "1--a--b--2", 104 "", "A", "ABC", "aBc", "A1", "A-1", "1-A", 105 "-", "a-", "-a", "1-", "-1", 106 "_", "a_", "_a", "a_b", "1_", "_1", "1_2", 107 ".", "a.", ".a", "a.b", "1.", ".1", "1.2", 108 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 109 strings.Repeat("a", 64), 110 } 111 for _, val := range badValues { 112 if msgs := IsDNS1035Label(val); len(msgs) == 0 { 113 t.Errorf("expected false for '%s'", val) 114 } 115 } 116 } 117 118 func TestIsCIdentifier(t *testing.T) { 119 goodValues := []string{ 120 "a", "ab", "abc", "a1", "_a", "a_", "a_b", "a_1", "a__1__2__b", "__abc_123", 121 "A", "AB", "AbC", "A1", "_A", "A_", "A_B", "A_1", "A__1__2__B", "__123_ABC", 122 } 123 for _, val := range goodValues { 124 if msgs := IsCIdentifier(val); len(msgs) != 0 { 125 t.Errorf("expected true for '%s': %v", val, msgs) 126 } 127 } 128 129 badValues := []string{ 130 "", "1", "123", "1a", 131 "-", "a-", "-a", "1-", "-1", "1_", "1_2", 132 ".", "a.", ".a", "a.b", "1.", ".1", "1.2", 133 " ", "a ", " a", "a b", "1 ", " 1", "1 2", 134 "#a#", 135 } 136 for _, val := range badValues { 137 if msgs := IsCIdentifier(val); len(msgs) == 0 { 138 t.Errorf("expected false for '%s'", val) 139 } 140 } 141 } 142 143 func TestIsValidPortNum(t *testing.T) { 144 goodValues := []int{1, 2, 1000, 16384, 32768, 65535} 145 for _, val := range goodValues { 146 if msgs := IsValidPortNum(val); len(msgs) != 0 { 147 t.Errorf("expected true for %d, got %v", val, msgs) 148 } 149 } 150 151 badValues := []int{0, -1, 65536, 100000} 152 for _, val := range badValues { 153 if msgs := IsValidPortNum(val); len(msgs) == 0 { 154 t.Errorf("expected false for %d", val) 155 } 156 } 157 } 158 159 func TestIsInRange(t *testing.T) { 160 goodValues := []struct { 161 value int 162 min int 163 max int 164 }{{1, 0, 10}, {5, 5, 20}, {25, 10, 25}} 165 for _, val := range goodValues { 166 if msgs := IsInRange(val.value, val.min, val.max); len(msgs) > 0 { 167 t.Errorf("expected no errors for %#v, but got %v", val, msgs) 168 } 169 } 170 171 badValues := []struct { 172 value int 173 min int 174 max int 175 }{{1, 2, 10}, {5, -4, 2}, {25, 100, 120}} 176 for _, val := range badValues { 177 if msgs := IsInRange(val.value, val.min, val.max); len(msgs) == 0 { 178 t.Errorf("expected errors for %#v", val) 179 } 180 } 181 } 182 183 func createGroupIDs(ids ...int64) []int64 { 184 var output []int64 185 for _, id := range ids { 186 output = append(output, int64(id)) 187 } 188 return output 189 } 190 191 func createUserIDs(ids ...int64) []int64 { 192 var output []int64 193 for _, id := range ids { 194 output = append(output, int64(id)) 195 } 196 return output 197 } 198 199 func TestIsValidGroupID(t *testing.T) { 200 goodValues := createGroupIDs(0, 1, 1000, 65535, 2147483647) 201 for _, val := range goodValues { 202 if msgs := IsValidGroupID(val); len(msgs) != 0 { 203 t.Errorf("expected true for '%d': %v", val, msgs) 204 } 205 } 206 207 badValues := createGroupIDs(-1, -1003, 2147483648, 4147483647) 208 for _, val := range badValues { 209 if msgs := IsValidGroupID(val); len(msgs) == 0 { 210 t.Errorf("expected false for '%d'", val) 211 } 212 } 213 } 214 215 func TestIsValidUserID(t *testing.T) { 216 goodValues := createUserIDs(0, 1, 1000, 65535, 2147483647) 217 for _, val := range goodValues { 218 if msgs := IsValidUserID(val); len(msgs) != 0 { 219 t.Errorf("expected true for '%d': %v", val, msgs) 220 } 221 } 222 223 badValues := createUserIDs(-1, -1003, 2147483648, 4147483647) 224 for _, val := range badValues { 225 if msgs := IsValidUserID(val); len(msgs) == 0 { 226 t.Errorf("expected false for '%d'", val) 227 } 228 } 229 } 230 231 func TestIsValidPortName(t *testing.T) { 232 goodValues := []string{"telnet", "re-mail-ck", "pop3", "a", "a-1", "1-a", "a-1-b-2-c", "1-a-2-b-3"} 233 for _, val := range goodValues { 234 if msgs := IsValidPortName(val); len(msgs) != 0 { 235 t.Errorf("expected true for %q: %v", val, msgs) 236 } 237 } 238 239 badValues := []string{"longerthan15characters", "", strings.Repeat("a", 16), "12345", "1-2-3-4", "-begin", "end-", "two--hyphens", "whois++"} 240 for _, val := range badValues { 241 if msgs := IsValidPortName(val); len(msgs) == 0 { 242 t.Errorf("expected false for %q", val) 243 } 244 } 245 } 246 247 func TestIsQualifiedName(t *testing.T) { 248 successCases := []string{ 249 "simple", 250 "now-with-dashes", 251 "1-starts-with-num", 252 "1234", 253 "simple/simple", 254 "now-with-dashes/simple", 255 "now-with-dashes/now-with-dashes", 256 "now.with.dots/simple", 257 "now-with.dashes-and.dots/simple", 258 "1-num.2-num/3-num", 259 "1234/5678", 260 "1.2.3.4/5678", 261 "Uppercase_Is_OK_123", 262 "example.com/Uppercase_Is_OK_123", 263 "requests.storage-foo", 264 strings.Repeat("a", 63), 265 strings.Repeat("a", 253) + "/" + strings.Repeat("b", 63), 266 } 267 for i := range successCases { 268 if errs := IsQualifiedName(successCases[i]); len(errs) != 0 { 269 t.Errorf("case[%d]: %q: expected success: %v", i, successCases[i], errs) 270 } 271 } 272 273 errorCases := []string{ 274 "nospecialchars%^=@", 275 "cantendwithadash-", 276 "-cantstartwithadash-", 277 "only/one/slash", 278 "Example.com/abc", 279 "example_com/abc", 280 "example.com/", 281 "/simple", 282 strings.Repeat("a", 64), 283 strings.Repeat("a", 254) + "/abc", 284 } 285 for i := range errorCases { 286 if errs := IsQualifiedName(errorCases[i]); len(errs) == 0 { 287 t.Errorf("case[%d]: %q: expected failure", i, errorCases[i]) 288 } 289 } 290 } 291 292 func TestIsValidLabelValue(t *testing.T) { 293 successCases := []string{ 294 "simple", 295 "now-with-dashes", 296 "1-starts-with-num", 297 "end-with-num-1", 298 "1234", // only num 299 strings.Repeat("a", 63), // to the limit 300 "", // empty value 301 } 302 for i := range successCases { 303 if errs := IsValidLabelValue(successCases[i]); len(errs) != 0 { 304 t.Errorf("case %s expected success: %v", successCases[i], errs) 305 } 306 } 307 308 errorCases := []string{ 309 "nospecialchars%^=@", 310 "Tama-nui-te-rā.is.Māori.sun", 311 "\\backslashes\\are\\bad", 312 "-starts-with-dash", 313 "ends-with-dash-", 314 ".starts.with.dot", 315 "ends.with.dot.", 316 strings.Repeat("a", 64), // over the limit 317 } 318 for i := range errorCases { 319 if errs := IsValidLabelValue(errorCases[i]); len(errs) == 0 { 320 t.Errorf("case[%d] expected failure", i) 321 } 322 } 323 } 324 325 func TestIsValidIP(t *testing.T) { 326 goodValues := []string{ 327 "::1", 328 "2a00:79e0:2:0:f1c3:e797:93c1:df80", 329 "::", 330 "2001:4860:4860::8888", 331 "::fff:1.1.1.1", 332 "1.1.1.1", 333 "1.1.1.01", 334 "255.0.0.1", 335 "1.0.0.0", 336 "0.0.0.0", 337 } 338 for _, val := range goodValues { 339 if msgs := IsValidIP(val); len(msgs) != 0 { 340 t.Errorf("expected true for %q: %v", val, msgs) 341 } 342 } 343 344 badValues := []string{ 345 "[2001:db8:0:1]:80", 346 "myhost.mydomain", 347 "-1.0.0.0", 348 "[2001:db8:0:1]", 349 "a", 350 } 351 for _, val := range badValues { 352 if msgs := IsValidIP(val); len(msgs) == 0 { 353 t.Errorf("expected false for %q", val) 354 } 355 } 356 } 357 358 func TestIsValidIPv4Address(t *testing.T) { 359 goodValues := []string{ 360 "1.1.1.1", 361 "1.1.1.01", 362 "255.0.0.1", 363 "1.0.0.0", 364 "0.0.0.0", 365 } 366 for _, val := range goodValues { 367 if msgs := IsValidIPv4Address(field.NewPath(""), val); len(msgs) != 0 { 368 t.Errorf("expected %q to be valid IPv4 address: %v", val, msgs) 369 } 370 } 371 372 badValues := []string{ 373 "[2001:db8:0:1]:80", 374 "myhost.mydomain", 375 "-1.0.0.0", 376 "[2001:db8:0:1]", 377 "a", 378 "2001:4860:4860::8888", 379 "::fff:1.1.1.1", 380 "::1", 381 "2a00:79e0:2:0:f1c3:e797:93c1:df80", 382 "::", 383 } 384 for _, val := range badValues { 385 if msgs := IsValidIPv4Address(field.NewPath(""), val); len(msgs) == 0 { 386 t.Errorf("expected %q to be invalid IPv4 address", val) 387 } 388 } 389 } 390 391 func TestIsValidIPv6Address(t *testing.T) { 392 goodValues := []string{ 393 "2001:4860:4860::8888", 394 "2a00:79e0:2:0:f1c3:e797:93c1:df80", 395 "2001:0db8:85a3:0000:0000:8a2e:0370:7334", 396 "::fff:1.1.1.1", 397 "::1", 398 "::", 399 } 400 401 for _, val := range goodValues { 402 if msgs := IsValidIPv6Address(field.NewPath(""), val); len(msgs) != 0 { 403 t.Errorf("expected %q to be valid IPv6 address: %v", val, msgs) 404 } 405 } 406 407 badValues := []string{ 408 "1.1.1.1", 409 "1.1.1.01", 410 "255.0.0.1", 411 "1.0.0.0", 412 "0.0.0.0", 413 "[2001:db8:0:1]:80", 414 "myhost.mydomain", 415 "2001:0db8:85a3:0000:0000:8a2e:0370:7334:2001:0db8:85a3:0000:0000:8a2e:0370:7334", 416 "-1.0.0.0", 417 "[2001:db8:0:1]", 418 "a", 419 } 420 for _, val := range badValues { 421 if msgs := IsValidIPv6Address(field.NewPath(""), val); len(msgs) == 0 { 422 t.Errorf("expected %q to be invalid IPv6 address", val) 423 } 424 } 425 } 426 427 func TestIsHTTPHeaderName(t *testing.T) { 428 goodValues := []string{ 429 // Common ones 430 "Accept-Encoding", "Host", "If-Modified-Since", "X-Forwarded-For", 431 // Weirdo, but still conforming names 432 "a", "ab", "abc", "a1", "-a", "a-", "a-b", "a-1", "a--1--2--b", "--abc-123", 433 "A", "AB", "AbC", "A1", "-A", "A-", "A-B", "A-1", "A--1--2--B", "--123-ABC", 434 } 435 for _, val := range goodValues { 436 if msgs := IsHTTPHeaderName(val); len(msgs) != 0 { 437 t.Errorf("expected true for '%s': %v", val, msgs) 438 } 439 } 440 441 badValues := []string{ 442 "Host:", "X-Forwarded-For:", "X-@Home", 443 "", "_", "a_", "_a", "1_", "1_2", ".", "a.", ".a", "a.b", "1.", ".1", "1.2", 444 " ", "a ", " a", "a b", "1 ", " 1", "1 2", "#a#", "^", ",", ";", "=", "<", 445 "?", "@", "{", 446 } 447 for _, val := range badValues { 448 if msgs := IsHTTPHeaderName(val); len(msgs) == 0 { 449 t.Errorf("expected false for '%s'", val) 450 } 451 } 452 } 453 454 func TestIsValidPercent(t *testing.T) { 455 goodValues := []string{ 456 "0%", 457 "00000%", 458 "1%", 459 "01%", 460 "99%", 461 "100%", 462 "101%", 463 } 464 for _, val := range goodValues { 465 if msgs := IsValidPercent(val); len(msgs) != 0 { 466 t.Errorf("expected true for %q: %v", val, msgs) 467 } 468 } 469 470 badValues := []string{ 471 "", 472 "0", 473 "100", 474 "0.0%", 475 "99.9%", 476 "hundred", 477 " 1%", 478 "1% ", 479 "-0%", 480 "-1%", 481 "+1%", 482 } 483 for _, val := range badValues { 484 if msgs := IsValidPercent(val); len(msgs) == 0 { 485 t.Errorf("expected false for %q", val) 486 } 487 } 488 } 489 490 func TestIsConfigMapKey(t *testing.T) { 491 successCases := []string{ 492 "a", 493 "good", 494 "good-good", 495 "still.good", 496 "this.is.also.good", 497 ".so.is.this", 498 "THIS_IS_GOOD", 499 "so_is_this_17", 500 } 501 502 for i := range successCases { 503 if errs := IsConfigMapKey(successCases[i]); len(errs) != 0 { 504 t.Errorf("[%d] expected success: %v", i, errs) 505 } 506 } 507 508 failureCases := []string{ 509 ".", 510 "..", 511 "..bad", 512 "b*d", 513 "bad!&bad", 514 } 515 516 for i := range failureCases { 517 if errs := IsConfigMapKey(failureCases[i]); len(errs) == 0 { 518 t.Errorf("[%d] expected failure", i) 519 } 520 } 521 } 522 523 func TestIsWildcardDNS1123Subdomain(t *testing.T) { 524 goodValues := []string{ 525 "*.example.com", 526 "*.bar.com", 527 "*.foo.bar.com", 528 } 529 for _, val := range goodValues { 530 if errs := IsWildcardDNS1123Subdomain(val); len(errs) != 0 { 531 t.Errorf("expected no errors for %q: %v", val, errs) 532 } 533 } 534 535 badValues := []string{ 536 "*.*.bar.com", 537 "*.foo.*.com", 538 "*bar.com", 539 "f*.bar.com", 540 "*", 541 } 542 for _, val := range badValues { 543 if errs := IsWildcardDNS1123Subdomain(val); len(errs) == 0 { 544 t.Errorf("expected errors for %q", val) 545 } 546 } 547 } 548 549 func TestIsFullyQualifiedDomainName(t *testing.T) { 550 goodValues := []string{ 551 "a.com", 552 "k8s.io", 553 "dev.k8s.io", 554 "dev.k8s.io.", 555 "foo.example.com", 556 "this.is.a.really.long.fqdn", 557 "bbc.co.uk", 558 "10.0.0.1", // DNS labels can start with numbers and there is no requirement for letters. 559 "hyphens-are-good.k8s.io", 560 strings.Repeat("a", 63) + ".k8s.io", 561 strings.Repeat("a", 63) + "." + strings.Repeat("b", 63) + "." + strings.Repeat("c", 63) + "." + strings.Repeat("d", 54) + ".k8s.io", 562 } 563 for _, val := range goodValues { 564 if err := IsFullyQualifiedDomainName(field.NewPath(""), val).ToAggregate(); err != nil { 565 t.Errorf("expected no errors for %q: %v", val, err) 566 } 567 } 568 569 badValues := []string{ 570 ".", 571 "...", 572 ".io", 573 "com", 574 ".com", 575 "Dev.k8s.io", 576 ".foo.example.com", 577 "*.example.com", 578 "*.bar.com", 579 "*.foo.bar.com", 580 "underscores_are_bad.k8s.io", 581 "foo@bar.example.com", 582 "http://foo.example.com", 583 strings.Repeat("a", 64) + ".k8s.io", 584 strings.Repeat("a", 63) + "." + strings.Repeat("b", 63) + "." + strings.Repeat("c", 63) + "." + strings.Repeat("d", 55) + ".k8s.io", 585 } 586 for _, val := range badValues { 587 if err := IsFullyQualifiedDomainName(field.NewPath(""), val).ToAggregate(); err == nil { 588 t.Errorf("expected errors for %q", val) 589 } 590 } 591 } 592 593 func TestIsFullyQualifiedName(t *testing.T) { 594 goodValues := []string{ 595 "dev.k8s.io", 596 "foo.example.com", 597 "this.is.a.really.long.fqdn", 598 "bbc.co.uk", 599 "10.0.0.1", // DNS labels can start with numbers and there is no requirement for letters. 600 "hyphens-are-good.k8s.io", 601 strings.Repeat("a", 246) + ".k8s.io", 602 } 603 for _, val := range goodValues { 604 if err := IsFullyQualifiedName(field.NewPath(""), val).ToAggregate(); err != nil { 605 t.Errorf("expected no errors for %q: %v", val, err) 606 } 607 } 608 609 badValues := []string{ 610 "...", 611 "dev.k8s.io.", 612 ".io", 613 "Dev.k8s.io", 614 "k8s.io", 615 "*.example.com", 616 "*.bar.com", 617 "*.foo.bar.com", 618 "underscores_are_bad.k8s.io", 619 "foo@bar.example.com", 620 "http://foo.example.com", 621 strings.Repeat("a", 247) + ".k8s.io", 622 } 623 for _, val := range badValues { 624 if err := IsFullyQualifiedName(field.NewPath(""), val).ToAggregate(); err == nil { 625 t.Errorf("expected errors for %q", val) 626 } 627 } 628 629 messageTests := []struct { 630 name string 631 targetName string 632 err string 633 }{{ 634 name: "name needs to be fully qualified, i.e., contains at least 2 dots", 635 targetName: "k8s.io", 636 err: "should be a domain with at least three segments separated by dots", 637 }, { 638 name: "name should not include scheme", 639 targetName: "http://foo.k8s.io", 640 err: "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters", 641 }, { 642 name: "email should be invalid", 643 targetName: "example@foo.k8s.io", 644 err: "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters", 645 }, { 646 name: "name cannot be empty", 647 targetName: "", 648 err: "Required value", 649 }, { 650 name: "name must conform to RFC 1123", 651 targetName: "A.B.C", 652 err: "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters", 653 }} 654 for _, tc := range messageTests { 655 err := IsFullyQualifiedName(field.NewPath(""), tc.targetName).ToAggregate() 656 switch { 657 case tc.err == "" && err != nil: 658 t.Errorf("%q: unexpected error: %v", tc.name, err) 659 case tc.err != "" && err == nil: 660 t.Errorf("%q: unexpected no error, expected %s", tc.name, tc.err) 661 case tc.err != "" && err != nil && !strings.Contains(err.Error(), tc.err): 662 t.Errorf("%q: expected %s, got %v", tc.name, tc.err, err) 663 } 664 } 665 } 666 667 func TestIsDomainPrefixedPath(t *testing.T) { 668 goodValues := []string{ 669 "a/b", 670 "a/b/c/d", 671 "a.com/foo", 672 "a.b.c.d/foo", 673 "k8s.io/foo/bar", 674 "k8s.io/FOO/BAR", 675 "dev.k8s.io/more/path", 676 "this.is.a.really.long.fqdn/even/longer/path/just/because", 677 "bbc.co.uk/path/goes/here", 678 "10.0.0.1/foo", 679 "hyphens-are-good.k8s.io/and-in-paths-too", 680 strings.Repeat("a", 240) + ".k8s.io/a", 681 "k8s.io/" + strings.Repeat("a", 240), 682 } 683 for _, val := range goodValues { 684 if err := IsDomainPrefixedPath(field.NewPath(""), val).ToAggregate(); err != nil { 685 t.Errorf("expected no errors for %q: %v", val, err) 686 } 687 } 688 689 badValues := []string{ 690 ".", 691 "...", 692 "/b", 693 "com", 694 ".com", 695 "a.b.c.d/foo?a=b", 696 "a.b.c.d/foo#a", 697 "Dev.k8s.io", 698 ".foo.example.com", 699 "*.example.com", 700 "example.com/foo{}[]@^`", 701 "underscores_are_bad.k8s.io", 702 "underscores_are_bad.k8s.io/foo", 703 "foo@bar.example.com", 704 "foo@bar.example.com/foo", 705 strings.Repeat("a", 247) + ".k8s.io", 706 } 707 for _, val := range badValues { 708 if err := IsDomainPrefixedPath(field.NewPath(""), val).ToAggregate(); err == nil { 709 t.Errorf("expected errors for %q", val) 710 } 711 } 712 } 713 714 func TestIsValidSocketAddr(t *testing.T) { 715 goodValues := []string{ 716 "0.0.0.0:10254", 717 "127.0.0.1:8888", 718 "[2001:db8:1f70::999:de8:7648:6e8]:10254", 719 "[::]:10254", 720 } 721 for _, val := range goodValues { 722 if errs := IsValidSocketAddr(val); len(errs) != 0 { 723 t.Errorf("expected no errors for %q: %v", val, errs) 724 } 725 } 726 727 badValues := []string{ 728 "0.0.0.0.0:2020", 729 "0.0.0.0", 730 "6.6.6.6:909090", 731 "2001:db8:1f70::999:de8:7648:6e8:87567:102545", 732 "", 733 "*", 734 } 735 for _, val := range badValues { 736 if errs := IsValidSocketAddr(val); len(errs) == 0 { 737 t.Errorf("expected errors for %q", val) 738 } 739 } 740 }