protocol_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 equalRequestHeader(x, y *protocol.RequestHeader) bool {
  17. return cmp.Equal(x, y, cmp.Comparer(func(x, y protocol.RequestHeader) bool {
  18. return x == y
  19. }))
  20. }
  21. func TestUDPEncodingDecoding(t *testing.T) {
  22. testRequests := []protocol.RequestHeader{
  23. {
  24. Version: Version,
  25. Command: protocol.RequestCommandUDP,
  26. Address: net.LocalHostIP,
  27. Port: 1234,
  28. User: &protocol.MemoryUser{
  29. Email: "[email protected]",
  30. Account: toAccount(&Account{
  31. Password: "password",
  32. CipherType: CipherType_AES_128_GCM,
  33. }),
  34. },
  35. },
  36. {
  37. Version: Version,
  38. Command: protocol.RequestCommandUDP,
  39. Address: net.LocalHostIP,
  40. Port: 1234,
  41. User: &protocol.MemoryUser{
  42. Email: "[email protected]",
  43. Account: toAccount(&Account{
  44. Password: "123",
  45. CipherType: CipherType_NONE,
  46. }),
  47. },
  48. },
  49. }
  50. for _, request := range testRequests {
  51. data := buf.New()
  52. common.Must2(data.WriteString("test string"))
  53. encodedData, err := EncodeUDPPacket(&request, data.Bytes())
  54. common.Must(err)
  55. validator := new(Validator)
  56. validator.Add(request.User)
  57. decodedRequest, decodedData, err := DecodeUDPPacket(validator, encodedData)
  58. common.Must(err)
  59. if r := cmp.Diff(decodedData.Bytes(), data.Bytes()); r != "" {
  60. t.Error("data: ", r)
  61. }
  62. if equalRequestHeader(decodedRequest, &request) == false {
  63. t.Error("different request")
  64. }
  65. }
  66. }
  67. func TestUDPDecodingWithPayloadTooShort(t *testing.T) {
  68. testAccounts := []protocol.Account{
  69. toAccount(&Account{
  70. Password: "password",
  71. CipherType: CipherType_AES_128_GCM,
  72. }),
  73. toAccount(&Account{
  74. Password: "password",
  75. CipherType: CipherType_NONE,
  76. }),
  77. }
  78. for _, account := range testAccounts {
  79. data := buf.New()
  80. data.WriteString("short payload")
  81. validator := new(Validator)
  82. validator.Add(&protocol.MemoryUser{
  83. Account: account,
  84. })
  85. _, _, err := DecodeUDPPacket(validator, data)
  86. if err == nil {
  87. t.Fatal("expected error")
  88. }
  89. }
  90. }
  91. func TestTCPRequest(t *testing.T) {
  92. cases := []struct {
  93. request *protocol.RequestHeader
  94. payload []byte
  95. }{
  96. {
  97. request: &protocol.RequestHeader{
  98. Version: Version,
  99. Command: protocol.RequestCommandTCP,
  100. Address: net.LocalHostIP,
  101. Port: 1234,
  102. User: &protocol.MemoryUser{
  103. Email: "[email protected]",
  104. Account: toAccount(&Account{
  105. Password: "tcp-password",
  106. CipherType: CipherType_AES_128_GCM,
  107. }),
  108. },
  109. },
  110. payload: []byte("test string"),
  111. },
  112. {
  113. request: &protocol.RequestHeader{
  114. Version: Version,
  115. Command: protocol.RequestCommandTCP,
  116. Address: net.LocalHostIPv6,
  117. Port: 1234,
  118. User: &protocol.MemoryUser{
  119. Email: "[email protected]",
  120. Account: toAccount(&Account{
  121. Password: "password",
  122. CipherType: CipherType_AES_256_GCM,
  123. }),
  124. },
  125. },
  126. payload: []byte("test string"),
  127. },
  128. {
  129. request: &protocol.RequestHeader{
  130. Version: Version,
  131. Command: protocol.RequestCommandTCP,
  132. Address: net.DomainAddress("example.com"),
  133. Port: 1234,
  134. User: &protocol.MemoryUser{
  135. Email: "[email protected]",
  136. Account: toAccount(&Account{
  137. Password: "password",
  138. CipherType: CipherType_CHACHA20_POLY1305,
  139. }),
  140. },
  141. },
  142. payload: []byte("test string"),
  143. },
  144. }
  145. runTest := func(request *protocol.RequestHeader, payload []byte) {
  146. data := buf.New()
  147. common.Must2(data.Write(payload))
  148. cache := buf.New()
  149. defer cache.Release()
  150. writer, err := WriteTCPRequest(request, cache)
  151. common.Must(err)
  152. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{data}))
  153. validator := new(Validator)
  154. validator.Add(request.User)
  155. decodedRequest, reader, err := ReadTCPSession(validator, cache)
  156. common.Must(err)
  157. if equalRequestHeader(decodedRequest, request) == false {
  158. t.Error("different request")
  159. }
  160. decodedData, err := reader.ReadMultiBuffer()
  161. common.Must(err)
  162. if r := cmp.Diff(decodedData[0].Bytes(), payload); r != "" {
  163. t.Error("data: ", r)
  164. }
  165. }
  166. for _, test := range cases {
  167. runTest(test.request, test.payload)
  168. }
  169. }
  170. func TestUDPReaderWriter(t *testing.T) {
  171. user := &protocol.MemoryUser{
  172. Account: toAccount(&Account{
  173. Password: "test-password",
  174. CipherType: CipherType_CHACHA20_POLY1305,
  175. }),
  176. }
  177. cache := buf.New()
  178. defer cache.Release()
  179. writer := &UDPWriter{
  180. Writer: cache,
  181. Request: &protocol.RequestHeader{
  182. Version: Version,
  183. Address: net.DomainAddress("example.com"),
  184. Port: 123,
  185. User: user,
  186. },
  187. }
  188. reader := &UDPReader{
  189. Reader: cache,
  190. User: user,
  191. }
  192. {
  193. b := buf.New()
  194. common.Must2(b.WriteString("test payload"))
  195. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
  196. payload, err := reader.ReadMultiBuffer()
  197. common.Must(err)
  198. if payload[0].String() != "test payload" {
  199. t.Error("unexpected output: ", payload[0].String())
  200. }
  201. }
  202. {
  203. b := buf.New()
  204. common.Must2(b.WriteString("test payload 2"))
  205. common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b}))
  206. payload, err := reader.ReadMultiBuffer()
  207. common.Must(err)
  208. if payload[0].String() != "test payload 2" {
  209. t.Error("unexpected output: ", payload[0].String())
  210. }
  211. }
  212. }