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