1
0

strmatcher_test.go 4.4 KB

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