fakeconns_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. closed: make(chan struct{}),
  26. }
  27. f.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
  28. return f.fileData[name], nil
  29. })
  30. f.IDReturns(id)
  31. f.CloseCalls(func(err error) {
  32. f.closeOnce.Do(func() {
  33. close(f.closed)
  34. })
  35. model.Closed(id, err)
  36. f.ClosedReturns(f.closed)
  37. })
  38. return f
  39. }
  40. type fakeConnection struct {
  41. *protocolmocks.Connection
  42. id protocol.DeviceID
  43. downloadProgressMessages []downloadProgressMessage
  44. files []protocol.FileInfo
  45. fileData map[string][]byte
  46. folder string
  47. model Model
  48. closed chan struct{}
  49. closeOnce sync.Once
  50. mut sync.Mutex
  51. }
  52. func (f *fakeConnection) setIndexFn(fn func(_ context.Context, folder string, fs []protocol.FileInfo) error) {
  53. f.IndexCalls(fn)
  54. f.IndexUpdateCalls(fn)
  55. }
  56. func (f *fakeConnection) DownloadProgress(_ context.Context, folder string, updates []protocol.FileDownloadProgressUpdate) {
  57. f.downloadProgressMessages = append(f.downloadProgressMessages, downloadProgressMessage{
  58. folder: folder,
  59. updates: updates,
  60. })
  61. }
  62. func (f *fakeConnection) addFileLocked(name string, flags uint32, ftype protocol.FileInfoType, data []byte, version protocol.Vector, localFlags uint32) {
  63. blockSize := protocol.BlockSize(int64(len(data)))
  64. blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), blockSize, int64(len(data)), nil, true)
  65. file := protocol.FileInfo{
  66. Name: name,
  67. Type: ftype,
  68. Version: version,
  69. Sequence: time.Now().UnixNano(),
  70. LocalFlags: localFlags,
  71. }
  72. switch ftype {
  73. case protocol.FileInfoTypeFile, protocol.FileInfoTypeDirectory:
  74. file.ModifiedS = time.Now().Unix()
  75. file.Permissions = flags
  76. if ftype == protocol.FileInfoTypeFile {
  77. file.Size = int64(len(data))
  78. file.RawBlockSize = blockSize
  79. file.Blocks = blocks
  80. }
  81. default: // Symlink
  82. file.Name = name
  83. file.Type = ftype
  84. file.Version = version
  85. file.SymlinkTarget = string(data)
  86. file.NoPermissions = true
  87. }
  88. f.files = append(f.files, file)
  89. if f.fileData == nil {
  90. f.fileData = make(map[string][]byte)
  91. }
  92. f.fileData[name] = data
  93. }
  94. func (f *fakeConnection) addFileWithLocalFlags(name string, ftype protocol.FileInfoType, localFlags uint32) {
  95. f.mut.Lock()
  96. defer f.mut.Unlock()
  97. var version protocol.Vector
  98. version = version.Update(f.id.Short())
  99. f.addFileLocked(name, 0, ftype, nil, version, localFlags)
  100. }
  101. func (f *fakeConnection) addFile(name string, flags uint32, ftype protocol.FileInfoType, data []byte) {
  102. f.mut.Lock()
  103. defer f.mut.Unlock()
  104. var version protocol.Vector
  105. version = version.Update(f.id.Short())
  106. f.addFileLocked(name, flags, ftype, data, version, 0)
  107. }
  108. func (f *fakeConnection) updateFile(name string, flags uint32, ftype protocol.FileInfoType, data []byte) {
  109. f.mut.Lock()
  110. defer f.mut.Unlock()
  111. for i, fi := range f.files {
  112. if fi.Name == name {
  113. f.files = append(f.files[:i], f.files[i+1:]...)
  114. f.addFileLocked(name, flags, ftype, data, fi.Version.Update(f.id.Short()), 0)
  115. return
  116. }
  117. }
  118. }
  119. func (f *fakeConnection) deleteFile(name string) {
  120. f.mut.Lock()
  121. defer f.mut.Unlock()
  122. for i, fi := range f.files {
  123. if fi.Name == name {
  124. fi.Deleted = true
  125. fi.ModifiedS = time.Now().Unix()
  126. fi.Version = fi.Version.Update(f.id.Short())
  127. fi.Sequence = time.Now().UnixNano()
  128. fi.Blocks = nil
  129. f.files = append(append(f.files[:i], f.files[i+1:]...), fi)
  130. return
  131. }
  132. }
  133. }
  134. func (f *fakeConnection) sendIndexUpdate() {
  135. toSend := make([]protocol.FileInfo, len(f.files))
  136. for i := range f.files {
  137. toSend[i] = prepareFileInfoForIndex(f.files[i])
  138. }
  139. f.model.IndexUpdate(f.id, f.folder, toSend)
  140. }
  141. func addFakeConn(m *testModel, dev protocol.DeviceID, folderID string) *fakeConnection {
  142. fc := newFakeConnection(dev, m)
  143. fc.folder = folderID
  144. m.AddConnection(fc, protocol.Hello{})
  145. m.ClusterConfig(dev, protocol.ClusterConfig{
  146. Folders: []protocol.Folder{
  147. {
  148. ID: folderID,
  149. Devices: []protocol.Device{
  150. {ID: myID},
  151. {ID: dev},
  152. },
  153. },
  154. },
  155. })
  156. return fc
  157. }