| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- package strmatcher_test
- import (
- "testing"
- "github.com/v2fly/v2ray-core/v4/common"
- . "github.com/v2fly/v2ray-core/v4/common/strmatcher"
- )
- func TestMatcher(t *testing.T) {
- cases := []struct {
- pattern string
- mType Type
- input string
- output bool
- }{
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "www.v2fly.org",
- output: true,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "v2fly.org",
- output: true,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "www.v3fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "2fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "xv2fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Full,
- input: "v2fly.org",
- output: true,
- },
- {
- pattern: "v2fly.org",
- mType: Full,
- input: "xv2fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Regex,
- input: "v2flyxorg",
- 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)
- }
- }
- }
- func TestACAutomaton(t *testing.T) {
- cases1 := []struct {
- pattern string
- mType Type
- input string
- output bool
- }{
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "www.v2fly.org",
- output: true,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "v2fly.org",
- output: true,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "www.v3fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "2fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Domain,
- input: "xv2fly.org",
- output: false,
- },
- {
- pattern: "v2fly.org",
- mType: Full,
- input: "v2fly.org",
- output: true,
- },
- {
- pattern: "v2fly.org",
- mType: Full,
- input: "xv2fly.org",
- output: false,
- },
- }
- for _, test := range cases1 {
- 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,
- },
- }
- 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,
- },
- }
- 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)
- }
- }
- }
- }
|