socks_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package protocol
  2. import (
  3. "bytes"
  4. "io"
  5. "testing"
  6. "github.com/v2ray/v2ray-core/common/alloc"
  7. v2testing "github.com/v2ray/v2ray-core/testing"
  8. "github.com/v2ray/v2ray-core/testing/assert"
  9. "github.com/v2ray/v2ray-core/transport"
  10. )
  11. func TestHasAuthenticationMethod(t *testing.T) {
  12. v2testing.Current(t)
  13. request := Socks5AuthenticationRequest{
  14. version: socksVersion,
  15. nMethods: byte(0x02),
  16. authMethods: [256]byte{0x01, 0x02},
  17. }
  18. assert.Bool(request.HasAuthMethod(byte(0x01))).IsTrue()
  19. request.authMethods[0] = byte(0x03)
  20. assert.Bool(request.HasAuthMethod(byte(0x01))).IsFalse()
  21. }
  22. func TestAuthenticationRequestRead(t *testing.T) {
  23. v2testing.Current(t)
  24. rawRequest := []byte{
  25. 0x05, // version
  26. 0x01, // nMethods
  27. 0x02, // methods
  28. }
  29. request, _, err := ReadAuthentication(bytes.NewReader(rawRequest))
  30. assert.Error(err).IsNil()
  31. assert.Byte(request.version).Named("Version").Equals(0x05)
  32. assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
  33. assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
  34. }
  35. func TestAuthenticationResponseWrite(t *testing.T) {
  36. v2testing.Current(t)
  37. response := NewAuthenticationResponse(byte(0x05))
  38. buffer := bytes.NewBuffer(make([]byte, 0, 10))
  39. WriteAuthentication(buffer, response)
  40. assert.Bytes(buffer.Bytes()).Equals([]byte{socksVersion, byte(0x05)})
  41. }
  42. func TestRequestRead(t *testing.T) {
  43. v2testing.Current(t)
  44. rawRequest := []byte{
  45. 0x05, // version
  46. 0x01, // cmd connect
  47. 0x00, // reserved
  48. 0x01, // ipv4 type
  49. 0x72, 0x72, 0x72, 0x72, // 114.114.114.114
  50. 0x00, 0x35, // port 53
  51. }
  52. request, err := ReadRequest(bytes.NewReader(rawRequest))
  53. assert.Error(err).IsNil()
  54. assert.Byte(request.Version).Named("Version").Equals(0x05)
  55. assert.Byte(request.Command).Named("Command").Equals(0x01)
  56. assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
  57. assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
  58. assert.Uint16(request.Port).Named("Port").Equals(53)
  59. }
  60. func TestResponseWrite(t *testing.T) {
  61. v2testing.Current(t)
  62. response := Socks5Response{
  63. socksVersion,
  64. ErrorSuccess,
  65. AddrTypeIPv4,
  66. [4]byte{0x72, 0x72, 0x72, 0x72},
  67. "",
  68. [16]byte{},
  69. uint16(53),
  70. }
  71. buffer := alloc.NewSmallBuffer().Clear()
  72. defer buffer.Release()
  73. response.Write(buffer)
  74. expectedBytes := []byte{
  75. socksVersion,
  76. ErrorSuccess,
  77. byte(0x00),
  78. AddrTypeIPv4,
  79. 0x72, 0x72, 0x72, 0x72,
  80. byte(0x00), byte(0x035),
  81. }
  82. assert.Bytes(buffer.Value).Named("raw response").Equals(expectedBytes)
  83. }
  84. func TestEOF(t *testing.T) {
  85. v2testing.Current(t)
  86. _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 0)))
  87. assert.Error(err).Equals(io.EOF)
  88. }
  89. func TestSignleByte(t *testing.T) {
  90. v2testing.Current(t)
  91. _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
  92. assert.Error(err).Equals(transport.CorruptedPacket)
  93. }