mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
tailcfg: use strings.CutPrefix for CheckTag; add test
Updates #cleanup Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: I42eddc7547a6dd50c4d5b2a9fc88a19aac9767aa
This commit is contained in:
parent
0a5bd63d32
commit
36d0ac6f8e
@ -609,10 +609,11 @@ func isAlpha(b byte) bool {
|
||||
//
|
||||
// We might relax these rules later.
|
||||
func CheckTag(tag string) error {
|
||||
if !strings.HasPrefix(tag, "tag:") {
|
||||
var ok bool
|
||||
tag, ok = strings.CutPrefix(tag, "tag:")
|
||||
if !ok {
|
||||
return errors.New("tags must start with 'tag:'")
|
||||
}
|
||||
tag = tag[4:]
|
||||
if tag == "" {
|
||||
return errors.New("tag names must not be empty")
|
||||
}
|
||||
|
@ -865,3 +865,27 @@ func TestDeps(t *testing.T) {
|
||||
},
|
||||
}.Check(t)
|
||||
}
|
||||
|
||||
func TestCheckTag(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tag string
|
||||
want bool
|
||||
}{
|
||||
{"empty", "", false},
|
||||
{"good", "tag:foo", true},
|
||||
{"bad", "tag:", false},
|
||||
{"no_leading_num", "tag:1foo", false},
|
||||
{"no_punctuation", "tag:foa@bar", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := CheckTag(tt.tag)
|
||||
if err == nil && !tt.want {
|
||||
t.Errorf("got nil; want error")
|
||||
} else if err != nil && tt.want {
|
||||
t.Errorf("got %v; want nil", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user