github.com/eagleql/xray-core@v1.4.4/app/proxyman/outbound/handler_test.go (about)

     1  package outbound_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/eagleql/xray-core/app/policy"
     8  	. "github.com/eagleql/xray-core/app/proxyman/outbound"
     9  	"github.com/eagleql/xray-core/app/stats"
    10  	"github.com/eagleql/xray-core/common/net"
    11  	"github.com/eagleql/xray-core/common/serial"
    12  	core "github.com/eagleql/xray-core/core"
    13  	"github.com/eagleql/xray-core/features/outbound"
    14  	"github.com/eagleql/xray-core/proxy/freedom"
    15  	"github.com/eagleql/xray-core/transport/internet"
    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.(*internet.StatCouterConnection)
    48  	if ok {
    49  		t.Errorf("Expected conn to not be StatCouterConnection")
    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.(*internet.StatCouterConnection)
    77  	if !ok {
    78  		t.Errorf("Expected conn to be StatCouterConnection")
    79  	}
    80  }