go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authdb/internal/globset/globset_test.go (about) 1 // Copyright 2019 The LUCI 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 globset 16 17 import ( 18 "testing" 19 20 . "github.com/smartystreets/goconvey/convey" 21 ) 22 23 func TestGlobSet(t *testing.T) { 24 Convey("Empty", t, func() { 25 gs, err := NewBuilder().Build() 26 So(err, ShouldBeNil) 27 So(gs, ShouldBeNil) 28 }) 29 30 Convey("Works", t, func() { 31 b := NewBuilder() 32 So(b.Add("user:*@example.com"), ShouldBeNil) 33 So(b.Add("user:*@other.example.com"), ShouldBeNil) 34 So(b.Add("service:*"), ShouldBeNil) 35 36 gs, err := b.Build() 37 So(err, ShouldBeNil) 38 39 So(gs, ShouldHaveLength, 2) 40 So(gs["user"].String(), ShouldEqual, `^((.*@example\.com)|(.*@other\.example\.com))$`) 41 So(gs["service"].String(), ShouldEqual, `^.*$`) 42 43 So(gs.Has("user:a@example.com"), ShouldBeTrue) 44 So(gs.Has("user:a@other.example.com"), ShouldBeTrue) 45 So(gs.Has("user:a@not-example.com"), ShouldBeFalse) 46 So(gs.Has("service:zzz"), ShouldBeTrue) 47 So(gs.Has("anonymous:anonymous"), ShouldBeFalse) 48 }) 49 50 Convey("Caches regexps", t, func() { 51 b := NewBuilder() 52 53 So(b.Add("user:*@example.com"), ShouldBeNil) 54 So(b.Add("user:*@other.example.com"), ShouldBeNil) 55 56 gs1, err := b.Build() 57 So(err, ShouldBeNil) 58 59 b.Reset() 60 So(b.Add("user:*@other.example.com"), ShouldBeNil) 61 So(b.Add("user:*@example.com"), ShouldBeNil) 62 63 gs2, err := b.Build() 64 So(err, ShouldBeNil) 65 66 // The exact same regexp object. 67 So(gs1["user"], ShouldEqual, gs2["user"]) 68 }) 69 70 Convey("Edge cases in Has", t, func() { 71 b := NewBuilder() 72 b.Add("user:a*@example.com") 73 b.Add("user:*@other.example.com") 74 gs, _ := b.Build() 75 76 So(gs.Has("abc@example.com"), ShouldBeFalse) // no "user:" prefix 77 So(gs.Has("service:abc@example.com"), ShouldBeFalse) // wrong prefix 78 So(gs.Has("user:abc@example.com\nsneaky"), ShouldBeFalse) // sneaky '/n' 79 So(gs.Has("user:bbc@example.com"), ShouldBeFalse) // '^' is checked 80 So(gs.Has("user:abc@example.com-and-stuff"), ShouldBeFalse) // '$' is checked 81 }) 82 }