condition_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  48. func TestRoutingRule(t *testing.T) {
  49. assert := assert.On(t)
  50. type ruleTest struct {
  51. input context.Context
  52. output bool
  53. }
  54. cases := []struct {
  55. rule *RoutingRule
  56. test []ruleTest
  57. }{
  58. {
  59. rule: &RoutingRule{
  60. Domain: []*Domain{
  61. {
  62. Value: "v2ray.com",
  63. Type: Domain_Plain,
  64. },
  65. {
  66. Value: "google.com",
  67. Type: Domain_Domain,
  68. },
  69. },
  70. },
  71. test: []ruleTest{
  72. ruleTest{
  73. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
  74. output: true,
  75. },
  76. ruleTest{
  77. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)),
  78. output: true,
  79. },
  80. ruleTest{
  81. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.co"), 80)),
  82. output: false,
  83. },
  84. ruleTest{
  85. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.google.com"), 80)),
  86. output: true,
  87. },
  88. },
  89. },
  90. {
  91. rule: &RoutingRule{
  92. Cidr: []*CIDR{
  93. {
  94. Ip: []byte{8, 8, 8, 8},
  95. Prefix: 32,
  96. },
  97. {
  98. Ip: []byte{8, 8, 8, 8},
  99. Prefix: 32,
  100. },
  101. {
  102. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  103. Prefix: 128,
  104. },
  105. },
  106. },
  107. test: []ruleTest{
  108. ruleTest{
  109. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)),
  110. output: true,
  111. },
  112. ruleTest{
  113. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)),
  114. output: false,
  115. },
  116. ruleTest{
  117. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)),
  118. output: true,
  119. },
  120. },
  121. },
  122. }
  123. for _, test := range cases {
  124. cond, err := test.rule.BuildCondition()
  125. assert.Error(err).IsNil()
  126. for _, t := range test.test {
  127. assert.Bool(cond.Apply(t.input)).Equals(t.output)
  128. }
  129. }
  130. }