github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/strmatcher/indexmatcher_mph_test.go (about) 1 package strmatcher_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/v2fly/v2ray-core/v5/common" 8 . "github.com/v2fly/v2ray-core/v5/common/strmatcher" 9 ) 10 11 func TestMphIndexMatcher(t *testing.T) { 12 rules := []struct { 13 Type Type 14 Domain string 15 }{ 16 { 17 Type: Regex, 18 Domain: "apis\\.us$", 19 }, 20 { 21 Type: Substr, 22 Domain: "apis", 23 }, 24 { 25 Type: Domain, 26 Domain: "googleapis.com", 27 }, 28 { 29 Type: Domain, 30 Domain: "com", 31 }, 32 { 33 Type: Full, 34 Domain: "www.baidu.com", 35 }, 36 { 37 Type: Substr, 38 Domain: "apis", 39 }, 40 { 41 Type: Domain, 42 Domain: "googleapis.com", 43 }, 44 { 45 Type: Full, 46 Domain: "fonts.googleapis.com", 47 }, 48 { 49 Type: Full, 50 Domain: "www.baidu.com", 51 }, 52 { 53 Type: Domain, 54 Domain: "example.com", 55 }, 56 } 57 cases := []struct { 58 Input string 59 Output []uint32 60 }{ 61 { 62 Input: "www.baidu.com", 63 Output: []uint32{5, 9, 4}, 64 }, 65 { 66 Input: "fonts.googleapis.com", 67 Output: []uint32{8, 3, 7, 4, 2, 6}, 68 }, 69 { 70 Input: "example.googleapis.com", 71 Output: []uint32{3, 7, 4, 2, 6}, 72 }, 73 { 74 Input: "testapis.us", 75 Output: []uint32{2, 6, 1}, 76 }, 77 { 78 Input: "example.com", 79 Output: []uint32{10, 4}, 80 }, 81 } 82 matcherGroup := NewMphIndexMatcher() 83 for _, rule := range rules { 84 matcher, err := rule.Type.New(rule.Domain) 85 common.Must(err) 86 matcherGroup.Add(matcher) 87 } 88 matcherGroup.Build() 89 for _, test := range cases { 90 if m := matcherGroup.Match(test.Input); !reflect.DeepEqual(m, test.Output) { 91 t.Error("unexpected output: ", m, " for test case ", test) 92 } 93 } 94 }