1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/common/strmatcher/matchers_test.go

243 lines
4.0 KiB
Go
Raw Normal View History

2018-06-26 15:57:41 -04:00
package strmatcher_test
import (
"testing"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common"
. "github.com/v2fly/v2ray-core/v4/common/strmatcher"
2018-06-26 15:57:41 -04:00
)
func TestMatcher(t *testing.T) {
cases := []struct {
pattern string
mType Type
input string
output bool
}{
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "www.v2fly.org",
2018-06-26 15:57:41 -04:00
output: true,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "v2fly.org",
2018-06-26 15:57:41 -04:00
output: true,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "www.v3fly.org",
2018-06-26 15:57:41 -04:00
output: false,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "2fly.org",
2018-06-26 15:57:41 -04:00
output: false,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "xv2fly.org",
2018-06-26 15:57:41 -04:00
output: false,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Full,
2021-02-16 15:31:50 -05:00
input: "v2fly.org",
2018-06-26 15:57:41 -04:00
output: true,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-06-26 15:57:41 -04:00
mType: Full,
2021-02-16 15:31:50 -05:00
input: "xv2fly.org",
2018-06-26 15:57:41 -04:00
output: false,
},
2018-08-20 09:39:58 -04:00
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
2018-08-20 09:39:58 -04:00
mType: Regex,
2021-02-16 15:31:50 -05:00
input: "v2flyxorg",
2018-08-20 09:39:58 -04:00
output: true,
},
2018-06-26 15:57:41 -04:00
}
for _, test := range cases {
matcher, err := test.mType.New(test.pattern)
common.Must(err)
2019-01-08 17:27:02 -05:00
if m := matcher.Match(test.input); m != test.output {
t.Error("unexpected output: ", m, " for test case ", test)
}
2018-06-26 15:57:41 -04:00
}
}
2021-05-19 17:28:52 -04:00
func TestACAutomaton(t *testing.T) {
cases1 := []struct {
pattern string
mType Type
input string
output bool
}{
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "www.v2fly.org",
output: true,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "v2fly.org",
output: true,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "www.v3fly.org",
output: false,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "2fly.org",
output: false,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Domain,
2021-02-16 15:31:50 -05:00
input: "xv2fly.org",
output: false,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Full,
2021-02-16 15:31:50 -05:00
input: "v2fly.org",
output: true,
},
{
2021-02-16 15:31:50 -05:00
pattern: "v2fly.org",
mType: Full,
2021-02-16 15:31:50 -05:00
input: "xv2fly.org",
output: false,
},
}
for _, test := range cases1 {
2021-05-19 17:28:52 -04:00
ac := NewACAutomaton()
ac.Add(test.pattern, test.mType)
ac.Build()
if m := ac.Match(test.input); m != test.output {
t.Error("unexpected output: ", m, " for test case ", test)
}
}
{
cases2Input := []struct {
pattern string
mType Type
}{
{
pattern: "163.com",
mType: Domain,
},
{
pattern: "m.126.com",
mType: Full,
},
{
pattern: "3.com",
mType: Full,
},
{
pattern: "google.com",
mType: Substr,
},
{
pattern: "vgoogle.com",
mType: Substr,
},
}
2021-05-19 17:28:52 -04:00
ac := NewACAutomaton()
for _, test := range cases2Input {
ac.Add(test.pattern, test.mType)
}
ac.Build()
cases2Output := []struct {
pattern string
res bool
}{
{
pattern: "126.com",
res: false,
},
{
pattern: "m.163.com",
res: true,
},
{
pattern: "mm163.com",
res: false,
},
{
pattern: "m.126.com",
res: true,
},
{
pattern: "163.com",
res: true,
},
{
pattern: "63.com",
res: false,
},
{
pattern: "oogle.com",
res: false,
},
{
pattern: "vvgoogle.com",
res: true,
},
}
for _, test := range cases2Output {
if m := ac.Match(test.pattern); m != test.res {
t.Error("unexpected output: ", m, " for test case ", test)
}
}
}
{
cases3Input := []struct {
pattern string
mType Type
}{
{
pattern: "video.google.com",
mType: Domain,
},
{
pattern: "gle.com",
mType: Domain,
},
}
2021-05-19 17:28:52 -04:00
ac := NewACAutomaton()
for _, test := range cases3Input {
ac.Add(test.pattern, test.mType)
}
ac.Build()
cases3Output := []struct {
pattern string
res bool
}{
{
pattern: "google.com",
res: false,
},
}
for _, test := range cases3Output {
if m := ac.Match(test.pattern); m != test.res {
t.Error("unexpected output: ", m, " for test case ", test)
}
}
}
}