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