range_perm_cache_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 TestUnifyParams(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("c")}},
  44. []*rangePerm{{[]byte("a"), []byte("c")}},
  45. },
  46. {
  47. []*rangePerm{{[]byte("a"), []byte("c")}, {[]byte("b"), []byte("d")}},
  48. []*rangePerm{{[]byte("a"), []byte("d")}},
  49. },
  50. {
  51. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}, {[]byte("d"), []byte("e")}},
  52. []*rangePerm{{[]byte("a"), []byte("c")}, {[]byte("d"), []byte("e")}},
  53. },
  54. {
  55. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  56. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  57. },
  58. {
  59. []*rangePerm{{[]byte("e"), []byte("f")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("b")}},
  60. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("e"), []byte("f")}},
  61. },
  62. {
  63. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}},
  64. []*rangePerm{{[]byte("a"), []byte("z")}},
  65. },
  66. {
  67. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}, {[]byte("1"), []byte("9")}},
  68. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("z")}},
  69. },
  70. {
  71. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("c"), []byte("d")}, {[]byte("a"), []byte("z")}, {[]byte("1"), []byte("a")}},
  72. []*rangePerm{{[]byte("1"), []byte("z")}},
  73. },
  74. {
  75. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("a"), []byte("z")}, {[]byte("5"), []byte("6")}, {[]byte("1"), []byte("9")}},
  76. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("z")}},
  77. },
  78. {
  79. []*rangePerm{{[]byte("a"), []byte("b")}, {[]byte("b"), []byte("c")}, {[]byte("c"), []byte("d")}, {[]byte("d"), []byte("f")}, {[]byte("1"), []byte("9")}},
  80. []*rangePerm{{[]byte("1"), []byte("9")}, {[]byte("a"), []byte("f")}},
  81. },
  82. }
  83. for i, tt := range tests {
  84. result := mergeRangePerms(tt.params)
  85. if !isPermsEqual(result, tt.want) {
  86. t.Errorf("#%d: result=%q, want=%q", i, result, tt.want)
  87. }
  88. }
  89. }