2018-08-20 03:57:06 -04:00
|
|
|
package strmatcher_test
|
|
|
|
|
|
|
|
import (
|
2020-08-11 01:31:04 -04:00
|
|
|
"reflect"
|
2018-08-20 03:57:06 -04:00
|
|
|
"testing"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v4/common/strmatcher"
|
2018-08-20 03:57:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFullMatcherGroup(t *testing.T) {
|
|
|
|
g := new(FullMatcherGroup)
|
2021-02-16 15:31:50 -05:00
|
|
|
g.Add("v2fly.org", 1)
|
2018-08-20 03:57:06 -04:00
|
|
|
g.Add("google.com", 2)
|
|
|
|
g.Add("x.a.com", 3)
|
2020-08-11 01:31:04 -04:00
|
|
|
g.Add("x.y.com", 4)
|
|
|
|
g.Add("x.y.com", 6)
|
2018-08-20 03:57:06 -04:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
Domain string
|
2020-08-11 01:31:04 -04:00
|
|
|
Result []uint32
|
2018-08-20 03:57:06 -04:00
|
|
|
}{
|
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
Domain: "v2fly.org",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: []uint32{1},
|
2018-08-20 03:57:06 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "y.com",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "x.y.com",
|
|
|
|
Result: []uint32{4, 6},
|
2018-08-20 03:57:06 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
r := g.Match(testCase.Domain)
|
2020-08-11 01:31:04 -04:00
|
|
|
if !reflect.DeepEqual(r, testCase.Result) {
|
2018-08-20 03:57:06 -04:00
|
|
|
t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyFullMatcherGroup(t *testing.T) {
|
|
|
|
g := new(FullMatcherGroup)
|
2021-02-16 15:31:50 -05:00
|
|
|
r := g.Match("v2fly.org")
|
2020-08-11 01:31:04 -04:00
|
|
|
if len(r) != 0 {
|
|
|
|
t.Error("Expect [], but ", r)
|
2018-08-20 03:57:06 -04:00
|
|
|
}
|
|
|
|
}
|