2016-06-07 21:45:29 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-02-26 21:38:30 +03:00
|
|
|
"github.com/coreos/etcd/auth/authpb"
|
2017-03-22 11:29:48 +03:00
|
|
|
"github.com/coreos/etcd/pkg/adt"
|
2018-04-28 00:07:22 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2017-03-22 11:29:48 +03:00
|
|
|
)
|
2016-06-07 21:45:29 +03:00
|
|
|
|
2017-03-22 11:29:48 +03:00
|
|
|
func TestRangePermission(t *testing.T) {
|
2016-06-07 21:45:29 +03:00
|
|
|
tests := []struct {
|
2017-03-22 11:29:48 +03:00
|
|
|
perms []adt.Interval
|
2017-04-05 05:41:17 +03:00
|
|
|
begin []byte
|
|
|
|
end []byte
|
2017-03-22 11:29:48 +03:00
|
|
|
want bool
|
2016-06-07 21:45:29 +03:00
|
|
|
}{
|
|
|
|
{
|
2017-04-05 05:41:17 +03:00
|
|
|
[]adt.Interval{adt.NewBytesAffineInterval([]byte("a"), []byte("c")), adt.NewBytesAffineInterval([]byte("x"), []byte("z"))},
|
|
|
|
[]byte("a"), []byte("z"),
|
2017-03-22 11:29:48 +03:00
|
|
|
false,
|
2016-06-07 21:45:29 +03:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 05:41:17 +03:00
|
|
|
[]adt.Interval{adt.NewBytesAffineInterval([]byte("a"), []byte("f")), adt.NewBytesAffineInterval([]byte("c"), []byte("d")), adt.NewBytesAffineInterval([]byte("f"), []byte("z"))},
|
|
|
|
[]byte("a"), []byte("z"),
|
2017-03-22 11:29:48 +03:00
|
|
|
true,
|
2016-06-07 21:45:29 +03:00
|
|
|
},
|
|
|
|
{
|
2017-04-05 05:41:17 +03:00
|
|
|
[]adt.Interval{adt.NewBytesAffineInterval([]byte("a"), []byte("d")), adt.NewBytesAffineInterval([]byte("a"), []byte("b")), adt.NewBytesAffineInterval([]byte("c"), []byte("f"))},
|
|
|
|
[]byte("a"), []byte("f"),
|
2017-03-22 11:29:48 +03:00
|
|
|
true,
|
2016-06-14 03:13:55 +03:00
|
|
|
},
|
2016-06-07 21:45:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2017-03-22 11:29:48 +03:00
|
|
|
readPerms := &adt.IntervalTree{}
|
|
|
|
for _, p := range tt.perms {
|
|
|
|
readPerms.Insert(p, struct{}{})
|
2016-06-07 21:45:29 +03:00
|
|
|
}
|
2016-12-14 02:42:12 +03:00
|
|
|
|
2018-04-28 00:07:22 +03:00
|
|
|
result := checkKeyInterval(zap.NewExample(), &unifiedRangePermissions{readPerms: readPerms}, tt.begin, tt.end, authpb.READ)
|
2017-03-22 11:29:48 +03:00
|
|
|
if result != tt.want {
|
|
|
|
t.Errorf("#%d: result=%t, want=%t", i, result, tt.want)
|
|
|
|
}
|
2016-12-14 02:42:12 +03:00
|
|
|
}
|
|
|
|
}
|