protocol_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package socks_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "v2ray.com/core/common/buf"
  6. "v2ray.com/core/common/net"
  7. _ "v2ray.com/core/common/net/testing"
  8. "v2ray.com/core/common/protocol"
  9. . "v2ray.com/core/proxy/socks"
  10. . "v2ray.com/ext/assert"
  11. )
  12. func TestUDPEncoding(t *testing.T) {
  13. assert := With(t)
  14. b := buf.New()
  15. request := &protocol.RequestHeader{
  16. Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
  17. Port: 1024,
  18. }
  19. writer := buf.NewSequentialWriter(NewUDPWriter(request, b))
  20. content := []byte{'a'}
  21. payload := buf.New()
  22. payload.Append(content)
  23. assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil)
  24. reader := NewUDPReader(b)
  25. decodedPayload, err := reader.ReadMultiBuffer()
  26. assert(err, IsNil)
  27. assert(decodedPayload[0].Bytes(), Equals, content)
  28. }
  29. func TestReadAddress(t *testing.T) {
  30. assert := With(t)
  31. data := []struct {
  32. AddrType byte
  33. Input []byte
  34. Address net.Address
  35. Port net.Port
  36. Error bool
  37. }{
  38. {
  39. AddrType: 0,
  40. Input: []byte{0, 0, 0, 0},
  41. Error: true,
  42. },
  43. {
  44. AddrType: 1,
  45. Input: []byte{0, 0, 0, 0, 0, 53},
  46. Address: net.IPAddress([]byte{0, 0, 0, 0}),
  47. Port: net.Port(53),
  48. },
  49. {
  50. AddrType: 4,
  51. Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 80},
  52. Address: net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}),
  53. Port: net.Port(80),
  54. },
  55. {
  56. AddrType: 3,
  57. Input: []byte{9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 80},
  58. Address: net.DomainAddress("v2ray.com"),
  59. Port: net.Port(80),
  60. },
  61. {
  62. AddrType: 3,
  63. Input: []byte{9, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0},
  64. Error: true,
  65. },
  66. }
  67. for _, tc := range data {
  68. b := buf.New()
  69. addr, port, err := ReadAddress(b, tc.AddrType, bytes.NewBuffer(tc.Input))
  70. b.Release()
  71. if tc.Error {
  72. assert(err, IsNotNil)
  73. } else {
  74. assert(addr, Equals, tc.Address)
  75. assert(port, Equals, tc.Port)
  76. }
  77. }
  78. }