github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/router/router_test.go (about)

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