vmess_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package vmess
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/v2ray/v2ray-core"
  6. "github.com/v2ray/v2ray-core/common/alloc"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. "github.com/v2ray/v2ray-core/testing/mocks"
  9. "github.com/v2ray/v2ray-core/testing/unit"
  10. )
  11. func TestVMessInAndOut(t *testing.T) {
  12. assert := unit.Assert(t)
  13. data2Send := "The data to be send to outbound server."
  14. portA := uint16(17392)
  15. ich := &mocks.InboundConnectionHandler{
  16. Data2Send: []byte(data2Send),
  17. DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
  18. }
  19. core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  20. configA := mocks.Config{
  21. PortValue: portA,
  22. InboundConfigValue: &mocks.ConnectionConfig{
  23. ProtocolValue: "mock_ich",
  24. SettingsValue: nil,
  25. },
  26. OutboundConfigValue: &mocks.ConnectionConfig{
  27. ProtocolValue: "vmess",
  28. SettingsValue: &VMessOutboundConfig{
  29. []VNextConfig{
  30. VNextConfig{
  31. Address: "127.0.0.1",
  32. Port: 13829,
  33. Network: "tcp",
  34. Users: []VMessUser{
  35. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  36. },
  37. },
  38. },
  39. },
  40. },
  41. }
  42. pointA, err := core.NewPoint(&configA)
  43. assert.Error(err).IsNil()
  44. err = pointA.Start()
  45. assert.Error(err).IsNil()
  46. portB := uint16(13829)
  47. och := &mocks.OutboundConnectionHandler{
  48. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  49. Data2Return: []byte("The data to be returned to inbound server."),
  50. }
  51. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  52. configB := mocks.Config{
  53. PortValue: portB,
  54. InboundConfigValue: &mocks.ConnectionConfig{
  55. ProtocolValue: "vmess",
  56. SettingsValue: &VMessInboundConfig{
  57. AllowedClients: []VMessUser{
  58. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  59. },
  60. },
  61. },
  62. OutboundConfigValue: &mocks.ConnectionConfig{
  63. ProtocolValue: "mock_och",
  64. SettingsValue: nil,
  65. },
  66. }
  67. pointB, err := core.NewPoint(&configB)
  68. assert.Error(err).IsNil()
  69. err = pointB.Start()
  70. assert.Error(err).IsNil()
  71. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  72. ich.Communicate(v2net.NewPacket(dest, nil, true))
  73. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  74. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  75. }
  76. func TestVMessInAndOutUDP(t *testing.T) {
  77. assert := unit.Assert(t)
  78. data2Send := "The data to be send to outbound server."
  79. portA := uint16(17394)
  80. ich := &mocks.InboundConnectionHandler{
  81. Data2Send: []byte(data2Send),
  82. DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
  83. }
  84. core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  85. configA := mocks.Config{
  86. PortValue: portA,
  87. InboundConfigValue: &mocks.ConnectionConfig{
  88. ProtocolValue: "mock_ich",
  89. SettingsValue: nil,
  90. },
  91. OutboundConfigValue: &mocks.ConnectionConfig{
  92. ProtocolValue: "vmess",
  93. SettingsValue: &VMessOutboundConfig{
  94. []VNextConfig{
  95. VNextConfig{
  96. Address: "127.0.0.1",
  97. Port: 13841,
  98. Network: "udp",
  99. Users: []VMessUser{
  100. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  101. },
  102. },
  103. },
  104. },
  105. },
  106. }
  107. pointA, err := core.NewPoint(&configA)
  108. assert.Error(err).IsNil()
  109. err = pointA.Start()
  110. assert.Error(err).IsNil()
  111. portB := uint16(13841)
  112. och := &mocks.OutboundConnectionHandler{
  113. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  114. Data2Return: []byte("The data to be returned to inbound server."),
  115. }
  116. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  117. configB := mocks.Config{
  118. PortValue: portB,
  119. InboundConfigValue: &mocks.ConnectionConfig{
  120. ProtocolValue: "vmess",
  121. SettingsValue: &VMessInboundConfig{
  122. AllowedClients: []VMessUser{
  123. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  124. },
  125. UDPEnabled: true,
  126. },
  127. },
  128. OutboundConfigValue: &mocks.ConnectionConfig{
  129. ProtocolValue: "mock_och",
  130. SettingsValue: nil,
  131. },
  132. }
  133. pointB, err := core.NewPoint(&configB)
  134. assert.Error(err).IsNil()
  135. err = pointB.Start()
  136. assert.Error(err).IsNil()
  137. data2SendBuffer := alloc.NewBuffer()
  138. data2SendBuffer.Clear()
  139. data2SendBuffer.Append([]byte(data2Send))
  140. dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  141. ich.Communicate(v2net.NewPacket(dest, data2SendBuffer, false))
  142. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  143. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  144. }