router_test.go 4.3 KB

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