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

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