range_perm_cache_test.go 1.8 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 []byte
  24. end []byte
  25. want bool
  26. }{
  27. {
  28. []adt.Interval{adt.NewBytesAffineInterval([]byte("a"), []byte("c")), adt.NewBytesAffineInterval([]byte("x"), []byte("z"))},
  29. []byte("a"), []byte("z"),
  30. false,
  31. },
  32. {
  33. []adt.Interval{adt.NewBytesAffineInterval([]byte("a"), []byte("f")), adt.NewBytesAffineInterval([]byte("c"), []byte("d")), adt.NewBytesAffineInterval([]byte("f"), []byte("z"))},
  34. []byte("a"), []byte("z"),
  35. true,
  36. },
  37. {
  38. []adt.Interval{adt.NewBytesAffineInterval([]byte("a"), []byte("d")), adt.NewBytesAffineInterval([]byte("a"), []byte("b")), adt.NewBytesAffineInterval([]byte("c"), []byte("f"))},
  39. []byte("a"), []byte("f"),
  40. true,
  41. },
  42. }
  43. for i, tt := range tests {
  44. readPerms := adt.NewIntervalTree()
  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. }