2016-07-23 13:17:51 +02:00
|
|
|
package encoding_test
|
2016-02-27 22:37:14 +01:00
|
|
|
|
|
|
|
import (
|
2017-01-29 12:58:52 +01:00
|
|
|
"context"
|
2016-02-27 22:37:14 +01:00
|
|
|
"testing"
|
|
|
|
|
2016-12-09 11:35:27 +01:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-08-20 20:55:45 +02:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-15 11:51:09 +01:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core/common/uuid"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
|
|
|
. "v2ray.com/core/proxy/vmess/encoding"
|
|
|
|
"v2ray.com/core/testing/assert"
|
2016-02-27 22:37:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestSerialization(t *testing.T) {
|
2016-05-24 21:55:46 +02:00
|
|
|
assert := assert.On(t)
|
2016-02-27 22:37:14 +01:00
|
|
|
|
2016-09-18 00:41:21 +02:00
|
|
|
user := &protocol.User{
|
|
|
|
Level: 0,
|
|
|
|
Email: "test@v2ray.com",
|
|
|
|
}
|
2016-10-12 18:43:55 +02:00
|
|
|
account := &vmess.Account{
|
2016-09-18 00:41:21 +02:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
AlterId: 0,
|
2016-07-25 17:36:24 +02:00
|
|
|
}
|
2016-12-15 11:51:09 +01:00
|
|
|
user.Account = serial.ToTypedMessage(account)
|
2016-02-27 22:37:14 +01:00
|
|
|
|
|
|
|
expectedRequest := &protocol.RequestHeader{
|
2016-12-07 17:32:40 +01:00
|
|
|
Version: 1,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2016-12-12 13:08:31 +01:00
|
|
|
Option: protocol.RequestOptionConnectionReuse,
|
2016-12-07 17:32:40 +01:00
|
|
|
Address: v2net.DomainAddress("www.v2ray.com"),
|
|
|
|
Port: v2net.Port(443),
|
|
|
|
Security: protocol.Security(protocol.SecurityType_AES128_GCM),
|
2016-02-27 22:37:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 12:08:25 +01:00
|
|
|
buffer := buf.New()
|
2016-02-27 22:37:14 +01:00
|
|
|
client := NewClientSession(protocol.DefaultIDHash)
|
|
|
|
client.EncodeRequestHeader(expectedRequest, buffer)
|
|
|
|
|
2017-02-12 14:00:17 +01:00
|
|
|
buffer2 := buf.New()
|
|
|
|
buffer2.Append(buffer.Bytes())
|
|
|
|
|
2017-01-29 12:58:52 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-02-12 16:53:23 +01:00
|
|
|
sessionHistory := NewSessionHistory(ctx)
|
|
|
|
|
2017-01-29 12:58:52 +01:00
|
|
|
userValidator := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
|
2016-02-27 22:37:14 +01:00
|
|
|
userValidator.Add(user)
|
|
|
|
|
2017-02-12 16:53:23 +01:00
|
|
|
server := NewServerSession(userValidator, sessionHistory)
|
2016-02-27 22:37:14 +01:00
|
|
|
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 15:29:08 +02:00
|
|
|
assert.Address(expectedRequest.Address).Equals(actualRequest.Address)
|
|
|
|
assert.Port(expectedRequest.Port).Equals(actualRequest.Port)
|
2016-12-07 17:32:40 +01:00
|
|
|
assert.Byte(byte(expectedRequest.Security)).Equals(byte(actualRequest.Security))
|
2017-02-12 14:00:17 +01:00
|
|
|
|
|
|
|
_, err = server.DecodeRequestHeader(buffer2)
|
2017-02-21 15:48:21 +01:00
|
|
|
// anti replay attack
|
2017-02-12 14:00:17 +01:00
|
|
|
assert.Error(err).IsNotNil()
|
|
|
|
|
2017-01-29 12:58:52 +01:00
|
|
|
cancel()
|
2016-02-27 22:37:14 +01:00
|
|
|
}
|