github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/v2ray_test.go (about)

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