range_perm_cache_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. "github.com/coreos/etcd/auth/authpb"
  18. "github.com/coreos/etcd/pkg/adt"
  19. )
  20. func TestRangePermission(t *testing.T) {
  21. tests := []struct {
  22. perms []adt.Interval
  23. begin string
  24. end string
  25. want bool
  26. }{
  27. {
  28. []adt.Interval{adt.NewStringAffineInterval("a", "c"), adt.NewStringAffineInterval("x", "z")},
  29. "a", "z",
  30. false,
  31. },
  32. {
  33. []adt.Interval{adt.NewStringAffineInterval("a", "f"), adt.NewStringAffineInterval("c", "d"), adt.NewStringAffineInterval("f", "z")},
  34. "a", "z",
  35. true,
  36. },
  37. {
  38. []adt.Interval{adt.NewStringAffineInterval("a", "d"), adt.NewStringAffineInterval("a", "b"), adt.NewStringAffineInterval("c", "f")},
  39. "a", "f",
  40. true,
  41. },
  42. }
  43. for i, tt := range tests {
  44. readPerms := &adt.IntervalTree{}
  45. for _, p := range tt.perms {
  46. readPerms.Insert(p, struct{}{})
  47. }
  48. result := checkKeyInterval(&unifiedRangePermissions{readPerms: readPerms}, tt.begin, tt.end, authpb.READ)
  49. if result != tt.want {
  50. t.Errorf("#%d: result=%t, want=%t", i, result, tt.want)
  51. }
  52. }
  53. }