all: Refactor the protocol/model interface a bit (ref #8981) (#9007)

This commit is contained in:
Jakob Borg
2023-07-29 10:24:44 +02:00
committed by GitHub
parent b806026990
commit 9d21b91124
21 changed files with 431 additions and 359 deletions
+4 -4
View File
@@ -32,12 +32,12 @@ func newFakeConnection(id protocol.DeviceID, model Model) *fakeConnection {
f.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
return f.fileData[name], nil
})
f.IDReturns(id)
f.DeviceIDReturns(id)
f.CloseCalls(func(err error) {
f.closeOnce.Do(func() {
close(f.closed)
})
model.Closed(id, err)
model.Closed(f, err)
f.ClosedReturns(f.closed)
})
return f
@@ -157,7 +157,7 @@ func (f *fakeConnection) sendIndexUpdate() {
for i := range f.files {
toSend[i] = prepareFileInfoForIndex(f.files[i])
}
f.model.IndexUpdate(f.id, f.folder, toSend)
f.model.IndexUpdate(f, f.folder, toSend)
}
func addFakeConn(m *testModel, dev protocol.DeviceID, folderID string) *fakeConnection {
@@ -165,7 +165,7 @@ func addFakeConn(m *testModel, dev protocol.DeviceID, folderID string) *fakeConn
fc.folder = folderID
m.AddConnection(fc, protocol.Hello{})
m.ClusterConfig(dev, protocol.ClusterConfig{
m.ClusterConfig(fc, protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: folderID,
+13 -13
View File
@@ -30,7 +30,7 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
defer wcfgCancel()
ffs := f.Filesystem(nil)
defer cleanupModel(m)
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
// Create some test data
@@ -45,7 +45,7 @@ func TestRecvOnlyRevertDeletes(t *testing.T) {
// Send and index update for the known stuff
must(t, m.Index(device1, "ro", knownFiles))
must(t, m.Index(conn, "ro", knownFiles))
f.updateLocalsFromScanning(knownFiles)
size := globalSize(t, m, "ro")
@@ -112,7 +112,7 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
defer wcfgCancel()
ffs := f.Filesystem(nil)
defer cleanupModel(m)
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
// Create some test data
@@ -122,7 +122,7 @@ func TestRecvOnlyRevertNeeds(t *testing.T) {
// Send and index update for the known stuff
must(t, m.Index(device1, "ro", knownFiles))
must(t, m.Index(conn, "ro", knownFiles))
f.updateLocalsFromScanning(knownFiles)
// Scan the folder.
@@ -202,7 +202,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
defer wcfgCancel()
ffs := f.Filesystem(nil)
defer cleanupModel(m)
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
// Create some test data
@@ -212,7 +212,7 @@ func TestRecvOnlyUndoChanges(t *testing.T) {
// Send an index update for the known stuff
must(t, m.Index(device1, "ro", knownFiles))
must(t, m.Index(conn, "ro", knownFiles))
f.updateLocalsFromScanning(knownFiles)
// Scan the folder.
@@ -272,7 +272,7 @@ func TestRecvOnlyDeletedRemoteDrop(t *testing.T) {
defer wcfgCancel()
ffs := f.Filesystem(nil)
defer cleanupModel(m)
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
// Create some test data
@@ -282,7 +282,7 @@ func TestRecvOnlyDeletedRemoteDrop(t *testing.T) {
// Send an index update for the known stuff
must(t, m.Index(device1, "ro", knownFiles))
must(t, m.Index(conn, "ro", knownFiles))
f.updateLocalsFromScanning(knownFiles)
// Scan the folder.
@@ -337,7 +337,7 @@ func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
defer wcfgCancel()
ffs := f.Filesystem(nil)
defer cleanupModel(m)
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
// Create some test data
@@ -347,7 +347,7 @@ func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
// Send an index update for the known stuff
must(t, m.Index(device1, "ro", knownFiles))
must(t, m.Index(conn, "ro", knownFiles))
f.updateLocalsFromScanning(knownFiles)
// Scan the folder.
@@ -402,7 +402,7 @@ func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
return true
})
snap.Release()
must(t, m.IndexUpdate(device1, "ro", files))
must(t, m.IndexUpdate(conn, "ro", files))
// Ensure the pull to resolve conflicts (content identical) happened
must(t, f.doInSync(func() error {
@@ -427,7 +427,7 @@ func TestRecvOnlyRevertOwnID(t *testing.T) {
defer wcfgCancel()
ffs := f.Filesystem(nil)
defer cleanupModel(m)
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
// Create some test data
@@ -470,7 +470,7 @@ func TestRecvOnlyRevertOwnID(t *testing.T) {
}()
// Receive an index update with an older version, but valid and then revert
must(t, m.Index(device1, f.ID, []protocol.FileInfo{fi}))
must(t, m.Index(conn, f.ID, []protocol.FileInfo{fi}))
f.Revert()
select {
+2 -2
View File
@@ -1278,7 +1278,7 @@ func TestPullSymlinkOverExistingWindows(t *testing.T) {
m, f, wcfgCancel := setupSendReceiveFolder(t)
defer wcfgCancel()
addFakeConn(m, device1, f.ID)
conn := addFakeConn(m, device1, f.ID)
name := "foo"
if fd, err := f.mtimefs.Create(name); err != nil {
@@ -1296,7 +1296,7 @@ func TestPullSymlinkOverExistingWindows(t *testing.T) {
if !ok {
t.Fatal("file missing")
}
must(t, m.Index(device1, f.ID, []protocol.FileInfo{{Name: name, Type: protocol.FileInfoTypeSymlink, Version: file.Version.Update(device1.Short())}}))
must(t, m.Index(conn, f.ID, []protocol.FileInfo{{Name: name, Type: protocol.FileInfoTypeSymlink, Version: file.Version.Update(device1.Short())}}))
scanChan := make(chan string)
+30 -30
View File
@@ -56,10 +56,10 @@ func newIndexHandler(conn protocol.Connection, downloads *deviceDownloadState, f
// the IndexID, or something else weird has
// happened. We send a full index to reset the
// situation.
l.Infof("Device %v folder %s is delta index compatible, but seems out of sync with reality", conn.ID().Short(), folder.Description())
l.Infof("Device %v folder %s is delta index compatible, but seems out of sync with reality", conn.DeviceID().Short(), folder.Description())
startSequence = 0
} else {
l.Debugf("Device %v folder %s is delta index compatible (mlv=%d)", conn.ID().Short(), folder.Description(), startInfo.local.MaxSequence)
l.Debugf("Device %v folder %s is delta index compatible (mlv=%d)", conn.DeviceID().Short(), folder.Description(), startInfo.local.MaxSequence)
startSequence = startInfo.local.MaxSequence
}
} else if startInfo.local.IndexID != 0 {
@@ -67,10 +67,10 @@ func newIndexHandler(conn protocol.Connection, downloads *deviceDownloadState, f
// not the right one. Either they are confused or we
// must have reset our database since last talking to
// them. We'll start with a full index transfer.
l.Infof("Device %v folder %s has mismatching index ID for us (%v != %v)", conn.ID().Short(), folder.Description(), startInfo.local.IndexID, myIndexID)
l.Infof("Device %v folder %s has mismatching index ID for us (%v != %v)", conn.DeviceID().Short(), folder.Description(), startInfo.local.IndexID, myIndexID)
startSequence = 0
} else {
l.Debugf("Device %v folder %s has no index ID for us", conn.ID().Short(), folder.Description())
l.Debugf("Device %v folder %s has no index ID for us", conn.DeviceID().Short(), folder.Description())
}
// This is the other side's description of themselves. We
@@ -78,23 +78,23 @@ func newIndexHandler(conn protocol.Connection, downloads *deviceDownloadState, f
// otherwise we drop our old index data and expect to get a
// completely new set.
theirIndexID := fset.IndexID(conn.ID())
theirIndexID := fset.IndexID(conn.DeviceID())
if startInfo.remote.IndexID == 0 {
// They're not announcing an index ID. This means they
// do not support delta indexes and we should clear any
// information we have from them before accepting their
// index, which will presumably be a full index.
l.Debugf("Device %v folder %s does not announce an index ID", conn.ID().Short(), folder.Description())
fset.Drop(conn.ID())
l.Debugf("Device %v folder %s does not announce an index ID", conn.DeviceID().Short(), folder.Description())
fset.Drop(conn.DeviceID())
} else if startInfo.remote.IndexID != theirIndexID {
// The index ID we have on file is not what they're
// announcing. They must have reset their database and
// will probably send us a full index. We drop any
// information we have and remember this new index ID
// instead.
l.Infof("Device %v folder %s has a new index ID (%v)", conn.ID().Short(), folder.Description(), startInfo.remote.IndexID)
fset.Drop(conn.ID())
fset.SetIndexID(conn.ID(), startInfo.remote.IndexID)
l.Infof("Device %v folder %s has a new index ID (%v)", conn.DeviceID().Short(), folder.Description(), startInfo.remote.IndexID)
fset.Drop(conn.DeviceID())
fset.SetIndexID(conn.DeviceID(), startInfo.remote.IndexID)
}
return &indexHandler{
@@ -129,12 +129,12 @@ func (s *indexHandler) waitForFileset(ctx context.Context) (*db.FileSet, error)
}
func (s *indexHandler) Serve(ctx context.Context) (err error) {
l.Debugf("Starting index handler for %s to %s at %s (slv=%d)", s.folder, s.conn.ID(), s.conn, s.prevSequence)
l.Debugf("Starting index handler for %s to %s at %s (slv=%d)", s.folder, s.conn.DeviceID().Short(), s.conn, s.prevSequence)
stop := make(chan struct{})
defer func() {
err = svcutil.NoRestartErr(err)
l.Debugf("Exiting index handler for %s to %s at %s: %v", s.folder, s.conn.ID(), s.conn, err)
l.Debugf("Exiting index handler for %s to %s at %s: %v", s.folder, s.conn.DeviceID().Short(), s.conn, err)
close(stop)
}()
@@ -308,7 +308,7 @@ func (s *indexHandler) sendIndexTo(ctx context.Context, fset *db.FileSet) error
}
func (s *indexHandler) receive(fs []protocol.FileInfo, update bool, op string) error {
deviceID := s.conn.ID()
deviceID := s.conn.DeviceID()
s.cond.L.Lock()
paused := s.paused
@@ -369,7 +369,7 @@ func prepareFileInfoForIndex(f protocol.FileInfo) protocol.FileInfo {
}
func (s *indexHandler) String() string {
return fmt.Sprintf("indexHandler@%p for %s to %s at %s", s, s.folder, s.conn.ID().Short(), s.conn)
return fmt.Sprintf("indexHandler@%p for %s to %s at %s", s, s.folder, s.conn.DeviceID().Short(), s.conn)
}
type indexHandlerRegistry struct {
@@ -414,7 +414,7 @@ func newIndexHandlerRegistry(conn protocol.Connection, downloads *deviceDownload
}
func (r *indexHandlerRegistry) String() string {
return fmt.Sprintf("indexHandlerRegistry/%v", r.conn.ID().Short())
return fmt.Sprintf("indexHandlerRegistry/%v", r.conn.DeviceID().Short())
}
func (r *indexHandlerRegistry) GetSupervisor() *suture.Supervisor {
@@ -447,11 +447,11 @@ func (r *indexHandlerRegistry) AddIndexInfo(folder string, startInfo *clusterCon
if is, ok := r.indexHandlers[folder]; ok {
r.sup.RemoveAndWait(is.token, 0)
delete(r.indexHandlers, folder)
l.Debugf("Removed index sender for device %v and folder %v due to added pending", r.conn.ID().Short(), folder)
l.Debugf("Removed index sender for device %v and folder %v due to added pending", r.conn.DeviceID().Short(), folder)
}
folderState, ok := r.folderStates[folder]
if !ok {
l.Debugf("Pending index handler for device %v and folder %v", r.conn.ID().Short(), folder)
l.Debugf("Pending index handler for device %v and folder %v", r.conn.DeviceID().Short(), folder)
r.startInfos[folder] = startInfo
return
}
@@ -464,13 +464,13 @@ func (r *indexHandlerRegistry) Remove(folder string) {
r.mut.Lock()
defer r.mut.Unlock()
l.Debugf("Removing index handler for device %v and folder %v", r.conn.ID().Short(), folder)
l.Debugf("Removing index handler for device %v and folder %v", r.conn.DeviceID().Short(), folder)
if is, ok := r.indexHandlers[folder]; ok {
r.sup.RemoveAndWait(is.token, 0)
delete(r.indexHandlers, folder)
}
delete(r.startInfos, folder)
l.Debugf("Removed index handler for device %v and folder %v", r.conn.ID().Short(), folder)
l.Debugf("Removed index handler for device %v and folder %v", r.conn.DeviceID().Short(), folder)
}
// RemoveAllExcept stops all running index handlers and removes those pending to be started,
@@ -484,13 +484,13 @@ func (r *indexHandlerRegistry) RemoveAllExcept(except map[string]remoteFolderSta
if _, ok := except[folder]; !ok {
r.sup.RemoveAndWait(is.token, 0)
delete(r.indexHandlers, folder)
l.Debugf("Removed index handler for device %v and folder %v (removeAllExcept)", r.conn.ID().Short(), folder)
l.Debugf("Removed index handler for device %v and folder %v (removeAllExcept)", r.conn.DeviceID().Short(), folder)
}
}
for folder := range r.startInfos {
if _, ok := except[folder]; !ok {
delete(r.startInfos, folder)
l.Debugf("Removed pending index handler for device %v and folder %v (removeAllExcept)", r.conn.ID().Short(), folder)
l.Debugf("Removed pending index handler for device %v and folder %v (removeAllExcept)", r.conn.DeviceID().Short(), folder)
}
}
}
@@ -499,7 +499,7 @@ func (r *indexHandlerRegistry) RemoveAllExcept(except map[string]remoteFolderSta
// changes. The exception being if the folder is removed entirely, then call
// Remove. The fset and runner arguments may be nil, if given folder is paused.
func (r *indexHandlerRegistry) RegisterFolderState(folder config.FolderConfiguration, fset *db.FileSet, runner service) {
if !folder.SharedWith(r.conn.ID()) {
if !folder.SharedWith(r.conn.DeviceID()) {
r.Remove(folder.ID)
return
}
@@ -516,13 +516,13 @@ func (r *indexHandlerRegistry) RegisterFolderState(folder config.FolderConfigura
// folderPausedLocked stops a running index handler.
// It is a noop if the folder isn't known or has not been started yet.
func (r *indexHandlerRegistry) folderPausedLocked(folder string) {
l.Debugf("Pausing index handler for device %v and folder %v", r.conn.ID().Short(), folder)
l.Debugf("Pausing index handler for device %v and folder %v", r.conn.DeviceID().Short(), folder)
delete(r.folderStates, folder)
if is, ok := r.indexHandlers[folder]; ok {
is.pause()
l.Debugf("Paused index handler for device %v and folder %v", r.conn.ID().Short(), folder)
l.Debugf("Paused index handler for device %v and folder %v", r.conn.DeviceID().Short(), folder)
} else {
l.Debugf("No index handler for device %v and folder %v to pause", r.conn.ID().Short(), folder)
l.Debugf("No index handler for device %v and folder %v to pause", r.conn.DeviceID().Short(), folder)
}
}
@@ -541,16 +541,16 @@ func (r *indexHandlerRegistry) folderRunningLocked(folder config.FolderConfigura
if isOk {
r.sup.RemoveAndWait(is.token, 0)
delete(r.indexHandlers, folder.ID)
l.Debugf("Removed index handler for device %v and folder %v in resume", r.conn.ID().Short(), folder.ID)
l.Debugf("Removed index handler for device %v and folder %v in resume", r.conn.DeviceID().Short(), folder.ID)
}
r.startLocked(folder, fset, runner, info)
delete(r.startInfos, folder.ID)
l.Debugf("Started index handler for device %v and folder %v in resume", r.conn.ID().Short(), folder.ID)
l.Debugf("Started index handler for device %v and folder %v in resume", r.conn.DeviceID().Short(), folder.ID)
} else if isOk {
l.Debugf("Resuming index handler for device %v and folder %v", r.conn.ID().Short(), folder)
l.Debugf("Resuming index handler for device %v and folder %v", r.conn.DeviceID().Short(), folder)
is.resume(fset, runner)
} else {
l.Debugf("Not resuming index handler for device %v and folder %v as none is paused and there is no start info", r.conn.ID().Short(), folder.ID)
l.Debugf("Not resuming index handler for device %v and folder %v as none is paused and there is no start info", r.conn.DeviceID().Short(), folder.ID)
}
}
@@ -560,7 +560,7 @@ func (r *indexHandlerRegistry) ReceiveIndex(folder string, fs []protocol.FileInf
is, isOk := r.indexHandlers[folder]
if !isOk {
l.Infof("%v for nonexistent or paused folder %q", op, folder)
return ErrFolderMissing
return fmt.Errorf("%s: %w", folder, ErrFolderMissing)
}
return is.receive(fs, update, op)
}
+36 -36
View File
@@ -44,16 +44,16 @@ type Model struct {
arg1 string
arg2 string
}
ClosedStub func(protocol.DeviceID, error)
ClosedStub func(protocol.Connection, error)
closedMutex sync.RWMutex
closedArgsForCall []struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 error
}
ClusterConfigStub func(protocol.DeviceID, protocol.ClusterConfig) error
ClusterConfigStub func(protocol.Connection, protocol.ClusterConfig) error
clusterConfigMutex sync.RWMutex
clusterConfigArgsForCall []struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 protocol.ClusterConfig
}
clusterConfigReturns struct {
@@ -200,10 +200,10 @@ type Model struct {
dismissPendingFolderReturnsOnCall map[int]struct {
result1 error
}
DownloadProgressStub func(protocol.DeviceID, string, []protocol.FileDownloadProgressUpdate) error
DownloadProgressStub func(protocol.Connection, string, []protocol.FileDownloadProgressUpdate) error
downloadProgressMutex sync.RWMutex
downloadProgressArgsForCall []struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 []protocol.FileDownloadProgressUpdate
}
@@ -303,10 +303,10 @@ type Model struct {
result1 []*model.TreeEntry
result2 error
}
IndexStub func(protocol.DeviceID, string, []protocol.FileInfo) error
IndexStub func(protocol.Connection, string, []protocol.FileInfo) error
indexMutex sync.RWMutex
indexArgsForCall []struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 []protocol.FileInfo
}
@@ -316,10 +316,10 @@ type Model struct {
indexReturnsOnCall map[int]struct {
result1 error
}
IndexUpdateStub func(protocol.DeviceID, string, []protocol.FileInfo) error
IndexUpdateStub func(protocol.Connection, string, []protocol.FileInfo) error
indexUpdateMutex sync.RWMutex
indexUpdateArgsForCall []struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 []protocol.FileInfo
}
@@ -447,10 +447,10 @@ type Model struct {
result1 []db.FileInfoTruncated
result2 error
}
RequestStub func(protocol.DeviceID, string, string, int32, int32, int64, []byte, uint32, bool) (protocol.RequestResponse, error)
RequestStub func(protocol.Connection, string, string, int32, int32, int64, []byte, uint32, bool) (protocol.RequestResponse, error)
requestMutex sync.RWMutex
requestArgsForCall []struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 string
arg4 int32
@@ -728,10 +728,10 @@ func (fake *Model) BringToFrontArgsForCall(i int) (string, string) {
return argsForCall.arg1, argsForCall.arg2
}
func (fake *Model) Closed(arg1 protocol.DeviceID, arg2 error) {
func (fake *Model) Closed(arg1 protocol.Connection, arg2 error) {
fake.closedMutex.Lock()
fake.closedArgsForCall = append(fake.closedArgsForCall, struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 error
}{arg1, arg2})
stub := fake.ClosedStub
@@ -748,24 +748,24 @@ func (fake *Model) ClosedCallCount() int {
return len(fake.closedArgsForCall)
}
func (fake *Model) ClosedCalls(stub func(protocol.DeviceID, error)) {
func (fake *Model) ClosedCalls(stub func(protocol.Connection, error)) {
fake.closedMutex.Lock()
defer fake.closedMutex.Unlock()
fake.ClosedStub = stub
}
func (fake *Model) ClosedArgsForCall(i int) (protocol.DeviceID, error) {
func (fake *Model) ClosedArgsForCall(i int) (protocol.Connection, error) {
fake.closedMutex.RLock()
defer fake.closedMutex.RUnlock()
argsForCall := fake.closedArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *Model) ClusterConfig(arg1 protocol.DeviceID, arg2 protocol.ClusterConfig) error {
func (fake *Model) ClusterConfig(arg1 protocol.Connection, arg2 protocol.ClusterConfig) error {
fake.clusterConfigMutex.Lock()
ret, specificReturn := fake.clusterConfigReturnsOnCall[len(fake.clusterConfigArgsForCall)]
fake.clusterConfigArgsForCall = append(fake.clusterConfigArgsForCall, struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 protocol.ClusterConfig
}{arg1, arg2})
stub := fake.ClusterConfigStub
@@ -787,13 +787,13 @@ func (fake *Model) ClusterConfigCallCount() int {
return len(fake.clusterConfigArgsForCall)
}
func (fake *Model) ClusterConfigCalls(stub func(protocol.DeviceID, protocol.ClusterConfig) error) {
func (fake *Model) ClusterConfigCalls(stub func(protocol.Connection, protocol.ClusterConfig) error) {
fake.clusterConfigMutex.Lock()
defer fake.clusterConfigMutex.Unlock()
fake.ClusterConfigStub = stub
}
func (fake *Model) ClusterConfigArgsForCall(i int) (protocol.DeviceID, protocol.ClusterConfig) {
func (fake *Model) ClusterConfigArgsForCall(i int) (protocol.Connection, protocol.ClusterConfig) {
fake.clusterConfigMutex.RLock()
defer fake.clusterConfigMutex.RUnlock()
argsForCall := fake.clusterConfigArgsForCall[i]
@@ -1484,7 +1484,7 @@ func (fake *Model) DismissPendingFolderReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *Model) DownloadProgress(arg1 protocol.DeviceID, arg2 string, arg3 []protocol.FileDownloadProgressUpdate) error {
func (fake *Model) DownloadProgress(arg1 protocol.Connection, arg2 string, arg3 []protocol.FileDownloadProgressUpdate) error {
var arg3Copy []protocol.FileDownloadProgressUpdate
if arg3 != nil {
arg3Copy = make([]protocol.FileDownloadProgressUpdate, len(arg3))
@@ -1493,7 +1493,7 @@ func (fake *Model) DownloadProgress(arg1 protocol.DeviceID, arg2 string, arg3 []
fake.downloadProgressMutex.Lock()
ret, specificReturn := fake.downloadProgressReturnsOnCall[len(fake.downloadProgressArgsForCall)]
fake.downloadProgressArgsForCall = append(fake.downloadProgressArgsForCall, struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 []protocol.FileDownloadProgressUpdate
}{arg1, arg2, arg3Copy})
@@ -1516,13 +1516,13 @@ func (fake *Model) DownloadProgressCallCount() int {
return len(fake.downloadProgressArgsForCall)
}
func (fake *Model) DownloadProgressCalls(stub func(protocol.DeviceID, string, []protocol.FileDownloadProgressUpdate) error) {
func (fake *Model) DownloadProgressCalls(stub func(protocol.Connection, string, []protocol.FileDownloadProgressUpdate) error) {
fake.downloadProgressMutex.Lock()
defer fake.downloadProgressMutex.Unlock()
fake.DownloadProgressStub = stub
}
func (fake *Model) DownloadProgressArgsForCall(i int) (protocol.DeviceID, string, []protocol.FileDownloadProgressUpdate) {
func (fake *Model) DownloadProgressArgsForCall(i int) (protocol.Connection, string, []protocol.FileDownloadProgressUpdate) {
fake.downloadProgressMutex.RLock()
defer fake.downloadProgressMutex.RUnlock()
argsForCall := fake.downloadProgressArgsForCall[i]
@@ -1990,7 +1990,7 @@ func (fake *Model) GlobalDirectoryTreeReturnsOnCall(i int, result1 []*model.Tree
}{result1, result2}
}
func (fake *Model) Index(arg1 protocol.DeviceID, arg2 string, arg3 []protocol.FileInfo) error {
func (fake *Model) Index(arg1 protocol.Connection, arg2 string, arg3 []protocol.FileInfo) error {
var arg3Copy []protocol.FileInfo
if arg3 != nil {
arg3Copy = make([]protocol.FileInfo, len(arg3))
@@ -1999,7 +1999,7 @@ func (fake *Model) Index(arg1 protocol.DeviceID, arg2 string, arg3 []protocol.Fi
fake.indexMutex.Lock()
ret, specificReturn := fake.indexReturnsOnCall[len(fake.indexArgsForCall)]
fake.indexArgsForCall = append(fake.indexArgsForCall, struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 []protocol.FileInfo
}{arg1, arg2, arg3Copy})
@@ -2022,13 +2022,13 @@ func (fake *Model) IndexCallCount() int {
return len(fake.indexArgsForCall)
}
func (fake *Model) IndexCalls(stub func(protocol.DeviceID, string, []protocol.FileInfo) error) {
func (fake *Model) IndexCalls(stub func(protocol.Connection, string, []protocol.FileInfo) error) {
fake.indexMutex.Lock()
defer fake.indexMutex.Unlock()
fake.IndexStub = stub
}
func (fake *Model) IndexArgsForCall(i int) (protocol.DeviceID, string, []protocol.FileInfo) {
func (fake *Model) IndexArgsForCall(i int) (protocol.Connection, string, []protocol.FileInfo) {
fake.indexMutex.RLock()
defer fake.indexMutex.RUnlock()
argsForCall := fake.indexArgsForCall[i]
@@ -2058,7 +2058,7 @@ func (fake *Model) IndexReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *Model) IndexUpdate(arg1 protocol.DeviceID, arg2 string, arg3 []protocol.FileInfo) error {
func (fake *Model) IndexUpdate(arg1 protocol.Connection, arg2 string, arg3 []protocol.FileInfo) error {
var arg3Copy []protocol.FileInfo
if arg3 != nil {
arg3Copy = make([]protocol.FileInfo, len(arg3))
@@ -2067,7 +2067,7 @@ func (fake *Model) IndexUpdate(arg1 protocol.DeviceID, arg2 string, arg3 []proto
fake.indexUpdateMutex.Lock()
ret, specificReturn := fake.indexUpdateReturnsOnCall[len(fake.indexUpdateArgsForCall)]
fake.indexUpdateArgsForCall = append(fake.indexUpdateArgsForCall, struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 []protocol.FileInfo
}{arg1, arg2, arg3Copy})
@@ -2090,13 +2090,13 @@ func (fake *Model) IndexUpdateCallCount() int {
return len(fake.indexUpdateArgsForCall)
}
func (fake *Model) IndexUpdateCalls(stub func(protocol.DeviceID, string, []protocol.FileInfo) error) {
func (fake *Model) IndexUpdateCalls(stub func(protocol.Connection, string, []protocol.FileInfo) error) {
fake.indexUpdateMutex.Lock()
defer fake.indexUpdateMutex.Unlock()
fake.IndexUpdateStub = stub
}
func (fake *Model) IndexUpdateArgsForCall(i int) (protocol.DeviceID, string, []protocol.FileInfo) {
func (fake *Model) IndexUpdateArgsForCall(i int) (protocol.Connection, string, []protocol.FileInfo) {
fake.indexUpdateMutex.RLock()
defer fake.indexUpdateMutex.RUnlock()
argsForCall := fake.indexUpdateArgsForCall[i]
@@ -2666,7 +2666,7 @@ func (fake *Model) RemoteNeedFolderFilesReturnsOnCall(i int, result1 []db.FileIn
}{result1, result2}
}
func (fake *Model) Request(arg1 protocol.DeviceID, arg2 string, arg3 string, arg4 int32, arg5 int32, arg6 int64, arg7 []byte, arg8 uint32, arg9 bool) (protocol.RequestResponse, error) {
func (fake *Model) Request(arg1 protocol.Connection, arg2 string, arg3 string, arg4 int32, arg5 int32, arg6 int64, arg7 []byte, arg8 uint32, arg9 bool) (protocol.RequestResponse, error) {
var arg7Copy []byte
if arg7 != nil {
arg7Copy = make([]byte, len(arg7))
@@ -2675,7 +2675,7 @@ func (fake *Model) Request(arg1 protocol.DeviceID, arg2 string, arg3 string, arg
fake.requestMutex.Lock()
ret, specificReturn := fake.requestReturnsOnCall[len(fake.requestArgsForCall)]
fake.requestArgsForCall = append(fake.requestArgsForCall, struct {
arg1 protocol.DeviceID
arg1 protocol.Connection
arg2 string
arg3 string
arg4 int32
@@ -2704,13 +2704,13 @@ func (fake *Model) RequestCallCount() int {
return len(fake.requestArgsForCall)
}
func (fake *Model) RequestCalls(stub func(protocol.DeviceID, string, string, int32, int32, int64, []byte, uint32, bool) (protocol.RequestResponse, error)) {
func (fake *Model) RequestCalls(stub func(protocol.Connection, string, string, int32, int32, int64, []byte, uint32, bool) (protocol.RequestResponse, error)) {
fake.requestMutex.Lock()
defer fake.requestMutex.Unlock()
fake.RequestStub = stub
}
func (fake *Model) RequestArgsForCall(i int) (protocol.DeviceID, string, string, int32, int32, int64, []byte, uint32, bool) {
func (fake *Model) RequestArgsForCall(i int) (protocol.Connection, string, string, int32, int32, int64, []byte, uint32, bool) {
fake.requestMutex.RLock()
defer fake.requestMutex.RUnlock()
argsForCall := fake.requestArgsForCall[i]
+18 -12
View File
@@ -847,7 +847,7 @@ func (comp *FolderCompletion) setComplectionPct() {
}
// Map returns the members as a map, e.g. used in api to serialize as JSON.
func (comp FolderCompletion) Map() map[string]interface{} {
func (comp *FolderCompletion) Map() map[string]interface{} {
return map[string]interface{}{
"completion": comp.CompletionPct,
"globalBytes": comp.GlobalBytes,
@@ -1110,22 +1110,23 @@ func (p *pager) done() bool {
// Index is called when a new device is connected and we receive their full index.
// Implements the protocol.Model interface.
func (m *model) Index(deviceID protocol.DeviceID, folder string, fs []protocol.FileInfo) error {
return m.handleIndex(deviceID, folder, fs, false)
func (m *model) Index(conn protocol.Connection, folder string, fs []protocol.FileInfo) error {
return m.handleIndex(conn, folder, fs, false)
}
// IndexUpdate is called for incremental updates to connected devices' indexes.
// Implements the protocol.Model interface.
func (m *model) IndexUpdate(deviceID protocol.DeviceID, folder string, fs []protocol.FileInfo) error {
return m.handleIndex(deviceID, folder, fs, true)
func (m *model) IndexUpdate(conn protocol.Connection, folder string, fs []protocol.FileInfo) error {
return m.handleIndex(conn, folder, fs, true)
}
func (m *model) handleIndex(deviceID protocol.DeviceID, folder string, fs []protocol.FileInfo, update bool) error {
func (m *model) handleIndex(conn protocol.Connection, folder string, fs []protocol.FileInfo, update bool) error {
op := "Index"
if update {
op += " update"
}
deviceID := conn.DeviceID()
l.Debugf("%v (in): %s / %q: %d files", op, deviceID, folder, len(fs))
if cfg, ok := m.cfg.Folder(folder); !ok || !cfg.SharedWith(deviceID) {
@@ -1159,12 +1160,13 @@ type ClusterConfigReceivedEventData struct {
Device protocol.DeviceID `json:"device"`
}
func (m *model) ClusterConfig(deviceID protocol.DeviceID, cm protocol.ClusterConfig) error {
func (m *model) ClusterConfig(conn protocol.Connection, cm protocol.ClusterConfig) error {
// Check the peer device's announced folders against our own. Emits events
// for folders that we don't expect (unknown or not shared).
// Also, collect a list of folders we do share, and if he's interested in
// temporary indexes, subscribe the connection.
deviceID := conn.DeviceID()
l.Debugf("Handling ClusterConfig from %v", deviceID.Short())
m.pmut.RLock()
@@ -1544,7 +1546,7 @@ func (m *model) sendClusterConfig(ids []protocol.DeviceID) {
m.pmut.RUnlock()
// Generating cluster-configs acquires fmut -> must happen outside of pmut.
for _, conn := range ccConns {
cm, passwords := m.generateClusterConfig(conn.ID())
cm, passwords := m.generateClusterConfig(conn.DeviceID())
conn.SetFolderPasswords(passwords)
go conn.ClusterConfig(cm)
}
@@ -1774,7 +1776,8 @@ func (m *model) introduceDevice(device protocol.Device, introducerCfg config.Dev
}
// Closed is called when a connection has been closed
func (m *model) Closed(device protocol.DeviceID, err error) {
func (m *model) Closed(conn protocol.Connection, err error) {
device := conn.DeviceID()
m.pmut.Lock()
conn, ok := m.conn[device]
if !ok {
@@ -1834,11 +1837,13 @@ func (r *requestResponse) Wait() {
// Request returns the specified data segment by reading it from local disk.
// Implements the protocol.Model interface.
func (m *model) Request(deviceID protocol.DeviceID, folder, name string, _, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (out protocol.RequestResponse, err error) {
func (m *model) Request(conn protocol.Connection, folder, name string, _, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (out protocol.RequestResponse, err error) {
if size < 0 || offset < 0 {
return nil, protocol.ErrInvalid
}
deviceID := conn.DeviceID()
m.fmut.RLock()
folderCfg, ok := m.folderCfgs[folder]
folderIgnores := m.folderIgnores[folder]
@@ -2214,7 +2219,7 @@ func (m *model) GetHello(id protocol.DeviceID) protocol.HelloIntf {
// be sent to the connected peer, thereafter index updates whenever the local
// folder changes.
func (m *model) AddConnection(conn protocol.Connection, hello protocol.Hello) {
deviceID := conn.ID()
deviceID := conn.DeviceID()
device, ok := m.cfg.Device(deviceID)
if !ok {
l.Infoln("Trying to add connection to unknown device")
@@ -2303,11 +2308,12 @@ func (m *model) AddConnection(conn protocol.Connection, hello protocol.Hello) {
m.deviceWasSeen(deviceID)
}
func (m *model) DownloadProgress(device protocol.DeviceID, folder string, updates []protocol.FileDownloadProgressUpdate) error {
func (m *model) DownloadProgress(conn protocol.Connection, folder string, updates []protocol.FileDownloadProgressUpdate) error {
m.fmut.RLock()
cfg, ok := m.folderCfgs[folder]
m.fmut.RUnlock()
device := conn.DeviceID()
if !ok || cfg.DisableTempIndexes || !cfg.SharedWith(device) {
return nil
}
+81 -74
View File
@@ -94,7 +94,7 @@ func TestRequest(t *testing.T) {
m.ScanFolder("default")
// Existing, shared file
res, err := m.Request(device1, "default", "foo", 0, 6, 0, nil, 0, false)
res, err := m.Request(device1Conn, "default", "foo", 0, 6, 0, nil, 0, false)
if err != nil {
t.Fatal(err)
}
@@ -104,35 +104,35 @@ func TestRequest(t *testing.T) {
}
// Existing, nonshared file
_, err = m.Request(device2, "default", "foo", 0, 6, 0, nil, 0, false)
_, err = m.Request(device2Conn, "default", "foo", 0, 6, 0, nil, 0, false)
if err == nil {
t.Error("Unexpected nil error on insecure file read")
}
// Nonexistent file
_, err = m.Request(device1, "default", "nonexistent", 0, 6, 0, nil, 0, false)
_, err = m.Request(device1Conn, "default", "nonexistent", 0, 6, 0, nil, 0, false)
if err == nil {
t.Error("Unexpected nil error on insecure file read")
}
// Shared folder, but disallowed file name
_, err = m.Request(device1, "default", "../walk.go", 0, 6, 0, nil, 0, false)
_, err = m.Request(device1Conn, "default", "../walk.go", 0, 6, 0, nil, 0, false)
if err == nil {
t.Error("Unexpected nil error on insecure file read")
}
// Negative offset
_, err = m.Request(device1, "default", "foo", 0, -4, 0, nil, 0, false)
_, err = m.Request(device1Conn, "default", "foo", 0, -4, 0, nil, 0, false)
if err == nil {
t.Error("Unexpected nil error on insecure file read")
}
// Larger block than available
_, err = m.Request(device1, "default", "foo", 0, 42, 0, []byte("hash necessary but not checked"), 0, false)
_, err = m.Request(device1Conn, "default", "foo", 0, 42, 0, []byte("hash necessary but not checked"), 0, false)
if err == nil {
t.Error("Unexpected nil error on read past end of file")
}
_, err = m.Request(device1, "default", "foo", 0, 42, 0, nil, 0, false)
_, err = m.Request(device1Conn, "default", "foo", 0, 42, 0, nil, 0, false)
if err != nil {
t.Error("Unexpected error when large read should be permitted")
}
@@ -168,11 +168,11 @@ func benchmarkIndex(b *testing.B, nfiles int) {
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem(nil).URI())
files := genFiles(nfiles)
must(b, m.Index(device1, fcfg.ID, files))
must(b, m.Index(device1Conn, fcfg.ID, files))
b.ResetTimer()
for i := 0; i < b.N; i++ {
must(b, m.Index(device1, fcfg.ID, files))
must(b, m.Index(device1Conn, fcfg.ID, files))
}
b.ReportAllocs()
}
@@ -197,11 +197,11 @@ func benchmarkIndexUpdate(b *testing.B, nfiles, nufiles int) {
files := genFiles(nfiles)
ufiles := genFiles(nufiles)
must(b, m.Index(device1, fcfg.ID, files))
must(b, m.Index(device1Conn, fcfg.ID, files))
b.ResetTimer()
for i := 0; i < b.N; i++ {
must(b, m.IndexUpdate(device1, fcfg.ID, ufiles))
must(b, m.IndexUpdate(device1Conn, fcfg.ID, ufiles))
}
b.ReportAllocs()
}
@@ -218,7 +218,7 @@ func BenchmarkRequestOut(b *testing.B) {
fc.addFile(f.Name, 0o644, protocol.FileInfoTypeFile, []byte("some data to return"))
}
m.AddConnection(fc, protocol.Hello{})
must(b, m.Index(device1, "default", files))
must(b, m.Index(device1Conn, "default", files))
b.ResetTimer()
for i := 0; i < b.N; i++ {
@@ -247,7 +247,7 @@ func BenchmarkRequestInSingleFile(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := m.Request(device1, "default", "request/for/a/file/in/a/couple/of/dirs/128k", 0, 128<<10, 0, nil, 0, false); err != nil {
if _, err := m.Request(device1Conn, "default", "request/for/a/file/in/a/couple/of/dirs/128k", 0, 128<<10, 0, nil, 0, false); err != nil {
b.Error(err)
}
}
@@ -287,7 +287,7 @@ func TestDeviceRename(t *testing.T) {
t.Errorf("Device already has a name")
}
m.Closed(conn.ID(), protocol.ErrTimeout)
m.Closed(conn, protocol.ErrTimeout)
hello.DeviceName = "tester"
m.AddConnection(conn, hello)
@@ -295,7 +295,7 @@ func TestDeviceRename(t *testing.T) {
t.Errorf("Device did not get a name")
}
m.Closed(conn.ID(), protocol.ErrTimeout)
m.Closed(conn, protocol.ErrTimeout)
hello.DeviceName = "tester2"
m.AddConnection(conn, hello)
@@ -317,7 +317,7 @@ func TestDeviceRename(t *testing.T) {
t.Errorf("Device name not saved in config")
}
m.Closed(conn.ID(), protocol.ErrTimeout)
m.Closed(conn, protocol.ErrTimeout)
waiter, err := cfg.Modify(func(cfg *config.Configuration) {
cfg.Options.OverwriteRemoteDevNames = true
@@ -528,7 +528,7 @@ func TestIntroducer(t *testing.T) {
SkipIntroductionRemovals: true,
EncryptionPasswordToken: []byte("faketoken"),
})
m.ClusterConfig(device1, cc)
m.ClusterConfig(device1Conn, cc)
if newDev, ok := m.cfg.Device(device2); !ok || !newDev.Introducer || !newDev.SkipIntroductionRemovals {
t.Error("device 2 missing or wrong flags")
@@ -584,7 +584,7 @@ func TestIntroducer(t *testing.T) {
Introducer: true,
SkipIntroductionRemovals: true,
})
m.ClusterConfig(device1, cc)
m.ClusterConfig(device1Conn, cc)
// Should not get introducer, as it's already unset, and it's an existing device.
if newDev, ok := m.cfg.Device(device2); !ok || newDev.Introducer || newDev.SkipIntroductionRemovals {
@@ -634,7 +634,7 @@ func TestIntroducer(t *testing.T) {
},
},
})
m.ClusterConfig(device1, protocol.ClusterConfig{})
m.ClusterConfig(device1Conn, protocol.ClusterConfig{})
if _, ok := m.cfg.Device(device2); ok {
t.Error("device 2 should have been removed")
@@ -686,7 +686,7 @@ func TestIntroducer(t *testing.T) {
},
},
})
m.ClusterConfig(device1, protocol.ClusterConfig{})
m.ClusterConfig(device1Conn, protocol.ClusterConfig{})
if _, ok := m.cfg.Device(device2); !ok {
t.Error("device 2 should not have been removed")
@@ -743,7 +743,7 @@ func TestIntroducer(t *testing.T) {
Introducer: true,
SkipIntroductionRemovals: true,
})
m.ClusterConfig(device1, cc)
m.ClusterConfig(device1Conn, cc)
if _, ok := m.cfg.Device(device2); !ok {
t.Error("device 2 should not have been removed")
@@ -794,7 +794,7 @@ func TestIntroducer(t *testing.T) {
},
},
})
m.ClusterConfig(device1, protocol.ClusterConfig{})
m.ClusterConfig(device1Conn, protocol.ClusterConfig{})
if _, ok := m.cfg.Device(device2); !ok {
t.Error("device 2 should not have been removed")
@@ -847,7 +847,7 @@ func TestIntroducer(t *testing.T) {
})
defer cleanupModel(m)
defer cancel()
m.ClusterConfig(device1, protocol.ClusterConfig{})
m.ClusterConfig(device1Conn, protocol.ClusterConfig{})
if _, ok := m.cfg.Device(device2); !ok {
t.Error("device 2 should not have been removed")
@@ -906,14 +906,14 @@ func TestIssue5063(t *testing.T) {
for _, c := range m.conn {
conn := c.(*fakeConnection)
conn.CloseCalls(func(_ error) {})
defer m.Closed(c.ID(), errStopped) // to unblock deferred m.Stop()
defer m.Closed(c, errStopped) // to unblock deferred m.Stop()
}
m.pmut.Unlock()
wg := sync.WaitGroup{}
addAndVerify := func(id string) {
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("expected shared", id)
}
@@ -951,7 +951,7 @@ func TestAutoAcceptRejected(t *testing.T) {
// defer cleanupModel(m)
defer cancel()
id := srand.String(8)
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if cfg, ok := m.cfg.Folder(id); ok && cfg.SharedWith(device1) {
t.Error("unexpected shared", id)
@@ -964,7 +964,7 @@ func TestAutoAcceptNewFolder(t *testing.T) {
defer cleanupModel(m)
defer cancel()
id := srand.String(8)
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("expected shared", id)
}
@@ -976,14 +976,14 @@ func TestAutoAcceptNewFolderFromTwoDevices(t *testing.T) {
defer cancel()
id := srand.String(8)
defer os.RemoveAll(id)
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("expected shared", id)
}
if fcfg, ok := m.cfg.Folder(id); !ok || fcfg.SharedWith(device2) {
t.Error("unexpected expected shared", id)
}
m.ClusterConfig(device2, createClusterConfig(device2, id))
m.ClusterConfig(device2Conn, createClusterConfig(device2, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device2) {
t.Error("expected shared", id)
}
@@ -997,14 +997,14 @@ func TestAutoAcceptNewFolderFromOnlyOneDevice(t *testing.T) {
defer os.RemoveAll(id)
defer cleanupModel(m)
defer cancel()
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("expected shared", id)
}
if fcfg, ok := m.cfg.Folder(id); !ok || fcfg.SharedWith(device2) {
t.Error("unexpected expected shared", id)
}
m.ClusterConfig(device2, createClusterConfig(device2, id))
m.ClusterConfig(device2Conn, createClusterConfig(device2, id))
if fcfg, ok := m.cfg.Folder(id); !ok || fcfg.SharedWith(device2) {
t.Error("unexpected shared", id)
}
@@ -1035,10 +1035,10 @@ func TestAutoAcceptNewFolderPremutationsNoPanic(t *testing.T) {
cfg.Folders = append(cfg.Folders, fcfg)
}
m, cancel := newState(t, cfg)
m.ClusterConfig(device1, protocol.ClusterConfig{
m.ClusterConfig(device1Conn, protocol.ClusterConfig{
Folders: []protocol.Folder{dev1folder},
})
m.ClusterConfig(device2, protocol.ClusterConfig{
m.ClusterConfig(device2Conn, protocol.ClusterConfig{
Folders: []protocol.Folder{dev2folder},
})
cleanupModel(m)
@@ -1058,7 +1058,7 @@ func TestAutoAcceptMultipleFolders(t *testing.T) {
m, cancel := newState(t, defaultAutoAcceptCfg)
defer cleanupModel(m)
defer cancel()
m.ClusterConfig(device1, createClusterConfig(device1, id1, id2))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id1, id2))
if fcfg, ok := m.cfg.Folder(id1); !ok || !fcfg.SharedWith(device1) {
t.Error("expected shared", id1)
}
@@ -1086,7 +1086,7 @@ func TestAutoAcceptExistingFolder(t *testing.T) {
if fcfg, ok := m.cfg.Folder(id); !ok || fcfg.SharedWith(device1) {
t.Error("missing folder, or shared", id)
}
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) || fcfg.Path != idOther {
t.Error("missing folder, or unshared, or path changed", id)
@@ -1112,7 +1112,7 @@ func TestAutoAcceptNewAndExistingFolder(t *testing.T) {
if fcfg, ok := m.cfg.Folder(id1); !ok || fcfg.SharedWith(device1) {
t.Error("missing folder, or shared", id1)
}
m.ClusterConfig(device1, createClusterConfig(device1, id1, id2))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id1, id2))
for i, id := range []string{id1, id2} {
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
@@ -1143,7 +1143,7 @@ func TestAutoAcceptAlreadyShared(t *testing.T) {
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("missing folder, or not shared", id)
}
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
if fcfg, ok := m.cfg.Folder(id); !ok || !fcfg.SharedWith(device1) {
t.Error("missing folder, or not shared", id)
@@ -1159,7 +1159,7 @@ func TestAutoAcceptNameConflict(t *testing.T) {
m, cancel := newState(t, defaultAutoAcceptCfg)
defer cleanupModel(m)
defer cancel()
m.ClusterConfig(device1, protocol.ClusterConfig{
m.ClusterConfig(device1Conn, protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: id,
@@ -1179,7 +1179,7 @@ func TestAutoAcceptPrefersLabel(t *testing.T) {
label := srand.String(8)
defer cleanupModel(m)
defer cancel()
m.ClusterConfig(device1, addFolderDevicesToClusterConfig(protocol.ClusterConfig{
m.ClusterConfig(device1Conn, addFolderDevicesToClusterConfig(protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: id,
@@ -1203,7 +1203,7 @@ func TestAutoAcceptFallsBackToID(t *testing.T) {
}
defer cleanupModel(m)
defer cancel()
m.ClusterConfig(device1, addFolderDevicesToClusterConfig(protocol.ClusterConfig{
m.ClusterConfig(device1Conn, addFolderDevicesToClusterConfig(protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: id,
@@ -1245,7 +1245,7 @@ func TestAutoAcceptPausedWhenFolderConfigChanged(t *testing.T) {
t.Fatal("folder running?")
}
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
m.generateClusterConfig(device1)
if fcfg, ok := m.cfg.Folder(id); !ok {
@@ -1294,7 +1294,7 @@ func TestAutoAcceptPausedWhenFolderConfigNotChanged(t *testing.T) {
t.Fatal("folder running?")
}
m.ClusterConfig(device1, createClusterConfig(device1, id))
m.ClusterConfig(device1Conn, createClusterConfig(device1, id))
m.generateClusterConfig(device1)
if fcfg, ok := m.cfg.Folder(id); !ok {
@@ -1338,7 +1338,7 @@ func TestAutoAcceptEnc(t *testing.T) {
// would panic.
clusterConfig := func(deviceID protocol.DeviceID, cm protocol.ClusterConfig) {
m.AddConnection(newFakeConnection(deviceID, m), protocol.Hello{})
m.ClusterConfig(deviceID, cm)
m.ClusterConfig(&protocolmocks.Connection{DeviceIDStub: func() protocol.DeviceID { return deviceID }}, cm)
}
clusterConfig(device1, basicCC())
@@ -1703,7 +1703,7 @@ func TestRWScanRecovery(t *testing.T) {
}
func TestGlobalDirectoryTree(t *testing.T) {
m, _, fcfg, wCancel := setupModelWithConnection(t)
m, conn, fcfg, wCancel := setupModelWithConnection(t)
defer wCancel()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem(nil).URI())
@@ -1807,7 +1807,7 @@ func TestGlobalDirectoryTree(t *testing.T) {
return string(bytes)
}
must(t, m.Index(device1, "default", testdata))
must(t, m.Index(conn, "default", testdata))
result, _ := m.GlobalDirectoryTree("default", "", -1, false)
@@ -2014,7 +2014,7 @@ func benchmarkTree(b *testing.B, n1, n2 int) {
m.ScanFolder(fcfg.ID)
files := genDeepFiles(n1, n2)
must(b, m.Index(device1, fcfg.ID, files))
must(b, m.Index(device1Conn, fcfg.ID, files))
b.ResetTimer()
for i := 0; i < b.N; i++ {
@@ -2160,7 +2160,7 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) {
conn2 := newFakeConnection(device2, m)
m.AddConnection(conn2, protocol.Hello{})
m.ClusterConfig(device1, protocol.ClusterConfig{
m.ClusterConfig(conn1, protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: "default",
@@ -2172,7 +2172,7 @@ func TestSharedWithClearedOnDisconnect(t *testing.T) {
},
},
})
m.ClusterConfig(device2, protocol.ClusterConfig{
m.ClusterConfig(conn2, protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: "default",
@@ -2398,7 +2398,7 @@ func TestCustomMarkerName(t *testing.T) {
}
func TestRemoveDirWithContent(t *testing.T) {
m, _, fcfg, wcfgCancel := setupModelWithConnection(t)
m, conn, fcfg, wcfgCancel := setupModelWithConnection(t)
defer wcfgCancel()
tfs := fcfg.Filesystem(nil)
defer cleanupModelAndRemoveDir(m, tfs.URI())
@@ -2425,7 +2425,7 @@ func TestRemoveDirWithContent(t *testing.T) {
file.Deleted = true
file.Version = file.Version.Update(device1.Short()).Update(device1.Short())
must(t, m.IndexUpdate(device1, fcfg.ID, []protocol.FileInfo{dir, file}))
must(t, m.IndexUpdate(conn, fcfg.ID, []protocol.FileInfo{dir, file}))
// Is there something we could trigger on instead of just waiting?
timeout := time.NewTimer(5 * time.Second)
@@ -2919,19 +2919,19 @@ func TestRequestLimit(t *testing.T) {
})
must(t, err)
waiter.Wait()
m, _ := setupModelWithConnectionFromWrapper(t, wrapper)
m, conn := setupModelWithConnectionFromWrapper(t, wrapper)
defer cleanupModel(m)
m.ScanFolder("default")
befReq := time.Now()
first, err := m.Request(device1, "default", file, 0, 2000, 0, nil, 0, false)
first, err := m.Request(conn, "default", file, 0, 2000, 0, nil, 0, false)
if err != nil {
t.Fatalf("First request failed: %v", err)
}
reqDur := time.Since(befReq)
returned := make(chan struct{})
go func() {
second, err := m.Request(device1, "default", file, 0, 2000, 0, nil, 0, false)
second, err := m.Request(conn, "default", file, 0, 2000, 0, nil, 0, false)
if err != nil {
t.Errorf("Second request failed: %v", err)
}
@@ -2969,12 +2969,16 @@ func TestConnCloseOnRestart(t *testing.T) {
br := &testutils.BlockingRW{}
nw := &testutils.NoopRW{}
m.AddConnection(protocol.NewConnection(device1, br, nw, testutils.NoopCloser{}, m, new(protocolmocks.ConnectionInfo), protocol.CompressionNever, nil, m.keyGen), protocol.Hello{})
ci := &protocolmocks.ConnectionInfo{}
m.AddConnection(protocol.NewConnection(device1, br, nw, testutils.NoopCloser{}, m, ci, protocol.CompressionNever, nil, m.keyGen), protocol.Hello{})
m.pmut.RLock()
if len(m.closed) != 1 {
t.Fatalf("Expected just one conn (len(m.conn) == %v)", len(m.conn))
t.Fatalf("Expected just one conn (len(m.closed) == %v)", len(m.closed))
}
var closed chan struct{}
for _, c := range m.closed {
closed = c
}
closed := m.closed[device1]
m.pmut.RUnlock()
waiter, err := w.RemoveDevice(device1)
@@ -3069,7 +3073,10 @@ func TestDevicePause(t *testing.T) {
defer sub.Unsubscribe()
m.pmut.RLock()
closed := m.closed[device1]
var closed chan struct{}
for _, c := range m.closed {
closed = c
}
m.pmut.RUnlock()
pauseDevice(t, m.cfg, device1, true)
@@ -3567,7 +3574,7 @@ func TestAddFolderCompletion(t *testing.T) {
}
func TestScanDeletedROChangedOnSR(t *testing.T) {
m, _, fcfg, wCancel := setupModelWithConnection(t)
m, conn, fcfg, wCancel := setupModelWithConnection(t)
ffs := fcfg.Filesystem(nil)
defer wCancel()
defer cleanupModelAndRemoveDir(m, ffs.URI())
@@ -3585,7 +3592,7 @@ func TestScanDeletedROChangedOnSR(t *testing.T) {
}
// A remote must have the file, otherwise the deletion below is
// automatically resolved as not a ro-changed item.
must(t, m.IndexUpdate(device1, fcfg.ID, []protocol.FileInfo{file}))
must(t, m.IndexUpdate(conn, fcfg.ID, []protocol.FileInfo{file}))
must(t, ffs.Remove(name))
m.ScanFolders()
@@ -3690,17 +3697,17 @@ func TestIssue6961(t *testing.T) {
}
m.ServeBackground()
defer cleanupModelAndRemoveDir(m, tfs.URI())
addFakeConn(m, device1, fcfg.ID)
addFakeConn(m, device2, fcfg.ID)
conn1 := addFakeConn(m, device1, fcfg.ID)
conn2 := addFakeConn(m, device2, fcfg.ID)
m.ScanFolders()
name := "foo"
version := protocol.Vector{}.Update(device1.Short())
// Remote, valid and existing file
must(t, m.Index(device1, fcfg.ID, []protocol.FileInfo{{Name: name, Version: version, Sequence: 1}}))
must(t, m.Index(conn1, fcfg.ID, []protocol.FileInfo{{Name: name, Version: version, Sequence: 1}}))
// Remote, invalid (receive-only) and existing file
must(t, m.Index(device2, fcfg.ID, []protocol.FileInfo{{Name: name, RawInvalid: true, Sequence: 1}}))
must(t, m.Index(conn2, fcfg.ID, []protocol.FileInfo{{Name: name, RawInvalid: true, Sequence: 1}}))
// Create a local file
if fd, err := tfs.OpenFile(name, fs.OptCreate, 0o666); err != nil {
t.Fatal(err)
@@ -3726,7 +3733,7 @@ func TestIssue6961(t *testing.T) {
m.ScanFolders()
// Drop the remote index, add some other file.
must(t, m.Index(device2, fcfg.ID, []protocol.FileInfo{{Name: "bar", RawInvalid: true, Sequence: 1}}))
must(t, m.Index(conn2, fcfg.ID, []protocol.FileInfo{{Name: "bar", RawInvalid: true, Sequence: 1}}))
// Pause and unpause folder to create new db.FileSet and thus recalculate everything
pauseFolder(t, wcfg, fcfg.ID, true)
@@ -3740,7 +3747,7 @@ func TestIssue6961(t *testing.T) {
}
func TestCompletionEmptyGlobal(t *testing.T) {
m, _, fcfg, wcfgCancel := setupModelWithConnection(t)
m, conn, fcfg, wcfgCancel := setupModelWithConnection(t)
defer wcfgCancel()
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem(nil).URI())
files := []protocol.FileInfo{{Name: "foo", Version: protocol.Vector{}.Update(myID.Short()), Sequence: 1}}
@@ -3749,7 +3756,7 @@ func TestCompletionEmptyGlobal(t *testing.T) {
m.fmut.Unlock()
files[0].Deleted = true
files[0].Version = files[0].Version.Update(device1.Short())
must(t, m.IndexUpdate(device1, fcfg.ID, files))
must(t, m.IndexUpdate(conn, fcfg.ID, files))
comp := m.testCompletion(protocol.LocalDeviceID, fcfg.ID)
if comp.CompletionPct != 95 {
t.Error("Expected completion of 95%, got", comp.CompletionPct)
@@ -3762,34 +3769,34 @@ func TestNeedMetaAfterIndexReset(t *testing.T) {
addDevice2(t, w, fcfg)
m := setupModel(t, w)
defer cleanupModelAndRemoveDir(m, fcfg.Path)
addFakeConn(m, device1, fcfg.ID)
addFakeConn(m, device2, fcfg.ID)
conn1 := addFakeConn(m, device1, fcfg.ID)
conn2 := addFakeConn(m, device2, fcfg.ID)
var seq int64 = 1
files := []protocol.FileInfo{{Name: "foo", Size: 10, Version: protocol.Vector{}.Update(device1.Short()), Sequence: seq}}
// Start with two remotes having one file, then both deleting it, then
// only one adding it again.
must(t, m.Index(device1, fcfg.ID, files))
must(t, m.Index(device2, fcfg.ID, files))
must(t, m.Index(conn1, fcfg.ID, files))
must(t, m.Index(conn2, fcfg.ID, files))
seq++
files[0].SetDeleted(device2.Short())
files[0].Sequence = seq
must(t, m.IndexUpdate(device2, fcfg.ID, files))
must(t, m.IndexUpdate(device1, fcfg.ID, files))
must(t, m.IndexUpdate(conn1, fcfg.ID, files))
must(t, m.IndexUpdate(conn2, fcfg.ID, files))
seq++
files[0].Deleted = false
files[0].Size = 20
files[0].Version = files[0].Version.Update(device1.Short())
files[0].Sequence = seq
must(t, m.IndexUpdate(device1, fcfg.ID, files))
must(t, m.IndexUpdate(conn1, fcfg.ID, files))
if comp := m.testCompletion(device2, fcfg.ID); comp.NeedItems != 1 {
t.Error("Expected one needed item for device2, got", comp.NeedItems)
}
// Pretend we had an index reset on device 1
must(t, m.Index(device1, fcfg.ID, files))
must(t, m.Index(conn1, fcfg.ID, files))
if comp := m.testCompletion(device2, fcfg.ID); comp.NeedItems != 1 {
t.Error("Expected one needed item for device2, got", comp.NeedItems)
}
+4 -4
View File
@@ -315,15 +315,15 @@ func (t *ProgressEmitter) emptyLocked() bool {
func (t *ProgressEmitter) temporaryIndexSubscribe(conn protocol.Connection, folders []string) {
t.mut.Lock()
defer t.mut.Unlock()
t.connections[conn.ID()] = conn
t.foldersByConns[conn.ID()] = folders
t.connections[conn.DeviceID()] = conn
t.foldersByConns[conn.DeviceID()] = folders
}
func (t *ProgressEmitter) temporaryIndexUnsubscribe(conn protocol.Connection) {
t.mut.Lock()
defer t.mut.Unlock()
delete(t.connections, conn.ID())
delete(t.foldersByConns, conn.ID())
delete(t.connections, conn.DeviceID())
delete(t.foldersByConns, conn.DeviceID())
}
func (t *ProgressEmitter) clearLocked() {
+1 -1
View File
@@ -463,7 +463,7 @@ func TestSendDownloadProgressMessages(t *testing.T) {
p.temporaryIndexUnsubscribe(fc)
sendMsgs(p)
_, ok := p.sentDownloadStates[fc.ID()]
_, ok := p.sentDownloadStates[fc.DeviceID()]
if ok {
t.Error("Should not be there")
}
+12 -12
View File
@@ -107,7 +107,7 @@ func TestSymlinkTraversalRead(t *testing.T) {
<-done
// Request a file by traversing the symlink
res, err := m.Request(device1, "default", "symlink/requests_test.go", 0, 10, 0, nil, 0, false)
res, err := m.Request(device1Conn, "default", "symlink/requests_test.go", 0, 10, 0, nil, 0, false)
if err == nil || res != nil {
t.Error("Managed to traverse symlink")
}
@@ -439,7 +439,7 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
t.Fatalf("unexpected weak hash: %d != 103547413", f.Blocks[0].WeakHash)
}
res, err := m.Request(device1, "default", "foo", 0, int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
res, err := m.Request(device1Conn, "default", "foo", 0, int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
if err != nil {
t.Fatal(err)
}
@@ -453,7 +453,7 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
writeFile(t, tfs, "foo", payload)
_, err = m.Request(device1, "default", "foo", 0, int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
_, err = m.Request(device1Conn, "default", "foo", 0, int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
if err == nil {
t.Fatalf("expected failure")
}
@@ -1122,7 +1122,7 @@ func TestRequestIndexSenderPause(t *testing.T) {
cc := basicClusterConfig(device1, myID, fcfg.ID)
cc.Folders[0].Paused = true
m.ClusterConfig(device1, cc)
m.ClusterConfig(fc, cc)
seq++
files[0].Sequence = seq
@@ -1143,7 +1143,7 @@ func TestRequestIndexSenderPause(t *testing.T) {
// Remote unpaused
cc.Folders[0].Paused = false
m.ClusterConfig(device1, cc)
m.ClusterConfig(fc, cc)
select {
case <-time.After(5 * time.Second):
t.Fatal("timed out before receiving index")
@@ -1168,12 +1168,12 @@ func TestRequestIndexSenderPause(t *testing.T) {
// Local and remote paused, then first resume remote, then local
cc.Folders[0].Paused = true
m.ClusterConfig(device1, cc)
m.ClusterConfig(fc, cc)
pauseFolder(t, m.cfg, fcfg.ID, true)
cc.Folders[0].Paused = false
m.ClusterConfig(device1, cc)
m.ClusterConfig(fc, cc)
pauseFolder(t, m.cfg, fcfg.ID, false)
@@ -1190,7 +1190,7 @@ func TestRequestIndexSenderPause(t *testing.T) {
// Folder removed on remote
cc = protocol.ClusterConfig{}
m.ClusterConfig(device1, cc)
m.ClusterConfig(fc, cc)
seq++
files[0].Sequence = seq
@@ -1304,7 +1304,7 @@ func TestRequestReceiveEncrypted(t *testing.T) {
return nil
})
m.AddConnection(fc, protocol.Hello{})
m.ClusterConfig(device1, protocol.ClusterConfig{
m.ClusterConfig(fc, protocol.ClusterConfig{
Folders: []protocol.Folder{
{
ID: "default",
@@ -1354,7 +1354,7 @@ func TestRequestReceiveEncrypted(t *testing.T) {
}
// Simulate request from device that is untrusted too, i.e. with non-empty, but garbage hash
_, err := m.Request(device1, fcfg.ID, name, 0, 1064, 0, []byte("garbage"), 0, false)
_, err := m.Request(fc, fcfg.ID, name, 0, 1064, 0, []byte("garbage"), 0, false)
must(t, err)
changed, err := m.LocalChangedFolderFiles(fcfg.ID, 1, 10)
@@ -1380,7 +1380,7 @@ func TestRequestGlobalInvalidToValid(t *testing.T) {
})
must(t, err)
waiter.Wait()
addFakeConn(m, device2, fcfg.ID)
conn := addFakeConn(m, device2, fcfg.ID)
tfs := fcfg.Filesystem(nil)
defer cleanupModelAndRemoveDir(m, tfs.URI())
@@ -1405,7 +1405,7 @@ func TestRequestGlobalInvalidToValid(t *testing.T) {
file := fc.files[0]
fc.mut.Unlock()
file.SetIgnored()
m.IndexUpdate(device2, fcfg.ID, []protocol.FileInfo{prepareFileInfoForIndex(file)})
m.IndexUpdate(conn, fcfg.ID, []protocol.FileInfo{prepareFileInfoForIndex(file)})
// Wait for the ignored file to be received and possible pulled
timeout := time.After(10 * time.Second)
+5
View File
@@ -19,6 +19,7 @@ import (
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/ignore"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/protocol/mocks"
"github.com/syncthing/syncthing/lib/rand"
)
@@ -29,12 +30,16 @@ var (
defaultFolderConfig config.FolderConfiguration
defaultCfg config.Configuration
defaultAutoAcceptCfg config.Configuration
device1Conn = &mocks.Connection{}
device2Conn = &mocks.Connection{}
)
func init() {
myID, _ = protocol.DeviceIDFromString("ZNWFSWE-RWRV2BD-45BLMCV-LTDE2UR-4LJDW6J-R5BPWEB-TXD27XJ-IZF5RA4")
device1, _ = protocol.DeviceIDFromString("AIR6LPZ-7K4PTTV-UXQSMUU-CPQ5YWH-OEDFIIQ-JUG777G-2YQXXR5-YD6AWQR")
device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
device1Conn.DeviceIDReturns(device1)
device2Conn.DeviceIDReturns(device2)
cfg := config.New(myID)
cfg.Options.MinHomeDiskFree.Value = 0 // avoids unnecessary free space checks