vmess_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package protocol_test
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "io"
  6. "testing"
  7. "time"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. "github.com/v2ray/v2ray-core/common/uuid"
  10. "github.com/v2ray/v2ray-core/proxy/vmess"
  11. . "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
  12. protocoltesting "github.com/v2ray/v2ray-core/proxy/vmess/protocol/testing"
  13. v2testing "github.com/v2ray/v2ray-core/testing"
  14. "github.com/v2ray/v2ray-core/testing/assert"
  15. )
  16. type FakeTimestampGenerator struct {
  17. timestamp Timestamp
  18. }
  19. func (this *FakeTimestampGenerator) Next() Timestamp {
  20. return this.timestamp
  21. }
  22. type TestUser struct {
  23. id *vmess.ID
  24. level vmess.UserLevel
  25. }
  26. func (u *TestUser) ID() *vmess.ID {
  27. return u.id
  28. }
  29. func (this *TestUser) Level() vmess.UserLevel {
  30. return this.level
  31. }
  32. func (this *TestUser) AlterIDs() []*vmess.ID {
  33. return nil
  34. }
  35. func (this *TestUser) AnyValidID() *vmess.ID {
  36. return this.id
  37. }
  38. func TestVMessSerialization(t *testing.T) {
  39. v2testing.Current(t)
  40. id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
  41. assert.Error(err).IsNil()
  42. userId := vmess.NewID(id)
  43. testUser := &TestUser{
  44. id: userId,
  45. }
  46. userSet := protocoltesting.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
  47. userSet.AddUser(testUser)
  48. request := new(VMessRequest)
  49. request.Version = byte(0x01)
  50. request.User = testUser
  51. randBytes := make([]byte, 36)
  52. _, err = rand.Read(randBytes)
  53. assert.Error(err).IsNil()
  54. request.RequestIV = randBytes[:16]
  55. request.RequestKey = randBytes[16:32]
  56. request.ResponseHeader = randBytes[32:]
  57. request.Command = byte(0x01)
  58. request.Address = v2net.DomainAddress("v2ray.com")
  59. request.Port = v2net.Port(80)
  60. mockTime := Timestamp(1823730)
  61. buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. userSet.UserHashes[string(buffer.Value[:16])] = 0
  66. userSet.Timestamps[string(buffer.Value[:16])] = mockTime
  67. requestReader := NewVMessRequestReader(&userSet)
  68. actualRequest, err := requestReader.Read(bytes.NewReader(buffer.Value))
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01))
  73. assert.String(actualRequest.User.ID()).Named("UserId").Equals(request.User.ID().String())
  74. assert.Bytes(actualRequest.RequestIV).Named("RequestIV").Equals(request.RequestIV[:])
  75. assert.Bytes(actualRequest.RequestKey).Named("RequestKey").Equals(request.RequestKey[:])
  76. assert.Bytes(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader[:])
  77. assert.Byte(actualRequest.Command).Named("Command").Equals(request.Command)
  78. assert.String(actualRequest.Address).Named("Address").Equals(request.Address.String())
  79. }
  80. func TestReadSingleByte(t *testing.T) {
  81. v2testing.Current(t)
  82. reader := NewVMessRequestReader(nil)
  83. _, err := reader.Read(bytes.NewReader(make([]byte, 1)))
  84. assert.Error(err).Equals(io.EOF)
  85. }
  86. func BenchmarkVMessRequestWriting(b *testing.B) {
  87. id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
  88. assert.Error(err).IsNil()
  89. userId := vmess.NewID(id)
  90. userSet := protocoltesting.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]Timestamp)}
  91. testUser := &TestUser{
  92. id: userId,
  93. }
  94. userSet.AddUser(testUser)
  95. request := new(VMessRequest)
  96. request.Version = byte(0x01)
  97. request.User = testUser
  98. randBytes := make([]byte, 36)
  99. rand.Read(randBytes)
  100. request.RequestIV = randBytes[:16]
  101. request.RequestKey = randBytes[16:32]
  102. request.ResponseHeader = randBytes[32:]
  103. request.Command = byte(0x01)
  104. request.Address = v2net.DomainAddress("v2ray.com")
  105. request.Port = v2net.Port(80)
  106. for i := 0; i < b.N; i++ {
  107. request.ToBytes(NewRandomTimestampGenerator(Timestamp(time.Now().Unix()), 30), nil)
  108. }
  109. }