vmess_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package vmess_test
  2. import (
  3. "bytes"
  4. "testing"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  7. "github.com/v2ray/v2ray-core/common/uuid"
  8. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  9. proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
  10. vmess "github.com/v2ray/v2ray-core/proxy/vmess"
  11. _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound"
  12. inboundjson "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json"
  13. vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json"
  14. _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound"
  15. outboundjson "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json"
  16. "github.com/v2ray/v2ray-core/shell/point"
  17. "github.com/v2ray/v2ray-core/shell/point/testing/mocks"
  18. v2testing "github.com/v2ray/v2ray-core/testing"
  19. "github.com/v2ray/v2ray-core/testing/assert"
  20. )
  21. func TestVMessInAndOut(t *testing.T) {
  22. v2testing.Current(t)
  23. id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
  24. assert.Error(err).IsNil()
  25. testAccount := vmess.NewID(id)
  26. portA := v2nettesting.PickPort()
  27. portB := v2nettesting.PickPort()
  28. ichConnInput := []byte("The data to be send to outbound server.")
  29. ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  30. ich := &proxymocks.InboundConnectionHandler{
  31. ConnInput: bytes.NewReader(ichConnInput),
  32. ConnOutput: ichConnOutput,
  33. }
  34. connhandler.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  35. configA := mocks.Config{
  36. PortValue: portA,
  37. InboundConfigValue: &mocks.ConnectionConfig{
  38. ProtocolValue: "mock_ich",
  39. SettingsValue: nil,
  40. },
  41. OutboundConfigValue: &mocks.ConnectionConfig{
  42. ProtocolValue: "vmess",
  43. SettingsValue: &outboundjson.Outbound{
  44. []*outboundjson.ConfigTarget{
  45. &outboundjson.ConfigTarget{
  46. Destination: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), portB),
  47. Users: []*vmessjson.ConfigUser{
  48. &vmessjson.ConfigUser{Id: testAccount},
  49. },
  50. },
  51. },
  52. },
  53. },
  54. }
  55. pointA, err := point.NewPoint(&configA)
  56. assert.Error(err).IsNil()
  57. err = pointA.Start()
  58. assert.Error(err).IsNil()
  59. ochConnInput := []byte("The data to be returned to inbound server.")
  60. ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
  61. och := &proxymocks.OutboundConnectionHandler{
  62. ConnInput: bytes.NewReader(ochConnInput),
  63. ConnOutput: ochConnOutput,
  64. }
  65. connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  66. configB := mocks.Config{
  67. PortValue: portB,
  68. InboundConfigValue: &mocks.ConnectionConfig{
  69. ProtocolValue: "vmess",
  70. SettingsValue: &inboundjson.Inbound{
  71. AllowedClients: []*vmessjson.ConfigUser{
  72. &vmessjson.ConfigUser{Id: testAccount},
  73. },
  74. },
  75. },
  76. OutboundConfigValue: &mocks.ConnectionConfig{
  77. ProtocolValue: "mock_och",
  78. SettingsValue: nil,
  79. },
  80. }
  81. pointB, err := point.NewPoint(&configB)
  82. assert.Error(err).IsNil()
  83. err = pointB.Start()
  84. assert.Error(err).IsNil()
  85. dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
  86. ich.Communicate(v2net.NewPacket(dest, nil, true))
  87. assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes())
  88. assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput)
  89. }