domain_matcher_test.go 1.0 KB

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