vmess_test.go 5.0 KB

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