protocol_test.go 4.4 KB

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