socks4_test.go 1.0 KB

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