vmess_test.go 3.1 KB

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