Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package strmatcher_test
 
import (
    "reflect"
    "testing"
 
    . "github.com/v2fly/v2ray-core/v5/common/strmatcher"
)
 
func TestDomainMatcherGroup(t *testing.T) {
    patterns := []struct {
        Pattern string
        Value   uint32
    }{
        {
            Pattern: "v2fly.org",
            Value:   1,
        },
        {
            Pattern: "google.com",
            Value:   2,
        },
        {
            Pattern: "x.a.com",
            Value:   3,
        },
        {
            Pattern: "a.b.com",
            Value:   4,
        },
        {
            Pattern: "c.a.b.com",
            Value:   5,
        },
        {
            Pattern: "x.y.com",
            Value:   4,
        },
        {
            Pattern: "x.y.com",
            Value:   6,
        },
    }
    testCases := []struct {
        Domain string
        Result []uint32
    }{
        {
            Domain: "x.v2fly.org",
            Result: []uint32{1},
        },
        {
            Domain: "y.com",
            Result: nil,
        },
        {
            Domain: "a.b.com",
            Result: []uint32{4},
        },
        { // Matches [c.a.b.com, a.b.com]
            Domain: "c.a.b.com",
            Result: []uint32{5, 4},
        },
        {
            Domain: "c.a..b.com",
            Result: nil,
        },
        {
            Domain: ".com",
            Result: nil,
        },
        {
            Domain: "com",
            Result: nil,
        },
        {
            Domain: "",
            Result: nil,
        },
        {
            Domain: "x.y.com",
            Result: []uint32{4, 6},
        },
    }
    g := new(DomainMatcherGroup)
    for _, pattern := range patterns {
        AddMatcherToGroup(g, DomainMatcher(pattern.Pattern), pattern.Value)
    }
    for _, testCase := range testCases {
        r := g.Match(testCase.Domain)
        if !reflect.DeepEqual(r, testCase.Result) {
            t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
        }
    }
}
 
func TestEmptyDomainMatcherGroup(t *testing.T) {
    g := new(DomainMatcherGroup)
    r := g.Match("v2fly.org")
    if len(r) != 0 {
        t.Error("Expect [], but ", r)
    }
}