condition_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package router_test
  2. import (
  3. "context"
  4. "testing"
  5. . "v2ray.com/core/app/router"
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/proxy"
  8. "v2ray.com/core/testing/assert"
  9. )
  10. func TestSubDomainMatcher(t *testing.T) {
  11. assert := assert.On(t)
  12. cases := []struct {
  13. pattern string
  14. input context.Context
  15. output bool
  16. }{
  17. {
  18. pattern: "v2ray.com",
  19. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com"), 80)),
  20. output: true,
  21. },
  22. {
  23. pattern: "v2ray.com",
  24. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
  25. output: true,
  26. },
  27. {
  28. pattern: "v2ray.com",
  29. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v3ray.com"), 80)),
  30. output: false,
  31. },
  32. {
  33. pattern: "v2ray.com",
  34. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("2ray.com"), 80)),
  35. output: false,
  36. },
  37. {
  38. pattern: "v2ray.com",
  39. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("xv2ray.com"), 80)),
  40. output: false,
  41. },
  42. }
  43. for _, test := range cases {
  44. matcher := NewSubDomainMatcher(test.pattern)
  45. assert.Bool(matcher.Apply(test.input) == test.output).IsTrue()
  46. }
  47. }