go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/identity/glob_test.go (about)

     1  // Copyright 2015 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 identity
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestGlob(t *testing.T) {
    24  	Convey("MakeGlob works", t, func() {
    25  		g, err := MakeGlob("user:*@example.com")
    26  		So(err, ShouldBeNil)
    27  		So(g, ShouldEqual, Glob("user:*@example.com"))
    28  		So(g.Kind(), ShouldEqual, User)
    29  		So(g.Pattern(), ShouldEqual, "*@example.com")
    30  
    31  		_, err = MakeGlob("bad ident")
    32  		So(err, ShouldNotBeNil)
    33  	})
    34  
    35  	Convey("Validate works", t, func() {
    36  		So(Glob("user:*@example.com").Validate(), ShouldBeNil)
    37  		So(Glob("user:").Validate(), ShouldNotBeNil)
    38  		So(Glob(":abc").Validate(), ShouldNotBeNil)
    39  		So(Glob("abc@example.com").Validate(), ShouldNotBeNil)
    40  		So(Glob("user:\n").Validate(), ShouldNotBeNil)
    41  	})
    42  
    43  	Convey("Kind works", t, func() {
    44  		So(Glob("user:*@example.com").Kind(), ShouldEqual, User)
    45  		So(Glob("???").Kind(), ShouldEqual, Anonymous)
    46  	})
    47  
    48  	Convey("Pattern works", t, func() {
    49  		So(Glob("service:*").Pattern(), ShouldEqual, "*")
    50  		So(Glob("???").Pattern(), ShouldEqual, "")
    51  	})
    52  
    53  	Convey("Match works", t, func() {
    54  		trials := []struct {
    55  			g      Glob
    56  			id     Identity
    57  			result bool
    58  		}{
    59  			{"user:abc@example.com", "user:abc@example.com", true},
    60  			{"user:*@example.com", "user:abc@example.com", true},
    61  			{"user:*", "user:abc@example.com", true},
    62  			{"user:prefix-*", "user:prefix-zzz", true},
    63  			{"user:prefix-*", "user:another-prefix-zzz", false},
    64  			{"user:prefix-*-suffix", "user:prefix-zzz-suffix", true},
    65  			{"user:prefix-*-suffix", "user:prefix-zzz-suffizzz", false},
    66  			{"user:*", "user:\n", false},
    67  			{"user:\n", "user:zzz", false},
    68  			{"bad glob", "user:abc@example.com", false},
    69  			{"user:*", "bad ident", false},
    70  			{"user:*", "service:abc", false},
    71  		}
    72  		for _, entry := range trials {
    73  			So(entry.g.Match(entry.id), ShouldEqual, entry.result)
    74  		}
    75  	})
    76  
    77  	Convey("translate works", t, func() {
    78  		trials := []struct {
    79  			pat string
    80  			reg string
    81  		}{
    82  			{"", `^$`},
    83  			{"*", `^.*$`},
    84  			{"abc", `^abc$`},
    85  			{".?", `^\.\?$`},
    86  			{"perfix-*@suffix", `^perfix-.*@suffix$`},
    87  		}
    88  		for _, entry := range trials {
    89  			re, err := translate(entry.pat)
    90  			So(err, ShouldBeNil)
    91  			So(re, ShouldEqual, entry.reg)
    92  		}
    93  	})
    94  
    95  	Convey("translate reject newline", t, func() {
    96  		_, err := translate("blah\nblah")
    97  		So(err, ShouldNotBeNil)
    98  	})
    99  
   100  	Convey("Preprocess works", t, func() {
   101  		k, r, err := Glob("user:*@example.com").Preprocess()
   102  		So(err, ShouldBeNil)
   103  		So(k, ShouldEqual, User)
   104  		So(r, ShouldEqual, "^.*@example\\.com$")
   105  	})
   106  
   107  	Convey("Preprocess rejects malformed identity globs", t, func() {
   108  		_, _, err := Glob("*@example.com").Preprocess()
   109  		So(err, ShouldNotBeNil)
   110  		_, _, err = Glob("unknown:*@example.com").Preprocess()
   111  		So(err, ShouldNotBeNil)
   112  		_, _, err = Glob("user:*\n@example.com").Preprocess()
   113  		So(err, ShouldNotBeNil)
   114  	})
   115  }