range_perm_cache_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "fmt"
  18. "reflect"
  19. "testing"
  20. )
  21. func isPermsEqual(a, b []*rangePerm) bool {
  22. if len(a) != len(b) {
  23. return false
  24. }
  25. for i := range a {
  26. if len(b) <= i {
  27. return false
  28. }
  29. if !bytes.Equal(a[i].begin, b[i].begin) || !bytes.Equal(a[i].end, b[i].end) {
  30. return false
  31. }
  32. }
  33. return true
  34. }
  35. func TestGetMergedPerms(t *testing.T) {
  36. tests := []struct {
  37. params []*rangePerm
  38. want []*rangePerm
  39. }{
  40. {
  41. []*rangePerm{{[]byte("a"), []byte("b")}},
  42. []*rangePerm{{[]byte("a"), []byte("b")}},
  43. },
  44. {
  45. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("")}},
  46. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("")}},
  47. },
  48. {
  49. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}},
  50. []*rangePerm{{[]byte("a"), []byte("c")}},
  51. },
  52. {
  53. []*rangePerm{{[]byte("a"), []byte("c")}, {[]byte("b"), []byte("d")}},
  54. []*rangePerm{{[]byte("a"), []byte("d")}},
  55. },
  56. {
  57. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}, {[]byte("d"), []byte("e")}},
  58. []*rangePerm{{[]byte("a"), []byte("c")}, {[]byte("d"), []byte("e")}},
  59. },
  60. {
  61. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  62. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  63. },
  64. {
  65. []*rangePerm{{[]byte("e"), []byte("f")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("b")}},
  66. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  67. },
  68. {
  69. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}},
  70. []*rangePerm{{[]byte("a"), []byte("z")}},
  71. },
  72. {
  73. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}, {[]byte("1"), []byte("9")}},
  74. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("z")}},
  75. },
  76. {
  77. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}, {[]byte("1"), []byte("a")}},
  78. []*rangePerm{{[]byte("1"), []byte("z")}},
  79. },
  80. {
  81. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("a"), []byte("z")}, {[]byte("5"), []byte("6")}, {[]byte("1"), []byte("9")}},
  82. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("z")}},
  83. },
  84. {
  85. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}, {[]byte("c"), []byte("d")}, {[]byte("d"), []byte("f")}, {[]byte("1"), []byte("9")}},
  86. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("f")}},
  87. },
  88. // overlapping
  89. {
  90. []*rangePerm{{[]byte("a"), []byte("f")}, {[]byte("b"), []byte("g")}},
  91. []*rangePerm{{[]byte("a"), []byte("g")}},
  92. },
  93. // keys
  94. {
  95. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("")}},
  96. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("")}},
  97. },
  98. {
  99. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("a"), []byte("c")}},
  100. []*rangePerm{{[]byte("a"), []byte("c")}},
  101. },
  102. {
  103. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("a"), []byte("c")}, {[]byte("b"), []byte("")}},
  104. []*rangePerm{{[]byte("a"), []byte("c")}},
  105. },
  106. {
  107. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("c")}, {[]byte("b"), []byte("")}, {[]byte("c"), []byte("")}, {[]byte("d"), []byte("")}},
  108. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("b"), []byte("c")}, {[]byte("c"), []byte("")}, {[]byte("d"), []byte("")}},
  109. },
  110. // duplicate ranges
  111. {
  112. []*rangePerm{{[]byte("a"), []byte("f")}, {[]byte("a"), []byte("f")}},
  113. []*rangePerm{{[]byte("a"), []byte("f")}},
  114. },
  115. // duplicate keys
  116. {
  117. []*rangePerm{{[]byte("a"), []byte("")}, {[]byte("a"), []byte("")}, {[]byte("a"), []byte("")}},
  118. []*rangePerm{{[]byte("a"), []byte("")}},
  119. },
  120. }
  121. for i, tt := range tests {
  122. result := mergeRangePerms(tt.params)
  123. if !isPermsEqual(result, tt.want) {
  124. t.Errorf("#%d: result=%q, want=%q", i, result, tt.want)
  125. }
  126. }
  127. }
  128. func TestRemoveSubsetRangePerms(t *testing.T) {
  129. tests := []struct {
  130. perms []*rangePerm
  131. expect []*rangePerm
  132. }{
  133. { // subsets converge
  134. []*rangePerm{{[]byte{2}, []byte{3}}, {[]byte{2}, []byte{5}}, {[]byte{1}, []byte{4}}},
  135. []*rangePerm{{[]byte{1}, []byte{4}}, {[]byte{2}, []byte{5}}},
  136. },
  137. { // subsets converge
  138. []*rangePerm{{[]byte{0}, []byte{3}}, {[]byte{0}, []byte{1}}, {[]byte{2}, []byte{4}}, {[]byte{0}, []byte{2}}},
  139. []*rangePerm{{[]byte{0}, []byte{3}}, {[]byte{2}, []byte{4}}},
  140. },
  141. { // biggest range at the end
  142. []*rangePerm{{[]byte{2}, []byte{3}}, {[]byte{0}, []byte{2}}, {[]byte{1}, []byte{4}}, {[]byte{0}, []byte{5}}},
  143. []*rangePerm{{[]byte{0}, []byte{5}}},
  144. },
  145. { // biggest range at the beginning
  146. []*rangePerm{{[]byte{0}, []byte{5}}, {[]byte{2}, []byte{3}}, {[]byte{0}, []byte{2}}, {[]byte{1}, []byte{4}}},
  147. []*rangePerm{{[]byte{0}, []byte{5}}},
  148. },
  149. { // no overlapping ranges
  150. []*rangePerm{{[]byte{2}, []byte{3}}, {[]byte{0}, []byte{1}}, {[]byte{4}, []byte{7}}, {[]byte{8}, []byte{15}}},
  151. []*rangePerm{{[]byte{0}, []byte{1}}, {[]byte{2}, []byte{3}}, {[]byte{4}, []byte{7}}, {[]byte{8}, []byte{15}}},
  152. },
  153. }
  154. for i, tt := range tests {
  155. rs := removeSubsetRangePerms(tt.perms)
  156. if !reflect.DeepEqual(rs, tt.expect) {
  157. t.Fatalf("#%d: unexpected rangePerms %q, got %q", i, printPerms(rs), printPerms(tt.expect))
  158. }
  159. }
  160. }
  161. func printPerms(rs []*rangePerm) (txt string) {
  162. for i, p := range rs {
  163. if i != 0 {
  164. txt += ","
  165. }
  166. txt += fmt.Sprintf("%+v", *p)
  167. }
  168. return
  169. }