github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/wildcard/wildcard_test.go (about)

     1  /*
     2   * Copyright (c) 2018, Psiphon Inc.
     3   * All rights reserved.
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package wildcard
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  )
    26  
    27  const target = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    28  
    29  func TestMatch(t *testing.T) {
    30  
    31  	testCases := []struct {
    32  		pattern string
    33  		target  string
    34  		match   bool
    35  	}{
    36  		{"*", target, true},
    37  		{target, target, true},
    38  		{"Lorem*", target, true},
    39  		{"*aliqua.", target, true},
    40  		{"*tempor*", target, true},
    41  		{"*dolor*eiusmod*magna*", target, true},
    42  		{"Lorem*dolor*eiusmod*magna*", target, true},
    43  		{"*ipsum*elit*aliqua.", target, true},
    44  		{"Lorem*dolor*eiusmod*dolore*aliqua.", target, true},
    45  		{"*dolor* sit*", target, true},
    46  		{"*aliqua.*", target, true},
    47  
    48  		{"", target, false},
    49  		{"L-rem*", target, false},
    50  		{"L-rem**", target, false},
    51  		{"*aliqua-", target, false},
    52  		{"*temp-r*", target, false},
    53  		{"*dolor*ei-smod*magna*", target, false},
    54  		{"Lorem*dolor*eiu-mod*magna*", target, false},
    55  		{"*ipsum*eli-*aliqua.", target, false},
    56  		{"Lorem*dolor*eiusm-d*dolore*aliqua.", target, false},
    57  
    58  		{"Lorem**", target, true},
    59  		{"**aliqua.", target, true},
    60  		{"**tempor**", target, true},
    61  	}
    62  
    63  	for _, testCase := range testCases {
    64  		t.Run(fmt.Sprintf("match: %+v", testCase), func(t *testing.T) {
    65  			if Match(testCase.pattern, testCase.target) != testCase.match {
    66  				t.Errorf("unexpected result")
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func BenchmarkFixedMatch(b *testing.B) {
    73  	for i := 0; i < b.N; i++ {
    74  		if !Match(target, target) {
    75  			b.Fatalf("unexpected result")
    76  		}
    77  	}
    78  }
    79  
    80  func BenchmarkPrefixMatch(b *testing.B) {
    81  	for i := 0; i < b.N; i++ {
    82  		if !Match("Lorem*", target) {
    83  			b.Fatalf("unexpected result")
    84  		}
    85  	}
    86  }
    87  
    88  func BenchmarkSuffixMatch(b *testing.B) {
    89  	for i := 0; i < b.N; i++ {
    90  		if !Match("*aliqua.", target) {
    91  			b.Fatalf("unexpected result")
    92  		}
    93  	}
    94  }
    95  
    96  func BenchmarkMultipleMatch(b *testing.B) {
    97  	for i := 0; i < b.N; i++ {
    98  		if !Match("*dolor*eiusmod*magna*", target) {
    99  			b.Fatalf("unexpected result")
   100  		}
   101  	}
   102  }