dokodemo_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package dokodemo
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/app/point"
  6. "github.com/v2ray/v2ray-core/app/point/config/testing/mocks"
  7. v2netjson "github.com/v2ray/v2ray-core/common/net/json"
  8. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  9. "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json"
  10. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  11. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  12. "github.com/v2ray/v2ray-core/testing/unit"
  13. )
  14. func TestDokodemoTCP(t *testing.T) {
  15. assert := unit.Assert(t)
  16. port := v2nettesting.PickPort()
  17. data2Send := "Data to be sent to remote."
  18. tcpServer := &tcp.Server{
  19. Port: port,
  20. MsgProcessor: func(data []byte) []byte {
  21. buffer := make([]byte, 0, 2048)
  22. buffer = append(buffer, []byte("Processed: ")...)
  23. buffer = append(buffer, data...)
  24. return buffer
  25. },
  26. }
  27. _, err := tcpServer.Start()
  28. assert.Error(err).IsNil()
  29. pointPort := v2nettesting.PickPort()
  30. networkList := v2netjson.NetworkList([]string{"tcp"})
  31. config := mocks.Config{
  32. PortValue: pointPort,
  33. InboundConfigValue: &mocks.ConnectionConfig{
  34. ProtocolValue: "dokodemo-door",
  35. SettingsValue: &json.DokodemoConfig{
  36. Host: "127.0.0.1",
  37. Port: int(port),
  38. Network: &networkList,
  39. Timeout: 0,
  40. },
  41. },
  42. OutboundConfigValue: &mocks.ConnectionConfig{
  43. ProtocolValue: "freedom",
  44. SettingsValue: nil,
  45. },
  46. }
  47. point, err := point.NewPoint(&config)
  48. assert.Error(err).IsNil()
  49. err = point.Start()
  50. assert.Error(err).IsNil()
  51. tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  52. IP: []byte{127, 0, 0, 1},
  53. Port: int(pointPort),
  54. Zone: "",
  55. })
  56. assert.Error(err).IsNil()
  57. tcpClient.Write([]byte(data2Send))
  58. tcpClient.CloseWrite()
  59. response := make([]byte, 1024)
  60. nBytes, err := tcpClient.Read(response)
  61. assert.Error(err).IsNil()
  62. tcpClient.Close()
  63. assert.String("Processed: " + data2Send).Equals(string(response[:nBytes]))
  64. }