github.com/moqsien/xraycore@v1.8.5/app/proxyman/outbound/handler_test.go (about) 1 package outbound_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/moqsien/xraycore/app/policy" 8 . "github.com/moqsien/xraycore/app/proxyman/outbound" 9 "github.com/moqsien/xraycore/app/stats" 10 "github.com/moqsien/xraycore/common/net" 11 "github.com/moqsien/xraycore/common/serial" 12 core "github.com/moqsien/xraycore/core" 13 "github.com/moqsien/xraycore/features/outbound" 14 "github.com/moqsien/xraycore/proxy/freedom" 15 "github.com/moqsien/xraycore/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 }