socks_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if err != nil {
  16. t.Errorf("Unexpected error %v", err)
  17. }
  18. assert.Byte(request.version).Named("Version").Equals(0x05)
  19. assert.Byte(request.nMethods).Named("#Methods").Equals(0x01)
  20. assert.Byte(request.authMethods[0]).Named("Auth Method").Equals(0x02)
  21. }
  22. func TestAuthenticationResponseToBytes(t *testing.T) {
  23. assert := unit.Assert(t)
  24. socksVersion := uint8(5)
  25. authMethod := uint8(1)
  26. response := Socks5AuthenticationResponse{socksVersion, authMethod}
  27. bytes := response.ToBytes()
  28. assert.Byte(bytes[0]).Named("Version").Equals(socksVersion)
  29. assert.Byte(bytes[1]).Named("Auth Method").Equals(authMethod)
  30. }
  31. func TestRequestRead(t *testing.T) {
  32. assert := unit.Assert(t)
  33. rawRequest := []byte{
  34. 0x05, // version
  35. 0x01, // cmd connect
  36. 0x00, // reserved
  37. 0x01, // ipv4 type
  38. 0x72, 0x72, 0x72, 0x72, // 114.114.114.114
  39. 0x00, 0x35, // port 53
  40. }
  41. request, err := ReadRequest(bytes.NewReader(rawRequest))
  42. if err != nil {
  43. t.Errorf("Unexpected error %v", err)
  44. }
  45. assert.Byte(request.Version).Named("Version").Equals(0x05)
  46. assert.Byte(request.Command).Named("Command").Equals(0x01)
  47. assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
  48. assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
  49. assert.Uint16(request.Port).Named("Port").Equals(53)
  50. }
  51. func TestResponseToBytes(t *testing.T) {
  52. assert := unit.Assert(t)
  53. response := Socks5Response{
  54. socksVersion,
  55. ErrorSuccess,
  56. AddrTypeIPv4,
  57. [4]byte{0x72, 0x72, 0x72, 0x72},
  58. "",
  59. [16]byte{},
  60. uint16(53),
  61. }
  62. rawResponse := response.toBytes()
  63. expectedBytes := []byte{
  64. socksVersion,
  65. ErrorSuccess,
  66. byte(0x00),
  67. AddrTypeIPv4,
  68. 0x72, 0x72, 0x72, 0x72,
  69. byte(0x00), byte(0x035),
  70. }
  71. assert.Bytes(rawResponse).Named("raw response").Equals(expectedBytes)
  72. }