freedom_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package freedom
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net"
  6. "testing"
  7. "golang.org/x/net/proxy"
  8. "github.com/v2ray/v2ray-core/app/point"
  9. "github.com/v2ray/v2ray-core/common/alloc"
  10. v2net "github.com/v2ray/v2ray-core/common/net"
  11. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  12. _ "github.com/v2ray/v2ray-core/proxy/socks"
  13. "github.com/v2ray/v2ray-core/proxy/socks/config/json"
  14. "github.com/v2ray/v2ray-core/testing/mocks"
  15. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  16. "github.com/v2ray/v2ray-core/testing/servers/udp"
  17. "github.com/v2ray/v2ray-core/testing/unit"
  18. )
  19. func TestUDPSend(t *testing.T) {
  20. assert := unit.Assert(t)
  21. data2Send := "Data to be sent to remote"
  22. udpServer := &udp.Server{
  23. Port: 0,
  24. MsgProcessor: func(data []byte) []byte {
  25. buffer := make([]byte, 0, 2048)
  26. buffer = append(buffer, []byte("Processed: ")...)
  27. buffer = append(buffer, data...)
  28. return buffer
  29. },
  30. }
  31. udpServerAddr, err := udpServer.Start()
  32. assert.Error(err).IsNil()
  33. ich := &mocks.InboundConnectionHandler{
  34. Data2Send: []byte("Not Used"),
  35. DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)),
  36. }
  37. connhandler.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
  38. pointPort := uint16(38724)
  39. config := mocks.Config{
  40. PortValue: pointPort,
  41. InboundConfigValue: &mocks.ConnectionConfig{
  42. ProtocolValue: "mock_ich",
  43. SettingsValue: nil,
  44. },
  45. OutboundConfigValue: &mocks.ConnectionConfig{
  46. ProtocolValue: "freedom",
  47. SettingsValue: nil,
  48. },
  49. }
  50. point, err := point.NewPoint(&config)
  51. assert.Error(err).IsNil()
  52. err = point.Start()
  53. assert.Error(err).IsNil()
  54. data2SendBuffer := alloc.NewBuffer()
  55. data2SendBuffer.Clear()
  56. data2SendBuffer.Append([]byte(data2Send))
  57. dest := v2net.NewUDPDestination(udpServerAddr)
  58. ich.Communicate(v2net.NewPacket(dest, data2SendBuffer, false))
  59. assert.Bytes(ich.DataReturned.Bytes()).Equals([]byte("Processed: Data to be sent to remote"))
  60. }
  61. func TestSocksTcpConnect(t *testing.T) {
  62. assert := unit.Assert(t)
  63. port := uint16(38293)
  64. data2Send := "Data to be sent to remote"
  65. tcpServer := &tcp.Server{
  66. Port: port,
  67. MsgProcessor: func(data []byte) []byte {
  68. buffer := make([]byte, 0, 2048)
  69. buffer = append(buffer, []byte("Processed: ")...)
  70. buffer = append(buffer, data...)
  71. return buffer
  72. },
  73. }
  74. _, err := tcpServer.Start()
  75. assert.Error(err).IsNil()
  76. pointPort := uint16(38724)
  77. config := mocks.Config{
  78. PortValue: pointPort,
  79. InboundConfigValue: &mocks.ConnectionConfig{
  80. ProtocolValue: "socks",
  81. SettingsValue: &json.SocksConfig{
  82. AuthMethod: "auth",
  83. },
  84. },
  85. OutboundConfigValue: &mocks.ConnectionConfig{
  86. ProtocolValue: "freedom",
  87. SettingsValue: nil,
  88. },
  89. }
  90. point, err := point.NewPoint(&config)
  91. assert.Error(err).IsNil()
  92. err = point.Start()
  93. assert.Error(err).IsNil()
  94. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38724", nil, proxy.Direct)
  95. assert.Error(err).IsNil()
  96. targetServer := "127.0.0.1:38293"
  97. conn, err := socks5Client.Dial("tcp", targetServer)
  98. assert.Error(err).IsNil()
  99. conn.Write([]byte(data2Send))
  100. if tcpConn, ok := conn.(*net.TCPConn); ok {
  101. tcpConn.CloseWrite()
  102. }
  103. dataReturned, err := ioutil.ReadAll(conn)
  104. assert.Error(err).IsNil()
  105. conn.Close()
  106. assert.Bytes(dataReturned).Equals([]byte("Processed: Data to be sent to remote"))
  107. }