socks4_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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).HasCode(1000)
  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 := &Socks4AuthenticationResponse{
  26. result: byte(0x10),
  27. port: 443,
  28. ip: []byte{1, 2, 3, 4},
  29. }
  30. buffer := alloc.NewSmallBuffer().Clear()
  31. defer buffer.Release()
  32. response.Write(buffer)
  33. assert.Bytes(buffer.Value).Equals([]byte{0x00, 0x10, 0x01, 0xBB, 0x01, 0x02, 0x03, 0x04})
  34. }