2015-09-08 19:11:02 -04:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-11 11:27:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/net"
|
|
|
|
"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-08 19:11:02 -04:00
|
|
|
userId, err := core.UUIDToVID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userSet := core.NewVUserSet()
|
|
|
|
userSet.AddUser(core.VUser{userId})
|
|
|
|
|
|
|
|
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-09-11 11:27:36 -04:00
|
|
|
_, err = rand.Read(request.RequestIV[:])
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
_, err = rand.Read(request.RequestKey[:])
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
_, err = rand.Read(request.ResponseHeader[:])
|
2015-09-08 19:11:02 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, 300))
|
2015-09-11 11:27:36 -04:00
|
|
|
requestWriter := NewVMessRequestWriter()
|
2015-09-08 19:11:02 -04:00
|
|
|
err = requestWriter.Write(buffer, request)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestReader := NewVMessRequestReader(userSet)
|
|
|
|
actualRequest, err := requestReader.Read(buffer)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-09-11 11:27:36 -04:00
|
|
|
assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01))
|
|
|
|
assert.Bytes(actualRequest.UserId[:]).Named("UserId").Equals(request.UserId[:])
|
|
|
|
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
|
|
|
}
|