protocol_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "io/ioutil"
  9. "strings"
  10. "testing"
  11. "testing/quick"
  12. "time"
  13. "github.com/syncthing/syncthing/lib/rand"
  14. )
  15. var (
  16. c0ID = NewDeviceID([]byte{1})
  17. c1ID = NewDeviceID([]byte{2})
  18. quickCfg = &quick.Config{}
  19. )
  20. func TestPing(t *testing.T) {
  21. ar, aw := io.Pipe()
  22. br, bw := io.Pipe()
  23. c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  24. c0.Start()
  25. c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  26. c1.Start()
  27. c0.ClusterConfig(ClusterConfig{})
  28. c1.ClusterConfig(ClusterConfig{})
  29. if ok := c0.ping(); !ok {
  30. t.Error("c0 ping failed")
  31. }
  32. if ok := c1.ping(); !ok {
  33. t.Error("c1 ping failed")
  34. }
  35. }
  36. func TestClose(t *testing.T) {
  37. m0 := newTestModel()
  38. m1 := newTestModel()
  39. ar, aw := io.Pipe()
  40. br, bw := io.Pipe()
  41. c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
  42. c0.Start()
  43. c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
  44. c1.Start()
  45. c0.ClusterConfig(ClusterConfig{})
  46. c1.ClusterConfig(ClusterConfig{})
  47. c0.close(errors.New("manual close"))
  48. <-c0.closed
  49. if err := m0.closedError(); err == nil || !strings.Contains(err.Error(), "manual close") {
  50. t.Fatal("Connection should be closed")
  51. }
  52. // None of these should panic, some should return an error
  53. if c0.ping() {
  54. t.Error("Ping should not return true")
  55. }
  56. c0.Index("default", nil)
  57. c0.Index("default", nil)
  58. if _, err := c0.Request("default", "foo", 0, 0, nil, false); err == nil {
  59. t.Error("Request should return an error")
  60. }
  61. }
  62. func TestMarshalIndexMessage(t *testing.T) {
  63. if testing.Short() {
  64. quickCfg.MaxCount = 10
  65. }
  66. f := func(m1 Index) bool {
  67. if len(m1.Files) == 0 {
  68. m1.Files = nil
  69. }
  70. for i, f := range m1.Files {
  71. if len(f.Blocks) == 0 {
  72. m1.Files[i].Blocks = nil
  73. } else {
  74. for j := range f.Blocks {
  75. f.Blocks[j].Offset = 0
  76. if len(f.Blocks[j].Hash) == 0 {
  77. f.Blocks[j].Hash = nil
  78. }
  79. }
  80. }
  81. if len(f.Version.Counters) == 0 {
  82. m1.Files[i].Version.Counters = nil
  83. }
  84. }
  85. return testMarshal(t, "index", &m1, &Index{})
  86. }
  87. if err := quick.Check(f, quickCfg); err != nil {
  88. t.Error(err)
  89. }
  90. }
  91. func TestMarshalRequestMessage(t *testing.T) {
  92. if testing.Short() {
  93. quickCfg.MaxCount = 10
  94. }
  95. f := func(m1 Request) bool {
  96. if len(m1.Hash) == 0 {
  97. m1.Hash = nil
  98. }
  99. return testMarshal(t, "request", &m1, &Request{})
  100. }
  101. if err := quick.Check(f, quickCfg); err != nil {
  102. t.Error(err)
  103. }
  104. }
  105. func TestMarshalResponseMessage(t *testing.T) {
  106. if testing.Short() {
  107. quickCfg.MaxCount = 10
  108. }
  109. f := func(m1 Response) bool {
  110. if len(m1.Data) == 0 {
  111. m1.Data = nil
  112. }
  113. return testMarshal(t, "response", &m1, &Response{})
  114. }
  115. if err := quick.Check(f, quickCfg); err != nil {
  116. t.Error(err)
  117. }
  118. }
  119. func TestMarshalClusterConfigMessage(t *testing.T) {
  120. if testing.Short() {
  121. quickCfg.MaxCount = 10
  122. }
  123. f := func(m1 ClusterConfig) bool {
  124. if len(m1.Folders) == 0 {
  125. m1.Folders = nil
  126. }
  127. for i := range m1.Folders {
  128. if len(m1.Folders[i].Devices) == 0 {
  129. m1.Folders[i].Devices = nil
  130. }
  131. }
  132. return testMarshal(t, "clusterconfig", &m1, &ClusterConfig{})
  133. }
  134. if err := quick.Check(f, quickCfg); err != nil {
  135. t.Error(err)
  136. }
  137. }
  138. func TestMarshalCloseMessage(t *testing.T) {
  139. if testing.Short() {
  140. quickCfg.MaxCount = 10
  141. }
  142. f := func(m1 Close) bool {
  143. return testMarshal(t, "close", &m1, &Close{})
  144. }
  145. if err := quick.Check(f, quickCfg); err != nil {
  146. t.Error(err)
  147. }
  148. }
  149. func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
  150. buf, err := m1.Marshal()
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. err = m2.Unmarshal(buf)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. bs1, _ := json.MarshalIndent(m1, "", " ")
  159. bs2, _ := json.MarshalIndent(m2, "", " ")
  160. if !bytes.Equal(bs1, bs2) {
  161. ioutil.WriteFile(prefix+"-1.txt", bs1, 0644)
  162. ioutil.WriteFile(prefix+"-2.txt", bs1, 0644)
  163. return false
  164. }
  165. return true
  166. }
  167. func TestMarshalledIndexMessageSize(t *testing.T) {
  168. // We should be able to handle a 1 TiB file without
  169. // blowing the default max message size.
  170. if testing.Short() {
  171. t.Skip("this test requires a lot of memory")
  172. return
  173. }
  174. const (
  175. maxMessageSize = MaxMessageLen
  176. fileSize = 1 << 40
  177. blockSize = BlockSize
  178. )
  179. f := FileInfo{
  180. Name: "a normal length file name withoout any weird stuff.txt",
  181. Type: FileInfoTypeFile,
  182. Size: fileSize,
  183. Permissions: 0666,
  184. ModifiedS: time.Now().Unix(),
  185. Version: Vector{Counters: []Counter{{ID: 1 << 60, Value: 1}, {ID: 2 << 60, Value: 1}}},
  186. Blocks: make([]BlockInfo, fileSize/blockSize),
  187. }
  188. for i := 0; i < fileSize/blockSize; i++ {
  189. f.Blocks[i].Offset = int64(i) * blockSize
  190. f.Blocks[i].Size = blockSize
  191. f.Blocks[i].Hash = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 30, 1, 2}
  192. }
  193. idx := Index{
  194. Folder: "some folder ID",
  195. Files: []FileInfo{f},
  196. }
  197. msgSize := idx.ProtoSize()
  198. if msgSize > maxMessageSize {
  199. t.Errorf("Message size %d bytes is larger than max %d", msgSize, maxMessageSize)
  200. }
  201. }
  202. func TestLZ4Compression(t *testing.T) {
  203. c := new(rawConnection)
  204. for i := 0; i < 10; i++ {
  205. dataLen := 150 + rand.Intn(150)
  206. data := make([]byte, dataLen)
  207. _, err := io.ReadFull(rand.Reader, data[100:])
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. comp, err := c.lz4Compress(data)
  212. if err != nil {
  213. t.Errorf("compressing %d bytes: %v", dataLen, err)
  214. continue
  215. }
  216. res, err := c.lz4Decompress(comp)
  217. if err != nil {
  218. t.Errorf("decompressing %d bytes to %d: %v", len(comp), dataLen, err)
  219. continue
  220. }
  221. if len(res) != len(data) {
  222. t.Errorf("Incorrect len %d != expected %d", len(res), len(data))
  223. }
  224. if !bytes.Equal(data, res) {
  225. t.Error("Incorrect decompressed data")
  226. }
  227. t.Logf("OK #%d, %d -> %d -> %d", i, dataLen, len(comp), dataLen)
  228. }
  229. }