2016-11-30 09:32:28 +00:00
|
|
|
// Copyright (C) 2016 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-11-30 09:32:28 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-11-19 08:56:53 +00:00
|
|
|
"context"
|
2018-02-25 08:39:00 +00:00
|
|
|
"errors"
|
2016-11-30 09:32:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-07-25 09:36:09 +00:00
|
|
|
"path/filepath"
|
2016-12-13 10:24:10 +00:00
|
|
|
"runtime"
|
2019-07-19 17:39:52 +00:00
|
|
|
"strconv"
|
2016-12-13 10:24:10 +00:00
|
|
|
"strings"
|
2016-11-30 09:32:28 +00:00
|
|
|
"testing"
|
2016-12-13 10:24:10 +00:00
|
|
|
"time"
|
2016-11-30 09:32:28 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2018-02-25 08:39:00 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2018-02-25 08:39:00 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
2016-11-30 09:32:28 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestSimple(t *testing.T) {
|
|
|
|
// Verify that the model performs a request and creates a file based on
|
|
|
|
// an incoming index update.
|
|
|
|
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
2019-03-08 20:29:09 +00:00
|
|
|
tfs := fcfg.Filesystem()
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, tfs.URI())
|
2016-11-30 09:32:28 +00:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
done := make(chan struct{})
|
2016-11-30 12:11:06 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-05 18:01:56 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Error("More than one index update sent")
|
2019-02-05 18:01:56 +00:00
|
|
|
default:
|
|
|
|
}
|
2016-11-30 09:32:28 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "testfile" {
|
|
|
|
close(done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 12:11:06 +00:00
|
|
|
fc.mut.Unlock()
|
2016-11-30 09:32:28 +00:00
|
|
|
|
|
|
|
// Send an update for the test file, wait for it to sync and be reported back.
|
|
|
|
contents := []byte("test file contents\n")
|
2016-12-13 10:24:10 +00:00
|
|
|
fc.addFile("testfile", 0644, protocol.FileInfoTypeFile, contents)
|
2016-11-30 09:32:28 +00:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// Verify the contents
|
2019-03-08 20:29:09 +00:00
|
|
|
if err := equalContents(filepath.Join(tfs.URI(), "testfile"), contents); err != nil {
|
2016-11-30 09:32:28 +00:00
|
|
|
t.Error("File did not sync correctly:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 10:24:10 +00:00
|
|
|
func TestSymlinkTraversalRead(t *testing.T) {
|
|
|
|
// Verify that a symlink can not be traversed for reading.
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no symlink support on CI")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
|
2016-12-13 10:24:10 +00:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
done := make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-05 18:01:56 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Error("More than one index update sent")
|
2019-02-05 18:01:56 +00:00
|
|
|
default:
|
|
|
|
}
|
2016-12-13 10:24:10 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "symlink" {
|
|
|
|
close(done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the symlink, wait for it to sync and be reported back.
|
|
|
|
contents := []byte("..")
|
2017-02-07 08:34:24 +00:00
|
|
|
fc.addFile("symlink", 0644, protocol.FileInfoTypeSymlink, contents)
|
2016-12-13 10:24:10 +00:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// Request a file by traversing the symlink
|
2018-11-13 07:53:55 +00:00
|
|
|
res, err := m.Request(device1, "default", "symlink/requests_test.go", 10, 0, nil, 0, false)
|
|
|
|
if err == nil || res != nil {
|
2016-12-13 10:24:10 +00:00
|
|
|
t.Error("Managed to traverse symlink")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinkTraversalWrite(t *testing.T) {
|
|
|
|
// Verify that a symlink can not be traversed for writing.
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no symlink support on CI")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
|
2016-12-13 10:24:10 +00:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected names.
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
badReq := make(chan string, 1)
|
|
|
|
badIdx := make(chan string, 1)
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2016-12-13 10:24:10 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "symlink" {
|
|
|
|
done <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(f.Name, "symlink") {
|
|
|
|
badIdx <- f.Name
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 08:56:53 +00:00
|
|
|
fc.requestFn = func(_ context.Context, folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
|
2016-12-13 10:24:10 +00:00
|
|
|
if name != "symlink" && strings.HasPrefix(name, "symlink") {
|
|
|
|
badReq <- name
|
|
|
|
}
|
|
|
|
return fc.fileData[name], nil
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the symlink, wait for it to sync and be reported back.
|
|
|
|
contents := []byte("..")
|
2017-02-07 08:34:24 +00:00
|
|
|
fc.addFile("symlink", 0644, protocol.FileInfoTypeSymlink, contents)
|
2016-12-13 10:24:10 +00:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// Send an update for things behind the symlink, wait for requests for
|
|
|
|
// blocks for any of them to come back, or index entries. Hopefully none
|
|
|
|
// of that should happen.
|
|
|
|
contents = []byte("testdata testdata\n")
|
|
|
|
fc.addFile("symlink/testfile", 0644, protocol.FileInfoTypeFile, contents)
|
|
|
|
fc.addFile("symlink/testdir", 0644, protocol.FileInfoTypeDirectory, contents)
|
2017-02-07 08:34:24 +00:00
|
|
|
fc.addFile("symlink/testsyml", 0644, protocol.FileInfoTypeSymlink, contents)
|
2016-12-13 10:24:10 +00:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case name := <-badReq:
|
|
|
|
t.Fatal("Should not have requested the data for", name)
|
|
|
|
case name := <-badIdx:
|
|
|
|
t.Fatal("Should not have sent the index entry for", name)
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
// Unfortunately not much else to trigger on here. The puller sleep
|
|
|
|
// interval is 1s so if we didn't get any requests within two
|
|
|
|
// iterations we should be fine.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestCreateTmpSymlink(t *testing.T) {
|
2017-11-11 19:18:17 +00:00
|
|
|
// Test that an update for a temporary file is invalidated
|
2016-12-13 10:24:10 +00:00
|
|
|
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
|
2016-12-13 10:24:10 +00:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
2017-11-11 19:18:17 +00:00
|
|
|
goodIdx := make(chan struct{})
|
|
|
|
name := fs.TempName("testlink")
|
2016-12-13 10:24:10 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2016-12-13 10:24:10 +00:00
|
|
|
for _, f := range fs {
|
2017-11-11 19:18:17 +00:00
|
|
|
if f.Name == name {
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.IsInvalid() {
|
2017-11-11 19:18:17 +00:00
|
|
|
goodIdx <- struct{}{}
|
|
|
|
} else {
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Error("Received index with non-invalid temporary file")
|
|
|
|
close(goodIdx)
|
2017-11-11 19:18:17 +00:00
|
|
|
}
|
2016-12-13 10:24:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the test file, wait for it to sync and be reported back.
|
2017-11-11 19:18:17 +00:00
|
|
|
fc.addFile(name, 0644, protocol.FileInfoTypeSymlink, []byte(".."))
|
2016-12-13 10:24:10 +00:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
2017-11-11 19:18:17 +00:00
|
|
|
case <-goodIdx:
|
2016-12-13 10:24:10 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2017-11-11 19:18:17 +00:00
|
|
|
t.Fatal("Timed out without index entry being sent")
|
2016-12-13 10:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 09:36:09 +00:00
|
|
|
func TestRequestVersioningSymlinkAttack(t *testing.T) {
|
2017-08-08 06:05:24 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no symlink support on Windows")
|
|
|
|
}
|
|
|
|
|
2017-07-25 09:36:09 +00:00
|
|
|
// Sets up a folder with trashcan versioning and tries to use a
|
|
|
|
// deleted symlink to escape
|
|
|
|
|
2019-03-08 20:29:09 +00:00
|
|
|
w, fcfg := tmpDefaultWrapper()
|
2019-02-12 15:04:04 +00:00
|
|
|
defer func() {
|
2019-03-08 20:29:09 +00:00
|
|
|
os.RemoveAll(fcfg.Filesystem().URI())
|
2019-02-12 15:04:04 +00:00
|
|
|
os.Remove(w.ConfigPath())
|
|
|
|
}()
|
2018-03-13 13:03:10 +00:00
|
|
|
|
2019-02-12 15:04:04 +00:00
|
|
|
fcfg.Versioning = config.VersioningConfiguration{Type: "trashcan"}
|
|
|
|
w.SetFolder(fcfg)
|
|
|
|
m, fc := setupModelWithConnectionFromWrapper(w)
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModel(m)
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
// Create a temporary directory that we will use as target to see if
|
|
|
|
// we can escape to it
|
|
|
|
tmpdir, err := ioutil.TempDir("", "syncthing-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-08 20:29:09 +00:00
|
|
|
defer os.RemoveAll(tmpdir)
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
idx := make(chan int)
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2017-07-25 09:36:09 +00:00
|
|
|
idx <- len(fs)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
2019-11-08 17:53:51 +00:00
|
|
|
waitForIdx := func() {
|
|
|
|
select {
|
|
|
|
case c := <-idx:
|
|
|
|
if c == 0 {
|
|
|
|
t.Fatal("Got empty index update")
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("timed out before receiving index update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 09:36:09 +00:00
|
|
|
// Send an update for the test file, wait for it to sync and be reported back.
|
|
|
|
fc.addFile("foo", 0644, protocol.FileInfoTypeSymlink, []byte(tmpdir))
|
|
|
|
fc.sendIndexUpdate()
|
2019-11-08 17:53:51 +00:00
|
|
|
waitForIdx()
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
// Delete the symlink, hoping for it to get versioned
|
|
|
|
fc.deleteFile("foo")
|
|
|
|
fc.sendIndexUpdate()
|
2019-11-08 17:53:51 +00:00
|
|
|
waitForIdx()
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
// Recreate foo and a file in it with some data
|
2018-07-10 15:40:06 +00:00
|
|
|
fc.updateFile("foo", 0755, protocol.FileInfoTypeDirectory, nil)
|
2017-07-25 09:36:09 +00:00
|
|
|
fc.addFile("foo/test", 0644, protocol.FileInfoTypeFile, []byte("testtesttest"))
|
|
|
|
fc.sendIndexUpdate()
|
2019-11-08 17:53:51 +00:00
|
|
|
waitForIdx()
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
// Remove the test file and see if it escaped
|
|
|
|
fc.deleteFile("foo/test")
|
|
|
|
fc.sendIndexUpdate()
|
2019-11-08 17:53:51 +00:00
|
|
|
waitForIdx()
|
2017-07-25 09:36:09 +00:00
|
|
|
|
2018-08-16 10:11:48 +00:00
|
|
|
path := filepath.Join(tmpdir, "test")
|
2017-07-25 09:36:09 +00:00
|
|
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
|
|
|
t.Fatal("File escaped to", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func TestPullInvalidIgnoredSO(t *testing.T) {
|
|
|
|
pullInvalidIgnored(t, config.FolderTypeSendOnly)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullInvalidIgnoredSR(t *testing.T) {
|
|
|
|
pullInvalidIgnored(t, config.FolderTypeSendReceive)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks that (un-)ignored/invalid/deleted files are treated as expected.
|
|
|
|
func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
|
2019-02-12 06:50:07 +00:00
|
|
|
w := createTmpWrapper(defaultCfgWrapper.RawCopy())
|
2019-03-08 20:29:09 +00:00
|
|
|
fcfg := testFolderConfigTmp()
|
|
|
|
fss := fcfg.Filesystem()
|
2019-02-12 06:50:07 +00:00
|
|
|
fcfg.Type = ft
|
|
|
|
w.SetFolder(fcfg)
|
2019-05-18 06:52:50 +00:00
|
|
|
m := setupModel(w)
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, fss.URI())
|
2018-02-25 08:39:00 +00:00
|
|
|
|
2019-11-08 09:56:16 +00:00
|
|
|
m.removeFolder(fcfg)
|
|
|
|
m.addFolder(fcfg)
|
2018-02-25 08:39:00 +00:00
|
|
|
// Reach in and update the ignore matcher to one that always does
|
|
|
|
// reloads when asked to, instead of checking file mtimes. This is
|
|
|
|
// because we might be changing the files on disk often enough that the
|
|
|
|
// mtimes will be unreliable to determine change status.
|
|
|
|
m.fmut.Lock()
|
2019-03-08 20:29:09 +00:00
|
|
|
m.folderIgnores["default"] = ignore.New(fss, ignore.WithChangeDetector(newAlwaysChanged()))
|
2018-02-25 08:39:00 +00:00
|
|
|
m.fmut.Unlock()
|
2019-11-08 09:56:16 +00:00
|
|
|
m.startFolder(fcfg.ID)
|
2019-05-18 06:52:50 +00:00
|
|
|
|
|
|
|
fc := addFakeConn(m, device1)
|
|
|
|
fc.folder = "default"
|
2018-02-25 08:39:00 +00:00
|
|
|
|
|
|
|
if err := m.SetIgnores("default", []string{"*ignored*"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
contents := []byte("test file contents\n")
|
|
|
|
otherContents := []byte("other test file contents\n")
|
|
|
|
|
|
|
|
invIgn := "invalid:ignored"
|
|
|
|
invDel := "invalid:deleted"
|
|
|
|
ign := "ignoredNonExisting"
|
|
|
|
ignExisting := "ignoredExisting"
|
|
|
|
|
|
|
|
fc.addFile(invIgn, 0644, protocol.FileInfoTypeFile, contents)
|
|
|
|
fc.addFile(invDel, 0644, protocol.FileInfoTypeFile, contents)
|
|
|
|
fc.deleteFile(invDel)
|
|
|
|
fc.addFile(ign, 0644, protocol.FileInfoTypeFile, contents)
|
|
|
|
fc.addFile(ignExisting, 0644, protocol.FileInfoTypeFile, contents)
|
2019-03-08 20:29:09 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(fss.URI(), ignExisting), otherContents, 0644); err != nil {
|
2018-02-25 08:39:00 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2018-02-25 08:39:00 +00:00
|
|
|
expected := map[string]struct{}{invIgn: {}, ign: {}, ignExisting: {}}
|
|
|
|
for _, f := range fs {
|
|
|
|
if _, ok := expected[f.Name]; !ok {
|
2018-06-02 13:08:32 +00:00
|
|
|
t.Errorf("Unexpected file %v was added to index", f.Name)
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
2018-06-24 07:50:18 +00:00
|
|
|
if !f.IsInvalid() {
|
2018-02-25 08:39:00 +00:00
|
|
|
t.Errorf("File %v wasn't marked as invalid", f.Name)
|
|
|
|
}
|
|
|
|
delete(expected, f.Name)
|
|
|
|
}
|
|
|
|
for name := range expected {
|
|
|
|
t.Errorf("File %v wasn't added to index", name)
|
|
|
|
}
|
2019-05-18 06:52:50 +00:00
|
|
|
close(done)
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
sub := m.evLogger.Subscribe(events.FolderErrors)
|
|
|
|
defer sub.Unsubscribe()
|
2018-02-25 08:39:00 +00:00
|
|
|
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ev := <-sub.C():
|
2019-05-18 06:52:50 +00:00
|
|
|
t.Fatalf("Errors while scanning/pulling: %v", ev)
|
2019-03-22 13:43:47 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2018-02-25 08:39:00 +00:00
|
|
|
t.Fatalf("timed out before index was received")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
|
2019-05-18 06:52:50 +00:00
|
|
|
done = make(chan struct{})
|
2019-06-17 12:23:28 +00:00
|
|
|
expected := map[string]struct{}{ign: {}, ignExisting: {}}
|
|
|
|
// The indexes will normally arrive in one update, but it is possible
|
|
|
|
// that they arrive in separate ones.
|
2018-02-25 08:39:00 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2018-02-25 08:39:00 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
if _, ok := expected[f.Name]; !ok {
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Errorf("Unexpected file %v was updated in index", f.Name)
|
|
|
|
continue
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.IsInvalid() {
|
2018-02-25 08:39:00 +00:00
|
|
|
t.Errorf("File %v is still marked as invalid", f.Name)
|
|
|
|
}
|
2019-10-01 13:34:59 +00:00
|
|
|
ev := protocol.Vector{}
|
2018-02-25 08:39:00 +00:00
|
|
|
if f.Name == ign {
|
2019-10-01 13:34:59 +00:00
|
|
|
// The unignored deleted file should have an
|
|
|
|
// empty version, to make it not override
|
|
|
|
// existing global files.
|
2018-02-25 08:39:00 +00:00
|
|
|
if !f.Deleted {
|
|
|
|
t.Errorf("File %v was not marked as deleted", f.Name)
|
|
|
|
}
|
2019-10-01 13:34:59 +00:00
|
|
|
} else {
|
|
|
|
// The unignored existing file should have a
|
|
|
|
// version with only a local counter, to make
|
|
|
|
// it conflict changed global files.
|
|
|
|
ev = protocol.Vector{}.Update(myID.Short())
|
|
|
|
if f.Deleted {
|
|
|
|
t.Errorf("File %v is marked as deleted", f.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v := f.Version; !v.Equal(ev) {
|
|
|
|
t.Errorf("File %v has version %v, expected %v", f.Name, v, ev)
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
|
|
|
delete(expected, f.Name)
|
|
|
|
}
|
2019-06-17 12:23:28 +00:00
|
|
|
if len(expected) == 0 {
|
|
|
|
close(done)
|
2018-02-25 08:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure pulling doesn't interfere, as index updates are racy and
|
|
|
|
// thus we cannot distinguish between scan and pull results.
|
2019-11-19 08:56:53 +00:00
|
|
|
fc.requestFn = func(_ context.Context, folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
|
2018-02-25 08:39:00 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
if err := m.SetIgnores("default", []string{"*:ignored*"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2019-03-22 13:43:47 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2019-08-01 09:07:41 +00:00
|
|
|
t.Fatal("timed out before receiving index updates for all existing files, missing", expected)
|
2018-02-25 08:39:00 +00:00
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 20:24:20 +00:00
|
|
|
func TestIssue4841(t *testing.T) {
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
|
2018-03-27 20:24:20 +00:00
|
|
|
|
2019-07-30 15:50:51 +00:00
|
|
|
received := make(chan []protocol.FileInfo)
|
2018-03-27 20:24:20 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, _ string, fs []protocol.FileInfo) {
|
2019-07-30 15:50:51 +00:00
|
|
|
received <- fs
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
checkReceived := func(fs []protocol.FileInfo) protocol.FileInfo {
|
|
|
|
t.Helper()
|
2018-03-27 20:24:20 +00:00
|
|
|
if len(fs) != 1 {
|
|
|
|
t.Fatalf("Sent index with %d files, should be 1", len(fs))
|
|
|
|
}
|
|
|
|
if fs[0].Name != "foo" {
|
|
|
|
t.Fatalf(`Sent index with file %v, should be "foo"`, fs[0].Name)
|
|
|
|
}
|
2019-07-30 15:50:51 +00:00
|
|
|
return fs[0]
|
2018-03-27 20:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup file from remote that was ignored locally
|
2019-04-07 11:29:17 +00:00
|
|
|
folder := m.folderRunners[defaultFolderConfig.ID].(*sendReceiveFolder)
|
|
|
|
folder.updateLocals([]protocol.FileInfo{{
|
2018-06-24 07:50:18 +00:00
|
|
|
Name: "foo",
|
|
|
|
Type: protocol.FileInfoTypeFile,
|
|
|
|
LocalFlags: protocol.FlagLocalIgnored,
|
2019-02-06 08:32:03 +00:00
|
|
|
Version: protocol.Vector{}.Update(device1.Short()),
|
2018-03-27 20:24:20 +00:00
|
|
|
}})
|
2019-07-30 15:50:51 +00:00
|
|
|
|
|
|
|
checkReceived(<-received)
|
2018-03-27 20:24:20 +00:00
|
|
|
|
|
|
|
// Scan without ignore patterns with "foo" not existing locally
|
|
|
|
if err := m.ScanFolder("default"); err != nil {
|
|
|
|
t.Fatal("Failed scanning:", err)
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:50:51 +00:00
|
|
|
f := checkReceived(<-received)
|
|
|
|
|
2019-10-01 13:34:59 +00:00
|
|
|
if !f.Version.Equal(protocol.Vector{}) {
|
|
|
|
t.Errorf("Got Version == %v, expected empty version", f.Version)
|
2018-03-27 20:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 08:24:44 +00:00
|
|
|
func TestRescanIfHaveInvalidContent(t *testing.T) {
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
2019-03-08 20:29:09 +00:00
|
|
|
tmpDir := fcfg.Filesystem().URI()
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, tmpDir)
|
2018-05-05 08:24:44 +00:00
|
|
|
|
|
|
|
payload := []byte("hello")
|
|
|
|
|
2019-03-09 18:45:36 +00:00
|
|
|
must(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777))
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-07-30 15:50:51 +00:00
|
|
|
received := make(chan []protocol.FileInfo)
|
2018-05-05 08:24:44 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, _ string, fs []protocol.FileInfo) {
|
2019-07-30 15:50:51 +00:00
|
|
|
received <- fs
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
checkReceived := func(fs []protocol.FileInfo) protocol.FileInfo {
|
|
|
|
t.Helper()
|
2018-05-05 08:24:44 +00:00
|
|
|
if len(fs) != 1 {
|
|
|
|
t.Fatalf("Sent index with %d files, should be 1", len(fs))
|
|
|
|
}
|
|
|
|
if fs[0].Name != "foo" {
|
|
|
|
t.Fatalf(`Sent index with file %v, should be "foo"`, fs[0].Name)
|
|
|
|
}
|
2019-07-30 15:50:51 +00:00
|
|
|
return fs[0]
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan without ignore patterns with "foo" not existing locally
|
|
|
|
if err := m.ScanFolder("default"); err != nil {
|
|
|
|
t.Fatal("Failed scanning:", err)
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:50:51 +00:00
|
|
|
f := checkReceived(<-received)
|
2018-05-05 08:24:44 +00:00
|
|
|
if f.Blocks[0].WeakHash != 103547413 {
|
|
|
|
t.Fatalf("unexpected weak hash: %d != 103547413", f.Blocks[0].WeakHash)
|
|
|
|
}
|
|
|
|
|
2019-02-06 08:32:03 +00:00
|
|
|
res, err := m.Request(device1, "default", "foo", int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
|
2018-05-05 08:24:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-13 07:53:55 +00:00
|
|
|
buf := res.Data()
|
2018-05-05 08:24:44 +00:00
|
|
|
if !bytes.Equal(buf, payload) {
|
|
|
|
t.Errorf("%s != %s", buf, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = []byte("bye")
|
|
|
|
buf = make([]byte, len(payload))
|
|
|
|
|
2019-03-09 18:45:36 +00:00
|
|
|
must(t, ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777))
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-06 08:32:03 +00:00
|
|
|
_, err = m.Request(device1, "default", "foo", int32(len(payload)), 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false)
|
2018-05-05 08:24:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2019-07-30 15:50:51 +00:00
|
|
|
case fs := <-received:
|
|
|
|
f := checkReceived(fs)
|
2018-05-05 08:24:44 +00:00
|
|
|
if f.Blocks[0].WeakHash != 41943361 {
|
|
|
|
t.Fatalf("unexpected weak hash: %d != 41943361", f.Blocks[0].WeakHash)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 15:40:06 +00:00
|
|
|
func TestParentDeletion(t *testing.T) {
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
2019-03-08 20:29:09 +00:00
|
|
|
testFs := fcfg.Filesystem()
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, testFs.URI())
|
2018-07-10 15:40:06 +00:00
|
|
|
|
|
|
|
parent := "foo"
|
|
|
|
child := filepath.Join(parent, "bar")
|
|
|
|
|
|
|
|
received := make(chan []protocol.FileInfo)
|
|
|
|
fc.addFile(parent, 0777, protocol.FileInfoTypeDirectory, nil)
|
|
|
|
fc.addFile(child, 0777, protocol.FileInfoTypeDirectory, nil)
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2018-07-10 15:40:06 +00:00
|
|
|
received <- fs
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
// Get back index from initial setup
|
|
|
|
select {
|
|
|
|
case fs := <-received:
|
|
|
|
if len(fs) != 2 {
|
|
|
|
t.Fatalf("Sent index with %d files, should be 2", len(fs))
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete parent dir
|
2019-03-09 18:45:36 +00:00
|
|
|
must(t, testFs.RemoveAll(parent))
|
2018-07-10 15:40:06 +00:00
|
|
|
|
|
|
|
// Scan only the child dir (not the parent)
|
|
|
|
if err := m.ScanFolderSubdirs("default", []string{child}); err != nil {
|
|
|
|
t.Fatal("Failed scanning:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case fs := <-received:
|
|
|
|
if len(fs) != 1 {
|
|
|
|
t.Fatalf("Sent index with %d files, should be 1", len(fs))
|
|
|
|
}
|
|
|
|
if fs[0].Name != child {
|
|
|
|
t.Fatalf(`Sent index with file "%v", should be "%v"`, fs[0].Name, child)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate the child dir on the remote
|
|
|
|
fc.updateFile(child, 0777, protocol.FileInfoTypeDirectory, nil)
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
// Wait for the child dir to be recreated and sent to the remote
|
|
|
|
select {
|
|
|
|
case fs := <-received:
|
|
|
|
l.Debugln("sent:", fs)
|
|
|
|
found := false
|
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == child {
|
|
|
|
if f.Deleted {
|
|
|
|
t.Fatalf(`File "%v" is still deleted`, child)
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf(`File "%v" is missing in index`, child)
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 08:32:35 +00:00
|
|
|
// TestRequestSymlinkWindows checks that symlinks aren't marked as deleted on windows
|
|
|
|
// Issue: https://github.com/syncthing/syncthing/issues/5125
|
|
|
|
func TestRequestSymlinkWindows(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("windows specific test")
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
defer cleanupModelAndRemoveDir(m, fcfg.Filesystem().URI())
|
2018-08-25 08:32:35 +00:00
|
|
|
|
2019-07-30 15:50:51 +00:00
|
|
|
received := make(chan []protocol.FileInfo)
|
2018-08-25 08:32:35 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-05 18:01:56 +00:00
|
|
|
select {
|
2019-07-30 15:50:51 +00:00
|
|
|
case <-received:
|
|
|
|
t.Error("More than one index update sent")
|
2019-02-05 18:01:56 +00:00
|
|
|
default:
|
|
|
|
}
|
2019-07-30 15:50:51 +00:00
|
|
|
received <- fs
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
fc.addFile("link", 0644, protocol.FileInfoTypeSymlink, nil)
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case fs := <-received:
|
|
|
|
close(received)
|
2018-08-25 08:32:35 +00:00
|
|
|
// expected first index
|
|
|
|
if len(fs) != 1 {
|
|
|
|
t.Fatalf("Expected just one file in index, got %v", fs)
|
|
|
|
}
|
|
|
|
f := fs[0]
|
|
|
|
if f.Name != "link" {
|
|
|
|
t.Fatalf(`Got file info with path "%v", expected "link"`, f.Name)
|
|
|
|
}
|
|
|
|
if !f.IsInvalid() {
|
|
|
|
t.Errorf(`File info was not marked as invalid`)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timed out before pull was finished")
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
sub := m.evLogger.Subscribe(events.StateChanged | events.LocalIndexUpdated)
|
|
|
|
defer sub.Unsubscribe()
|
2018-08-25 08:32:35 +00:00
|
|
|
|
|
|
|
m.ScanFolder("default")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-sub.C():
|
|
|
|
switch data := ev.Data.(map[string]interface{}); {
|
|
|
|
case ev.Type == events.LocalIndexUpdated:
|
|
|
|
t.Fatalf("Local index was updated unexpectedly: %v", data)
|
|
|
|
case ev.Type == events.StateChanged:
|
|
|
|
if data["from"] == "scanning" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Timed out before scan finished")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func equalContents(path string, contents []byte) error {
|
|
|
|
if bs, err := ioutil.ReadFile(path); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !bytes.Equal(bs, contents) {
|
|
|
|
return errors.New("incorrect data")
|
|
|
|
}
|
|
|
|
return nil
|
2016-11-30 09:32:28 +00:00
|
|
|
}
|
2018-09-16 07:48:14 +00:00
|
|
|
|
|
|
|
func TestRequestRemoteRenameChanged(t *testing.T) {
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
2019-03-08 20:29:09 +00:00
|
|
|
tfs := fcfg.Filesystem()
|
|
|
|
tmpDir := tfs.URI()
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, tfs.URI())
|
2018-09-16 07:48:14 +00:00
|
|
|
|
2019-07-30 15:50:51 +00:00
|
|
|
received := make(chan []protocol.FileInfo)
|
2018-09-16 07:48:14 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-05 18:01:56 +00:00
|
|
|
select {
|
2019-07-30 15:50:51 +00:00
|
|
|
case <-received:
|
|
|
|
t.Error("More than one index update sent")
|
2019-02-05 18:01:56 +00:00
|
|
|
default:
|
|
|
|
}
|
2019-07-30 15:50:51 +00:00
|
|
|
received <- fs
|
2018-09-16 07:48:14 +00:00
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// setup
|
|
|
|
a := "a"
|
|
|
|
b := "b"
|
|
|
|
data := map[string][]byte{
|
|
|
|
a: []byte("aData"),
|
|
|
|
b: []byte("bData"),
|
|
|
|
}
|
|
|
|
for _, n := range [2]string{a, b} {
|
|
|
|
fc.addFile(n, 0644, protocol.FileInfoTypeFile, data[n])
|
|
|
|
}
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
select {
|
2019-07-30 15:50:51 +00:00
|
|
|
case fs := <-received:
|
|
|
|
close(received)
|
|
|
|
if len(fs) != 2 {
|
|
|
|
t.Fatalf("Received index with %v indexes instead of 2", len(fs))
|
|
|
|
}
|
2018-09-16 07:48:14 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range [2]string{a, b} {
|
2019-03-09 18:45:36 +00:00
|
|
|
must(t, equalContents(filepath.Join(tmpDir, n), data[n]))
|
2018-09-16 07:48:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 15:05:20 +00:00
|
|
|
var gotA, gotB, gotConfl bool
|
2019-07-28 20:29:31 +00:00
|
|
|
bIntermediateVersion := protocol.Vector{}.Update(fc.id.Short()).Update(myID.Short())
|
|
|
|
bFinalVersion := bIntermediateVersion.Copy().Update(fc.id.Short())
|
2019-07-30 15:50:51 +00:00
|
|
|
done := make(chan struct{})
|
2019-02-12 15:05:20 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-12 15:05:20 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Error("Received more index updates than expected")
|
|
|
|
return
|
2019-02-12 15:05:20 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
for _, f := range fs {
|
|
|
|
switch {
|
|
|
|
case f.Name == a:
|
|
|
|
if gotA {
|
|
|
|
t.Error("Got more than one index update for", f.Name)
|
|
|
|
}
|
|
|
|
gotA = true
|
|
|
|
case f.Name == b:
|
|
|
|
if gotB {
|
|
|
|
t.Error("Got more than one index update for", f.Name)
|
|
|
|
}
|
2019-07-28 20:29:31 +00:00
|
|
|
if f.Version.Equal(bIntermediateVersion) {
|
|
|
|
// This index entry might be superseeded
|
|
|
|
// by the final one or sent before it separately.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if f.Version.Equal(bFinalVersion) {
|
|
|
|
gotB = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Errorf("Got unexpected version %v for file %v in index update", f.Version, f.Name)
|
2019-02-12 15:05:20 +00:00
|
|
|
case strings.HasPrefix(f.Name, "b.sync-conflict-"):
|
|
|
|
if gotConfl {
|
|
|
|
t.Error("Got more than one index update for conflicts of", f.Name)
|
|
|
|
}
|
|
|
|
gotConfl = true
|
|
|
|
default:
|
|
|
|
t.Error("Got unexpected file in index update", f.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gotA && gotB && gotConfl {
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
2018-09-16 07:48:14 +00:00
|
|
|
fd, err := tfs.OpenFile(b, fs.OptReadWrite, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
otherData := []byte("otherData")
|
|
|
|
if _, err = fd.Write(otherData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// rename
|
|
|
|
fc.deleteFile(a)
|
|
|
|
fc.updateFile(b, 0644, protocol.FileInfoTypeFile, data[a])
|
2019-02-12 15:05:20 +00:00
|
|
|
// Make sure the remote file for b is newer and thus stays global -> local conflict
|
|
|
|
fc.mut.Lock()
|
|
|
|
for i := range fc.files {
|
|
|
|
if fc.files[i].Name == b {
|
|
|
|
fc.files[i].ModifiedS += 100
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
2018-09-16 07:48:14 +00:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(10 * time.Second):
|
2019-02-12 15:05:20 +00:00
|
|
|
t.Errorf("timed out without receiving all expected index updates")
|
2018-09-16 07:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check outcome
|
|
|
|
tfs.Walk(".", func(path string, info fs.FileInfo, err error) error {
|
|
|
|
switch {
|
|
|
|
case path == a:
|
|
|
|
t.Errorf(`File "a" was not removed`)
|
|
|
|
case path == b:
|
2019-02-12 15:05:20 +00:00
|
|
|
if err := equalContents(filepath.Join(tmpDir, b), data[a]); err != nil {
|
|
|
|
t.Error(`File "b" has unexpected content (renamed from a on remote)`)
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(path, b+".sync-conflict-"):
|
|
|
|
if err := equalContents(filepath.Join(tmpDir, path), otherData); err != nil {
|
|
|
|
t.Error(`Sync conflict of "b" has unexptected content`)
|
2018-09-16 07:48:14 +00:00
|
|
|
}
|
2019-02-12 15:05:20 +00:00
|
|
|
case path == "." || strings.HasPrefix(path, ".stfolder"):
|
|
|
|
default:
|
|
|
|
t.Error("Found unexpected file", path)
|
2018-09-16 07:48:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestRemoteRenameConflict(t *testing.T) {
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
2019-03-08 20:29:09 +00:00
|
|
|
tfs := fcfg.Filesystem()
|
|
|
|
tmpDir := tfs.URI()
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, tmpDir)
|
2018-09-16 07:48:14 +00:00
|
|
|
|
|
|
|
recv := make(chan int)
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2018-09-16 07:48:14 +00:00
|
|
|
recv <- len(fs)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// setup
|
|
|
|
a := "a"
|
|
|
|
b := "b"
|
|
|
|
data := map[string][]byte{
|
|
|
|
a: []byte("aData"),
|
|
|
|
b: []byte("bData"),
|
|
|
|
}
|
|
|
|
for _, n := range [2]string{a, b} {
|
|
|
|
fc.addFile(n, 0644, protocol.FileInfoTypeFile, data[n])
|
|
|
|
}
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
select {
|
|
|
|
case i := <-recv:
|
|
|
|
if i != 2 {
|
|
|
|
t.Fatalf("received %v items in index, expected 1", i)
|
|
|
|
}
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range [2]string{a, b} {
|
2019-03-09 18:45:36 +00:00
|
|
|
must(t, equalContents(filepath.Join(tmpDir, n), data[n]))
|
2018-09-16 07:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := tfs.OpenFile(b, fs.OptReadWrite, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
otherData := []byte("otherData")
|
|
|
|
if _, err = fd.Write(otherData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
m.ScanFolders()
|
|
|
|
select {
|
|
|
|
case i := <-recv:
|
|
|
|
if i != 1 {
|
|
|
|
t.Fatalf("received %v items in index, expected 1", i)
|
|
|
|
}
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the following rename is more recent (not concurrent)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
// rename
|
|
|
|
fc.deleteFile(a)
|
|
|
|
fc.updateFile(b, 0644, protocol.FileInfoTypeFile, data[a])
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
select {
|
|
|
|
case <-recv:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check outcome
|
|
|
|
foundB := false
|
|
|
|
foundBConfl := false
|
|
|
|
tfs.Walk(".", func(path string, info fs.FileInfo, err error) error {
|
|
|
|
switch {
|
|
|
|
case path == a:
|
|
|
|
t.Errorf(`File "a" was not removed`)
|
|
|
|
case path == b:
|
|
|
|
foundB = true
|
|
|
|
case strings.HasPrefix(path, b) && strings.Contains(path, ".sync-conflict-"):
|
|
|
|
foundBConfl = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if !foundB {
|
|
|
|
t.Errorf(`File "b" was removed`)
|
|
|
|
}
|
|
|
|
if !foundBConfl {
|
|
|
|
t.Errorf(`No conflict file for "b" was created`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestDeleteChanged(t *testing.T) {
|
2019-05-19 12:29:07 +00:00
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
2019-03-08 20:29:09 +00:00
|
|
|
tfs := fcfg.Filesystem()
|
2019-05-19 12:29:07 +00:00
|
|
|
defer cleanupModelAndRemoveDir(m, tfs.URI())
|
2018-09-16 07:48:14 +00:00
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-05 18:01:56 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Error("More than one index update sent")
|
2019-02-05 18:01:56 +00:00
|
|
|
default:
|
|
|
|
}
|
2018-09-16 07:48:14 +00:00
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// setup
|
|
|
|
a := "a"
|
|
|
|
data := []byte("aData")
|
|
|
|
fc.addFile(a, 0644, protocol.FileInfoTypeFile, data)
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
done = make(chan struct{})
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
2019-02-05 18:01:56 +00:00
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-02-05 18:01:56 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
2019-07-30 15:50:51 +00:00
|
|
|
t.Error("More than one index update sent")
|
2019-02-05 18:01:56 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
2018-09-16 07:48:14 +00:00
|
|
|
fd, err := tfs.OpenFile(a, fs.OptReadWrite, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
otherData := []byte("otherData")
|
|
|
|
if _, err = fd.Write(otherData); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
// rename
|
|
|
|
fc.deleteFile(a)
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check outcome
|
|
|
|
if _, err := tfs.Lstat(a); err != nil {
|
2019-01-11 12:56:05 +00:00
|
|
|
if fs.IsNotExist(err) {
|
2018-09-16 07:48:14 +00:00
|
|
|
t.Error(`Modified file "a" was removed`)
|
|
|
|
} else {
|
|
|
|
t.Error(`Error stating file "a":`, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 17:39:52 +00:00
|
|
|
|
|
|
|
func TestNeedFolderFiles(t *testing.T) {
|
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
tfs := fcfg.Filesystem()
|
|
|
|
tmpDir := tfs.URI()
|
|
|
|
defer cleanupModelAndRemoveDir(m, tmpDir)
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
sub := m.evLogger.Subscribe(events.RemoteIndexUpdated)
|
|
|
|
defer sub.Unsubscribe()
|
2019-07-19 17:39:52 +00:00
|
|
|
|
|
|
|
errPreventSync := errors.New("you aren't getting any of this")
|
|
|
|
fc.mut.Lock()
|
2019-11-19 08:56:53 +00:00
|
|
|
fc.requestFn = func(context.Context, string, string, int64, int, []byte, bool) ([]byte, error) {
|
2019-07-19 17:39:52 +00:00
|
|
|
return nil, errPreventSync
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
data := []byte("foo")
|
|
|
|
num := 20
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
fc.addFile(strconv.Itoa(i), 0644, protocol.FileInfoTypeFile, data)
|
|
|
|
}
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sub.C():
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("Timed out before receiving index")
|
|
|
|
}
|
|
|
|
|
|
|
|
progress, queued, rest := m.NeedFolderFiles(fcfg.ID, 1, 100)
|
|
|
|
if got := len(progress) + len(queued) + len(rest); got != num {
|
|
|
|
t.Errorf("Got %v needed items, expected %v", got, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := 10
|
|
|
|
for page := 1; page < 3; page++ {
|
|
|
|
progress, queued, rest := m.NeedFolderFiles(fcfg.ID, page, exp)
|
|
|
|
if got := len(progress) + len(queued) + len(rest); got != exp {
|
|
|
|
t.Errorf("Got %v needed items on page %v, expected %v", got, page, exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 13:34:59 +00:00
|
|
|
|
|
|
|
// TestIgnoreDeleteUnignore checks that the deletion of an ignored file is not
|
|
|
|
// propagated upon un-ignoring.
|
|
|
|
// https://github.com/syncthing/syncthing/issues/6038
|
|
|
|
func TestIgnoreDeleteUnignore(t *testing.T) {
|
|
|
|
w, fcfg := tmpDefaultWrapper()
|
|
|
|
m := setupModel(w)
|
|
|
|
fss := fcfg.Filesystem()
|
|
|
|
tmpDir := fss.URI()
|
|
|
|
defer cleanupModelAndRemoveDir(m, tmpDir)
|
|
|
|
|
2019-11-08 09:56:16 +00:00
|
|
|
m.removeFolder(fcfg)
|
|
|
|
m.addFolder(fcfg)
|
2019-10-01 13:34:59 +00:00
|
|
|
// Reach in and update the ignore matcher to one that always does
|
|
|
|
// reloads when asked to, instead of checking file mtimes. This is
|
|
|
|
// because we might be changing the files on disk often enough that the
|
|
|
|
// mtimes will be unreliable to determine change status.
|
|
|
|
m.fmut.Lock()
|
|
|
|
m.folderIgnores["default"] = ignore.New(fss, ignore.WithChangeDetector(newAlwaysChanged()))
|
|
|
|
m.fmut.Unlock()
|
2019-11-08 09:56:16 +00:00
|
|
|
m.startFolder(fcfg.ID)
|
2019-10-01 13:34:59 +00:00
|
|
|
|
|
|
|
fc := addFakeConn(m, device1)
|
|
|
|
fc.folder = "default"
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
file := "foobar"
|
|
|
|
contents := []byte("test file contents\n")
|
|
|
|
|
|
|
|
basicCheck := func(fs []protocol.FileInfo) {
|
|
|
|
t.Helper()
|
|
|
|
if len(fs) != 1 {
|
|
|
|
t.Fatal("expected a single index entry, got", len(fs))
|
|
|
|
} else if fs[0].Name != file {
|
|
|
|
t.Fatalf("expected a index entry for %v, got one for %v", file, fs[0].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-10-01 13:34:59 +00:00
|
|
|
basicCheck(fs)
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(fss.URI(), file), contents, 0644); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
m.ScanFolders()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("timed out before index was received")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
|
|
|
|
done = make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-10-01 13:34:59 +00:00
|
|
|
basicCheck(fs)
|
|
|
|
f := fs[0]
|
|
|
|
if !f.IsInvalid() {
|
|
|
|
t.Errorf("Received non-invalid index update")
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
if err := m.SetIgnores("default", []string{"foobar"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("timed out before receiving index update")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
|
|
|
|
done = make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
2019-11-25 10:07:36 +00:00
|
|
|
fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
|
2019-10-01 13:34:59 +00:00
|
|
|
basicCheck(fs)
|
|
|
|
f := fs[0]
|
|
|
|
if f.IsInvalid() {
|
|
|
|
t.Errorf("Received invalid index update")
|
|
|
|
}
|
|
|
|
if !f.Version.Equal(protocol.Vector{}) && f.Deleted {
|
|
|
|
t.Error("Received deleted index entry with non-empty version")
|
|
|
|
}
|
|
|
|
l.Infoln(f)
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
if err := fss.Remove(file); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := m.SetIgnores("default", []string{}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("timed out before index was received")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 11:21:22 +00:00
|
|
|
|
|
|
|
// TestRequestLastFileProgress checks that the last pulled file (here only) is registered
|
|
|
|
// as in progress.
|
|
|
|
func TestRequestLastFileProgress(t *testing.T) {
|
|
|
|
m, fc, fcfg := setupModelWithConnection()
|
|
|
|
tfs := fcfg.Filesystem()
|
|
|
|
defer cleanupModelAndRemoveDir(m, tfs.URI())
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.requestFn = func(_ context.Context, folder, name string, _ int64, _ int, _ []byte, _ bool) ([]byte, error) {
|
|
|
|
defer close(done)
|
|
|
|
progress, queued, rest := m.NeedFolderFiles(folder, 1, 10)
|
|
|
|
if len(queued)+len(rest) != 0 {
|
|
|
|
t.Error(`There should not be any queued or "rest" items`)
|
|
|
|
}
|
|
|
|
if len(progress) != 1 {
|
|
|
|
t.Error("Expected exactly one item in progress.")
|
|
|
|
}
|
|
|
|
return fc.fileData[name], nil
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
contents := []byte("test file contents\n")
|
|
|
|
fc.addFile("testfile", 0644, protocol.FileInfoTypeFile, contents)
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("Timed out before file was requested")
|
|
|
|
}
|
|
|
|
}
|