matchergroup_ac_automation_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 TestACAutomatonMatcherGroup(t *testing.T) {
  8. cases1 := []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. for _, test := range cases1 {
  58. ac := NewACAutomatonMatcherGroup()
  59. matcher, err := test.mType.New(test.pattern)
  60. common.Must(err)
  61. common.Must(AddMatcherToGroup(ac, matcher, 0))
  62. ac.Build()
  63. if m := ac.MatchAny(test.input); m != test.output {
  64. t.Error("unexpected output: ", m, " for test case ", test)
  65. }
  66. }
  67. {
  68. cases2Input := []struct {
  69. pattern string
  70. mType Type
  71. }{
  72. {
  73. pattern: "163.com",
  74. mType: Domain,
  75. },
  76. {
  77. pattern: "m.126.com",
  78. mType: Full,
  79. },
  80. {
  81. pattern: "3.com",
  82. mType: Full,
  83. },
  84. {
  85. pattern: "google.com",
  86. mType: Substr,
  87. },
  88. {
  89. pattern: "vgoogle.com",
  90. mType: Substr,
  91. },
  92. }
  93. ac := NewACAutomatonMatcherGroup()
  94. for _, test := range cases2Input {
  95. matcher, err := test.mType.New(test.pattern)
  96. common.Must(err)
  97. common.Must(AddMatcherToGroup(ac, matcher, 0))
  98. }
  99. ac.Build()
  100. cases2Output := []struct {
  101. pattern string
  102. res bool
  103. }{
  104. {
  105. pattern: "126.com",
  106. res: false,
  107. },
  108. {
  109. pattern: "m.163.com",
  110. res: true,
  111. },
  112. {
  113. pattern: "mm163.com",
  114. res: false,
  115. },
  116. {
  117. pattern: "m.126.com",
  118. res: true,
  119. },
  120. {
  121. pattern: "163.com",
  122. res: true,
  123. },
  124. {
  125. pattern: "63.com",
  126. res: false,
  127. },
  128. {
  129. pattern: "oogle.com",
  130. res: false,
  131. },
  132. {
  133. pattern: "vvgoogle.com",
  134. res: true,
  135. },
  136. }
  137. for _, test := range cases2Output {
  138. if m := ac.MatchAny(test.pattern); m != test.res {
  139. t.Error("unexpected output: ", m, " for test case ", test)
  140. }
  141. }
  142. }
  143. {
  144. cases3Input := []struct {
  145. pattern string
  146. mType Type
  147. }{
  148. {
  149. pattern: "video.google.com",
  150. mType: Domain,
  151. },
  152. {
  153. pattern: "gle.com",
  154. mType: Domain,
  155. },
  156. }
  157. ac := NewACAutomatonMatcherGroup()
  158. for _, test := range cases3Input {
  159. matcher, err := test.mType.New(test.pattern)
  160. common.Must(err)
  161. common.Must(AddMatcherToGroup(ac, matcher, 0))
  162. }
  163. ac.Build()
  164. cases3Output := []struct {
  165. pattern string
  166. res bool
  167. }{
  168. {
  169. pattern: "google.com",
  170. res: false,
  171. },
  172. }
  173. for _, test := range cases3Output {
  174. if m := ac.MatchAny(test.pattern); m != test.res {
  175. t.Error("unexpected output: ", m, " for test case ", test)
  176. }
  177. }
  178. }
  179. }