socks_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package protocol
  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 := NewAuthenticationResponse(byte(0x05))
  34. buffer := bytes.NewBuffer(make([]byte, 0, 10))
  35. WriteAuthentication(buffer, response)
  36. assert.Bytes(buffer.Bytes()).Equals([]byte{socksVersion, byte(0x05)})
  37. }
  38. func TestRequestRead(t *testing.T) {
  39. assert := unit.Assert(t)
  40. rawRequest := []byte{
  41. 0x05, // version
  42. 0x01, // cmd connect
  43. 0x00, // reserved
  44. 0x01, // ipv4 type
  45. 0x72, 0x72, 0x72, 0x72, // 114.114.114.114
  46. 0x00, 0x35, // port 53
  47. }
  48. request, err := ReadRequest(bytes.NewReader(rawRequest))
  49. assert.Error(err).IsNil()
  50. assert.Byte(request.Version).Named("Version").Equals(0x05)
  51. assert.Byte(request.Command).Named("Command").Equals(0x01)
  52. assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
  53. assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
  54. assert.Uint16(request.Port).Named("Port").Equals(53)
  55. }
  56. func TestResponseToBytes(t *testing.T) {
  57. assert := unit.Assert(t)
  58. response := Socks5Response{
  59. socksVersion,
  60. ErrorSuccess,
  61. AddrTypeIPv4,
  62. [4]byte{0x72, 0x72, 0x72, 0x72},
  63. "",
  64. [16]byte{},
  65. uint16(53),
  66. }
  67. rawResponse := response.toBytes()
  68. expectedBytes := []byte{
  69. socksVersion,
  70. ErrorSuccess,
  71. byte(0x00),
  72. AddrTypeIPv4,
  73. 0x72, 0x72, 0x72, 0x72,
  74. byte(0x00), byte(0x035),
  75. }
  76. assert.Bytes(rawResponse).Named("raw response").Equals(expectedBytes)
  77. }