socks_test.go 4.7 KB

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