domain_matcher_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package strmatcher_test
  2. import (
  3. "reflect"
  4. "testing"
  5. . "github.com/xtls/xray-core/common/strmatcher"
  6. )
  7. func TestDomainMatcherGroup(t *testing.T) {
  8. g := new(DomainMatcherGroup)
  9. g.Add("example.com", 1)
  10. g.Add("google.com", 2)
  11. g.Add("x.a.com", 3)
  12. g.Add("a.b.com", 4)
  13. g.Add("c.a.b.com", 5)
  14. g.Add("x.y.com", 4)
  15. g.Add("x.y.com", 6)
  16. testCases := []struct {
  17. Domain string
  18. Result []uint32
  19. }{
  20. {
  21. Domain: "x.example.com",
  22. Result: []uint32{1},
  23. },
  24. {
  25. Domain: "y.com",
  26. Result: nil,
  27. },
  28. {
  29. Domain: "a.b.com",
  30. Result: []uint32{4},
  31. },
  32. { // Matches [c.a.b.com, a.b.com]
  33. Domain: "c.a.b.com",
  34. Result: []uint32{5, 4},
  35. },
  36. {
  37. Domain: "c.a..b.com",
  38. Result: nil,
  39. },
  40. {
  41. Domain: ".com",
  42. Result: nil,
  43. },
  44. {
  45. Domain: "com",
  46. Result: nil,
  47. },
  48. {
  49. Domain: "",
  50. Result: nil,
  51. },
  52. {
  53. Domain: "x.y.com",
  54. Result: []uint32{4, 6},
  55. },
  56. }
  57. for _, testCase := range testCases {
  58. r := g.Match(testCase.Domain)
  59. if !reflect.DeepEqual(r, testCase.Result) {
  60. t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
  61. }
  62. }
  63. }
  64. func TestEmptyDomainMatcherGroup(t *testing.T) {
  65. g := new(DomainMatcherGroup)
  66. r := g.Match("example.com")
  67. if len(r) != 0 {
  68. t.Error("Expect [], but ", r)
  69. }
  70. }