socks_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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. rawRequest := []byte{
  27. 0x05, // version
  28. 0x01, // nMethods
  29. 0x02, // methods
  30. }
  31. request, _, err := ReadAuthentication(bytes.NewReader(rawRequest))
  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. v2netassert.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 TestEOF(t *testing.T) {
  87. v2testing.Current(t)
  88. _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 0)))
  89. assert.Error(err).Equals(io.EOF)
  90. }
  91. func TestSignleByte(t *testing.T) {
  92. v2testing.Current(t)
  93. _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
  94. assert.Error(err).Equals(transport.CorruptedPacket)
  95. }