matchergroup_mph_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 TestMphMatcherGroup(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. mph := NewMphMatcherGroup()
  59. matcher, err := test.mType.New(test.pattern)
  60. common.Must(err)
  61. common.Must(AddMatcherToGroup(mph, matcher, 0))
  62. mph.Build()
  63. if m := mph.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. mph := NewMphMatcherGroup()
  86. for _, test := range cases2Input {
  87. matcher, err := test.mType.New(test.pattern)
  88. common.Must(err)
  89. common.Must(AddMatcherToGroup(mph, matcher, 0))
  90. }
  91. mph.Build()
  92. cases2Output := []struct {
  93. pattern string
  94. res bool
  95. }{
  96. {
  97. pattern: "126.com",
  98. res: false,
  99. },
  100. {
  101. pattern: "m.163.com",
  102. res: true,
  103. },
  104. {
  105. pattern: "mm163.com",
  106. res: false,
  107. },
  108. {
  109. pattern: "m.126.com",
  110. res: true,
  111. },
  112. {
  113. pattern: "163.com",
  114. res: true,
  115. },
  116. {
  117. pattern: "63.com",
  118. res: false,
  119. },
  120. {
  121. pattern: "oogle.com",
  122. res: false,
  123. },
  124. {
  125. pattern: "vvgoogle.com",
  126. res: false,
  127. },
  128. }
  129. for _, test := range cases2Output {
  130. if m := mph.MatchAny(test.pattern); m != test.res {
  131. t.Error("unexpected output: ", m, " for test case ", test)
  132. }
  133. }
  134. }
  135. {
  136. cases3Input := []struct {
  137. pattern string
  138. mType Type
  139. }{
  140. {
  141. pattern: "video.google.com",
  142. mType: Domain,
  143. },
  144. {
  145. pattern: "gle.com",
  146. mType: Domain,
  147. },
  148. }
  149. mph := NewMphMatcherGroup()
  150. for _, test := range cases3Input {
  151. matcher, err := test.mType.New(test.pattern)
  152. common.Must(err)
  153. common.Must(AddMatcherToGroup(mph, matcher, 0))
  154. }
  155. mph.Build()
  156. cases3Output := []struct {
  157. pattern string
  158. res bool
  159. }{
  160. {
  161. pattern: "google.com",
  162. res: false,
  163. },
  164. }
  165. for _, test := range cases3Output {
  166. if m := mph.MatchAny(test.pattern); m != test.res {
  167. t.Error("unexpected output: ", m, " for test case ", test)
  168. }
  169. }
  170. }
  171. }