protocol_test.go 4.3 KB

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