socks_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package socks
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/testing/unit"
  6. )
  7. func TestHasAuthenticationMethod(t *testing.T) {
  8. assert := unit.Assert(t)
  9. request := Socks5AuthenticationRequest{
  10. version: socksVersion,
  11. nMethods: byte(0x02),
  12. authMethods: [256]byte{0x01, 0x02},
  13. }
  14. assert.Bool(request.HasAuthMethod(byte(0x01))).IsTrue()
  15. request.authMethods[0] = byte(0x03)
  16. assert.Bool(request.HasAuthMethod(byte(0x01))).IsFalse()
  17. }
  18. func TestAuthenticationRequestRead(t *testing.T) {
  19. assert := unit.Assert(t)
  20. rawRequest := []byte{
  21. 0x05, // version
  22. 0x01, // nMethods
  23. 0x02, // methods
  24. }
  25. request, err := ReadAuthentication(bytes.NewReader(rawRequest))
  26. assert.Error(err).IsNil()
  27. assert.Byte(request.version).Named("Version").Equals(0x05)
  28. assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
  29. assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
  30. }
  31. func TestAuthenticationResponseWrite(t *testing.T) {
  32. assert := unit.Assert(t)
  33. response := Socks5AuthenticationResponse{
  34. version: socksVersion,
  35. authMethod: byte(0x05),
  36. }
  37. buffer := bytes.NewBuffer(make([]byte, 0, 10))
  38. WriteAuthentication(buffer, &response)
  39. assert.Bytes(buffer.Bytes()).Equals([]byte{socksVersion, byte(0x05)})
  40. }
  41. func TestRequestRead(t *testing.T) {
  42. assert := unit.Assert(t)
  43. rawRequest := []byte{
  44. 0x05, // version
  45. 0x01, // cmd connect
  46. 0x00, // reserved
  47. 0x01, // ipv4 type
  48. 0x72, 0x72, 0x72, 0x72, // 114.114.114.114
  49. 0x00, 0x35, // port 53
  50. }
  51. request, err := ReadRequest(bytes.NewReader(rawRequest))
  52. assert.Error(err).IsNil()
  53. assert.Byte(request.Version).Named("Version").Equals(0x05)
  54. assert.Byte(request.Command).Named("Command").Equals(0x01)
  55. assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
  56. assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
  57. assert.Uint16(request.Port).Named("Port").Equals(53)
  58. }
  59. func TestResponseToBytes(t *testing.T) {
  60. assert := unit.Assert(t)
  61. response := Socks5Response{
  62. socksVersion,
  63. ErrorSuccess,
  64. AddrTypeIPv4,
  65. [4]byte{0x72, 0x72, 0x72, 0x72},
  66. "",
  67. [16]byte{},
  68. uint16(53),
  69. }
  70. rawResponse := response.toBytes()
  71. expectedBytes := []byte{
  72. socksVersion,
  73. ErrorSuccess,
  74. byte(0x00),
  75. AddrTypeIPv4,
  76. 0x72, 0x72, 0x72, 0x72,
  77. byte(0x00), byte(0x035),
  78. }
  79. assert.Bytes(rawResponse).Named("raw response").Equals(expectedBytes)
  80. }