protocol_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package shadowsocks_test
  2. import (
  3. "testing"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
  7. . "github.com/v2ray/v2ray-core/proxy/shadowsocks"
  8. v2testing "github.com/v2ray/v2ray-core/testing"
  9. "github.com/v2ray/v2ray-core/testing/assert"
  10. )
  11. func TestNormalRequestParsing(t *testing.T) {
  12. v2testing.Current(t)
  13. buffer := alloc.NewSmallBuffer().Clear()
  14. buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80)
  15. request, err := ReadRequest(buffer, nil, false)
  16. assert.Error(err).IsNil()
  17. netassert.Address(request.Address).Equals(v2net.IPAddress([]byte{127, 0, 0, 1}))
  18. netassert.Port(request.Port).Equals(v2net.Port(80))
  19. assert.Bool(request.OTA).IsFalse()
  20. }
  21. func TestOTARequest(t *testing.T) {
  22. v2testing.Current(t)
  23. buffer := alloc.NewSmallBuffer().Clear()
  24. buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 0)
  25. auth := NewAuthenticator(HeaderKeyGenerator(
  26. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
  27. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
  28. request, err := ReadRequest(buffer, auth, false)
  29. assert.Error(err).IsNil()
  30. netassert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
  31. assert.Bool(request.OTA).IsTrue()
  32. }
  33. func TestUDPRequestParsing(t *testing.T) {
  34. v2testing.Current(t)
  35. buffer := alloc.NewSmallBuffer().Clear()
  36. buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80, 1, 2, 3, 4, 5, 6)
  37. request, err := ReadRequest(buffer, nil, true)
  38. assert.Error(err).IsNil()
  39. netassert.Address(request.Address).Equals(v2net.IPAddress([]byte{127, 0, 0, 1}))
  40. netassert.Port(request.Port).Equals(v2net.Port(80))
  41. assert.Bool(request.OTA).IsFalse()
  42. assert.Bytes(request.UDPPayload.Value).Equals([]byte{1, 2, 3, 4, 5, 6})
  43. }
  44. func TestUDPRequestWithOTA(t *testing.T) {
  45. v2testing.Current(t)
  46. buffer := alloc.NewSmallBuffer().Clear()
  47. buffer.AppendBytes(
  48. 0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0,
  49. 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
  50. 58, 32, 223, 30, 57, 199, 50, 139, 143, 101)
  51. auth := NewAuthenticator(HeaderKeyGenerator(
  52. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
  53. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
  54. request, err := ReadRequest(buffer, auth, true)
  55. assert.Error(err).IsNil()
  56. netassert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
  57. assert.Bool(request.OTA).IsTrue()
  58. assert.Bytes(request.UDPPayload.Value).Equals([]byte{
  59. 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
  60. }