2016-07-23 07:17:51 -04:00
|
|
|
package encoding_test
|
2016-02-27 16:37:14 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/alloc"
|
2016-10-17 08:35:13 -04:00
|
|
|
"v2ray.com/core/common/loader"
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/uuid"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
|
|
|
. "v2ray.com/core/proxy/vmess/encoding"
|
|
|
|
"v2ray.com/core/testing/assert"
|
2016-02-27 16:37:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestSerialization(t *testing.T) {
|
2016-05-24 15:55:46 -04:00
|
|
|
assert := assert.On(t)
|
2016-02-27 16:37:14 -05:00
|
|
|
|
2016-09-17 18:41:21 -04:00
|
|
|
user := &protocol.User{
|
|
|
|
Level: 0,
|
|
|
|
Email: "test@v2ray.com",
|
|
|
|
}
|
2016-10-12 12:43:55 -04:00
|
|
|
account := &vmess.Account{
|
2016-09-17 18:41:21 -04:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
AlterId: 0,
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
2016-10-17 08:35:13 -04:00
|
|
|
user.Account = loader.NewTypedSettings(account)
|
2016-02-27 16:37:14 -05:00
|
|
|
|
|
|
|
expectedRequest := &protocol.RequestHeader{
|
|
|
|
Version: 1,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
Option: protocol.RequestOption(0),
|
|
|
|
Address: v2net.DomainAddress("www.v2ray.com"),
|
|
|
|
Port: v2net.Port(443),
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := alloc.NewBuffer().Clear()
|
|
|
|
client := NewClientSession(protocol.DefaultIDHash)
|
|
|
|
client.EncodeRequestHeader(expectedRequest, buffer)
|
|
|
|
|
2016-07-25 11:36:24 -04:00
|
|
|
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
|
2016-02-27 16:37:14 -05:00
|
|
|
userValidator.Add(user)
|
|
|
|
|
|
|
|
server := NewServerSession(userValidator)
|
|
|
|
actualRequest, err := server.DecodeRequestHeader(buffer)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
assert.Byte(expectedRequest.Version).Equals(actualRequest.Version)
|
|
|
|
assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command))
|
|
|
|
assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option))
|
2016-05-24 09:29:08 -04:00
|
|
|
assert.Address(expectedRequest.Address).Equals(actualRequest.Address)
|
|
|
|
assert.Port(expectedRequest.Port).Equals(actualRequest.Port)
|
2016-02-27 16:37:14 -05:00
|
|
|
}
|