matchergroup_domain_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package strmatcher_test
  2. import (
  3. "reflect"
  4. "testing"
  5. . "github.com/v2fly/v2ray-core/v5/common/strmatcher"
  6. )
  7. func TestDomainMatcherGroup(t *testing.T) {
  8. patterns := []struct {
  9. Pattern string
  10. Value uint32
  11. }{
  12. {
  13. Pattern: "v2fly.org",
  14. Value: 1,
  15. },
  16. {
  17. Pattern: "google.com",
  18. Value: 2,
  19. },
  20. {
  21. Pattern: "x.a.com",
  22. Value: 3,
  23. },
  24. {
  25. Pattern: "a.b.com",
  26. Value: 4,
  27. },
  28. {
  29. Pattern: "c.a.b.com",
  30. Value: 5,
  31. },
  32. {
  33. Pattern: "x.y.com",
  34. Value: 4,
  35. },
  36. {
  37. Pattern: "x.y.com",
  38. Value: 6,
  39. },
  40. }
  41. testCases := []struct {
  42. Domain string
  43. Result []uint32
  44. }{
  45. {
  46. Domain: "x.v2fly.org",
  47. Result: []uint32{1},
  48. },
  49. {
  50. Domain: "y.com",
  51. Result: nil,
  52. },
  53. {
  54. Domain: "a.b.com",
  55. Result: []uint32{4},
  56. },
  57. { // Matches [c.a.b.com, a.b.com]
  58. Domain: "c.a.b.com",
  59. Result: []uint32{5, 4},
  60. },
  61. {
  62. Domain: "c.a..b.com",
  63. Result: nil,
  64. },
  65. {
  66. Domain: ".com",
  67. Result: nil,
  68. },
  69. {
  70. Domain: "com",
  71. Result: nil,
  72. },
  73. {
  74. Domain: "",
  75. Result: nil,
  76. },
  77. {
  78. Domain: "x.y.com",
  79. Result: []uint32{4, 6},
  80. },
  81. }
  82. g := new(DomainMatcherGroup)
  83. for _, pattern := range patterns {
  84. AddMatcherToGroup(g, DomainMatcher(pattern.Pattern), pattern.Value)
  85. }
  86. for _, testCase := range testCases {
  87. r := g.Match(testCase.Domain)
  88. if !reflect.DeepEqual(r, testCase.Result) {
  89. t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
  90. }
  91. }
  92. }
  93. func TestEmptyDomainMatcherGroup(t *testing.T) {
  94. g := new(DomainMatcherGroup)
  95. r := g.Match("v2fly.org")
  96. if len(r) != 0 {
  97. t.Error("Expect [], but ", r)
  98. }
  99. }