strmatcher_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package strmatcher_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/v2fly/v2ray-core/v4/common"
  6. . "github.com/v2fly/v2ray-core/v4/common/strmatcher"
  7. )
  8. // See https://github.com/v2fly/v2ray-core/issues/92#issuecomment-673238489
  9. func TestMatcherGroup(t *testing.T) {
  10. rules := []struct {
  11. Type Type
  12. Domain string
  13. }{
  14. {
  15. Type: Regex,
  16. Domain: "apis\\.us$",
  17. },
  18. {
  19. Type: Substr,
  20. Domain: "apis",
  21. },
  22. {
  23. Type: Domain,
  24. Domain: "googleapis.com",
  25. },
  26. {
  27. Type: Domain,
  28. Domain: "com",
  29. },
  30. {
  31. Type: Full,
  32. Domain: "www.baidu.com",
  33. },
  34. {
  35. Type: Substr,
  36. Domain: "apis",
  37. },
  38. {
  39. Type: Domain,
  40. Domain: "googleapis.com",
  41. },
  42. {
  43. Type: Full,
  44. Domain: "fonts.googleapis.com",
  45. },
  46. {
  47. Type: Full,
  48. Domain: "www.baidu.com",
  49. },
  50. {
  51. Type: Domain,
  52. Domain: "example.com",
  53. },
  54. }
  55. cases := []struct {
  56. Input string
  57. Output []uint32
  58. }{
  59. {
  60. Input: "www.baidu.com",
  61. Output: []uint32{5, 9, 4},
  62. },
  63. {
  64. Input: "fonts.googleapis.com",
  65. Output: []uint32{8, 3, 7, 4, 2, 6},
  66. },
  67. {
  68. Input: "example.googleapis.com",
  69. Output: []uint32{3, 7, 4, 2, 6},
  70. },
  71. {
  72. Input: "testapis.us",
  73. Output: []uint32{1, 2, 6},
  74. },
  75. {
  76. Input: "example.com",
  77. Output: []uint32{10, 4},
  78. },
  79. }
  80. matcherGroup := &MatcherGroup{}
  81. for _, rule := range rules {
  82. matcher, err := rule.Type.New(rule.Domain)
  83. common.Must(err)
  84. matcherGroup.Add(matcher)
  85. }
  86. for _, test := range cases {
  87. if m := matcherGroup.Match(test.Input); !reflect.DeepEqual(m, test.Output) {
  88. t.Error("unexpected output: ", m, " for test case ", test)
  89. }
  90. }
  91. }