vmess_test.go 3.5 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/common/connhandler"
  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. inboundjson "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
  15. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
  16. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  17. outboundjson "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
  18. "github.com/v2ray/v2ray-core/shell/point"
  19. "github.com/v2ray/v2ray-core/shell/point/testing/mocks"
  20. v2testing "github.com/v2ray/v2ray-core/testing"
  21. "github.com/v2ray/v2ray-core/testing/assert"
  22. )
  23. func TestVMessInAndOut(t *testing.T) {
  24. v2testing.Current(t)
  25. id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  26. assert.Error(err).IsNil()
  27. testAccount := vmess.NewID(id)
  28. portA := v2nettesting.PickPort()
  29. portB := v2nettesting.PickPort()
  30. ichConnInput := []byte("The data to be send to outbound server.")
  31. ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  32. ich := &proxymocks.InboundConnectionHandler{
  33. ConnInput: bytes.NewReader(ichConnInput),
  34. ConnOutput: ichConnOutput,
  35. }
  36. protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
  37. ich.Space = space
  38. return ich, nil
  39. })
  40. assert.Error(err).IsNil()
  41. configA := mocks.Config{
  42. PortValue: portA,
  43. InboundConfigValue: &mocks.ConnectionConfig{
  44. ProtocolValue: protocol,
  45. SettingsValue: nil,
  46. },
  47. OutboundConfigValue: &mocks.ConnectionConfig{
  48. ProtocolValue: "vmess",
  49. SettingsValue: &outboundjson.Outbound{
  50. []*outboundjson.ConfigTarget{
  51. &outboundjson.ConfigTarget{
  52. Destination: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), portB),
  53. Users: []*vmessjson.ConfigUser{
  54. &vmessjson.ConfigUser{Id: testAccount},
  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{}) (connhandler.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: &inboundjson.Inbound{
  80. AllowedClients: []*vmessjson.ConfigUser{
  81. &vmessjson.ConfigUser{Id: testAccount},
  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. }