matchers_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package strmatcher_test
  2. import (
  3. "testing"
  4. "github.com/v2fly/v2ray-core/v4/common"
  5. . "github.com/v2fly/v2ray-core/v4/common/strmatcher"
  6. )
  7. func TestMatcher(t *testing.T) {
  8. cases := []struct {
  9. pattern string
  10. mType Type
  11. input string
  12. output bool
  13. }{
  14. {
  15. pattern: "v2fly.org",
  16. mType: Domain,
  17. input: "www.v2fly.org",
  18. output: true,
  19. },
  20. {
  21. pattern: "v2fly.org",
  22. mType: Domain,
  23. input: "v2fly.org",
  24. output: true,
  25. },
  26. {
  27. pattern: "v2fly.org",
  28. mType: Domain,
  29. input: "www.v3fly.org",
  30. output: false,
  31. },
  32. {
  33. pattern: "v2fly.org",
  34. mType: Domain,
  35. input: "2fly.org",
  36. output: false,
  37. },
  38. {
  39. pattern: "v2fly.org",
  40. mType: Domain,
  41. input: "xv2fly.org",
  42. output: false,
  43. },
  44. {
  45. pattern: "v2fly.org",
  46. mType: Full,
  47. input: "v2fly.org",
  48. output: true,
  49. },
  50. {
  51. pattern: "v2fly.org",
  52. mType: Full,
  53. input: "xv2fly.org",
  54. output: false,
  55. },
  56. {
  57. pattern: "v2fly.org",
  58. mType: Regex,
  59. input: "v2flyxorg",
  60. output: true,
  61. },
  62. }
  63. for _, test := range cases {
  64. matcher, err := test.mType.New(test.pattern)
  65. common.Must(err)
  66. if m := matcher.Match(test.input); m != test.output {
  67. t.Error("unexpected output: ", m, " for test case ", test)
  68. }
  69. }
  70. }
  71. func TestACAutomaton(t *testing.T) {
  72. cases1 := []struct {
  73. pattern string
  74. mType Type
  75. input string
  76. output bool
  77. }{
  78. {
  79. pattern: "v2fly.org",
  80. mType: Domain,
  81. input: "www.v2fly.org",
  82. output: true,
  83. },
  84. {
  85. pattern: "v2fly.org",
  86. mType: Domain,
  87. input: "v2fly.org",
  88. output: true,
  89. },
  90. {
  91. pattern: "v2fly.org",
  92. mType: Domain,
  93. input: "www.v3fly.org",
  94. output: false,
  95. },
  96. {
  97. pattern: "v2fly.org",
  98. mType: Domain,
  99. input: "2fly.org",
  100. output: false,
  101. },
  102. {
  103. pattern: "v2fly.org",
  104. mType: Domain,
  105. input: "xv2fly.org",
  106. output: false,
  107. },
  108. {
  109. pattern: "v2fly.org",
  110. mType: Full,
  111. input: "v2fly.org",
  112. output: true,
  113. },
  114. {
  115. pattern: "v2fly.org",
  116. mType: Full,
  117. input: "xv2fly.org",
  118. output: false,
  119. },
  120. }
  121. for _, test := range cases1 {
  122. ac := NewACAutomaton()
  123. ac.Add(test.pattern, test.mType)
  124. ac.Build()
  125. if m := ac.Match(test.input); m != test.output {
  126. t.Error("unexpected output: ", m, " for test case ", test)
  127. }
  128. }
  129. {
  130. cases2Input := []struct {
  131. pattern string
  132. mType Type
  133. }{
  134. {
  135. pattern: "163.com",
  136. mType: Domain,
  137. },
  138. {
  139. pattern: "m.126.com",
  140. mType: Full,
  141. },
  142. {
  143. pattern: "3.com",
  144. mType: Full,
  145. },
  146. {
  147. pattern: "google.com",
  148. mType: Substr,
  149. },
  150. {
  151. pattern: "vgoogle.com",
  152. mType: Substr,
  153. },
  154. }
  155. ac := NewACAutomaton()
  156. for _, test := range cases2Input {
  157. ac.Add(test.pattern, test.mType)
  158. }
  159. ac.Build()
  160. cases2Output := []struct {
  161. pattern string
  162. res bool
  163. }{
  164. {
  165. pattern: "126.com",
  166. res: false,
  167. },
  168. {
  169. pattern: "m.163.com",
  170. res: true,
  171. },
  172. {
  173. pattern: "mm163.com",
  174. res: false,
  175. },
  176. {
  177. pattern: "m.126.com",
  178. res: true,
  179. },
  180. {
  181. pattern: "163.com",
  182. res: true,
  183. },
  184. {
  185. pattern: "63.com",
  186. res: false,
  187. },
  188. {
  189. pattern: "oogle.com",
  190. res: false,
  191. },
  192. {
  193. pattern: "vvgoogle.com",
  194. res: true,
  195. },
  196. }
  197. for _, test := range cases2Output {
  198. if m := ac.Match(test.pattern); m != test.res {
  199. t.Error("unexpected output: ", m, " for test case ", test)
  200. }
  201. }
  202. }
  203. {
  204. cases3Input := []struct {
  205. pattern string
  206. mType Type
  207. }{
  208. {
  209. pattern: "video.google.com",
  210. mType: Domain,
  211. },
  212. {
  213. pattern: "gle.com",
  214. mType: Domain,
  215. },
  216. }
  217. ac := NewACAutomaton()
  218. for _, test := range cases3Input {
  219. ac.Add(test.pattern, test.mType)
  220. }
  221. ac.Build()
  222. cases3Output := []struct {
  223. pattern string
  224. res bool
  225. }{
  226. {
  227. pattern: "google.com",
  228. res: false,
  229. },
  230. }
  231. for _, test := range cases3Output {
  232. if m := ac.Match(test.pattern); m != test.res {
  233. t.Error("unexpected output: ", m, " for test case ", test)
  234. }
  235. }
  236. }
  237. }