1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 02:46:33 -05:00
v2fly/proxy/vmess/encoding/encoding_test.go

161 lines
4.2 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"
"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"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
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) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2016-02-27 16:37:14 -05:00
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)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2016-02-27 16:37:14 -05:00
2017-10-24 10:15:35 -04:00
assert(expectedRequest.Version, Equals, actualRequest.Version)
assert(byte(expectedRequest.Command), Equals, byte(actualRequest.Command))
assert(byte(expectedRequest.Option), Equals, byte(actualRequest.Option))
assert(expectedRequest.Address, Equals, actualRequest.Address)
assert(expectedRequest.Port, Equals, actualRequest.Port)
assert(byte(expectedRequest.Security), Equals, byte(actualRequest.Security))
2017-02-12 08:00:17 -05:00
_, err = server.DecodeRequestHeader(buffer2)
2017-02-21 09:48:21 -05:00
// anti replay attack
2017-10-24 10:15:35 -04:00
assert(err, IsNotNil)
2016-02-27 16:37:14 -05:00
}
2018-02-09 11:48:09 -05:00
func TestInvalidRequest(t *testing.T) {
assert := With(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)
assert(err, IsNotNil)
}
func TestMuxRequest(t *testing.T) {
assert := With(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,
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)
assert(err, IsNil)
assert(expectedRequest.Version, Equals, actualRequest.Version)
assert(byte(expectedRequest.Command), Equals, byte(actualRequest.Command))
assert(byte(expectedRequest.Option), Equals, byte(actualRequest.Option))
assert(byte(expectedRequest.Security), Equals, byte(actualRequest.Security))
}