socks_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package socks
  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/testing/mocks"
  10. "github.com/v2ray/v2ray-core/testing/unit"
  11. )
  12. func TestSocksTcpConnect(t *testing.T) {
  13. assert := unit.Assert(t)
  14. port := uint16(12385)
  15. och := &mocks.OutboundConnectionHandler{
  16. Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)),
  17. Data2Return: []byte("The data to be returned to socks server."),
  18. }
  19. core.RegisterOutboundConnectionHandlerFactory("mock_och", och)
  20. config := mocks.Config{
  21. PortValue: port,
  22. InboundConfigValue: &mocks.ConnectionConfig{
  23. ProtocolValue: "socks",
  24. ContentValue: []byte("{\"auth\": \"noauth\"}"),
  25. },
  26. OutboundConfigValue: &mocks.ConnectionConfig{
  27. ProtocolValue: "mock_och",
  28. ContentValue: nil,
  29. },
  30. }
  31. point, err := core.NewPoint(&config)
  32. assert.Error(err).IsNil()
  33. err = point.Start()
  34. assert.Error(err).IsNil()
  35. socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12385", nil, proxy.Direct)
  36. assert.Error(err).IsNil()
  37. conn, err := socks5Client.Dial("tcp", "google.com:80")
  38. assert.Error(err).IsNil()
  39. data2Send := "The data to be sent to remote server."
  40. conn.Write([]byte(data2Send))
  41. if tcpConn, ok := conn.(*net.TCPConn); ok {
  42. tcpConn.CloseWrite()
  43. }
  44. dataReturned, err := ioutil.ReadAll(conn)
  45. assert.Error(err).IsNil()
  46. conn.Close()
  47. assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes())
  48. assert.Bytes(dataReturned).Equals(och.Data2Return)
  49. }