protocol_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package shadowsocks_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. "github.com/xtls/xray-core/common"
  6. "github.com/xtls/xray-core/common/buf"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/common/protocol"
  9. . "github.com/xtls/xray-core/proxy/shadowsocks"
  10. )
  11. func toAccount(a *Account) protocol.Account {
  12. account, err := a.AsAccount()
  13. common.Must(err)
  14. return account
  15. }
  16. func TestUDPEncoding(t *testing.T) {
  17. request := &protocol.RequestHeader{
  18. Version: Version,
  19. Command: protocol.RequestCommandUDP,
  20. Address: net.LocalHostIP,
  21. Port: 1234,
  22. User: &protocol.MemoryUser{
  23. Email: "[email protected]",
  24. Account: toAccount(&Account{
  25. Password: "shadowsocks-password",
  26. CipherType: CipherType_AES_128_CFB,
  27. }),
  28. },
  29. }
  30. data := buf.New()
  31. common.Must2(data.WriteString("test string"))
  32. encodedData, err := EncodeUDPPacket(request, data.Bytes())
  33. common.Must(err)
  34. decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData)
  35. common.Must(err)
  36. if r := cmp.Diff(decodedData.Bytes(), data.Bytes()); r != "" {
  37. t.Error("data: ", r)
  38. }
  39. if r := cmp.Diff(decodedRequest, request); r != "" {
  40. t.Error("request: ", r)
  41. }
  42. }
  43. func TestTCPRequest(t *testing.T) {
  44. cases := []struct {
  45. request *protocol.RequestHeader
  46. payload []byte
  47. }{
  48. {
  49. request: &protocol.RequestHeader{
  50. Version: Version,
  51. Command: protocol.RequestCommandTCP,
  52. Address: net.LocalHostIP,
  53. Port: 1234,
  54. User: &protocol.MemoryUser{
  55. Email: "[email protected]",
  56. Account: toAccount(&Account{
  57. Password: "tcp-password",
  58. CipherType: CipherType_CHACHA20,
  59. }),
  60. },
  61. },
  62. payload: []byte("test string"),
  63. },
  64. {
  65. request: &protocol.RequestHeader{
  66. Version: Version,
  67. Command: protocol.RequestCommandTCP,
  68. Address: net.LocalHostIPv6,
  69. Port: 1234,
  70. User: &protocol.MemoryUser{
  71. Email: "[email protected]",
  72. Account: toAccount(&Account{
  73. Password: "password",
  74. CipherType: CipherType_AES_256_CFB,
  75. }),
  76. },
  77. },
  78. payload: []byte("test string"),
  79. },
  80. {
  81. request: &protocol.RequestHeader{
  82. Version: Version,
  83. Command: protocol.RequestCommandTCP,
  84. Address: net.DomainAddress("example.com"),
  85. Port: 1234,
  86. User: &protocol.MemoryUser{
  87. Email: "[email protected]",
  88. Account: toAccount(&Account{
  89. Password: "password",
  90. CipherType: CipherType_CHACHA20_IETF,
  91. }),
  92. },
  93. },
  94. payload: []byte("test string"),
  95. },
  96. }
  97. runTest := func(request *protocol.RequestHeader, payload []byte) {
  98. data := buf.New()
  99. common.Must2(data.Write(payload))
  100. cache := buf.New()
  101. defer cache.Release()
  102. writer, err := WriteTCPRequest(request, cache)
  103. common.Must(err)
  104. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
  105. decodedRequest, reader, err := ReadTCPSession(request.User, cache)
  106. common.Must(err)
  107. if r := cmp.Diff(decodedRequest, request); r != "" {
  108. t.Error("request: ", r)
  109. }
  110. decodedData, err := reader.ReadMultiBuffer()
  111. common.Must(err)
  112. if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
  113. t.Error("data: ", r)
  114. }
  115. }
  116. for _, test := range cases {
  117. runTest(test.request, test.payload)
  118. }
  119. }
  120. func TestUDPReaderWriter(t *testing.T) {
  121. user := &protocol.MemoryUser{
  122. Account: toAccount(&Account{
  123. Password: "test-password",
  124. CipherType: CipherType_CHACHA20_IETF,
  125. }),
  126. }
  127. cache := buf.New()
  128. defer cache.Release()
  129. writer := &UDPWriter{
  130. Writer: cache,
  131. Request: &protocol.RequestHeader{
  132. Version: Version,
  133. Address: net.DomainAddress("example.com"),
  134. Port: 123,
  135. User: user,
  136. },
  137. }
  138. reader := &UDPReader{
  139. Reader: cache,
  140. User: user,
  141. }
  142. {
  143. b := buf.New()
  144. common.Must2(b.WriteString("test payload"))
  145. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
  146. payload, err := reader.ReadMultiBuffer()
  147. common.Must(err)
  148. if payload[0].String() != "test payload" {
  149. t.Error("unexpected output: ", payload[0].String())
  150. }
  151. }
  152. {
  153. b := buf.New()
  154. common.Must2(b.WriteString("test payload 2"))
  155. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
  156. payload, err := reader.ReadMultiBuffer()
  157. common.Must(err)
  158. if payload[0].String() != "test payload 2" {
  159. t.Error("unexpected output: ", payload[0].String())
  160. }
  161. }
  162. }