1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/common/strmatcher/domain_matcher_test.go

77 lines
1.3 KiB
Go
Raw Normal View History

2018-08-19 15:04:15 -04:00
package strmatcher_test
import (
"reflect"
2018-08-19 15:04:15 -04:00
"testing"
2021-02-16 15:31:50 -05:00
. "github.com/v2fly/v2ray-core/v4/common/strmatcher"
2018-08-19 15:04:15 -04:00
)
func TestDomainMatcherGroup(t *testing.T) {
g := new(DomainMatcherGroup)
2021-02-16 15:31:50 -05:00
g.Add("v2fly.org", 1)
2018-08-19 15:04:15 -04:00
g.Add("google.com", 2)
g.Add("x.a.com", 3)
2018-09-08 15:54:09 -04:00
g.Add("a.b.com", 4)
g.Add("c.a.b.com", 5)
g.Add("x.y.com", 4)
g.Add("x.y.com", 6)
2018-08-19 15:04:15 -04:00
testCases := []struct {
Domain string
Result []uint32
2018-08-19 15:04:15 -04:00
}{
{
2021-02-16 15:31:50 -05:00
Domain: "x.v2fly.org",
Result: []uint32{1},
2018-08-19 15:04:15 -04:00
},
{
Domain: "y.com",
Result: nil,
2018-08-19 15:04:15 -04:00
},
2018-09-08 15:54:09 -04:00
{
Domain: "a.b.com",
Result: []uint32{4},
2018-09-08 15:54:09 -04:00
},
{ // Matches [c.a.b.com, a.b.com]
2018-09-08 15:54:09 -04:00
Domain: "c.a.b.com",
Result: []uint32{5, 4},
2018-09-08 15:54:09 -04:00
},
2018-11-02 07:14:41 -04:00
{
Domain: "c.a..b.com",
Result: nil,
2018-11-02 07:14:41 -04:00
},
{
Domain: ".com",
Result: nil,
2018-11-02 07:14:41 -04:00
},
{
Domain: "com",
Result: nil,
2018-11-02 07:14:41 -04:00
},
{
Domain: "",
Result: nil,
},
{
Domain: "x.y.com",
Result: []uint32{4, 6},
2018-11-02 07:14:41 -04:00
},
2018-08-19 15:04:15 -04:00
}
for _, testCase := range testCases {
r := g.Match(testCase.Domain)
if !reflect.DeepEqual(r, testCase.Result) {
2018-08-19 15:04:15 -04:00
t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
}
}
}
func TestEmptyDomainMatcherGroup(t *testing.T) {
g := new(DomainMatcherGroup)
2021-02-16 15:31:50 -05:00
r := g.Match("v2fly.org")
if len(r) != 0 {
t.Error("Expect [], but ", r)
}
}