vmess_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. ContentValue: nil,
  24. },
  25. OutboundConfigValue: &mocks.ConnectionConfig{
  26. ProtocolValue: "vmess",
  27. ContentValue: []byte("{\"vnext\":[{\"address\": \"127.0.0.1\", \"network\": \"tcp\", \"port\": 13829, \"users\":[{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}]}]}"),
  28. },
  29. }
  30. pointA, err := core.NewPoint(&configA)
  31. assert.Error(err).IsNil()
  32. err = pointA.Start()
  33. assert.Error(err).IsNil()
  34. portB := uint16(13829)
  35. och := &mocks.OutboundConnectionHandler{
  36. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  37. Data2Return: []byte("The data to be returned to inbound server."),
  38. }
  39. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  40. configB := mocks.Config{
  41. PortValue: portB,
  42. InboundConfigValue: &mocks.ConnectionConfig{
  43. ProtocolValue: "vmess",
  44. ContentValue: []byte("{\"clients\": [{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}]}"),
  45. },
  46. OutboundConfigValue: &mocks.ConnectionConfig{
  47. ProtocolValue: "mock_och",
  48. ContentValue: nil,
  49. },
  50. }
  51. pointB, err := core.NewPoint(&configB)
  52. assert.Error(err).IsNil()
  53. err = pointB.Start()
  54. assert.Error(err).IsNil()
  55. dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  56. ich.Communicate(v2net.NewPacket(dest, nil, true))
  57. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  58. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  59. }
  60. func TestVMessInAndOutUDP(t *testing.T) {
  61. assert := unit.Assert(t)
  62. data2Send := "The data to be send to outbound server."
  63. portA := uint16(17394)
  64. ich := &mocks.InboundConnectionHandler{
  65. Data2Send: []byte(data2Send),
  66. DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
  67. }
  68. core.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  69. configA := mocks.Config{
  70. PortValue: portA,
  71. InboundConfigValue: &mocks.ConnectionConfig{
  72. ProtocolValue: "mock_ich",
  73. ContentValue: nil,
  74. },
  75. OutboundConfigValue: &mocks.ConnectionConfig{
  76. ProtocolValue: "vmess",
  77. ContentValue: []byte("{\"vnext\":[{\"address\": \"127.0.0.1\", \"network\": \"udp\", \"port\": 13841, \"users\":[{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}]}]}"),
  78. },
  79. }
  80. pointA, err := core.NewPoint(&configA)
  81. assert.Error(err).IsNil()
  82. err = pointA.Start()
  83. assert.Error(err).IsNil()
  84. portB := uint16(13841)
  85. och := &mocks.OutboundConnectionHandler{
  86. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  87. Data2Return: []byte("The data to be returned to inbound server."),
  88. }
  89. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  90. configB := mocks.Config{
  91. PortValue: portB,
  92. InboundConfigValue: &mocks.ConnectionConfig{
  93. ProtocolValue: "vmess",
  94. ContentValue: []byte("{\"clients\": [{\"id\": \"ad937d9d-6e23-4a5a-ba23-bce5092a7c51\"}], \"udp\": true}"),
  95. },
  96. OutboundConfigValue: &mocks.ConnectionConfig{
  97. ProtocolValue: "mock_och",
  98. ContentValue: nil,
  99. },
  100. }
  101. pointB, err := core.NewPoint(&configB)
  102. assert.Error(err).IsNil()
  103. err = pointB.Start()
  104. assert.Error(err).IsNil()
  105. dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  106. ich.Communicate(v2net.NewPacket(dest, []byte(data2Send), false))
  107. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  108. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  109. }