router_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package router_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/golang/mock/gomock"
  6. . "v2ray.com/core/app/router"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/net"
  9. "v2ray.com/core/common/session"
  10. "v2ray.com/core/features/outbound"
  11. "v2ray.com/core/testing/mocks"
  12. )
  13. type mockOutboundManager struct {
  14. outbound.Manager
  15. outbound.HandlerSelector
  16. }
  17. func TestSimpleRouter(t *testing.T) {
  18. config := &Config{
  19. Rule: []*RoutingRule{
  20. {
  21. TargetTag: &RoutingRule_Tag{
  22. Tag: "test",
  23. },
  24. Networks: []net.Network{net.Network_TCP},
  25. },
  26. },
  27. }
  28. mockCtl := gomock.NewController(t)
  29. defer mockCtl.Finish()
  30. mockDns := mocks.NewDNSClient(mockCtl)
  31. mockOhm := mocks.NewOutboundManager(mockCtl)
  32. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  33. r := new(Router)
  34. common.Must(r.Init(config, mockDns, &mockOutboundManager{
  35. Manager: mockOhm,
  36. HandlerSelector: mockHs,
  37. }))
  38. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  39. tag, err := r.PickRoute(ctx)
  40. common.Must(err)
  41. if tag != "test" {
  42. t.Error("expect tag 'test', bug actually ", tag)
  43. }
  44. }
  45. func TestSimpleBalancer(t *testing.T) {
  46. config := &Config{
  47. Rule: []*RoutingRule{
  48. {
  49. TargetTag: &RoutingRule_BalancingTag{
  50. BalancingTag: "balance",
  51. },
  52. Networks: []net.Network{net.Network_TCP},
  53. },
  54. },
  55. BalancingRule: []*BalancingRule{
  56. {
  57. Tag: "balance",
  58. OutboundSelector: []string{"test-"},
  59. },
  60. },
  61. }
  62. mockCtl := gomock.NewController(t)
  63. defer mockCtl.Finish()
  64. mockDns := mocks.NewDNSClient(mockCtl)
  65. mockOhm := mocks.NewOutboundManager(mockCtl)
  66. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  67. mockHs.EXPECT().Select(gomock.Eq([]string{"test-"})).Return([]string{"test"})
  68. r := new(Router)
  69. common.Must(r.Init(config, mockDns, &mockOutboundManager{
  70. Manager: mockOhm,
  71. HandlerSelector: mockHs,
  72. }))
  73. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  74. tag, err := r.PickRoute(ctx)
  75. common.Must(err)
  76. if tag != "test" {
  77. t.Error("expect tag 'test', bug actually ", tag)
  78. }
  79. }
  80. func TestIPOnDemand(t *testing.T) {
  81. config := &Config{
  82. DomainStrategy: Config_IpOnDemand,
  83. Rule: []*RoutingRule{
  84. {
  85. TargetTag: &RoutingRule_Tag{
  86. Tag: "test",
  87. },
  88. Cidr: []*CIDR{
  89. {
  90. Ip: []byte{192, 168, 0, 0},
  91. Prefix: 16,
  92. },
  93. },
  94. },
  95. },
  96. }
  97. mockCtl := gomock.NewController(t)
  98. defer mockCtl.Finish()
  99. mockDns := mocks.NewDNSClient(mockCtl)
  100. mockDns.EXPECT().LookupIP(gomock.Eq("v2ray.com")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  101. r := new(Router)
  102. common.Must(r.Init(config, mockDns, nil))
  103. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  104. tag, err := r.PickRoute(ctx)
  105. common.Must(err)
  106. if tag != "test" {
  107. t.Error("expect tag 'test', bug actually ", tag)
  108. }
  109. }
  110. func TestIPIfNonMatchDomain(t *testing.T) {
  111. config := &Config{
  112. DomainStrategy: Config_IpIfNonMatch,
  113. Rule: []*RoutingRule{
  114. {
  115. TargetTag: &RoutingRule_Tag{
  116. Tag: "test",
  117. },
  118. Cidr: []*CIDR{
  119. {
  120. Ip: []byte{192, 168, 0, 0},
  121. Prefix: 16,
  122. },
  123. },
  124. },
  125. },
  126. }
  127. mockCtl := gomock.NewController(t)
  128. defer mockCtl.Finish()
  129. mockDns := mocks.NewDNSClient(mockCtl)
  130. mockDns.EXPECT().LookupIP(gomock.Eq("v2ray.com")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  131. r := new(Router)
  132. common.Must(r.Init(config, mockDns, nil))
  133. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  134. tag, err := r.PickRoute(ctx)
  135. common.Must(err)
  136. if tag != "test" {
  137. t.Error("expect tag 'test', bug actually ", tag)
  138. }
  139. }
  140. func TestIPIfNonMatchIP(t *testing.T) {
  141. config := &Config{
  142. DomainStrategy: Config_IpIfNonMatch,
  143. Rule: []*RoutingRule{
  144. {
  145. TargetTag: &RoutingRule_Tag{
  146. Tag: "test",
  147. },
  148. Cidr: []*CIDR{
  149. {
  150. Ip: []byte{127, 0, 0, 0},
  151. Prefix: 8,
  152. },
  153. },
  154. },
  155. },
  156. }
  157. mockCtl := gomock.NewController(t)
  158. defer mockCtl.Finish()
  159. mockDns := mocks.NewDNSClient(mockCtl)
  160. r := new(Router)
  161. common.Must(r.Init(config, mockDns, nil))
  162. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 80)})
  163. tag, err := r.PickRoute(ctx)
  164. common.Must(err)
  165. if tag != "test" {
  166. t.Error("expect tag 'test', bug actually ", tag)
  167. }
  168. }