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

150 lines
3.5 KiB
Go
Raw Normal View History

2018-06-26 15:57:41 -04:00
package strmatcher_test
import (
"reflect"
2018-06-26 15:57:41 -04:00
"testing"
"unsafe"
2018-06-26 15:57:41 -04:00
"github.com/v2fly/v2ray-core/v5/common"
. "github.com/v2fly/v2ray-core/v5/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
}
}
func TestToDomain(t *testing.T) {
{ // Test normal ASCII domain, which should not trigger new string data allocation
input := "v2fly.org"
domain, err := ToDomain(input)
if err != nil {
t.Error("unexpected error: ", err)
}
if domain != input {
t.Error("unexpected output: ", domain, " for test case ", input)
}
if (*reflect.StringHeader)(unsafe.Pointer(&input)).Data != (*reflect.StringHeader)(unsafe.Pointer(&domain)).Data {
t.Error("different string data of output: ", domain, " and test case ", input)
}
}
{ // Test ASCII domain containing upper case letter, which should be converted to lower case
input := "v2FLY.oRg"
domain, err := ToDomain(input)
if err != nil {
t.Error("unexpected error: ", err)
}
if domain != "v2fly.org" {
t.Error("unexpected output: ", domain, " for test case ", input)
}
}
{ // Test internationalized domain, which should be translated to ASCII punycode
input := "v2fly.公益"
domain, err := ToDomain(input)
if err != nil {
t.Error("unexpected error: ", err)
}
if domain != "v2fly.xn--55qw42g" {
t.Error("unexpected output: ", domain, " for test case ", input)
}
}
{ // Test internationalized domain containing upper case letter
input := "v2FLY.公益"
domain, err := ToDomain(input)
if err != nil {
t.Error("unexpected error: ", err)
}
if domain != "v2fly.xn--55qw42g" {
t.Error("unexpected output: ", domain, " for test case ", input)
}
}
{ // Test domain name of invalid character, which should return with error
input := "{"
_, err := ToDomain(input)
if err == nil {
t.Error("unexpected non error for test case ", input)
}
}
{ // Test domain name containing a space, which should return with error
input := "Mijia Cloud"
_, err := ToDomain(input)
if err == nil {
t.Error("unexpected non error for test case ", input)
}
}
{ // Test domain name containing an underscore, which should return with error
input := "Mijia_Cloud.com"
_, err := ToDomain(input)
if err == nil {
t.Error("unexpected non error for test case ", input)
}
}
{ // Test internationalized domain containing invalid character
input := "Mijia Cloud.公司"
_, err := ToDomain(input)
if err == nil {
t.Error("unexpected non error for test case ", input)
}
}
}