socks4_test.go 1.2 KB

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