domain_matcher_test.go 889 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. for _, testCase := range testCases {
  35. r := g.Match(testCase.Domain)
  36. if r != testCase.Result {
  37. t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
  38. }
  39. }
  40. }
  41. func TestEmptyDomainMatcherGroup(t *testing.T) {
  42. g := new(DomainMatcherGroup)
  43. r := g.Match("v2ray.com")
  44. if r != 0 {
  45. t.Error("Expect 0, but ", r)
  46. }
  47. }