github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/core/xray_test.go (about)

     1  package core_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/xtls/xray-core/app/dispatcher"
     7  	"github.com/xtls/xray-core/app/proxyman"
     8  	"github.com/xtls/xray-core/common"
     9  	"github.com/xtls/xray-core/common/net"
    10  	"github.com/xtls/xray-core/common/protocol"
    11  	"github.com/xtls/xray-core/common/serial"
    12  	"github.com/xtls/xray-core/common/uuid"
    13  	. "github.com/xtls/xray-core/core"
    14  	"github.com/xtls/xray-core/features/dns"
    15  	"github.com/xtls/xray-core/features/dns/localdns"
    16  	_ "github.com/xtls/xray-core/main/distro/all"
    17  	"github.com/xtls/xray-core/proxy/dokodemo"
    18  	"github.com/xtls/xray-core/proxy/vmess"
    19  	"github.com/xtls/xray-core/proxy/vmess/outbound"
    20  	"github.com/xtls/xray-core/testing/servers/tcp"
    21  	"google.golang.org/protobuf/proto"
    22  )
    23  
    24  func TestXrayDependency(t *testing.T) {
    25  	instance := new(Instance)
    26  
    27  	wait := make(chan bool, 1)
    28  	instance.RequireFeatures(func(d dns.Client) {
    29  		if d == nil {
    30  			t.Error("expected dns client fulfilled, but actually nil")
    31  		}
    32  		wait <- true
    33  	})
    34  	instance.AddFeature(localdns.New())
    35  	<-wait
    36  }
    37  
    38  func TestXrayClose(t *testing.T) {
    39  	port := tcp.PickPort()
    40  
    41  	userID := uuid.New()
    42  	config := &Config{
    43  		App: []*serial.TypedMessage{
    44  			serial.ToTypedMessage(&dispatcher.Config{}),
    45  			serial.ToTypedMessage(&proxyman.InboundConfig{}),
    46  			serial.ToTypedMessage(&proxyman.OutboundConfig{}),
    47  		},
    48  		Inbound: []*InboundHandlerConfig{
    49  			{
    50  				ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
    51  					PortList: &net.PortList{
    52  						Range: []*net.PortRange{net.SinglePortRange(port)},
    53  					},
    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},
    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("protobuf", cfgBytes)
    90  	common.Must(err)
    91  	server.Close()
    92  }