strmatcher_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }