socks_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package protocol
  2. import (
  3. "bytes"
  4. "io"
  5. "testing"
  6. "github.com/v2ray/v2ray-core/common/alloc"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
  9. "github.com/v2ray/v2ray-core/proxy"
  10. v2testing "github.com/v2ray/v2ray-core/testing"
  11. "github.com/v2ray/v2ray-core/testing/assert"
  12. "github.com/v2ray/v2ray-core/transport"
  13. )
  14. func TestHasAuthenticationMethod(t *testing.T) {
  15. v2testing.Current(t)
  16. request := Socks5AuthenticationRequest{
  17. version: socksVersion,
  18. nMethods: byte(0x02),
  19. authMethods: [256]byte{0x01, 0x02},
  20. }
  21. assert.Bool(request.HasAuthMethod(byte(0x01))).IsTrue()
  22. request.authMethods[0] = byte(0x03)
  23. assert.Bool(request.HasAuthMethod(byte(0x01))).IsFalse()
  24. }
  25. func TestAuthenticationRequestRead(t *testing.T) {
  26. v2testing.Current(t)
  27. buffer := alloc.NewBuffer().Clear().AppendBytes(
  28. 0x05, // version
  29. 0x01, // nMethods
  30. 0x02, // methods
  31. )
  32. request, _, err := ReadAuthentication(buffer)
  33. assert.Error(err).IsNil()
  34. assert.Byte(request.version).Named("Version").Equals(0x05)
  35. assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
  36. assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
  37. }
  38. func TestAuthenticationResponseWrite(t *testing.T) {
  39. v2testing.Current(t)
  40. response := NewAuthenticationResponse(byte(0x05))
  41. buffer := bytes.NewBuffer(make([]byte, 0, 10))
  42. WriteAuthentication(buffer, response)
  43. assert.Bytes(buffer.Bytes()).Equals([]byte{socksVersion, byte(0x05)})
  44. }
  45. func TestRequestRead(t *testing.T) {
  46. v2testing.Current(t)
  47. rawRequest := []byte{
  48. 0x05, // version
  49. 0x01, // cmd connect
  50. 0x00, // reserved
  51. 0x01, // ipv4 type
  52. 0x72, 0x72, 0x72, 0x72, // 114.114.114.114
  53. 0x00, 0x35, // port 53
  54. }
  55. request, err := ReadRequest(bytes.NewReader(rawRequest))
  56. assert.Error(err).IsNil()
  57. assert.Byte(request.Version).Named("Version").Equals(0x05)
  58. assert.Byte(request.Command).Named("Command").Equals(0x01)
  59. assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
  60. assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
  61. v2netassert.Port(request.Port).Named("Port").Equals(v2net.Port(53))
  62. }
  63. func TestResponseWrite(t *testing.T) {
  64. v2testing.Current(t)
  65. response := Socks5Response{
  66. socksVersion,
  67. ErrorSuccess,
  68. AddrTypeIPv4,
  69. [4]byte{0x72, 0x72, 0x72, 0x72},
  70. "",
  71. [16]byte{},
  72. v2net.Port(53),
  73. }
  74. buffer := alloc.NewSmallBuffer().Clear()
  75. defer buffer.Release()
  76. response.Write(buffer)
  77. expectedBytes := []byte{
  78. socksVersion,
  79. ErrorSuccess,
  80. byte(0x00),
  81. AddrTypeIPv4,
  82. 0x72, 0x72, 0x72, 0x72,
  83. byte(0x00), byte(0x035),
  84. }
  85. assert.Bytes(buffer.Value).Named("raw response").Equals(expectedBytes)
  86. }
  87. func TestSetIPv6(t *testing.T) {
  88. v2testing.Current(t)
  89. response := NewSocks5Response()
  90. response.SetIPv6([]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
  91. buffer := alloc.NewSmallBuffer().Clear()
  92. defer buffer.Release()
  93. response.Write(buffer)
  94. assert.Bytes(buffer.Value).Equals([]byte{
  95. socksVersion, 0, 0, AddrTypeIPv6, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 0})
  96. }
  97. func TestSetDomain(t *testing.T) {
  98. v2testing.Current(t)
  99. response := NewSocks5Response()
  100. response.SetDomain("v2ray.com")
  101. buffer := alloc.NewSmallBuffer().Clear()
  102. defer buffer.Release()
  103. response.Write(buffer)
  104. assert.Bytes(buffer.Value).Equals([]byte{
  105. socksVersion, 0, 0, AddrTypeDomain, 9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0})
  106. }
  107. func TestEmptyAuthRequest(t *testing.T) {
  108. v2testing.Current(t)
  109. _, _, err := ReadAuthentication(alloc.NewBuffer().Clear())
  110. assert.Error(err).Equals(io.EOF)
  111. }
  112. func TestSingleByteAuthRequest(t *testing.T) {
  113. v2testing.Current(t)
  114. _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
  115. assert.Error(err).Equals(transport.ErrorCorruptedPacket)
  116. }
  117. func TestZeroAuthenticationMethod(t *testing.T) {
  118. v2testing.Current(t)
  119. buffer := alloc.NewBuffer().Clear().AppendBytes(5, 0)
  120. _, _, err := ReadAuthentication(buffer)
  121. assert.Error(err).Equals(proxy.ErrorInvalidAuthentication)
  122. }
  123. func TestWrongProtocolVersion(t *testing.T) {
  124. v2testing.Current(t)
  125. buffer := alloc.NewBuffer().Clear().AppendBytes(6, 1, 0)
  126. _, _, err := ReadAuthentication(buffer)
  127. assert.Error(err).Equals(proxy.ErrorInvalidProtocolVersion)
  128. }
  129. func TestEmptyRequest(t *testing.T) {
  130. v2testing.Current(t)
  131. _, err := ReadRequest(alloc.NewBuffer().Clear())
  132. assert.Error(err).Equals(io.EOF)
  133. }
  134. func TestIPv6Request(t *testing.T) {
  135. v2testing.Current(t)
  136. request, err := ReadRequest(alloc.NewBuffer().Clear().AppendBytes(5, 1, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 8))
  137. assert.Error(err).IsNil()
  138. assert.Byte(request.Command).Equals(1)
  139. assert.Bytes(request.IPv6[:]).Equals([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6})
  140. v2netassert.Port(request.Port).Equals(8)
  141. }