github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/strmatcher/matchergroup_full_test.go (about)

     1  package strmatcher_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	. "github.com/v2fly/v2ray-core/v5/common/strmatcher"
     8  )
     9  
    10  func TestFullMatcherGroup(t *testing.T) {
    11  	patterns := []struct {
    12  		Pattern string
    13  		Value   uint32
    14  	}{
    15  		{
    16  			Pattern: "v2fly.org",
    17  			Value:   1,
    18  		},
    19  		{
    20  			Pattern: "google.com",
    21  			Value:   2,
    22  		},
    23  		{
    24  			Pattern: "x.a.com",
    25  			Value:   3,
    26  		},
    27  		{
    28  			Pattern: "x.y.com",
    29  			Value:   4,
    30  		},
    31  		{
    32  			Pattern: "x.y.com",
    33  			Value:   6,
    34  		},
    35  	}
    36  	testCases := []struct {
    37  		Domain string
    38  		Result []uint32
    39  	}{
    40  		{
    41  			Domain: "v2fly.org",
    42  			Result: []uint32{1},
    43  		},
    44  		{
    45  			Domain: "y.com",
    46  			Result: nil,
    47  		},
    48  		{
    49  			Domain: "x.y.com",
    50  			Result: []uint32{4, 6},
    51  		},
    52  	}
    53  	g := NewFullMatcherGroup()
    54  	for _, pattern := range patterns {
    55  		AddMatcherToGroup(g, FullMatcher(pattern.Pattern), pattern.Value)
    56  	}
    57  	for _, testCase := range testCases {
    58  		r := g.Match(testCase.Domain)
    59  		if !reflect.DeepEqual(r, testCase.Result) {
    60  			t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
    61  		}
    62  	}
    63  }
    64  
    65  func TestEmptyFullMatcherGroup(t *testing.T) {
    66  	g := NewFullMatcherGroup()
    67  	r := g.Match("v2fly.org")
    68  	if len(r) != 0 {
    69  		t.Error("Expect [], but ", r)
    70  	}
    71  }