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. v2net "github.com/v2ray/v2ray-core/common/net"
  7. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  8. "github.com/v2ray/v2ray-core/common/uuid"
  9. "github.com/v2ray/v2ray-core/proxy"
  10. proxytesting "github.com/v2ray/v2ray-core/proxy/testing"
  11. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  12. vmess "github.com/v2ray/v2ray-core/proxy/vmess"
  13. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  14. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  15. "github.com/v2ray/v2ray-core/shell/point"
  16. "github.com/v2ray/v2ray-core/shell/point/testing/mocks"
  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.InboundConnectionHandler, error) {
  34. ich.Space = space
  35. return ich, nil
  36. })
  37. assert.Error(err).IsNil()
  38. configA := mocks.Config{
  39. PortValue: portA,
  40. InboundConfigValue: &mocks.ConnectionConfig{
  41. ProtocolValue: protocol,
  42. SettingsValue: nil,
  43. },
  44. OutboundConfigValue: &mocks.ConnectionConfig{
  45. ProtocolValue: "vmess",
  46. SettingsValue: []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.OutboundConnectionHandler, error) {
  70. return och, nil
  71. })
  72. assert.Error(err).IsNil()
  73. configB := mocks.Config{
  74. PortValue: portB,
  75. InboundConfigValue: &mocks.ConnectionConfig{
  76. ProtocolValue: "vmess",
  77. SettingsValue: []byte(`{
  78. "clients": [
  79. {"id": "` + testAccount.String() + `"}
  80. ]
  81. }`),
  82. },
  83. OutboundConfigValue: &mocks.ConnectionConfig{
  84. ProtocolValue: protocol,
  85. SettingsValue: 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. }