2016-01-12 05:52:40 -05:00
|
|
|
package protocol_test
|
2015-09-08 19:11:02 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2015-11-02 17:48:47 -05:00
|
|
|
"io"
|
2015-09-08 19:11:02 -04:00
|
|
|
"testing"
|
2016-01-12 05:38:43 -05:00
|
|
|
"time"
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-12-12 15:40:16 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/uuid"
|
2015-12-07 14:32:38 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
2016-01-12 05:52:40 -05:00
|
|
|
. "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
|
|
|
|
protocoltesting "github.com/v2ray/v2ray-core/proxy/vmess/protocol/testing"
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2015-09-08 19:11:02 -04:00
|
|
|
)
|
|
|
|
|
2016-01-12 05:38:43 -05:00
|
|
|
type FakeTimestampGenerator struct {
|
2016-01-12 05:52:40 -05:00
|
|
|
timestamp Timestamp
|
2016-01-12 05:38:43 -05:00
|
|
|
}
|
|
|
|
|
2016-01-12 05:52:40 -05:00
|
|
|
func (this *FakeTimestampGenerator) Next() Timestamp {
|
2016-01-12 05:38:43 -05:00
|
|
|
return this.timestamp
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:11:02 -04:00
|
|
|
func TestVMessSerialization(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-09-11 11:27:36 -04:00
|
|
|
|
2015-12-12 15:40:16 -05:00
|
|
|
id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
userId := vmess.NewID(id)
|
2015-10-31 09:08:13 -04:00
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
testUser := &vmess.User{
|
|
|
|
ID: userId,
|
2015-10-31 09:08:13 -04:00
|
|
|
}
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
|
2015-10-31 04:39:45 -04:00
|
|
|
userSet.AddUser(testUser)
|
2015-09-08 19:11:02 -04:00
|
|
|
|
|
|
|
request := new(VMessRequest)
|
2015-09-11 11:27:36 -04:00
|
|
|
request.Version = byte(0x01)
|
2015-10-31 04:39:45 -04:00
|
|
|
request.User = testUser
|
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]
|
2016-01-21 04:08:00 -05:00
|
|
|
request.ResponseHeader = randBytes[32]
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
request.Command = byte(0x01)
|
2015-12-16 17:53:38 -05:00
|
|
|
request.Address = v2net.DomainAddress("v2ray.com")
|
|
|
|
request.Port = v2net.Port(80)
|
2015-09-08 19:11:02 -04:00
|
|
|
|
2016-01-12 05:52:40 -05:00
|
|
|
mockTime := Timestamp(1823730)
|
2015-10-21 16:28:26 -04:00
|
|
|
|
2016-01-12 05:38:43 -05:00
|
|
|
buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil)
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:28:26 -04:00
|
|
|
userSet.UserHashes[string(buffer.Value[:16])] = 0
|
|
|
|
userSet.Timestamps[string(buffer.Value[:16])] = mockTime
|
2015-09-14 13:03:31 -04:00
|
|
|
|
|
|
|
requestReader := NewVMessRequestReader(&userSet)
|
2015-10-21 16:28:26 -04:00
|
|
|
actualRequest, err := requestReader.Read(bytes.NewReader(buffer.Value))
|
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))
|
2016-01-15 06:43:06 -05:00
|
|
|
assert.String(actualRequest.User.ID).Named("UserId").Equals(request.User.ID.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[:])
|
2016-01-21 04:08:00 -05:00
|
|
|
assert.Byte(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader)
|
2015-09-11 11:27:36 -04:00
|
|
|
assert.Byte(actualRequest.Command).Named("Command").Equals(request.Command)
|
2015-12-02 10:49:34 -05:00
|
|
|
assert.String(actualRequest.Address).Named("Address").Equals(request.Address.String())
|
2015-09-08 19:11:02 -04:00
|
|
|
}
|
2015-09-14 15:59:44 -04:00
|
|
|
|
2015-11-02 17:48:47 -05:00
|
|
|
func TestReadSingleByte(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-11-02 17:48:47 -05:00
|
|
|
|
|
|
|
reader := NewVMessRequestReader(nil)
|
|
|
|
_, err := reader.Read(bytes.NewReader(make([]byte, 1)))
|
|
|
|
assert.Error(err).Equals(io.EOF)
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:59:44 -04:00
|
|
|
func BenchmarkVMessRequestWriting(b *testing.B) {
|
2015-12-12 15:40:16 -05:00
|
|
|
id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
userId := vmess.NewID(id)
|
2016-01-15 06:43:06 -05:00
|
|
|
userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
|
2015-10-31 09:08:13 -04:00
|
|
|
|
2016-01-15 06:43:06 -05:00
|
|
|
testUser := &vmess.User{
|
|
|
|
ID: userId,
|
2015-10-31 09:08:13 -04:00
|
|
|
}
|
2015-10-31 04:39:45 -04:00
|
|
|
userSet.AddUser(testUser)
|
2015-09-14 15:59:44 -04:00
|
|
|
|
|
|
|
request := new(VMessRequest)
|
|
|
|
request.Version = byte(0x01)
|
2015-10-31 04:39:45 -04:00
|
|
|
request.User = testUser
|
2015-09-14 15:59:44 -04:00
|
|
|
|
2015-10-07 08:50:17 -04:00
|
|
|
randBytes := make([]byte, 36)
|
|
|
|
rand.Read(randBytes)
|
|
|
|
request.RequestIV = randBytes[:16]
|
|
|
|
request.RequestKey = randBytes[16:32]
|
2016-01-21 04:08:00 -05:00
|
|
|
request.ResponseHeader = randBytes[32]
|
2015-09-14 15:59:44 -04:00
|
|
|
|
|
|
|
request.Command = byte(0x01)
|
2015-12-16 17:53:38 -05:00
|
|
|
request.Address = v2net.DomainAddress("v2ray.com")
|
|
|
|
request.Port = v2net.Port(80)
|
2015-09-14 15:59:44 -04:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-01-12 05:52:40 -05:00
|
|
|
request.ToBytes(NewRandomTimestampGenerator(Timestamp(time.Now().Unix()), 30), nil)
|
2015-09-14 15:59:44 -04:00
|
|
|
}
|
|
|
|
}
|