range_perm_cache_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "testing"
  17. )
  18. func isPermsEqual(a, b []*rangePerm) bool {
  19. if len(a) != len(b) {
  20. return false
  21. }
  22. for i := range a {
  23. if len(b) <= i {
  24. return false
  25. }
  26. if a[i].begin != b[i].begin || a[i].end != b[i].end {
  27. return false
  28. }
  29. }
  30. return true
  31. }
  32. func TestUnifyParams(t *testing.T) {
  33. tests := []struct {
  34. params []*rangePerm
  35. want []*rangePerm
  36. }{
  37. {
  38. []*rangePerm{{"a", "b"}},
  39. []*rangePerm{{"a", "b"}},
  40. },
  41. {
  42. []*rangePerm{{"a", "b"}, {"b", "c"}},
  43. []*rangePerm{{"a", "c"}},
  44. },
  45. {
  46. []*rangePerm{{"a", "c"}, {"b", "d"}},
  47. []*rangePerm{{"a", "d"}},
  48. },
  49. {
  50. []*rangePerm{{"a", "b"}, {"b", "c"}, {"d", "e"}},
  51. []*rangePerm{{"a", "c"}, {"d", "e"}},
  52. },
  53. {
  54. []*rangePerm{{"a", "b"}, {"c", "d"}, {"e", "f"}},
  55. []*rangePerm{{"a", "b"}, {"c", "d"}, {"e", "f"}},
  56. },
  57. {
  58. []*rangePerm{{"e", "f"}, {"c", "d"}, {"a", "b"}},
  59. []*rangePerm{{"a", "b"}, {"c", "d"}, {"e", "f"}},
  60. },
  61. {
  62. []*rangePerm{{"a", "b"}, {"c", "d"}, {"a", "z"}},
  63. []*rangePerm{{"a", "z"}},
  64. },
  65. {
  66. []*rangePerm{{"a", "b"}, {"c", "d"}, {"a", "z"}, {"1", "9"}},
  67. []*rangePerm{{"1", "9"}, {"a", "z"}},
  68. },
  69. {
  70. []*rangePerm{{"a", "b"}, {"c", "d"}, {"a", "z"}, {"1", "a"}},
  71. []*rangePerm{{"1", "z"}},
  72. },
  73. {
  74. []*rangePerm{{"a", "b"}, {"a", "z"}, {"5", "6"}, {"1", "9"}},
  75. []*rangePerm{{"1", "9"}, {"a", "z"}},
  76. },
  77. {
  78. []*rangePerm{{"a", "b"}, {"b", "c"}, {"c", "d"}, {"d", "f"}, {"1", "9"}},
  79. []*rangePerm{{"1", "9"}, {"a", "f"}},
  80. },
  81. }
  82. for i, tt := range tests {
  83. result := mergeRangePerms(tt.params)
  84. if !isPermsEqual(result, tt.want) {
  85. t.Errorf("#%d: result=%q, want=%q", i, result, tt.want)
  86. }
  87. }
  88. }