freedom_test.go 2.9 KB

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