protocol_test.go 2.9 KB

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