github.com/v2fly/v2ray-core/v4@v4.45.2/v2ray_test.go (about)

     1  package core_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/golang/protobuf/proto"
     7  
     8  	. "github.com/v2fly/v2ray-core/v4"
     9  	"github.com/v2fly/v2ray-core/v4/app/dispatcher"
    10  	"github.com/v2fly/v2ray-core/v4/app/proxyman"
    11  	"github.com/v2fly/v2ray-core/v4/common"
    12  	"github.com/v2fly/v2ray-core/v4/common/net"
    13  	"github.com/v2fly/v2ray-core/v4/common/protocol"
    14  	"github.com/v2fly/v2ray-core/v4/common/serial"
    15  	"github.com/v2fly/v2ray-core/v4/common/uuid"
    16  	"github.com/v2fly/v2ray-core/v4/features/dns"
    17  	"github.com/v2fly/v2ray-core/v4/features/dns/localdns"
    18  	_ "github.com/v2fly/v2ray-core/v4/main/distro/all"
    19  	"github.com/v2fly/v2ray-core/v4/proxy/dokodemo"
    20  	"github.com/v2fly/v2ray-core/v4/proxy/vmess"
    21  	"github.com/v2fly/v2ray-core/v4/proxy/vmess/outbound"
    22  	"github.com/v2fly/v2ray-core/v4/testing/servers/tcp"
    23  )
    24  
    25  func TestV2RayDependency(t *testing.T) {
    26  	instance := new(Instance)
    27  
    28  	wait := make(chan bool, 1)
    29  	instance.RequireFeatures(func(d dns.Client) {
    30  		if d == nil {
    31  			t.Error("expected dns client fulfilled, but actually nil")
    32  		}
    33  		wait <- true
    34  	})
    35  	instance.AddFeature(localdns.New())
    36  	<-wait
    37  }
    38  
    39  func TestV2RayClose(t *testing.T) {
    40  	port := tcp.PickPort()
    41  
    42  	userID := uuid.New()
    43  	config := &Config{
    44  		App: []*serial.TypedMessage{
    45  			serial.ToTypedMessage(&dispatcher.Config{}),
    46  			serial.ToTypedMessage(&proxyman.InboundConfig{}),
    47  			serial.ToTypedMessage(&proxyman.OutboundConfig{}),
    48  		},
    49  		Inbound: []*InboundHandlerConfig{
    50  			{
    51  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    52  					PortRange: net.SinglePortRange(port),
    53  					Listen:    net.NewIPOrDomain(net.LocalHostIP),
    54  				}),
    55  				ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
    56  					Address: net.NewIPOrDomain(net.LocalHostIP),
    57  					Port:    uint32(0),
    58  					NetworkList: &net.NetworkList{
    59  						Network: []net.Network{net.Network_TCP, net.Network_UDP},
    60  					},
    61  				}),
    62  			},
    63  		},
    64  		Outbound: []*OutboundHandlerConfig{
    65  			{
    66  				ProxySettings: serial.ToTypedMessage(&outbound.Config{
    67  					Receiver: []*protocol.ServerEndpoint{
    68  						{
    69  							Address: net.NewIPOrDomain(net.LocalHostIP),
    70  							Port:    uint32(0),
    71  							User: []*protocol.User{
    72  								{
    73  									Account: serial.ToTypedMessage(&vmess.Account{
    74  										Id: userID.String(),
    75  									}),
    76  								},
    77  							},
    78  						},
    79  					},
    80  				}),
    81  			},
    82  		},
    83  	}
    84  
    85  	cfgBytes, err := proto.Marshal(config)
    86  	common.Must(err)
    87  
    88  	server, err := StartInstance("protobuf", cfgBytes)
    89  	common.Must(err)
    90  	server.Close()
    91  }