protocol_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.SequentialWriter{Writer: NewUDPWriter(request, b)}
  20. content := []byte{'a'}
  21. payload := buf.New()
  22. payload.Write(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 TestReadUsernamePassword(t *testing.T) {
  30. testCases := []struct {
  31. Input []byte
  32. Username string
  33. Password string
  34. Error bool
  35. }{
  36. {
  37. Input: []byte{0x05, 0x01, 'a', 0x02, 'b', 'c'},
  38. Username: "a",
  39. Password: "bc",
  40. },
  41. {
  42. Input: []byte{0x05, 0x18, 'a', 0x02, 'b', 'c'},
  43. Error: true,
  44. },
  45. }
  46. for _, testCase := range testCases {
  47. reader := bytes.NewReader(testCase.Input)
  48. username, password, err := ReadUsernamePassword(reader)
  49. if testCase.Error {
  50. if err == nil {
  51. t.Error("for input: ", testCase.Input, " expect error, but actually nil")
  52. }
  53. } else {
  54. if err != nil {
  55. t.Error("for input: ", testCase.Input, " expect no error, but actually ", err.Error())
  56. }
  57. if testCase.Username != username {
  58. t.Error("for input: ", testCase.Input, " expect username ", testCase.Username, " but actually ", username)
  59. }
  60. if testCase.Password != password {
  61. t.Error("for input: ", testCase.Input, " expect passowrd ", testCase.Password, " but actually ", password)
  62. }
  63. }
  64. }
  65. }
  66. func TestReadUntilNull(t *testing.T) {
  67. testCases := []struct {
  68. Input []byte
  69. Output string
  70. Error bool
  71. }{
  72. {
  73. Input: []byte{'a', 'b', 0x00},
  74. Output: "ab",
  75. },
  76. {
  77. Input: []byte{'a'},
  78. Error: true,
  79. },
  80. }
  81. for _, testCase := range testCases {
  82. reader := bytes.NewReader(testCase.Input)
  83. value, err := ReadUntilNull(reader)
  84. if testCase.Error {
  85. if err == nil {
  86. t.Error("for input: ", testCase.Input, " expect error, but actually nil")
  87. }
  88. } else {
  89. if err != nil {
  90. t.Error("for input: ", testCase.Input, " expect no error, but actually ", err.Error())
  91. }
  92. if testCase.Output != value {
  93. t.Error("for input: ", testCase.Input, " expect output ", testCase.Output, " but actually ", value)
  94. }
  95. }
  96. }
  97. }