freedom_test.go 3.1 KB

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