2015-09-19 17:54:36 -04:00
|
|
|
package protocol
|
2015-09-08 19:11:02 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
|
2015-09-14 13:03:31 -04:00
|
|
|
"github.com/v2ray/v2ray-core/testing/mocks"
|
2015-09-11 11:27:36 -04:00
|
|
|
"github.com/v2ray/v2ray-core/testing/unit"
|
2015-09-08 19:11:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVMessSerialization(t *testing.T) {
|
2015-09-11 11:27:36 -04:00
|
|
|
assert := unit.Assert(t)
|
|
|
|
|
2015-09-19 18:11:14 -04:00
|
|
|
userId, err := user.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-19 18:11:14 -04:00
|
|
|
userSet := mocks.MockUserSet{[]user.ID{}, make(map[string]int), make(map[string]int64)}
|
|
|
|
userSet.AddUser(user.User{userId})
|
2015-09-08 19:11:02 -04:00
|
|
|
|
|
|
|
request := new(VMessRequest)
|
2015-09-11 11:27:36 -04:00
|
|
|
request.Version = byte(0x01)
|
|
|
|
request.UserId = userId
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2015-10-07 08:50:17 -04:00
|
|
|
randBytes := make([]byte, 36)
|
|
|
|
_, err = rand.Read(randBytes)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
request.RequestIV = randBytes[:16]
|
|
|
|
request.RequestKey = randBytes[16:32]
|
|
|
|
request.ResponseHeader = randBytes[32:]
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
request.Command = byte(0x01)
|
|
|
|
request.Address = v2net.DomainAddress("v2ray.com", 80)
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2015-09-16 15:13:13 -04:00
|
|
|
mockTime := int64(1823730)
|
2015-09-22 08:50:34 -04:00
|
|
|
buffer, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), func(base int64, delta int) int64 { return mockTime }, nil)
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-17 17:29:12 -04:00
|
|
|
userSet.UserHashes[string(buffer[:16])] = 0
|
|
|
|
userSet.Timestamps[string(buffer[:16])] = mockTime
|
2015-09-14 13:03:31 -04:00
|
|
|
|
|
|
|
requestReader := NewVMessRequestReader(&userSet)
|
2015-09-17 17:29:12 -04:00
|
|
|
actualRequest, err := requestReader.Read(bytes.NewReader(buffer))
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01))
|
2015-09-14 13:03:31 -04:00
|
|
|
assert.String(actualRequest.UserId.String).Named("UserId").Equals(request.UserId.String)
|
2015-10-07 08:50:17 -04:00
|
|
|
assert.Bytes(actualRequest.RequestIV).Named("RequestIV").Equals(request.RequestIV[:])
|
|
|
|
assert.Bytes(actualRequest.RequestKey).Named("RequestKey").Equals(request.RequestKey[:])
|
|
|
|
assert.Bytes(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader[:])
|
2015-09-11 11:27:36 -04:00
|
|
|
assert.Byte(actualRequest.Command).Named("Command").Equals(request.Command)
|
|
|
|
assert.String(actualRequest.Address.String()).Named("Address").Equals(request.Address.String())
|
2015-09-08 19:11:02 -04:00
|
|
|
}
|
2015-09-14 15:59:44 -04:00
|
|
|
|
|
|
|
func BenchmarkVMessRequestWriting(b *testing.B) {
|
2015-09-19 18:11:14 -04:00
|
|
|
userId, _ := user.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
|
|
|
userSet := mocks.MockUserSet{[]user.ID{}, make(map[string]int), make(map[string]int64)}
|
|
|
|
userSet.AddUser(user.User{userId})
|
2015-09-14 15:59:44 -04:00
|
|
|
|
|
|
|
request := new(VMessRequest)
|
|
|
|
request.Version = byte(0x01)
|
|
|
|
request.UserId = userId
|
|
|
|
|
2015-10-07 08:50:17 -04:00
|
|
|
randBytes := make([]byte, 36)
|
|
|
|
rand.Read(randBytes)
|
|
|
|
request.RequestIV = randBytes[:16]
|
|
|
|
request.RequestKey = randBytes[16:32]
|
|
|
|
request.ResponseHeader = randBytes[32:]
|
2015-09-14 15:59:44 -04:00
|
|
|
|
|
|
|
request.Command = byte(0x01)
|
|
|
|
request.Address = v2net.DomainAddress("v2ray.com", 80)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-09-22 08:50:34 -04:00
|
|
|
request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, nil)
|
2015-09-14 15:59:44 -04:00
|
|
|
}
|
|
|
|
}
|