vmess_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package vmess_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/app/dispatcher"
  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/common/uuid"
  10. "github.com/v2ray/v2ray-core/proxy"
  11. proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
  12. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  13. vmess "github.com/v2ray/v2ray-core/proxy/vmess"
  14. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  15. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  16. "github.com/v2ray/v2ray-core/shell/point"
  17. v2testing "github.com/v2ray/v2ray-core/testing"
  18. "github.com/v2ray/v2ray-core/testing/assert"
  19. )
  20. func TestVMessInAndOut(t *testing.T) {
  21. v2testing.Current(t)
  22. id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  23. assert.Error(err).IsNil()
  24. testAccount := vmess.NewID(id)
  25. portA := v2nettesting.PickPort()
  26. portB := v2nettesting.PickPort()
  27. ichConnInput := []byte("The data to be send to outbound server.")
  28. ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  29. ich := &proxymocks.InboundConnectionHandler{
  30. ConnInput: bytes.NewReader(ichConnInput),
  31. ConnOutput: ichConnOutput,
  32. }
  33. protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.InboundHandler, error) {
  34. ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
  35. return ich, nil
  36. })
  37. assert.Error(err).IsNil()
  38. configA := &point.Config{
  39. Port: portA,
  40. InboundConfig: &point.ConnectionConfig{
  41. Protocol: protocol,
  42. Settings: nil,
  43. },
  44. OutboundConfig: &point.ConnectionConfig{
  45. Protocol: "vmess",
  46. Settings: []byte(`{
  47. "vnext": [
  48. {
  49. "address": "127.0.0.1",
  50. "port": ` + portB.String() + `,
  51. "users": [
  52. {"id": "` + testAccount.String() + `"}
  53. ]
  54. }
  55. ]
  56. }`),
  57. },
  58. }
  59. pointA, err := point.NewPoint(configA)
  60. assert.Error(err).IsNil()
  61. err = pointA.Start()
  62. assert.Error(err).IsNil()
  63. ochConnInput := []byte("The data to be returned to inbound server.")
  64. ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  65. och := &proxymocks.OutboundConnectionHandler{
  66. ConnInput: bytes.NewReader(ochConnInput),
  67. ConnOutput: ochConnOutput,
  68. }
  69. protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  70. return och, nil
  71. })
  72. assert.Error(err).IsNil()
  73. configB := &point.Config{
  74. Port: portB,
  75. InboundConfig: &point.ConnectionConfig{
  76. Protocol: "vmess",
  77. Settings: []byte(`{
  78. "clients": [
  79. {"id": "` + testAccount.String() + `"}
  80. ]
  81. }`),
  82. },
  83. OutboundConfig: &point.ConnectionConfig{
  84. Protocol: protocol,
  85. Settings: nil,
  86. },
  87. }
  88. pointB, err := point.NewPoint(configB)
  89. assert.Error(err).IsNil()
  90. err = pointB.Start()
  91. assert.Error(err).IsNil()
  92. dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
  93. ich.Communicate(v2net.NewPacket(dest, nil, true))
  94. assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes())
  95. assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput)
  96. }