vmess_test.go 3.0 KB

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