github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/common/strmatcher/domain_matcher_test.go (about)

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