socks_test.go 2.0 KB

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