fakeconns_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "bytes"
  9. "context"
  10. "sync"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. protocolmocks "github.com/syncthing/syncthing/lib/protocol/mocks"
  14. "github.com/syncthing/syncthing/lib/scanner"
  15. )
  16. type downloadProgressMessage struct {
  17. folder string
  18. updates []protocol.FileDownloadProgressUpdate
  19. }
  20. func newFakeConnection(id protocol.DeviceID, model Model) *fakeConnection {
  21. f := &fakeConnection{
  22. Connection: new(protocolmocks.Connection),
  23. id: id,
  24. model: model,
  25. }
  26. f.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
  27. return f.fileData[name], nil
  28. })
  29. f.IDReturns(id)
  30. f.CloseCalls(func(err error) {
  31. model.Closed(f, err)
  32. f.ClosedReturns(true)
  33. })
  34. return f
  35. }
  36. type fakeConnection struct {
  37. *protocolmocks.Connection
  38. id protocol.DeviceID
  39. downloadProgressMessages []downloadProgressMessage
  40. files []protocol.FileInfo
  41. fileData map[string][]byte
  42. folder string
  43. model Model
  44. mut sync.Mutex
  45. }
  46. func (f *fakeConnection) setIndexFn(fn func(_ context.Context, folder string, fs []protocol.FileInfo) error) {
  47. f.IndexCalls(fn)
  48. f.IndexUpdateCalls(fn)
  49. }
  50. func (f *fakeConnection) DownloadProgress(_ context.Context, folder string, updates []protocol.FileDownloadProgressUpdate) {
  51. f.downloadProgressMessages = append(f.downloadProgressMessages, downloadProgressMessage{
  52. folder: folder,
  53. updates: updates,
  54. })
  55. }
  56. func (f *fakeConnection) addFileLocked(name string, flags uint32, ftype protocol.FileInfoType, data []byte, version protocol.Vector, localFlags uint32) {
  57. blockSize := protocol.BlockSize(int64(len(data)))
  58. blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), blockSize, int64(len(data)), nil, true)
  59. file := protocol.FileInfo{
  60. Name: name,
  61. Type: ftype,
  62. Version: version,
  63. Sequence: time.Now().UnixNano(),
  64. LocalFlags: localFlags,
  65. }
  66. switch ftype {
  67. case protocol.FileInfoTypeFile, protocol.FileInfoTypeDirectory:
  68. file.ModifiedS = time.Now().Unix()
  69. file.Permissions = flags
  70. if ftype == protocol.FileInfoTypeFile {
  71. file.Size = int64(len(data))
  72. file.RawBlockSize = blockSize
  73. file.Blocks = blocks
  74. }
  75. default: // Symlink
  76. file.Name = name
  77. file.Type = ftype
  78. file.Version = version
  79. file.SymlinkTarget = string(data)
  80. file.NoPermissions = true
  81. }
  82. f.files = append(f.files, file)
  83. if f.fileData == nil {
  84. f.fileData = make(map[string][]byte)
  85. }
  86. f.fileData[name] = data
  87. }
  88. func (f *fakeConnection) addFileWithLocalFlags(name string, ftype protocol.FileInfoType, localFlags uint32) {
  89. f.mut.Lock()
  90. defer f.mut.Unlock()
  91. var version protocol.Vector
  92. version = version.Update(f.id.Short())
  93. f.addFileLocked(name, 0, ftype, nil, version, localFlags)
  94. }
  95. func (f *fakeConnection) addFile(name string, flags uint32, ftype protocol.FileInfoType, data []byte) {
  96. f.mut.Lock()
  97. defer f.mut.Unlock()
  98. var version protocol.Vector
  99. version = version.Update(f.id.Short())
  100. f.addFileLocked(name, flags, ftype, data, version, 0)
  101. }
  102. func (f *fakeConnection) updateFile(name string, flags uint32, ftype protocol.FileInfoType, data []byte) {
  103. f.mut.Lock()
  104. defer f.mut.Unlock()
  105. for i, fi := range f.files {
  106. if fi.Name == name {
  107. f.files = append(f.files[:i], f.files[i+1:]...)
  108. f.addFileLocked(name, flags, ftype, data, fi.Version.Update(f.id.Short()), 0)
  109. return
  110. }
  111. }
  112. }
  113. func (f *fakeConnection) deleteFile(name string) {
  114. f.mut.Lock()
  115. defer f.mut.Unlock()
  116. for i, fi := range f.files {
  117. if fi.Name == name {
  118. fi.Deleted = true
  119. fi.ModifiedS = time.Now().Unix()
  120. fi.Version = fi.Version.Update(f.id.Short())
  121. fi.Sequence = time.Now().UnixNano()
  122. fi.Blocks = nil
  123. f.files = append(append(f.files[:i], f.files[i+1:]...), fi)
  124. return
  125. }
  126. }
  127. }
  128. func (f *fakeConnection) sendIndexUpdate() {
  129. toSend := make([]protocol.FileInfo, len(f.files))
  130. for i := range f.files {
  131. toSend[i] = prepareFileInfoForIndex(f.files[i])
  132. }
  133. f.model.IndexUpdate(f.id, f.folder, toSend)
  134. }
  135. func addFakeConn(m *testModel, dev protocol.DeviceID) *fakeConnection {
  136. fc := newFakeConnection(dev, m)
  137. m.AddConnection(fc, protocol.Hello{})
  138. m.ClusterConfig(dev, protocol.ClusterConfig{
  139. Folders: []protocol.Folder{
  140. {
  141. ID: "default",
  142. Devices: []protocol.Device{
  143. {ID: myID},
  144. {ID: device1},
  145. },
  146. },
  147. },
  148. })
  149. return fc
  150. }