vmess_test.go 4.3 KB

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