1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-09 05:26:39 -04:00
v2fly/proxy/vmess/encoding/encoding_test.go

68 lines
1.9 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 (
2017-01-29 06:58:52 -05:00
"context"
2016-02-27 16:37:14 -05:00
"testing"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
2016-12-15 05:51:09 -05:00
"v2ray.com/core/common/serial"
2016-08-20 14:55:45 -04: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 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-12-15 05:51:09 -05:00
user.Account = serial.ToTypedMessage(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,
2016-12-12 07:08:31 -05:00
Option: protocol.RequestOptionConnectionReuse,
2016-12-07 11:32:40 -05:00
Address: v2net.DomainAddress("www.v2ray.com"),
Port: v2net.Port(443),
Security: protocol.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)
client.EncodeRequestHeader(expectedRequest, buffer)
2017-02-12 08:00:17 -05:00
buffer2 := buf.New()
buffer2.Append(buffer.Bytes())
2017-01-29 06:58:52 -05:00
ctx, cancel := context.WithCancel(context.Background())
userValidator := vmess.NewTimedUserValidator(ctx, 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-12-07 11:32:40 -05:00
assert.Byte(byte(expectedRequest.Security)).Equals(byte(actualRequest.Security))
2017-02-12 08:00:17 -05:00
_, err = server.DecodeRequestHeader(buffer2)
// anti reply attack
assert.Error(err).IsNotNil()
2017-01-29 06:58:52 -05:00
cancel()
2016-02-27 16:37:14 -05:00
}