1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/io/vmess/vmess_test.go

94 lines
2.5 KiB
Go
Raw Normal View History

2015-09-08 19:11:02 -04:00
package vmess
import (
"bytes"
"crypto/rand"
2015-09-14 15:59:44 -04:00
"io/ioutil"
2015-09-08 19:11:02 -04:00
"testing"
"github.com/v2ray/v2ray-core"
v2net "github.com/v2ray/v2ray-core/net"
2015-09-14 13:03:31 -04:00
"github.com/v2ray/v2ray-core/testing/mocks"
"github.com/v2ray/v2ray-core/testing/unit"
2015-09-08 19:11:02 -04:00
)
func TestVMessSerialization(t *testing.T) {
2015-09-14 19:32:55 -04:00
t.Skip();
assert := unit.Assert(t)
2015-09-14 13:03:31 -04:00
userId, err := core.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
2015-09-08 19:11:02 -04:00
if err != nil {
t.Fatal(err)
}
2015-09-14 13:03:31 -04:00
userSet := mocks.MockUserSet{[]core.ID{}, make(map[string]int)}
2015-09-12 16:11:54 -04:00
userSet.AddUser(core.User{userId})
2015-09-08 19:11:02 -04:00
request := new(VMessRequest)
request.Version = byte(0x01)
request.UserId = userId
2015-09-08 19:11:02 -04:00
_, err = rand.Read(request.RequestIV[:])
2015-09-08 19:11:02 -04:00
if err != nil {
t.Fatal(err)
}
_, err = rand.Read(request.RequestKey[:])
2015-09-08 19:11:02 -04:00
if err != nil {
t.Fatal(err)
}
_, err = rand.Read(request.ResponseHeader[:])
2015-09-08 19:11:02 -04:00
if err != nil {
t.Fatal(err)
}
request.Command = byte(0x01)
request.Address = v2net.DomainAddress("v2ray.com", 80)
2015-09-08 19:11:02 -04:00
buffer := bytes.NewBuffer(make([]byte, 0, 300))
requestWriter := NewVMessRequestWriter()
2015-09-08 19:11:02 -04:00
err = requestWriter.Write(buffer, request)
if err != nil {
t.Fatal(err)
}
2015-09-14 15:59:44 -04:00
userSet.UserHashes[string(buffer.Bytes()[:16])] = 0
2015-09-14 13:03:31 -04:00
requestReader := NewVMessRequestReader(&userSet)
2015-09-08 19:11:02 -04:00
actualRequest, err := requestReader.Read(buffer)
if err != nil {
t.Fatal(err)
}
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)
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[:])
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) {
userId, _ := core.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userSet := mocks.MockUserSet{[]core.ID{}, make(map[string]int)}
userSet.AddUser(core.User{userId})
request := new(VMessRequest)
request.Version = byte(0x01)
request.UserId = userId
rand.Read(request.RequestIV[:])
rand.Read(request.RequestKey[:])
rand.Read(request.ResponseHeader[:])
request.Command = byte(0x01)
request.Address = v2net.DomainAddress("v2ray.com", 80)
requestWriter := NewVMessRequestWriter()
for i := 0; i < b.N; i++ {
requestWriter.Write(ioutil.Discard, request)
}
}