github.com/v2fly/v2ray-core/v4@v4.45.2/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 9 . "github.com/v2fly/v2ray-core/v4/app/router" 10 "github.com/v2fly/v2ray-core/v4/common" 11 "github.com/v2fly/v2ray-core/v4/common/net" 12 "github.com/v2fly/v2ray-core/v4/common/session" 13 "github.com/v2fly/v2ray-core/v4/features/outbound" 14 routing_session "github.com/v2fly/v2ray-core/v4/features/routing/session" 15 "github.com/v2fly/v2ray-core/v4/testing/mocks" 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(context.TODO(), config, mockDNS, &mockOutboundManager{ 44 Manager: mockOhm, 45 HandlerSelector: mockHs, 46 })) 47 48 ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 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(context.TODO(), config, mockDNS, &mockOutboundManager{ 85 Manager: mockOhm, 86 HandlerSelector: mockHs, 87 })) 88 89 ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 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("v2fly.org")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes() 120 121 r := new(Router) 122 common.Must(r.Init(context.TODO(), config, mockDNS, nil)) 123 124 ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)}) 125 route, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) 126 common.Must(err) 127 if tag := route.GetOutboundTag(); tag != "test" { 128 t.Error("expect tag 'test', bug actually ", tag) 129 } 130 } 131 132 func TestIPIfNonMatchDomain(t *testing.T) { 133 config := &Config{ 134 DomainStrategy: Config_IpIfNonMatch, 135 Rule: []*RoutingRule{ 136 { 137 TargetTag: &RoutingRule_Tag{ 138 Tag: "test", 139 }, 140 Cidr: []*CIDR{ 141 { 142 Ip: []byte{192, 168, 0, 0}, 143 Prefix: 16, 144 }, 145 }, 146 }, 147 }, 148 } 149 150 mockCtl := gomock.NewController(t) 151 defer mockCtl.Finish() 152 153 mockDNS := mocks.NewDNSClient(mockCtl) 154 mockDNS.EXPECT().LookupIP(gomock.Eq("v2fly.org")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes() 155 156 r := new(Router) 157 common.Must(r.Init(context.TODO(), config, mockDNS, nil)) 158 159 ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)}) 160 route, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) 161 common.Must(err) 162 if tag := route.GetOutboundTag(); tag != "test" { 163 t.Error("expect tag 'test', bug actually ", tag) 164 } 165 } 166 167 func TestIPIfNonMatchIP(t *testing.T) { 168 config := &Config{ 169 DomainStrategy: Config_IpIfNonMatch, 170 Rule: []*RoutingRule{ 171 { 172 TargetTag: &RoutingRule_Tag{ 173 Tag: "test", 174 }, 175 Cidr: []*CIDR{ 176 { 177 Ip: []byte{127, 0, 0, 0}, 178 Prefix: 8, 179 }, 180 }, 181 }, 182 }, 183 } 184 185 mockCtl := gomock.NewController(t) 186 defer mockCtl.Finish() 187 188 mockDNS := mocks.NewDNSClient(mockCtl) 189 190 r := new(Router) 191 common.Must(r.Init(context.TODO(), config, mockDNS, nil)) 192 193 ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 80)}) 194 route, err := r.PickRoute(routing_session.AsRoutingContext(ctx)) 195 common.Must(err) 196 if tag := route.GetOutboundTag(); tag != "test" { 197 t.Error("expect tag 'test', bug actually ", tag) 198 } 199 }