vmess_test.go 5.1 KB

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