2018-08-19 15:04:15 -04:00
|
|
|
package strmatcher_test
|
|
|
|
|
|
|
|
import (
|
2020-08-11 01:31:04 -04:00
|
|
|
"reflect"
|
2018-08-19 15:04:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v5/common/strmatcher"
|
2018-08-19 15:04:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDomainMatcherGroup(t *testing.T) {
|
2021-10-31 06:01:13 -04:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2018-08-19 15:04:15 -04:00
|
|
|
testCases := []struct {
|
|
|
|
Domain string
|
2020-08-11 01:31:04 -04:00
|
|
|
Result []uint32
|
2018-08-19 15:04:15 -04:00
|
|
|
}{
|
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
Domain: "x.v2fly.org",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: []uint32{1},
|
2018-08-19 15:04:15 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "y.com",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: nil,
|
2018-08-19 15:04:15 -04:00
|
|
|
},
|
2018-09-08 15:54:09 -04:00
|
|
|
{
|
|
|
|
Domain: "a.b.com",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: []uint32{4},
|
2018-09-08 15:54:09 -04:00
|
|
|
},
|
2020-08-13 03:26:30 -04:00
|
|
|
{ // Matches [c.a.b.com, a.b.com]
|
2018-09-08 15:54:09 -04:00
|
|
|
Domain: "c.a.b.com",
|
2020-08-13 03:26:30 -04:00
|
|
|
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",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: nil,
|
2018-11-02 07:14:41 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: ".com",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: nil,
|
2018-11-02 07:14:41 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "com",
|
2020-08-11 01:31:04 -04:00
|
|
|
Result: nil,
|
2018-11-02 07:14:41 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "",
|
2020-08-11 01:31:04 -04:00
|
|
|
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
|
|
|
}
|
2022-09-16 02:40:03 -04:00
|
|
|
g := NewDomainMatcherGroup()
|
2021-10-31 06:01:13 -04:00
|
|
|
for _, pattern := range patterns {
|
|
|
|
AddMatcherToGroup(g, DomainMatcher(pattern.Pattern), pattern.Value)
|
|
|
|
}
|
2018-08-19 15:04:15 -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-19 15:04:15 -04:00
|
|
|
t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-20 03:47:18 -04:00
|
|
|
|
|
|
|
func TestEmptyDomainMatcherGroup(t *testing.T) {
|
2022-09-16 02:40:03 -04:00
|
|
|
g := NewDomainMatcherGroup()
|
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:47:18 -04:00
|
|
|
}
|
|
|
|
}
|