router_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. NetworkList: &net.NetworkList{
  24. Network: []net.Network{net.Network_TCP},
  25. },
  26. },
  27. },
  28. }
  29. mockCtl := gomock.NewController(t)
  30. defer mockCtl.Finish()
  31. mockDns := mocks.NewDNSClient(mockCtl)
  32. mockOhm := mocks.NewOutboundManager(mockCtl)
  33. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  34. r := new(Router)
  35. common.Must(r.Init(config, mockDns, &mockOutboundManager{
  36. Manager: mockOhm,
  37. HandlerSelector: mockHs,
  38. }))
  39. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  40. tag, err := r.PickRoute(ctx)
  41. common.Must(err)
  42. if tag != "test" {
  43. t.Error("expect tag 'test', bug actually ", tag)
  44. }
  45. }
  46. func TestSimpleBalancer(t *testing.T) {
  47. config := &Config{
  48. Rule: []*RoutingRule{
  49. {
  50. TargetTag: &RoutingRule_BalancingTag{
  51. BalancingTag: "balance",
  52. },
  53. NetworkList: &net.NetworkList{
  54. Network: []net.Network{net.Network_TCP},
  55. },
  56. },
  57. },
  58. BalancingRule: []*BalancingRule{
  59. {
  60. Tag: "balance",
  61. OutboundSelector: []string{"test-"},
  62. },
  63. },
  64. }
  65. mockCtl := gomock.NewController(t)
  66. defer mockCtl.Finish()
  67. mockDns := mocks.NewDNSClient(mockCtl)
  68. mockOhm := mocks.NewOutboundManager(mockCtl)
  69. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  70. mockHs.EXPECT().Select(gomock.Eq([]string{"test-"})).Return([]string{"test"})
  71. r := new(Router)
  72. common.Must(r.Init(config, mockDns, &mockOutboundManager{
  73. Manager: mockOhm,
  74. HandlerSelector: mockHs,
  75. }))
  76. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  77. tag, err := r.PickRoute(ctx)
  78. common.Must(err)
  79. if tag != "test" {
  80. t.Error("expect tag 'test', bug actually ", tag)
  81. }
  82. }
  83. func TestIPOnDemand(t *testing.T) {
  84. config := &Config{
  85. DomainStrategy: Config_IpOnDemand,
  86. Rule: []*RoutingRule{
  87. {
  88. TargetTag: &RoutingRule_Tag{
  89. Tag: "test",
  90. },
  91. Cidr: []*CIDR{
  92. {
  93. Ip: []byte{192, 168, 0, 0},
  94. Prefix: 16,
  95. },
  96. },
  97. },
  98. },
  99. }
  100. mockCtl := gomock.NewController(t)
  101. defer mockCtl.Finish()
  102. mockDns := mocks.NewDNSClient(mockCtl)
  103. mockDns.EXPECT().LookupIP(gomock.Eq("v2ray.com")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  104. r := new(Router)
  105. common.Must(r.Init(config, mockDns, nil))
  106. ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  107. tag, err := r.PickRoute(ctx)
  108. common.Must(err)
  109. if tag != "test" {
  110. t.Error("expect tag 'test', bug actually ", tag)
  111. }
  112. }