dokodemo_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package dokodemo_test
  2. import (
  3. "net"
  4. "testing"
  5. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  6. _ "github.com/v2ray/v2ray-core/proxy/freedom"
  7. "github.com/v2ray/v2ray-core/shell/point"
  8. "github.com/v2ray/v2ray-core/shell/point/testing/mocks"
  9. v2testing "github.com/v2ray/v2ray-core/testing"
  10. "github.com/v2ray/v2ray-core/testing/assert"
  11. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  12. "github.com/v2ray/v2ray-core/testing/servers/udp"
  13. )
  14. func TestDokodemoTCP(t *testing.T) {
  15. v2testing.Current(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. config := mocks.Config{
  31. PortValue: pointPort,
  32. InboundConfigValue: &mocks.ConnectionConfig{
  33. ProtocolValue: "dokodemo-door",
  34. SettingsValue: []byte(`{
  35. "address": "127.0.0.1",
  36. "port": ` + port.String() + `,
  37. "network": "tcp",
  38. "timeout": 0
  39. }`),
  40. },
  41. OutboundConfigValue: &mocks.ConnectionConfig{
  42. ProtocolValue: "freedom",
  43. SettingsValue: nil,
  44. },
  45. }
  46. point, err := point.NewPoint(&config)
  47. assert.Error(err).IsNil()
  48. err = point.Start()
  49. assert.Error(err).IsNil()
  50. tcpClient, err := net.DialTCP("tcp", nil, &net.TCPAddr{
  51. IP: []byte{127, 0, 0, 1},
  52. Port: int(pointPort),
  53. Zone: "",
  54. })
  55. assert.Error(err).IsNil()
  56. tcpClient.Write([]byte(data2Send))
  57. tcpClient.CloseWrite()
  58. response := make([]byte, 1024)
  59. nBytes, err := tcpClient.Read(response)
  60. assert.Error(err).IsNil()
  61. tcpClient.Close()
  62. assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
  63. }
  64. func TestDokodemoUDP(t *testing.T) {
  65. v2testing.Current(t)
  66. port := v2nettesting.PickPort()
  67. data2Send := "Data to be sent to remote."
  68. udpServer := &udp.Server{
  69. Port: port,
  70. MsgProcessor: func(data []byte) []byte {
  71. buffer := make([]byte, 0, 2048)
  72. buffer = append(buffer, []byte("Processed: ")...)
  73. buffer = append(buffer, data...)
  74. return buffer
  75. },
  76. }
  77. _, err := udpServer.Start()
  78. assert.Error(err).IsNil()
  79. pointPort := v2nettesting.PickPort()
  80. config := mocks.Config{
  81. PortValue: pointPort,
  82. InboundConfigValue: &mocks.ConnectionConfig{
  83. ProtocolValue: "dokodemo-door",
  84. SettingsValue: []byte(`{
  85. "address": "127.0.0.1",
  86. "port": ` + port.String() + `,
  87. "network": "udp",
  88. "timeout": 0
  89. }`),
  90. },
  91. OutboundConfigValue: &mocks.ConnectionConfig{
  92. ProtocolValue: "freedom",
  93. SettingsValue: nil,
  94. },
  95. }
  96. point, err := point.NewPoint(&config)
  97. assert.Error(err).IsNil()
  98. err = point.Start()
  99. assert.Error(err).IsNil()
  100. udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{
  101. IP: []byte{127, 0, 0, 1},
  102. Port: int(pointPort),
  103. Zone: "",
  104. })
  105. assert.Error(err).IsNil()
  106. udpClient.Write([]byte(data2Send))
  107. response := make([]byte, 1024)
  108. nBytes, err := udpClient.Read(response)
  109. assert.Error(err).IsNil()
  110. udpClient.Close()
  111. assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
  112. }