protocol_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package socks_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/net"
  9. _ "v2ray.com/core/common/net/testing"
  10. "v2ray.com/core/common/protocol"
  11. . "v2ray.com/core/proxy/socks"
  12. )
  13. func TestUDPEncoding(t *testing.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. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{payload}))
  24. reader := NewUDPReader(b)
  25. decodedPayload, err := reader.ReadMultiBuffer()
  26. common.Must(err)
  27. if r := cmp.Diff(decodedPayload[0].Bytes(), content); r != "" {
  28. t.Error(r)
  29. }
  30. }
  31. func TestReadUsernamePassword(t *testing.T) {
  32. testCases := []struct {
  33. Input []byte
  34. Username string
  35. Password string
  36. Error bool
  37. }{
  38. {
  39. Input: []byte{0x05, 0x01, 'a', 0x02, 'b', 'c'},
  40. Username: "a",
  41. Password: "bc",
  42. },
  43. {
  44. Input: []byte{0x05, 0x18, 'a', 0x02, 'b', 'c'},
  45. Error: true,
  46. },
  47. }
  48. for _, testCase := range testCases {
  49. reader := bytes.NewReader(testCase.Input)
  50. username, password, err := ReadUsernamePassword(reader)
  51. if testCase.Error {
  52. if err == nil {
  53. t.Error("for input: ", testCase.Input, " expect error, but actually nil")
  54. }
  55. } else {
  56. if err != nil {
  57. t.Error("for input: ", testCase.Input, " expect no error, but actually ", err.Error())
  58. }
  59. if testCase.Username != username {
  60. t.Error("for input: ", testCase.Input, " expect username ", testCase.Username, " but actually ", username)
  61. }
  62. if testCase.Password != password {
  63. t.Error("for input: ", testCase.Input, " expect passowrd ", testCase.Password, " but actually ", password)
  64. }
  65. }
  66. }
  67. }
  68. func TestReadUntilNull(t *testing.T) {
  69. testCases := []struct {
  70. Input []byte
  71. Output string
  72. Error bool
  73. }{
  74. {
  75. Input: []byte{'a', 'b', 0x00},
  76. Output: "ab",
  77. },
  78. {
  79. Input: []byte{'a'},
  80. Error: true,
  81. },
  82. }
  83. for _, testCase := range testCases {
  84. reader := bytes.NewReader(testCase.Input)
  85. value, err := ReadUntilNull(reader)
  86. if testCase.Error {
  87. if err == nil {
  88. t.Error("for input: ", testCase.Input, " expect error, but actually nil")
  89. }
  90. } else {
  91. if err != nil {
  92. t.Error("for input: ", testCase.Input, " expect no error, but actually ", err.Error())
  93. }
  94. if testCase.Output != value {
  95. t.Error("for input: ", testCase.Input, " expect output ", testCase.Output, " but actually ", value)
  96. }
  97. }
  98. }
  99. }
  100. func BenchmarkReadUsernamePassword(b *testing.B) {
  101. input := []byte{0x05, 0x01, 'a', 0x02, 'b', 'c'}
  102. buffer := buf.New()
  103. buffer.Write(input)
  104. b.ResetTimer()
  105. for i := 0; i < b.N; i++ {
  106. _, _, err := ReadUsernamePassword(buffer)
  107. common.Must(err)
  108. buffer.Clear()
  109. buffer.Extend(int32(len(input)))
  110. }
  111. }