range_perm_cache_test.go 1.8 KB

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