github.com/eagleql/xray-core@v1.4.4/app/router/router_test.go (about)

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