github.com/v2fly/v2ray-core/v4@v4.45.2/common/strmatcher/matchers_test.go (about)

     1  package strmatcher_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/v2fly/v2ray-core/v4/common"
     7  	. "github.com/v2fly/v2ray-core/v4/common/strmatcher"
     8  )
     9  
    10  func TestMatcher(t *testing.T) {
    11  	cases := []struct {
    12  		pattern string
    13  		mType   Type
    14  		input   string
    15  		output  bool
    16  	}{
    17  		{
    18  			pattern: "v2fly.org",
    19  			mType:   Domain,
    20  			input:   "www.v2fly.org",
    21  			output:  true,
    22  		},
    23  		{
    24  			pattern: "v2fly.org",
    25  			mType:   Domain,
    26  			input:   "v2fly.org",
    27  			output:  true,
    28  		},
    29  		{
    30  			pattern: "v2fly.org",
    31  			mType:   Domain,
    32  			input:   "www.v3fly.org",
    33  			output:  false,
    34  		},
    35  		{
    36  			pattern: "v2fly.org",
    37  			mType:   Domain,
    38  			input:   "2fly.org",
    39  			output:  false,
    40  		},
    41  		{
    42  			pattern: "v2fly.org",
    43  			mType:   Domain,
    44  			input:   "xv2fly.org",
    45  			output:  false,
    46  		},
    47  		{
    48  			pattern: "v2fly.org",
    49  			mType:   Full,
    50  			input:   "v2fly.org",
    51  			output:  true,
    52  		},
    53  		{
    54  			pattern: "v2fly.org",
    55  			mType:   Full,
    56  			input:   "xv2fly.org",
    57  			output:  false,
    58  		},
    59  		{
    60  			pattern: "v2fly.org",
    61  			mType:   Regex,
    62  			input:   "v2flyxorg",
    63  			output:  true,
    64  		},
    65  	}
    66  	for _, test := range cases {
    67  		matcher, err := test.mType.New(test.pattern)
    68  		common.Must(err)
    69  		if m := matcher.Match(test.input); m != test.output {
    70  			t.Error("unexpected output: ", m, " for test case ", test)
    71  		}
    72  	}
    73  }
    74  
    75  func TestACAutomaton(t *testing.T) {
    76  	cases1 := []struct {
    77  		pattern string
    78  		mType   Type
    79  		input   string
    80  		output  bool
    81  	}{
    82  		{
    83  			pattern: "v2fly.org",
    84  			mType:   Domain,
    85  			input:   "www.v2fly.org",
    86  			output:  true,
    87  		},
    88  		{
    89  			pattern: "v2fly.org",
    90  			mType:   Domain,
    91  			input:   "v2fly.org",
    92  			output:  true,
    93  		},
    94  		{
    95  			pattern: "v2fly.org",
    96  			mType:   Domain,
    97  			input:   "www.v3fly.org",
    98  			output:  false,
    99  		},
   100  		{
   101  			pattern: "v2fly.org",
   102  			mType:   Domain,
   103  			input:   "2fly.org",
   104  			output:  false,
   105  		},
   106  		{
   107  			pattern: "v2fly.org",
   108  			mType:   Domain,
   109  			input:   "xv2fly.org",
   110  			output:  false,
   111  		},
   112  		{
   113  			pattern: "v2fly.org",
   114  			mType:   Full,
   115  			input:   "v2fly.org",
   116  			output:  true,
   117  		},
   118  		{
   119  			pattern: "v2fly.org",
   120  			mType:   Full,
   121  			input:   "xv2fly.org",
   122  			output:  false,
   123  		},
   124  	}
   125  	for _, test := range cases1 {
   126  		ac := NewACAutomaton()
   127  		ac.Add(test.pattern, test.mType)
   128  		ac.Build()
   129  		if m := ac.Match(test.input); m != test.output {
   130  			t.Error("unexpected output: ", m, " for test case ", test)
   131  		}
   132  	}
   133  	{
   134  		cases2Input := []struct {
   135  			pattern string
   136  			mType   Type
   137  		}{
   138  			{
   139  				pattern: "163.com",
   140  				mType:   Domain,
   141  			},
   142  			{
   143  				pattern: "m.126.com",
   144  				mType:   Full,
   145  			},
   146  			{
   147  				pattern: "3.com",
   148  				mType:   Full,
   149  			},
   150  			{
   151  				pattern: "google.com",
   152  				mType:   Substr,
   153  			},
   154  			{
   155  				pattern: "vgoogle.com",
   156  				mType:   Substr,
   157  			},
   158  		}
   159  		ac := NewACAutomaton()
   160  		for _, test := range cases2Input {
   161  			ac.Add(test.pattern, test.mType)
   162  		}
   163  		ac.Build()
   164  		cases2Output := []struct {
   165  			pattern string
   166  			res     bool
   167  		}{
   168  			{
   169  				pattern: "126.com",
   170  				res:     false,
   171  			},
   172  			{
   173  				pattern: "m.163.com",
   174  				res:     true,
   175  			},
   176  			{
   177  				pattern: "mm163.com",
   178  				res:     false,
   179  			},
   180  			{
   181  				pattern: "m.126.com",
   182  				res:     true,
   183  			},
   184  			{
   185  				pattern: "163.com",
   186  				res:     true,
   187  			},
   188  			{
   189  				pattern: "63.com",
   190  				res:     false,
   191  			},
   192  			{
   193  				pattern: "oogle.com",
   194  				res:     false,
   195  			},
   196  			{
   197  				pattern: "vvgoogle.com",
   198  				res:     true,
   199  			},
   200  		}
   201  		for _, test := range cases2Output {
   202  			if m := ac.Match(test.pattern); m != test.res {
   203  				t.Error("unexpected output: ", m, " for test case ", test)
   204  			}
   205  		}
   206  	}
   207  
   208  	{
   209  		cases3Input := []struct {
   210  			pattern string
   211  			mType   Type
   212  		}{
   213  			{
   214  				pattern: "video.google.com",
   215  				mType:   Domain,
   216  			},
   217  			{
   218  				pattern: "gle.com",
   219  				mType:   Domain,
   220  			},
   221  		}
   222  		ac := NewACAutomaton()
   223  		for _, test := range cases3Input {
   224  			ac.Add(test.pattern, test.mType)
   225  		}
   226  		ac.Build()
   227  		cases3Output := []struct {
   228  			pattern string
   229  			res     bool
   230  		}{
   231  			{
   232  				pattern: "google.com",
   233  				res:     false,
   234  			},
   235  		}
   236  		for _, test := range cases3Output {
   237  			if m := ac.Match(test.pattern); m != test.res {
   238  				t.Error("unexpected output: ", m, " for test case ", test)
   239  			}
   240  		}
   241  	}
   242  }