github.com/v2fly/v2ray-core/v4@v4.45.2/app/proxyman/outbound/handler_test.go (about)

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