validate_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2017, OpenCensus Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tag
  15. import (
  16. "strings"
  17. "testing"
  18. )
  19. func TestCheckKeyName(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. key string
  23. wantOK bool
  24. }{
  25. {
  26. name: "valid",
  27. key: "k1",
  28. wantOK: true,
  29. },
  30. {
  31. name: "invalid i",
  32. key: "k\x19",
  33. wantOK: false,
  34. },
  35. {
  36. name: "invalid ii",
  37. key: "k\x7f",
  38. wantOK: false,
  39. },
  40. {
  41. name: "empty",
  42. key: "",
  43. wantOK: false,
  44. },
  45. {
  46. name: "whitespace",
  47. key: " k ",
  48. wantOK: true,
  49. },
  50. {
  51. name: "long",
  52. key: strings.Repeat("a", 256),
  53. wantOK: false,
  54. },
  55. }
  56. for _, tt := range tests {
  57. ok := checkKeyName(tt.key)
  58. if ok != tt.wantOK {
  59. t.Errorf("%v: got %v; want %v", tt.name, ok, tt.wantOK)
  60. }
  61. }
  62. }
  63. func TestCheckValue(t *testing.T) {
  64. tests := []struct {
  65. name string
  66. value string
  67. wantOK bool
  68. }{
  69. {
  70. name: "valid",
  71. value: "v1",
  72. wantOK: true,
  73. },
  74. {
  75. name: "invalid i",
  76. value: "k\x19",
  77. wantOK: false,
  78. },
  79. {
  80. name: "invalid ii",
  81. value: "k\x7f",
  82. wantOK: false,
  83. },
  84. {
  85. name: "empty",
  86. value: "",
  87. wantOK: true,
  88. },
  89. {
  90. name: "whitespace",
  91. value: " v ",
  92. wantOK: true,
  93. },
  94. {
  95. name: "long",
  96. value: strings.Repeat("a", 256),
  97. wantOK: false,
  98. },
  99. }
  100. for _, tt := range tests {
  101. ok := checkValue(tt.value)
  102. if ok != tt.wantOK {
  103. t.Errorf("%v: got %v; want %v", tt.name, ok, tt.wantOK)
  104. }
  105. }
  106. }