2021-03-24 15:01:20 +00:00
|
|
|
package str_test
|
2020-11-25 11:01:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common"
|
2021-03-24 15:01:20 +00:00
|
|
|
. "github.com/xtls/xray-core/common/matcher/str"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMatcher(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
pattern string
|
|
|
|
mType Type
|
|
|
|
input string
|
|
|
|
output bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Domain,
|
|
|
|
input: "www.example.com",
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Domain,
|
|
|
|
input: "example.com",
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Domain,
|
|
|
|
input: "www.fxample.com",
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Domain,
|
|
|
|
input: "xample.com",
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Domain,
|
|
|
|
input: "xexample.com",
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Full,
|
|
|
|
input: "example.com",
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Full,
|
|
|
|
input: "xexample.com",
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern: "example.com",
|
|
|
|
mType: Regex,
|
|
|
|
input: "examplexcom",
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range cases {
|
|
|
|
matcher, err := test.mType.New(test.pattern)
|
|
|
|
common.Must(err)
|
|
|
|
if m := matcher.Match(test.input); m != test.output {
|
|
|
|
t.Error("unexpected output: ", m, " for test case ", test)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|