router_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }