vmess_test.go 2.7 KB

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