v2fly/app/proxyman/outbound/handler_test.go

84 lines
2.3 KiB
Go
Raw Normal View History

package outbound_test
import (
2020-06-27 04:28:53 +00:00
"context"
2021-06-19 13:36:54 +00:00
"google.golang.org/protobuf/types/known/anypb"
"testing"
2021-05-04 23:28:09 +00:00
_ "unsafe"
2021-02-16 20:31:50 +00:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/policy"
. "github.com/v2fly/v2ray-core/v4/app/proxyman/outbound"
"github.com/v2fly/v2ray-core/v4/app/stats"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/features/outbound"
"github.com/v2fly/v2ray-core/v4/proxy/freedom"
"github.com/v2fly/v2ray-core/v4/transport/internet"
)
func TestInterfaces(t *testing.T) {
2019-01-07 22:27:59 +00:00
_ = (outbound.Handler)(new(Handler))
_ = (outbound.Manager)(new(Manager))
}
2020-06-27 04:28:53 +00:00
2021-05-04 19:41:09 +00:00
//go:linkname toContext github.com/v2fly/v2ray-core/v4.toContext
func toContext(ctx context.Context, v *core.Instance) context.Context
2020-06-27 04:28:53 +00:00
func TestOutboundWithoutStatCounter(t *testing.T) {
config := &core.Config{
2021-06-19 13:36:54 +00:00
App: []*anypb.Any{
2020-06-27 04:28:53 +00:00
serial.ToTypedMessage(&stats.Config{}),
serial.ToTypedMessage(&policy.Config{
System: &policy.SystemPolicy{
Stats: &policy.SystemPolicy_Stats{
InboundUplink: true,
},
},
}),
},
}
v, _ := core.New(config)
v.AddFeature((outbound.Manager)(new(Manager)))
2021-05-04 19:41:09 +00:00
ctx := toContext(context.Background(), v)
2020-06-27 04:28:53 +00:00
h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
Tag: "tag",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
})
conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
_, ok := conn.(*internet.StatCouterConnection)
if ok {
t.Errorf("Expected conn to not be StatCouterConnection")
}
}
func TestOutboundWithStatCounter(t *testing.T) {
config := &core.Config{
2021-06-19 13:36:54 +00:00
App: []*anypb.Any{
2020-06-27 04:28:53 +00:00
serial.ToTypedMessage(&stats.Config{}),
serial.ToTypedMessage(&policy.Config{
System: &policy.SystemPolicy{
Stats: &policy.SystemPolicy_Stats{
2020-08-30 13:17:22 +00:00
OutboundUplink: true,
2020-06-27 04:28:53 +00:00
OutboundDownlink: true,
},
},
}),
},
}
v, _ := core.New(config)
v.AddFeature((outbound.Manager)(new(Manager)))
2021-05-04 19:41:09 +00:00
ctx := toContext(context.Background(), v)
2020-06-27 04:28:53 +00:00
h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
Tag: "tag",
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
})
conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
_, ok := conn.(*internet.StatCouterConnection)
if !ok {
t.Errorf("Expected conn to be StatCouterConnection")
}
}