github.com/eagleql/xray-core@v1.4.4/proxy/vless/encoding/encoding_test.go (about)

     1  package encoding_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	"github.com/eagleql/xray-core/common"
     9  	"github.com/eagleql/xray-core/common/buf"
    10  	"github.com/eagleql/xray-core/common/net"
    11  	"github.com/eagleql/xray-core/common/protocol"
    12  	"github.com/eagleql/xray-core/common/uuid"
    13  	"github.com/eagleql/xray-core/proxy/vless"
    14  	. "github.com/eagleql/xray-core/proxy/vless/encoding"
    15  )
    16  
    17  func toAccount(a *vless.Account) protocol.Account {
    18  	account, err := a.AsAccount()
    19  	common.Must(err)
    20  	return account
    21  }
    22  
    23  func TestRequestSerialization(t *testing.T) {
    24  	user := &protocol.MemoryUser{
    25  		Level: 0,
    26  		Email: "test@example.com",
    27  	}
    28  	id := uuid.New()
    29  	account := &vless.Account{
    30  		Id: id.String(),
    31  	}
    32  	user.Account = toAccount(account)
    33  
    34  	expectedRequest := &protocol.RequestHeader{
    35  		Version: Version,
    36  		User:    user,
    37  		Command: protocol.RequestCommandTCP,
    38  		Address: net.DomainAddress("www.example.com"),
    39  		Port:    net.Port(443),
    40  	}
    41  	expectedAddons := &Addons{}
    42  
    43  	buffer := buf.StackNew()
    44  	common.Must(EncodeRequestHeader(&buffer, expectedRequest, expectedAddons))
    45  
    46  	Validator := new(vless.Validator)
    47  	Validator.Add(user)
    48  
    49  	actualRequest, actualAddons, _, err := DecodeRequestHeader(false, nil, &buffer, Validator)
    50  	common.Must(err)
    51  
    52  	if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
    53  		t.Error(r)
    54  	}
    55  	if r := cmp.Diff(actualAddons, expectedAddons); r != "" {
    56  		t.Error(r)
    57  	}
    58  }
    59  
    60  func TestInvalidRequest(t *testing.T) {
    61  	user := &protocol.MemoryUser{
    62  		Level: 0,
    63  		Email: "test@example.com",
    64  	}
    65  	id := uuid.New()
    66  	account := &vless.Account{
    67  		Id: id.String(),
    68  	}
    69  	user.Account = toAccount(account)
    70  
    71  	expectedRequest := &protocol.RequestHeader{
    72  		Version: Version,
    73  		User:    user,
    74  		Command: protocol.RequestCommand(100),
    75  		Address: net.DomainAddress("www.example.com"),
    76  		Port:    net.Port(443),
    77  	}
    78  	expectedAddons := &Addons{}
    79  
    80  	buffer := buf.StackNew()
    81  	common.Must(EncodeRequestHeader(&buffer, expectedRequest, expectedAddons))
    82  
    83  	Validator := new(vless.Validator)
    84  	Validator.Add(user)
    85  
    86  	_, _, _, err := DecodeRequestHeader(false, nil, &buffer, Validator)
    87  	if err == nil {
    88  		t.Error("nil error")
    89  	}
    90  }
    91  
    92  func TestMuxRequest(t *testing.T) {
    93  	user := &protocol.MemoryUser{
    94  		Level: 0,
    95  		Email: "test@example.com",
    96  	}
    97  	id := uuid.New()
    98  	account := &vless.Account{
    99  		Id: id.String(),
   100  	}
   101  	user.Account = toAccount(account)
   102  
   103  	expectedRequest := &protocol.RequestHeader{
   104  		Version: Version,
   105  		User:    user,
   106  		Command: protocol.RequestCommandMux,
   107  		Address: net.DomainAddress("v1.mux.cool"),
   108  	}
   109  	expectedAddons := &Addons{}
   110  
   111  	buffer := buf.StackNew()
   112  	common.Must(EncodeRequestHeader(&buffer, expectedRequest, expectedAddons))
   113  
   114  	Validator := new(vless.Validator)
   115  	Validator.Add(user)
   116  
   117  	actualRequest, actualAddons, _, err := DecodeRequestHeader(false, nil, &buffer, Validator)
   118  	common.Must(err)
   119  
   120  	if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
   121  		t.Error(r)
   122  	}
   123  	if r := cmp.Diff(actualAddons, expectedAddons); r != "" {
   124  		t.Error(r)
   125  	}
   126  }