github.com/xraypb/xray-core@v1.6.6/app/proxyman/outbound/handler_test.go (about) 1 package outbound_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/xraypb/xray-core/app/policy" 8 . "github.com/xraypb/xray-core/app/proxyman/outbound" 9 "github.com/xraypb/xray-core/app/stats" 10 "github.com/xraypb/xray-core/common/net" 11 "github.com/xraypb/xray-core/common/serial" 12 core "github.com/xraypb/xray-core/core" 13 "github.com/xraypb/xray-core/features/outbound" 14 "github.com/xraypb/xray-core/proxy/freedom" 15 "github.com/xraypb/xray-core/transport/internet/stat" 16 ) 17 18 func TestInterfaces(t *testing.T) { 19 _ = (outbound.Handler)(new(Handler)) 20 _ = (outbound.Manager)(new(Manager)) 21 } 22 23 const xrayKey core.XrayKey = 1 24 25 func TestOutboundWithoutStatCounter(t *testing.T) { 26 config := &core.Config{ 27 App: []*serial.TypedMessage{ 28 serial.ToTypedMessage(&stats.Config{}), 29 serial.ToTypedMessage(&policy.Config{ 30 System: &policy.SystemPolicy{ 31 Stats: &policy.SystemPolicy_Stats{ 32 InboundUplink: true, 33 }, 34 }, 35 }), 36 }, 37 } 38 39 v, _ := core.New(config) 40 v.AddFeature((outbound.Manager)(new(Manager))) 41 ctx := context.WithValue(context.Background(), xrayKey, v) 42 h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{ 43 Tag: "tag", 44 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 45 }) 46 conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) 47 _, ok := conn.(*stat.CounterConnection) 48 if ok { 49 t.Errorf("Expected conn to not be CounterConnection") 50 } 51 } 52 53 func TestOutboundWithStatCounter(t *testing.T) { 54 config := &core.Config{ 55 App: []*serial.TypedMessage{ 56 serial.ToTypedMessage(&stats.Config{}), 57 serial.ToTypedMessage(&policy.Config{ 58 System: &policy.SystemPolicy{ 59 Stats: &policy.SystemPolicy_Stats{ 60 OutboundUplink: true, 61 OutboundDownlink: true, 62 }, 63 }, 64 }), 65 }, 66 } 67 68 v, _ := core.New(config) 69 v.AddFeature((outbound.Manager)(new(Manager))) 70 ctx := context.WithValue(context.Background(), xrayKey, v) 71 h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{ 72 Tag: "tag", 73 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 74 }) 75 conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) 76 _, ok := conn.(*stat.CounterConnection) 77 if !ok { 78 t.Errorf("Expected conn to be CounterConnection") 79 } 80 }