socks4_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package protocol
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. v2testing "github.com/v2ray/v2ray-core/testing"
  8. "github.com/v2ray/v2ray-core/testing/assert"
  9. )
  10. func TestSocks4AuthenticationRequestRead(t *testing.T) {
  11. v2testing.Current(t)
  12. rawRequest := []byte{
  13. 0x04, // version
  14. 0x01, // command
  15. 0x00, 0x35,
  16. 0x72, 0x72, 0x72, 0x72,
  17. }
  18. _, request4, err := ReadAuthentication(bytes.NewReader(rawRequest))
  19. assert.Error(err).Equals(Socks4Downgrade)
  20. assert.Byte(request4.Version).Named("Version").Equals(0x04)
  21. assert.Byte(request4.Command).Named("Command").Equals(0x01)
  22. assert.Port(request4.Port).Named("Port").Equals(v2net.Port(53))
  23. assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72})
  24. }
  25. func TestSocks4AuthenticationResponseToBytes(t *testing.T) {
  26. v2testing.Current(t)
  27. response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4})
  28. buffer := alloc.NewSmallBuffer().Clear()
  29. defer buffer.Release()
  30. response.Write(buffer)
  31. assert.Bytes(buffer.Value).Equals([]byte{0x00, 0x10, 0x01, 0xBB, 0x01, 0x02, 0x03, 0x04})
  32. }