vmess_test.go 4.6 KB

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