github.com/EagleQL/Xray-core@v1.4.3/proxy/vmess/encoding/encoding_test.go (about) 1 package encoding_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 9 "github.com/xtls/xray-core/common" 10 "github.com/xtls/xray-core/common/buf" 11 "github.com/xtls/xray-core/common/net" 12 "github.com/xtls/xray-core/common/protocol" 13 "github.com/xtls/xray-core/common/uuid" 14 "github.com/xtls/xray-core/proxy/vmess" 15 . "github.com/xtls/xray-core/proxy/vmess/encoding" 16 ) 17 18 func toAccount(a *vmess.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@example.com", 28 } 29 id := uuid.New() 30 account := &vmess.Account{ 31 Id: id.String(), 32 AlterId: 0, 33 } 34 user.Account = toAccount(account) 35 36 expectedRequest := &protocol.RequestHeader{ 37 Version: 1, 38 User: user, 39 Command: protocol.RequestCommandTCP, 40 Address: net.DomainAddress("www.example.com"), 41 Port: net.Port(443), 42 Security: protocol.SecurityType_AES128_GCM, 43 } 44 45 buffer := buf.New() 46 client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash) 47 common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) 48 49 buffer2 := buf.New() 50 buffer2.Write(buffer.Bytes()) 51 52 sessionHistory := NewSessionHistory() 53 defer common.Close(sessionHistory) 54 55 userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) 56 userValidator.Add(user) 57 defer common.Close(userValidator) 58 59 server := NewServerSession(userValidator, sessionHistory) 60 actualRequest, err := server.DecodeRequestHeader(buffer, false) 61 common.Must(err) 62 63 if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" { 64 t.Error(r) 65 } 66 67 _, err = server.DecodeRequestHeader(buffer2, false) 68 // anti replay attack 69 if err == nil { 70 t.Error("nil error") 71 } 72 } 73 74 func TestInvalidRequest(t *testing.T) { 75 user := &protocol.MemoryUser{ 76 Level: 0, 77 Email: "test@example.com", 78 } 79 id := uuid.New() 80 account := &vmess.Account{ 81 Id: id.String(), 82 AlterId: 0, 83 } 84 user.Account = toAccount(account) 85 86 expectedRequest := &protocol.RequestHeader{ 87 Version: 1, 88 User: user, 89 Command: protocol.RequestCommand(100), 90 Address: net.DomainAddress("www.example.com"), 91 Port: net.Port(443), 92 Security: protocol.SecurityType_AES128_GCM, 93 } 94 95 buffer := buf.New() 96 client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash) 97 common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) 98 99 buffer2 := buf.New() 100 buffer2.Write(buffer.Bytes()) 101 102 sessionHistory := NewSessionHistory() 103 defer common.Close(sessionHistory) 104 105 userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) 106 userValidator.Add(user) 107 defer common.Close(userValidator) 108 109 server := NewServerSession(userValidator, sessionHistory) 110 _, err := server.DecodeRequestHeader(buffer, false) 111 if err == nil { 112 t.Error("nil error") 113 } 114 } 115 116 func TestMuxRequest(t *testing.T) { 117 user := &protocol.MemoryUser{ 118 Level: 0, 119 Email: "test@example.com", 120 } 121 id := uuid.New() 122 account := &vmess.Account{ 123 Id: id.String(), 124 AlterId: 0, 125 } 126 user.Account = toAccount(account) 127 128 expectedRequest := &protocol.RequestHeader{ 129 Version: 1, 130 User: user, 131 Command: protocol.RequestCommandMux, 132 Security: protocol.SecurityType_AES128_GCM, 133 Address: net.DomainAddress("v1.mux.cool"), 134 } 135 136 buffer := buf.New() 137 client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash) 138 common.Must(client.EncodeRequestHeader(expectedRequest, buffer)) 139 140 buffer2 := buf.New() 141 buffer2.Write(buffer.Bytes()) 142 143 sessionHistory := NewSessionHistory() 144 defer common.Close(sessionHistory) 145 146 userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) 147 userValidator.Add(user) 148 defer common.Close(userValidator) 149 150 server := NewServerSession(userValidator, sessionHistory) 151 actualRequest, err := server.DecodeRequestHeader(buffer, false) 152 common.Must(err) 153 154 if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" { 155 t.Error(r) 156 } 157 }