range_perm_cache_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2016 The etcd 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 auth
  15. import (
  16. "bytes"
  17. "testing"
  18. )
  19. func isPermsEqual(a, b []*rangePerm) bool {
  20. if len(a) != len(b) {
  21. return false
  22. }
  23. for i := range a {
  24. if len(b) <= i {
  25. return false
  26. }
  27. if !bytes.Equal(a[i].begin, b[i].begin) || !bytes.Equal(a[i].end, b[i].end) {
  28. return false
  29. }
  30. }
  31. return true
  32. }
  33. func TestGetMergedPerms(t *testing.T) {
  34. tests := []struct {
  35. params []*rangePerm
  36. want []*rangePerm
  37. }{
  38. {
  39. []*rangePerm{{[]byte("a"), []byte("b")}},
  40. []*rangePerm{{[]byte("a"), []byte("b")}},
  41. },
  42. {
  43. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("")}},
  44. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("")}},
  45. },
  46. {
  47. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}},
  48. []*rangePerm{{[]byte("a"), []byte("c")}},
  49. },
  50. {
  51. []*rangePerm{{[]byte("a"), []byte("c")}, {[]byte("b"), []byte("d")}},
  52. []*rangePerm{{[]byte("a"), []byte("d")}},
  53. },
  54. {
  55. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}, {[]byte("d"), []byte("e")}},
  56. []*rangePerm{{[]byte("a"), []byte("c")}, {[]byte("d"), []byte("e")}},
  57. },
  58. {
  59. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  60. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  61. },
  62. {
  63. []*rangePerm{{[]byte("e"), []byte("f")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("b")}},
  64. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  65. },
  66. {
  67. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}},
  68. []*rangePerm{{[]byte("a"), []byte("z")}},
  69. },
  70. {
  71. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}, {[]byte("1"), []byte("9")}},
  72. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("z")}},
  73. },
  74. {
  75. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}, {[]byte("1"), []byte("a")}},
  76. []*rangePerm{{[]byte("1"), []byte("z")}},
  77. },
  78. {
  79. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("a"), []byte("z")}, {[]byte("5"), []byte("6")}, {[]byte("1"), []byte("9")}},
  80. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("z")}},
  81. },
  82. {
  83. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}, {[]byte("c"), []byte("d")}, {[]byte("d"), []byte("f")}, {[]byte("1"), []byte("9")}},
  84. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("f")}},
  85. },
  86. // overlapping
  87. {
  88. []*rangePerm{{[]byte("a"), []byte("f")}, {[]byte("b"), []byte("g")}},
  89. []*rangePerm{{[]byte("a"), []byte("g")}},
  90. },
  91. // keys
  92. {
  93. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("")}},
  94. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("")}},
  95. },
  96. {
  97. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("a"), []byte("c")}},
  98. []*rangePerm{{[]byte("a"), []byte("c")}},
  99. },
  100. {
  101. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("a"), []byte("c")}, {[]byte("b"), []byte("")}},
  102. []*rangePerm{{[]byte("a"), []byte("c")}},
  103. },
  104. {
  105. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("c")}, {[]byte("b"), []byte("")}, {[]byte("c"), []byte("")}, {[]byte("d"), []byte("")}},
  106. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("c")}, {[]byte("c"), []byte("")}, {[]byte("d"), []byte("")}},
  107. },
  108. // duplicate ranges
  109. {
  110. []*rangePerm{{[]byte("a"), []byte("f")}, {[]byte("a"), []byte("f")}},
  111. []*rangePerm{{[]byte("a"), []byte("f")}},
  112. },
  113. // duplicate keys
  114. {
  115. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("a"), []byte("")}, {[]byte("a"), []byte("")}},
  116. []*rangePerm{{[]byte("a"), []byte("")}},
  117. },
  118. }
  119. for i, tt := range tests {
  120. result := mergeRangePerms(tt.params)
  121. if !isPermsEqual(result, tt.want) {
  122. t.Errorf("#%d: result=%q, want=%q", i, result, tt.want)
  123. }
  124. }
  125. }