vmess_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package vmess
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/v2ray/v2ray-core"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/testing/mocks"
  8. "github.com/v2ray/v2ray-core/testing/unit"
  9. )
  10. func TestVMessInAndOut(t *testing.T) {
  11. assert := unit.Assert(t)
  12. data2Send := "The data to be send to outbound server."
  13. portA := uint16(17392)
  14. ich := &mocks.InboundConnectionHandler{
  15. Data2Send: []byte(data2Send),
  16. DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
  17. }
  18. core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  19. configA := mocks.Config{
  20. PortValue: portA,
  21. InboundConfigValue: &mocks.ConnectionConfig{
  22. ProtocolValue: "mock_ich",
  23. SettingsValue: nil,
  24. },
  25. OutboundConfigValue: &mocks.ConnectionConfig{
  26. ProtocolValue: "vmess",
  27. SettingsValue: &VMessOutboundConfig{
  28. []VNextConfig{
  29. VNextConfig{
  30. Address: "127.0.0.1",
  31. Port: 13829,
  32. Network: "tcp",
  33. Users: []VMessUser{
  34. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  35. },
  36. },
  37. },
  38. },
  39. },
  40. }
  41. pointA, err := core.NewPoint(&configA)
  42. assert.Error(err).IsNil()
  43. err = pointA.Start()
  44. assert.Error(err).IsNil()
  45. portB := uint16(13829)
  46. och := &mocks.OutboundConnectionHandler{
  47. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  48. Data2Return: []byte("The data to be returned to inbound server."),
  49. }
  50. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  51. configB := mocks.Config{
  52. PortValue: portB,
  53. InboundConfigValue: &mocks.ConnectionConfig{
  54. ProtocolValue: "vmess",
  55. SettingsValue: &VMessInboundConfig{
  56. AllowedClients: []VMessUser{
  57. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  58. },
  59. },
  60. },
  61. OutboundConfigValue: &mocks.ConnectionConfig{
  62. ProtocolValue: "mock_och",
  63. SettingsValue: nil,
  64. },
  65. }
  66. pointB, err := core.NewPoint(&configB)
  67. assert.Error(err).IsNil()
  68. err = pointB.Start()
  69. assert.Error(err).IsNil()
  70. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  71. ich.Communicate(v2net.NewPacket(dest, nil, true))
  72. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  73. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  74. }
  75. func TestVMessInAndOutUDP(t *testing.T) {
  76. assert := unit.Assert(t)
  77. data2Send := "The data to be send to outbound server."
  78. portA := uint16(17394)
  79. ich := &mocks.InboundConnectionHandler{
  80. Data2Send: []byte(data2Send),
  81. DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
  82. }
  83. core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  84. configA := mocks.Config{
  85. PortValue: portA,
  86. InboundConfigValue: &mocks.ConnectionConfig{
  87. ProtocolValue: "mock_ich",
  88. SettingsValue: nil,
  89. },
  90. OutboundConfigValue: &mocks.ConnectionConfig{
  91. ProtocolValue: "vmess",
  92. SettingsValue: &VMessOutboundConfig{
  93. []VNextConfig{
  94. VNextConfig{
  95. Address: "127.0.0.1",
  96. Port: 13841,
  97. Network: "udp",
  98. Users: []VMessUser{
  99. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  100. },
  101. },
  102. },
  103. },
  104. },
  105. }
  106. pointA, err := core.NewPoint(&configA)
  107. assert.Error(err).IsNil()
  108. err = pointA.Start()
  109. assert.Error(err).IsNil()
  110. portB := uint16(13841)
  111. och := &mocks.OutboundConnectionHandler{
  112. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  113. Data2Return: []byte("The data to be returned to inbound server."),
  114. }
  115. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  116. configB := mocks.Config{
  117. PortValue: portB,
  118. InboundConfigValue: &mocks.ConnectionConfig{
  119. ProtocolValue: "vmess",
  120. SettingsValue: &VMessInboundConfig{
  121. AllowedClients: []VMessUser{
  122. VMessUser{Id: "ad937d9d-6e23-4a5a-ba23-bce5092a7c51"},
  123. },
  124. UDPEnabled: true,
  125. },
  126. },
  127. OutboundConfigValue: &mocks.ConnectionConfig{
  128. ProtocolValue: "mock_och",
  129. SettingsValue: nil,
  130. },
  131. }
  132. pointB, err := core.NewPoint(&configB)
  133. assert.Error(err).IsNil()
  134. err = pointB.Start()
  135. assert.Error(err).IsNil()
  136. dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  137. ich.Communicate(v2net.NewPacket(dest, []byte(data2Send), false))
  138. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  139. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  140. }