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"
|
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
2020-12-21 10:40:51 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/testutils"
|
2019-05-10 11:33:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type downloadProgressMessage struct {
|
|
|
|
folder string
|
|
|
|
updates []protocol.FileDownloadProgressUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeConnection struct {
|
2020-12-21 10:40:51 +00:00
|
|
|
testutils.FakeConnectionInfo
|
2019-05-10 11:33:45 +00:00
|
|
|
id protocol.DeviceID
|
|
|
|
downloadProgressMessages []downloadProgressMessage
|
|
|
|
closed bool
|
|
|
|
files []protocol.FileInfo
|
|
|
|
fileData map[string][]byte
|
|
|
|
folder string
|
2020-11-17 12:19:04 +00:00
|
|
|
model *testModel
|
2019-11-25 10:07:36 +00:00
|
|
|
indexFn func(context.Context, string, []protocol.FileInfo)
|
2019-11-19 08:56:53 +00:00
|
|
|
requestFn func(ctx context.Context, folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error)
|
2019-05-10 11:33:45 +00:00
|
|
|
closeFn func(error)
|
2020-10-02 09:49:51 +00:00
|
|
|
clusterConfigFn func(protocol.ClusterConfig)
|
2019-05-10 11:33:45 +00:00
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) Close(err error) {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
if f.closeFn != nil {
|
|
|
|
f.closeFn(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.closed = true
|
|
|
|
f.model.Closed(f, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) Start() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) ID() protocol.DeviceID {
|
|
|
|
return f.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) Name() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) Option(string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-11-25 10:07:36 +00:00
|
|
|
func (f *fakeConnection) Index(ctx context.Context, folder string, fs []protocol.FileInfo) error {
|
2019-05-10 11:33:45 +00:00
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
if f.indexFn != nil {
|
2019-11-25 10:07:36 +00:00
|
|
|
f.indexFn(ctx, folder, fs)
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-25 10:07:36 +00:00
|
|
|
func (f *fakeConnection) IndexUpdate(ctx context.Context, folder string, fs []protocol.FileInfo) error {
|
2019-05-10 11:33:45 +00:00
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
if f.indexFn != nil {
|
2019-11-25 10:07:36 +00:00
|
|
|
f.indexFn(ctx, folder, fs)
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
func (f *fakeConnection) Request(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
|
2019-05-10 11:33:45 +00:00
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
if f.requestFn != nil {
|
2019-11-19 08:56:53 +00:00
|
|
|
return f.requestFn(ctx, folder, name, offset, size, hash, fromTemporary)
|
2019-05-10 11:33:45 +00:00
|
|
|
}
|
|
|
|
return f.fileData[name], nil
|
|
|
|
}
|
|
|
|
|
2020-10-02 09:49:51 +00:00
|
|
|
func (f *fakeConnection) ClusterConfig(cc protocol.ClusterConfig) {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
if f.clusterConfigFn != nil {
|
|
|
|
f.clusterConfigFn(cc)
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 11:33:45 +00:00
|
|
|
|
|
|
|
func (f *fakeConnection) Ping() bool {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
return f.closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) Closed() bool {
|
|
|
|
f.mut.Lock()
|
|
|
|
defer f.mut.Unlock()
|
|
|
|
return f.closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) Statistics() protocol.Statistics {
|
|
|
|
return protocol.Statistics{}
|
|
|
|
}
|
|
|
|
|
2019-11-25 10:07:36 +00:00
|
|
|
func (f *fakeConnection) DownloadProgress(_ context.Context, folder string, updates []protocol.FileDownloadProgressUpdate) {
|
2019-05-10 11:33:45 +00:00
|
|
|
f.downloadProgressMessages = append(f.downloadProgressMessages, downloadProgressMessage{
|
|
|
|
folder: folder,
|
|
|
|
updates: updates,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeConnection) addFileLocked(name string, flags uint32, ftype protocol.FileInfoType, data []byte, version protocol.Vector) {
|
|
|
|
blockSize := protocol.BlockSize(int64(len(data)))
|
|
|
|
blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), blockSize, int64(len(data)), nil, true)
|
|
|
|
|
|
|
|
if ftype == protocol.FileInfoTypeFile || ftype == protocol.FileInfoTypeDirectory {
|
|
|
|
f.files = append(f.files, protocol.FileInfo{
|
|
|
|
Name: name,
|
|
|
|
Type: ftype,
|
|
|
|
Size: int64(len(data)),
|
|
|
|
ModifiedS: time.Now().Unix(),
|
|
|
|
Permissions: flags,
|
|
|
|
Version: version,
|
|
|
|
Sequence: time.Now().UnixNano(),
|
2020-10-02 06:07:05 +00:00
|
|
|
RawBlockSize: blockSize,
|
2019-05-10 11:33:45 +00:00
|
|
|
Blocks: blocks,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Symlink
|
|
|
|
f.files = append(f.files, protocol.FileInfo{
|
|
|
|
Name: name,
|
|
|
|
Type: ftype,
|
|
|
|
Version: version,
|
|
|
|
Sequence: time.Now().UnixNano(),
|
|
|
|
SymlinkTarget: string(data),
|
|
|
|
NoPermissions: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.fileData == nil {
|
|
|
|
f.fileData = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
f.fileData[name] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
f.addFileLocked(name, flags, ftype, data, version)
|
|
|
|
}
|
|
|
|
|
|
|
|
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:]...)
|
|
|
|
f.addFileLocked(name, flags, ftype, data, fi.Version.Update(f.id.Short()))
|
|
|
|
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() {
|
|
|
|
f.model.IndexUpdate(f.id, f.folder, f.files)
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func addFakeConn(m *testModel, dev protocol.DeviceID) *fakeConnection {
|
2019-05-10 11:33:45 +00:00
|
|
|
fc := &fakeConnection{id: dev, model: m}
|
2020-09-29 11:17:38 +00:00
|
|
|
m.AddConnection(fc, protocol.Hello{})
|
2019-05-10 11:33:45 +00:00
|
|
|
|
|
|
|
m.ClusterConfig(dev, protocol.ClusterConfig{
|
|
|
|
Folders: []protocol.Folder{
|
|
|
|
{
|
|
|
|
ID: "default",
|
|
|
|
Devices: []protocol.Device{
|
|
|
|
{ID: myID},
|
|
|
|
{ID: device1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return fc
|
|
|
|
}
|