condition_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/ext/assert"
  10. )
  11. func TestSubDomainMatcher(t *testing.T) {
  12. assert := With(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(matcher.Apply(test.input) == test.output, IsTrue)
  47. }
  48. }
  49. func TestRoutingRule(t *testing.T) {
  50. assert := With(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. Value: "^facebook\\.com$",
  72. Type: Domain_Regex,
  73. },
  74. },
  75. },
  76. test: []ruleTest{
  77. ruleTest{
  78. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
  79. output: true,
  80. },
  81. ruleTest{
  82. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)),
  83. output: true,
  84. },
  85. ruleTest{
  86. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.co"), 80)),
  87. output: false,
  88. },
  89. ruleTest{
  90. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.google.com"), 80)),
  91. output: true,
  92. },
  93. ruleTest{
  94. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("facebook.com"), 80)),
  95. output: true,
  96. },
  97. ruleTest{
  98. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)),
  99. output: false,
  100. },
  101. ruleTest{
  102. input: context.Background(),
  103. output: false,
  104. },
  105. },
  106. },
  107. {
  108. rule: &RoutingRule{
  109. Cidr: []*CIDR{
  110. {
  111. Ip: []byte{8, 8, 8, 8},
  112. Prefix: 32,
  113. },
  114. {
  115. Ip: []byte{8, 8, 8, 8},
  116. Prefix: 32,
  117. },
  118. {
  119. Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
  120. Prefix: 128,
  121. },
  122. },
  123. },
  124. test: []ruleTest{
  125. ruleTest{
  126. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)),
  127. output: true,
  128. },
  129. ruleTest{
  130. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)),
  131. output: false,
  132. },
  133. ruleTest{
  134. input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)),
  135. output: true,
  136. },
  137. ruleTest{
  138. input: context.Background(),
  139. output: false,
  140. },
  141. },
  142. },
  143. {
  144. rule: &RoutingRule{
  145. UserEmail: []string{
  146. "admin@v2ray.com",
  147. },
  148. },
  149. test: []ruleTest{
  150. ruleTest{
  151. input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "admin@v2ray.com"}),
  152. output: true,
  153. },
  154. ruleTest{
  155. input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "love@v2ray.com"}),
  156. output: false,
  157. },
  158. ruleTest{
  159. input: context.Background(),
  160. output: false,
  161. },
  162. },
  163. },
  164. }
  165. for _, test := range cases {
  166. cond, err := test.rule.BuildCondition()
  167. assert(err, IsNil)
  168. for _, t := range test.test {
  169. assert(cond.Apply(t.input), Equals, t.output)
  170. }
  171. }
  172. }