go.etcd.io/etcd@v3.3.27+incompatible/auth/range_perm_cache_test.go (about)

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