1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/common/strmatcher/matchergroup_domain_test.go
Ye Zhihao d4da365c5f
Refactor: strmatcher module (#1333)
* Reorganize strmatcher's package structure

* Rename types in strmatcher package according to their file names

* Stablize strmatcher's Matcher interface

* Implement []matcherEntry as SimpleMatcherGroup

* Implement mph algorithm extracted from MphIndexMatcher as MphMatcherGroup

* Implement AddMatcher/AddFullMatcher/AddDomainMatcher/AddSubstrMatcher for each MatcherGroup

* Stablize strmatcher's MatcherGroup interface

* Stablize strmatcher's IndexMatcher interface

* Update strmatcher's benchmark

* Compatibility fix for app/router's DomainMatcher condition

* Fix code quality issue

* Fix basic matcher issues

* Update priority specification for Substr matcher
2021-10-31 18:01:13 +08:00

104 lines
1.6 KiB
Go

package strmatcher_test
import (
"reflect"
"testing"
. "github.com/v2fly/v2ray-core/v4/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)
}
}