name_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "strings"
  16. "testing"
  17. )
  18. func TestValidatePathSegmentName(t *testing.T) {
  19. testcases := map[string]struct {
  20. Name string
  21. Prefix bool
  22. ExpectedMsg string
  23. }{
  24. "empty": {
  25. Name: "",
  26. Prefix: false,
  27. ExpectedMsg: "",
  28. },
  29. "empty,prefix": {
  30. Name: "",
  31. Prefix: true,
  32. ExpectedMsg: "",
  33. },
  34. "valid": {
  35. Name: "foo.bar.baz",
  36. Prefix: false,
  37. ExpectedMsg: "",
  38. },
  39. "valid,prefix": {
  40. Name: "foo.bar.baz",
  41. Prefix: true,
  42. ExpectedMsg: "",
  43. },
  44. // Make sure mixed case, non DNS subdomain characters are tolerated
  45. "valid complex": {
  46. Name: "sha256:ABCDEF012345@ABCDEF012345",
  47. Prefix: false,
  48. ExpectedMsg: "",
  49. },
  50. // Make sure non-ascii characters are tolerated
  51. "valid extended charset": {
  52. Name: "Iñtërnâtiônàlizætiøn",
  53. Prefix: false,
  54. ExpectedMsg: "",
  55. },
  56. "dot": {
  57. Name: ".",
  58. Prefix: false,
  59. ExpectedMsg: ".",
  60. },
  61. "dot leading": {
  62. Name: ".test",
  63. Prefix: false,
  64. ExpectedMsg: "",
  65. },
  66. "dot,prefix": {
  67. Name: ".",
  68. Prefix: true,
  69. ExpectedMsg: "",
  70. },
  71. "dot dot": {
  72. Name: "..",
  73. Prefix: false,
  74. ExpectedMsg: "..",
  75. },
  76. "dot dot leading": {
  77. Name: "..test",
  78. Prefix: false,
  79. ExpectedMsg: "",
  80. },
  81. "dot dot,prefix": {
  82. Name: "..",
  83. Prefix: true,
  84. ExpectedMsg: "",
  85. },
  86. "slash": {
  87. Name: "foo/bar",
  88. Prefix: false,
  89. ExpectedMsg: "/",
  90. },
  91. "slash,prefix": {
  92. Name: "foo/bar",
  93. Prefix: true,
  94. ExpectedMsg: "/",
  95. },
  96. "percent": {
  97. Name: "foo%bar",
  98. Prefix: false,
  99. ExpectedMsg: "%",
  100. },
  101. "percent,prefix": {
  102. Name: "foo%bar",
  103. Prefix: true,
  104. ExpectedMsg: "%",
  105. },
  106. }
  107. for k, tc := range testcases {
  108. msgs := ValidatePathSegmentName(tc.Name, tc.Prefix)
  109. if len(tc.ExpectedMsg) == 0 && len(msgs) > 0 {
  110. t.Errorf("%s: expected no message, got %v", k, msgs)
  111. }
  112. if len(tc.ExpectedMsg) > 0 && len(msgs) == 0 {
  113. t.Errorf("%s: expected error message, got none", k)
  114. }
  115. if len(tc.ExpectedMsg) > 0 && !strings.Contains(msgs[0], tc.ExpectedMsg) {
  116. t.Errorf("%s: expected message containing %q, got %v", k, tc.ExpectedMsg, msgs[0])
  117. }
  118. }
  119. }