v2ray-core/proxy/vmess/encoding/encoding_test.go

157 lines
3.7 KiB
Go
Raw Normal View History

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