github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/proxyman/outbound/handler_test.go (about) 1 package outbound_test 2 3 import ( 4 "context" 5 "testing" 6 _ "unsafe" 7 8 "github.com/v2fly/v2ray-core/v5/common/environment/systemnetworkimpl" 9 10 "github.com/v2fly/v2ray-core/v5/common/environment" 11 "github.com/v2fly/v2ray-core/v5/common/environment/envctx" 12 "github.com/v2fly/v2ray-core/v5/common/environment/transientstorageimpl" 13 14 "google.golang.org/protobuf/types/known/anypb" 15 16 core "github.com/v2fly/v2ray-core/v5" 17 "github.com/v2fly/v2ray-core/v5/app/policy" 18 . "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound" 19 "github.com/v2fly/v2ray-core/v5/app/stats" 20 "github.com/v2fly/v2ray-core/v5/common/net" 21 "github.com/v2fly/v2ray-core/v5/common/serial" 22 "github.com/v2fly/v2ray-core/v5/features/outbound" 23 "github.com/v2fly/v2ray-core/v5/proxy/freedom" 24 "github.com/v2fly/v2ray-core/v5/transport/internet" 25 ) 26 27 func TestInterfaces(t *testing.T) { 28 _ = (outbound.Handler)(new(Handler)) 29 _ = (outbound.Manager)(new(Manager)) 30 } 31 32 //go:linkname toContext github.com/v2fly/v2ray-core/v5.toContext 33 func toContext(ctx context.Context, v *core.Instance) context.Context 34 35 func TestOutboundWithoutStatCounter(t *testing.T) { 36 config := &core.Config{ 37 App: []*anypb.Any{ 38 serial.ToTypedMessage(&stats.Config{}), 39 serial.ToTypedMessage(&policy.Config{ 40 System: &policy.SystemPolicy{ 41 Stats: &policy.SystemPolicy_Stats{ 42 InboundUplink: true, 43 }, 44 }, 45 }), 46 }, 47 } 48 49 v, _ := core.New(config) 50 v.AddFeature((outbound.Manager)(new(Manager))) 51 ctx := toContext(context.Background(), v) 52 defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault() 53 rootEnv := environment.NewRootEnvImpl(ctx, transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener()) 54 proxyEnvironment := rootEnv.ProxyEnvironment("o") 55 ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment) 56 h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{ 57 Tag: "tag", 58 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 59 }) 60 conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) 61 _, ok := conn.(*internet.StatCouterConnection) 62 if ok { 63 t.Errorf("Expected conn to not be StatCouterConnection") 64 } 65 } 66 67 func TestOutboundWithStatCounter(t *testing.T) { 68 config := &core.Config{ 69 App: []*anypb.Any{ 70 serial.ToTypedMessage(&stats.Config{}), 71 serial.ToTypedMessage(&policy.Config{ 72 System: &policy.SystemPolicy{ 73 Stats: &policy.SystemPolicy_Stats{ 74 OutboundUplink: true, 75 OutboundDownlink: true, 76 }, 77 }, 78 }), 79 }, 80 } 81 82 v, _ := core.New(config) 83 v.AddFeature((outbound.Manager)(new(Manager))) 84 ctx := toContext(context.Background(), v) 85 defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault() 86 rootEnv := environment.NewRootEnvImpl(ctx, transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener()) 87 proxyEnvironment := rootEnv.ProxyEnvironment("o") 88 ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment) 89 h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{ 90 Tag: "tag", 91 ProxySettings: serial.ToTypedMessage(&freedom.Config{}), 92 }) 93 conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) 94 _, ok := conn.(*internet.StatCouterConnection) 95 if !ok { 96 t.Errorf("Expected conn to be StatCouterConnection") 97 } 98 }