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

158 lines
3.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 (
"context"
2016-02-27 16:37:14 -05:00
"testing"
2019-02-10 09:02:28 -05:00
"github.com/google/go-cmp/cmp"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/uuid"
"github.com/v2fly/v2ray-core/v5/proxy/vmess"
. "github.com/v2fly/v2ray-core/v5/proxy/vmess/encoding"
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) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
2016-09-17 18:41:21 -04:00
Level: 0,
2021-02-16 15:31:50 -05:00
Email: "test@v2fly.org",
2016-09-17 18:41:21 -04:00
}
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,
2021-02-16 15:31:50 -05:00
Address: net.DomainAddress("www.v2fly.org"),
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()
client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash, 0)
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)
2019-02-02 16:19:30 -05:00
common.Must(err)
2016-02-27 16:37:14 -05:00
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
t.Error(r)
}
2017-02-12 08:00:17 -05:00
_, err = server.DecodeRequestHeader(buffer2)
2017-02-21 09:48:21 -05:00
// anti replay attack
2019-02-10 09:02:28 -05:00
if err == nil {
t.Error("nil error")
}
2016-02-27 16:37:14 -05:00
}
2018-02-09 11:48:09 -05:00
func TestInvalidRequest(t *testing.T) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
2018-02-09 11:48:09 -05:00
Level: 0,
2021-02-16 15:31:50 -05:00
Email: "test@v2fly.org",
2018-02-09 11:48:09 -05:00
}
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),
2021-02-16 15:31:50 -05:00
Address: net.DomainAddress("www.v2fly.org"),
2018-02-09 11:48:09 -05:00
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(context.TODO(), true, protocol.DefaultIDHash, 0)
2018-02-09 11:48:09 -05:00
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)
2019-02-10 09:02:28 -05:00
if err == nil {
t.Error("nil error")
}
2018-02-09 11:48:09 -05:00
}
func TestMuxRequest(t *testing.T) {
2018-08-26 18:11:32 -04:00
user := &protocol.MemoryUser{
2018-02-09 11:48:09 -05:00
Level: 0,
2021-02-16 15:31:50 -05:00
Email: "test@v2fly.org",
2018-02-09 11:48:09 -05:00
}
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,
2019-02-10 09:02:28 -05:00
Address: net.DomainAddress("v1.mux.cool"),
2018-02-09 11:48:09 -05:00
}
buffer := buf.New()
client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash, 0)
2018-02-09 11:48:09 -05:00
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)
2019-02-02 16:19:30 -05:00
common.Must(err)
2018-02-09 11:48:09 -05:00
2019-02-10 09:02:28 -05:00
if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
t.Error(r)
}
2018-02-09 11:48:09 -05:00
}