vmess_test.go 2.7 KB

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