2019-05-10 11:33:45 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2021-03-03 07:53:50 +00:00
|
|
|
protocolmocks "github.com/syncthing/syncthing/lib/protocol/mocks"
|
2023-08-21 16:39:13 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2019-05-10 11:33:45 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
|
|
|
)
|
|
|
|
|
|
|
|
type downloadProgressMessage struct {
|
|
|
|
folder string
|
|
|
|
updates []protocol.FileDownloadProgressUpdate
|
|
|
|
}
|
|
|
|
|
2021-03-03 07:53:50 +00:00
|
|
|
func newFakeConnection(id protocol.DeviceID, model Model) *fakeConnection {
|
|
|
|
f := &fakeConnection{
|
|
|
|
Connection: new(protocolmocks.Connection),
|
|
|
|
id: id,
|
|
|
|
model: model,
|
2021-06-17 11:57:44 +00:00
|
|
|
closed: make(chan struct{}),
|
2021-03-03 07:53:50 +00:00
|
|
|
}
|
2024-08-24 10:45:10 +00:00
|
|
|
f.RequestCalls(func(ctx context.Context, req *protocol.Request) ([]byte, error) {
|
|
|
|
return f.fileData[req.Name], nil
|
2021-03-03 07:53:50 +00:00
|
|
|
})
|
2023-07-29 08:24:44 +00:00
|
|
|
f.DeviceIDReturns(id)
|
2023-09-06 10:52:01 +00:00
|
|
|
f.ConnectionIDReturns(rand.String(16))
|
2021-03-03 07:53:50 +00:00
|
|
|
f.CloseCalls(func(err error) {
|
2021-06-17 11:57:44 +00:00
|
|
|
f.closeOnce.Do(func() {
|
|
|
|
close(f.closed)
|
2023-08-21 16:39:13 +00:00
|
|
|
model.Closed(f, err)
|
2021-06-17 11:57:44 +00:00
|
|
|
})
|
|
|
|
f.ClosedReturns(f.closed)
|
2021-03-03 07:53:50 +00:00
|
|
|
})
|
2023-08-21 16:39:13 +00:00
|
|
|
f.StringReturns(rand.String(8))
|
2021-03-03 07:53:50 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:33:45 +00:00
|
|
|
type fakeConnection struct {
|
2021-03-03 07:53:50 +00:00
|
|
|
*protocolmocks.Connection
|
2019-05-10 11:33:45 +00:00
|
|
|
id protocol.DeviceID
|
|
|
|
downloadProgressMessages []downloadProgressMessage
|
|
|
|
files []protocol.FileInfo
|
|
|
|
fileData map[string][]byte
|
|
|
|
folder string
|
2021-03-03 07:53:50 +00:00
|
|
|
model Model
|
2021-06-17 11:57:44 +00:00
|
|
|
closed chan struct{}
|
|
|
|
closeOnce sync.Once
|
2019-05-10 11:33:45 +00:00
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
2021-03-03 07:53:50 +00:00
|
|
|
func (f *fakeConnection) setIndexFn(fn func(_ context.Context, folder string, fs []protocol.FileInfo) error) {
|
2024-08-24 10:45:10 +00:00
|
|
|
f.IndexCalls(func(ctx context.Context, idx *protocol.Index) error { return fn(ctx, idx.Folder, idx.Files) })
|
|
|
|
f.IndexUpdateCalls(func(ctx context.Context, idxUp *protocol.IndexUpdate) error {
|
|
|
|
return fn(ctx, idxUp.Folder, idxUp.Files)
|
|
|
|
})
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 10:45:10 +00:00
|
|
|
func (f *fakeConnection) DownloadProgress(_ context.Context, dp *protocol.DownloadProgress) {
|
2019-05-10 11:33:45 +00:00
|
|
|
f.downloadProgressMessages = append(f.downloadProgressMessages, downloadProgressMessage{
|
2024-08-24 10:45:10 +00:00
|
|
|
folder: dp.Folder,
|
|
|
|
updates: dp.Updates,
|
2019-05-10 11:33:45 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-15 06:58:01 +00:00
|
|
|
func (f *fakeConnection) addFileLocked(name string, flags uint32, ftype protocol.FileInfoType, data []byte, version protocol.Vector, localFlags uint32) {
|
2019-05-10 11:33:45 +00:00
|
|
|
blockSize := protocol.BlockSize(int64(len(data)))
|
|
|
|
blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), blockSize, int64(len(data)), nil, true)
|
|
|
|
|
2021-03-15 06:58:01 +00:00
|
|
|
file := protocol.FileInfo{
|
|
|
|
Name: name,
|
|
|
|
Type: ftype,
|
|
|
|
Version: version,
|
|
|
|
Sequence: time.Now().UnixNano(),
|
|
|
|
LocalFlags: localFlags,
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
2021-03-15 06:58:01 +00:00
|
|
|
switch ftype {
|
|
|
|
case protocol.FileInfoTypeFile, protocol.FileInfoTypeDirectory:
|
|
|
|
file.ModifiedS = time.Now().Unix()
|
|
|
|
file.Permissions = flags
|
|
|
|
if ftype == protocol.FileInfoTypeFile {
|
|
|
|
file.Size = int64(len(data))
|
|
|
|
file.RawBlockSize = blockSize
|
|
|
|
file.Blocks = blocks
|
|
|
|
}
|
|
|
|
default: // Symlink
|
|
|
|
file.Name = name
|
|
|
|
file.Type = ftype
|
|
|
|
file.Version = version
|
|
|
|
file.SymlinkTarget = string(data)
|
|
|
|
file.NoPermissions = true
|
|
|
|
}
|
|
|
|
f.files = append(f.files, file)
|
2019-05-10 11:33:45 +00:00
|
|
|
|
|
|
|
if f.fileData == nil {
|
|
|
|
f.fileData = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
f.fileData[name] = data
|
|
|
|
}
|
|
|
|
|
2021-03-15 06:58:01 +00:00
|
|
|
func (f *fakeConnection) addFileWithLocalFlags(name string, ftype protocol.FileInfoType, localFlags uint32) {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
|
|
|
|
var version protocol.Vector
|
|
|
|
version = version.Update(f.id.Short())
|
|
|
|
f.addFileLocked(name, 0, ftype, nil, version, localFlags)
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:33:45 +00:00
|
|
|
func (f *fakeConnection) addFile(name string, flags uint32, ftype protocol.FileInfoType, data []byte) {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
|
|
|
|
var version protocol.Vector
|
|
|
|
version = version.Update(f.id.Short())
|
2021-03-15 06:58:01 +00:00
|
|
|
f.addFileLocked(name, flags, ftype, data, version, 0)
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) updateFile(name string, flags uint32, ftype protocol.FileInfoType, data []byte) {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
|
|
|
|
for i, fi := range f.files {
|
|
|
|
if fi.Name == name {
|
|
|
|
f.files = append(f.files[:i], f.files[i+1:]...)
|
2021-03-15 06:58:01 +00:00
|
|
|
f.addFileLocked(name, flags, ftype, data, fi.Version.Update(f.id.Short()), 0)
|
2019-05-10 11:33:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) deleteFile(name string) {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
|
|
|
|
for i, fi := range f.files {
|
|
|
|
if fi.Name == name {
|
|
|
|
fi.Deleted = true
|
|
|
|
fi.ModifiedS = time.Now().Unix()
|
|
|
|
fi.Version = fi.Version.Update(f.id.Short())
|
|
|
|
fi.Sequence = time.Now().UnixNano()
|
|
|
|
fi.Blocks = nil
|
|
|
|
|
|
|
|
f.files = append(append(f.files[:i], f.files[i+1:]...), fi)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) sendIndexUpdate() {
|
2021-03-15 06:58:01 +00:00
|
|
|
toSend := make([]protocol.FileInfo, len(f.files))
|
|
|
|
for i := range f.files {
|
|
|
|
toSend[i] = prepareFileInfoForIndex(f.files[i])
|
|
|
|
}
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
f.model.IndexUpdate(f, &protocol.IndexUpdate{Folder: f.folder, Files: toSend})
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 15:23:27 +00:00
|
|
|
func addFakeConn(m *testModel, dev protocol.DeviceID, folderID string) *fakeConnection {
|
2021-03-03 07:53:50 +00:00
|
|
|
fc := newFakeConnection(dev, m)
|
2021-11-10 08:46:21 +00:00
|
|
|
fc.folder = folderID
|
2020-09-29 11:17:38 +00:00
|
|
|
m.AddConnection(fc, protocol.Hello{})
|
2019-05-10 11:33:45 +00:00
|
|
|
|
lib/protocol: Refactor interface (#9375)
This is a refactor of the protocol/model interface to take the actual
message as the parameter, instead of the broken-out fields:
```diff
type Model interface {
// An index was received from the peer device
- Index(conn Connection, folder string, files []FileInfo) error
+ Index(conn Connection, idx *Index) error
// An index update was received from the peer device
- IndexUpdate(conn Connection, folder string, files []FileInfo) error
+ IndexUpdate(conn Connection, idxUp *IndexUpdate) error
// A request was made by the peer device
- Request(conn Connection, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error)
+ Request(conn Connection, req *Request) (RequestResponse, error)
// A cluster configuration message was received
- ClusterConfig(conn Connection, config ClusterConfig) error
+ ClusterConfig(conn Connection, config *ClusterConfig) error
// The peer device closed the connection or an error occurred
Closed(conn Connection, err error)
// The peer device sent progress updates for the files it is currently downloading
- DownloadProgress(conn Connection, folder string, updates []FileDownloadProgressUpdate) error
+ DownloadProgress(conn Connection, p *DownloadProgress) error
}
```
(and changing the `ClusterConfig` to `*ClusterConfig` for symmetry;
we'll be forced to use all pointers everywhere at some point anyway...)
The reason for this is that I have another thing cooking which is a
small troubleshooting change to check index consistency during transfer.
This required adding a field or two to the index/indexupdate messages,
and plumbing the extra parameters in umpteen changes is almost as big a
diff as this is. I figured let's do it once and avoid having to do that
in the future again...
The rest of the diff falls out of the change above, much of it being in
test code where we run these methods manually...
2024-01-31 07:18:27 +00:00
|
|
|
m.ClusterConfig(fc, &protocol.ClusterConfig{
|
2019-05-10 11:33:45 +00:00
|
|
|
Folders: []protocol.Folder{
|
|
|
|
{
|
2021-05-16 15:23:27 +00:00
|
|
|
ID: folderID,
|
2019-05-10 11:33:45 +00:00
|
|
|
Devices: []protocol.Device{
|
|
|
|
{ID: myID},
|
2021-05-16 15:23:27 +00:00
|
|
|
{ID: dev},
|
2019-05-10 11:33:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return fc
|
|
|
|
}
|