condition_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/common/protocol"
  8. "v2ray.com/core/proxy"
  9. "v2ray.com/core/testing/assert"
  10. )
  11. func TestSubDomainMatcher(t *testing.T) {
  12. assert := assert.On(t)
  13. cases := []struct {
  14. pattern string
  15. input context.Context
  16. output bool
  17. }{
  18. {
  19. pattern: "v2ray.com",
  20. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com"), 80)),
  21. output: true,
  22. },
  23. {
  24. pattern: "v2ray.com",
  25. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
  26. output: true,
  27. },
  28. {
  29. pattern: "v2ray.com",
  30. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v3ray.com"), 80)),
  31. output: false,
  32. },
  33. {
  34. pattern: "v2ray.com",
  35. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("2ray.com"), 80)),
  36. output: false,
  37. },
  38. {
  39. pattern: "v2ray.com",
  40. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("xv2ray.com"), 80)),
  41. output: false,
  42. },
  43. }
  44. for _, test := range cases {
  45. matcher := NewSubDomainMatcher(test.pattern)
  46. assert.Bool(matcher.Apply(test.input) == test.output).IsTrue()
  47. }
  48. }
  49. func TestRoutingRule(t *testing.T) {
  50. assert := assert.On(t)
  51. type ruleTest struct {
  52. input context.Context
  53. output bool
  54. }
  55. cases := []struct {
  56. rule *RoutingRule
  57. test []ruleTest
  58. }{
  59. {
  60. rule: &RoutingRule{
  61. Domain: []*Domain{
  62. {
  63. Value: "v2ray.com",
  64. Type: Domain_Plain,
  65. },
  66. {
  67. Value: "google.com",
  68. Type: Domain_Domain,
  69. },
  70. },
  71. },
  72. test: []ruleTest{
  73. ruleTest{
  74. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
  75. output: true,
  76. },
  77. ruleTest{
  78. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)),
  79. output: true,
  80. },
  81. ruleTest{
  82. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.co"), 80)),
  83. output: false,
  84. },
  85. ruleTest{
  86. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.google.com"), 80)),
  87. output: true,
  88. },
  89. },
  90. },
  91. {
  92. rule: &RoutingRule{
  93. Cidr: []*CIDR{
  94. {
  95. Ip: []byte{8, 8, 8, 8},
  96. Prefix: 32,
  97. },
  98. {
  99. Ip: []byte{8, 8, 8, 8},
  100. Prefix: 32,
  101. },
  102. {
  103. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  104. Prefix: 128,
  105. },
  106. },
  107. },
  108. test: []ruleTest{
  109. ruleTest{
  110. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)),
  111. output: true,
  112. },
  113. ruleTest{
  114. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)),
  115. output: false,
  116. },
  117. ruleTest{
  118. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)),
  119. output: true,
  120. },
  121. },
  122. },
  123. {
  124. rule: &RoutingRule{
  125. UserEmail: []string{
  126. "admin@v2ray.com",
  127. },
  128. },
  129. test: []ruleTest{
  130. ruleTest{
  131. input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "admin@v2ray.com"}),
  132. output: true,
  133. },
  134. ruleTest{
  135. input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "love@v2ray.com"}),
  136. output: false,
  137. },
  138. },
  139. },
  140. }
  141. for _, test := range cases {
  142. cond, err := test.rule.BuildCondition()
  143. assert.Error(err).IsNil()
  144. for _, t := range test.test {
  145. assert.Bool(cond.Apply(t.input)).Equals(t.output)
  146. }
  147. }
  148. }