vmess_test.go 3.3 KB

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