github.com/in-toto/in-toto-golang@v0.9.1-0.20240517212500-990269f763cf/in_toto/match_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found at https://golang.org/LICENSE.
     4  
     5  // this is a modified version of path.Match that removes handling of path separators
     6  
     7  package in_toto
     8  
     9  import "testing"
    10  
    11  type MatchTest struct {
    12  	pattern, s string
    13  	match      bool
    14  	err        error
    15  }
    16  
    17  var matchTests = []MatchTest{
    18  	{"*", "foo/bar", true, nil},
    19  	{"abc", "abc", true, nil},
    20  	{"*", "abc", true, nil},
    21  	{"*c", "abc", true, nil},
    22  	{"a*", "a", true, nil},
    23  	{"a*", "abc", true, nil},
    24  	{"a*", "ab/c", true, nil},
    25  	{"a*/b", "abc/b", true, nil},
    26  	{"a*/b", "a/c/b", true, nil},
    27  	{"a*b*c*d*e*/f", "axbxcxdxe/f", true, nil},
    28  	{"a*b*c*d*e*/f", "axbxcxdxexxx/f", true, nil},
    29  	{"a*b*c*d*e*/f", "axbxcxdxe/xxx/f", true, nil},
    30  	{"a*b*c*d*e*/f", "axbxcxdxexxx/fff", false, nil},
    31  	{"a*b?c*x", "abxbbxdbxebxczzx", true, nil},
    32  	{"a*b?c*x", "abxbbxdbxebxczzy", false, nil},
    33  	{"ab[c]", "abc", true, nil},
    34  	{"ab[b-d]", "abc", true, nil},
    35  	{"ab[e-g]", "abc", false, nil},
    36  	{"ab[^c]", "abc", false, nil},
    37  	{"ab[^b-d]", "abc", false, nil},
    38  	{"ab[^e-g]", "abc", true, nil},
    39  	{"a\\*b", "a*b", true, nil},
    40  	{"a\\*b", "ab", false, nil},
    41  	{"a?b", "a☺b", true, nil},
    42  	{"a[^a]b", "a☺b", true, nil},
    43  	{"a???b", "a☺b", false, nil},
    44  	{"a[^a][^a][^a]b", "a☺b", false, nil},
    45  	{"[a-ζ]*", "α", true, nil},
    46  	{"*[a-ζ]", "A", false, nil},
    47  	{"a?b", "a/b", true, nil},
    48  	{"a*b", "a/b", true, nil},
    49  	{"[\\]a]", "]", true, nil},
    50  	{"[\\-]", "-", true, nil},
    51  	{"[x\\-]", "x", true, nil},
    52  	{"[x\\-]", "-", true, nil},
    53  	{"[x\\-]", "z", false, nil},
    54  	{"[\\-x]", "x", true, nil},
    55  	{"[\\-x]", "-", true, nil},
    56  	{"[\\-x]", "a", false, nil},
    57  	{"[]a]", "]", false, errBadPattern},
    58  	{"[-]", "-", false, errBadPattern},
    59  	{"[x-]", "x", false, errBadPattern},
    60  	{"[x-]", "-", false, errBadPattern},
    61  	{"[x-]", "z", false, errBadPattern},
    62  	{"[-x]", "x", false, errBadPattern},
    63  	{"[-x]", "-", false, errBadPattern},
    64  	{"[-x]", "a", false, errBadPattern},
    65  	{"\\", "a", false, errBadPattern},
    66  	{"[a-b-c]", "a", false, errBadPattern},
    67  	{"[", "a", false, errBadPattern},
    68  	{"[^", "a", false, errBadPattern},
    69  	{"[^bc", "a", false, errBadPattern},
    70  	{"a[", "a", false, errBadPattern},
    71  	{"a[", "ab", false, errBadPattern},
    72  	{"a[", "x", false, errBadPattern},
    73  	{"a/b[", "x", false, errBadPattern},
    74  	{"a[\\", "x", false, errBadPattern},
    75  	{"*x", "xxx", true, nil},
    76  }
    77  
    78  func TestMatch(t *testing.T) {
    79  	for _, tt := range matchTests {
    80  		ok, err := match(tt.pattern, tt.s)
    81  		if ok != tt.match || err != tt.err {
    82  			t.Errorf("Match(%#q, %#q) = %v, %v want %v, %v", tt.pattern, tt.s, ok, err, tt.match, tt.err)
    83  		}
    84  	}
    85  }