1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 17:27:23 -05:00
v2fly/proxy/vmess/encoding/encoding_test.go

157 lines
3.7 KiB
Go
Raw Normal View History

2016-07-23 07:17:51 -04:00
package encoding_test
2016-02-27 16:37:14 -05:00
import (
"testing"
2019-02-10 09:02:28 -05:00
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
"v2ray.com/core/proxy/vmess"
. "v2ray.com/core/proxy/vmess/encoding"
2016-02-27 16:37:14 -05:00
)
2018-08-26 18:11:32 -04:00
func toAccount(a *vmess.Account) protocol.Account {
account, err := a.AsAccount()
common.Must(err)
return account
}
2016-02-27 16:37:14 -05:00
func TestRequestSerialization(t *testing.T) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
2016-09-17 18:41:21 -04:00
Level: 0,
Email: "test@v2ray.com",
}
2018-01-18 17:25:48 -05:00
id := uuid.New()
2016-10-12 12:43:55 -04:00
account := &vmess.Account{
2018-01-18 17:25:48 -05:00
Id: id.String(),
2016-09-17 18:41:21 -04:00
AlterId: 0,
2016-07-25 11:36:24 -04:00
}
2018-08-26 18:11:32 -04:00
user.Account = toAccount(account)
2016-02-27 16:37:14 -05:00
expectedRequest := &protocol.RequestHeader{
2016-12-07 11:32:40 -05:00
Version: 1,
User: user,
Command: protocol.RequestCommandTCP,
Address: net.DomainAddress("www.v2ray.com"),
Port: net.Port(443),
2018-02-23 06:36:33 -05:00
Security: protocol.SecurityType_AES128_GCM,
2016-02-27 16:37:14 -05:00
}
2016-12-09 06:08:25 -05:00
buffer := buf.New()
2016-02-27 16:37:14 -05:00
client := NewClientSession(protocol.DefaultIDHash)
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
2016-02-27 16:37:14 -05:00
2017-02-12 08:00:17 -05:00
buffer2 := buf.New()
2018-04-19 16:56:55 -04:00
buffer2.Write(buffer.Bytes())
2017-02-12 08:00:17 -05:00
2018-02-07 10:35:22 -05:00
sessionHistory := NewSessionHistory()
2018-02-08 10:13:27 -05:00
defer common.Close(sessionHistory)
2017-02-12 10:53:23 -05:00
2018-02-08 09:39:46 -05:00
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
2016-02-27 16:37:14 -05:00
userValidator.Add(user)
2018-02-08 09:39:46 -05:00
defer common.Close(userValidator)
2016-02-27 16:37:14 -05:00
2017-02-12 10:53:23 -05:00
server := NewServerSession(userValidator, sessionHistory)
2016-02-27 16:37:14 -05:00
actualRequest, err := server.DecodeRequestHeader(buffer)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-02-27 16:37:14 -05:00
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
t.Error(r)
}
2017-02-12 08:00:17 -05:00
_, err = server.DecodeRequestHeader(buffer2)
2017-02-21 09:48:21 -05:00
// anti replay attack
2019-02-10 09:02:28 -05:00
if err == nil {
t.Error("nil error")
}
2016-02-27 16:37:14 -05:00
}
2018-02-09 11:48:09 -05:00
func TestInvalidRequest(t *testing.T) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
2018-02-09 11:48:09 -05:00
Level: 0,
Email: "test@v2ray.com",
}
id := uuid.New()
account := &vmess.Account{
Id: id.String(),
AlterId: 0,
}
2018-08-26 18:11:32 -04:00
user.Account = toAccount(account)
2018-02-09 11:48:09 -05:00
expectedRequest := &protocol.RequestHeader{
Version: 1,
User: user,
Command: protocol.RequestCommand(100),
Address: net.DomainAddress("www.v2ray.com"),
Port: net.Port(443),
2018-02-23 06:36:33 -05:00
Security: protocol.SecurityType_AES128_GCM,
2018-02-09 11:48:09 -05:00
}
buffer := buf.New()
client := NewClientSession(protocol.DefaultIDHash)
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
buffer2 := buf.New()
2018-04-19 16:56:55 -04:00
buffer2.Write(buffer.Bytes())
2018-02-09 11:48:09 -05:00
sessionHistory := NewSessionHistory()
defer common.Close(sessionHistory)
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
userValidator.Add(user)
defer common.Close(userValidator)
server := NewServerSession(userValidator, sessionHistory)
_, err := server.DecodeRequestHeader(buffer)
2019-02-10 09:02:28 -05:00
if err == nil {
t.Error("nil error")
}
2018-02-09 11:48:09 -05:00
}
func TestMuxRequest(t *testing.T) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
2018-02-09 11:48:09 -05:00
Level: 0,
Email: "test@v2ray.com",
}
id := uuid.New()
account := &vmess.Account{
Id: id.String(),
AlterId: 0,
}
2018-08-26 18:11:32 -04:00
user.Account = toAccount(account)
2018-02-09 11:48:09 -05:00
expectedRequest := &protocol.RequestHeader{
Version: 1,
User: user,
Command: protocol.RequestCommandMux,
2018-02-23 06:36:33 -05:00
Security: protocol.SecurityType_AES128_GCM,
2019-02-10 09:02:28 -05:00
Address: net.DomainAddress("v1.mux.cool"),
2018-02-09 11:48:09 -05:00
}
buffer := buf.New()
client := NewClientSession(protocol.DefaultIDHash)
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
buffer2 := buf.New()
2018-04-19 16:56:55 -04:00
buffer2.Write(buffer.Bytes())
2018-02-09 11:48:09 -05:00
sessionHistory := NewSessionHistory()
defer common.Close(sessionHistory)
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
userValidator.Add(user)
defer common.Close(userValidator)
server := NewServerSession(userValidator, sessionHistory)
actualRequest, err := server.DecodeRequestHeader(buffer)
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-02-09 11:48:09 -05:00
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
t.Error(r)
}
2018-02-09 11:48:09 -05:00
}