vmess_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\", \"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.NewDestination(v2net.NetTCP, v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
  56. ich.Communicate(dest)
  57. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  58. assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return)
  59. }