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"
|
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"
|
|
|
|
"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"
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
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.
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
m, fc, tmpDir := setupModelWithConnection()
|
2016-11-30 09:32:28 +00:00
|
|
|
defer m.Stop()
|
2018-02-25 08:39:00 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
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()
|
2016-11-30 09:32:28 +00:00
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
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
|
2018-02-25 08:39:00 +00:00
|
|
|
if err := equalContents(filepath.Join(tmpDir, "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
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
m, fc, tmpDir := setupModelWithConnection()
|
2016-12-13 10:24:10 +00:00
|
|
|
defer m.Stop()
|
2018-02-25 08:39:00 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
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()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
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
|
|
|
|
buf := make([]byte, 10)
|
2018-05-05 08:24:44 +00:00
|
|
|
err := m.Request(device1, "default", "symlink/requests_test.go", 0, nil, 0, false, buf)
|
2016-12-13 10:24:10 +00:00
|
|
|
if err == nil || !bytes.Equal(buf, make([]byte, 10)) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
m, fc, tmpDir := setupModelWithConnection()
|
2016-12-13 10:24:10 +00:00
|
|
|
defer m.Stop()
|
2018-02-25 08:39:00 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
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()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "symlink" {
|
|
|
|
done <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(f.Name, "symlink") {
|
|
|
|
badIdx <- f.Name
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.requestFn = func(folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
|
|
|
|
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
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
m, fc, tmpDir := setupModelWithConnection()
|
2016-12-13 10:24:10 +00:00
|
|
|
defer m.Stop()
|
2018-02-25 08:39:00 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
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()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
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 {
|
|
|
|
t.Fatal("Received index with non-invalid temporary file")
|
|
|
|
}
|
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
|
|
|
|
|
2018-03-13 13:03:10 +00:00
|
|
|
tmpDir := createTmpDir()
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
cfg := defaultCfgWrapper.RawCopy()
|
2018-02-25 08:39:00 +00:00
|
|
|
cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpDir)
|
2017-07-25 09:36:09 +00:00
|
|
|
cfg.Folders[0].Devices = []config.FolderDeviceConfiguration{
|
|
|
|
{DeviceID: device1},
|
|
|
|
{DeviceID: device2},
|
|
|
|
}
|
|
|
|
cfg.Folders[0].Versioning = config.VersioningConfiguration{
|
|
|
|
Type: "trashcan",
|
|
|
|
}
|
2018-03-13 13:03:10 +00:00
|
|
|
w, path := createTmpWrapper(cfg)
|
|
|
|
defer os.Remove(path)
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
db := db.OpenMemory()
|
|
|
|
m := NewModel(w, device1, "syncthing", "dev", db, nil)
|
|
|
|
m.AddFolder(cfg.Folders[0])
|
|
|
|
m.ServeBackground()
|
|
|
|
m.StartFolder("default")
|
|
|
|
defer m.Stop()
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
2017-07-25 09:36:09 +00:00
|
|
|
|
|
|
|
fc := addFakeConn(m, device2)
|
|
|
|
fc.folder = "default"
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
idx := make(chan int)
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
idx <- len(fs)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the symlink, hoping for it to get versioned
|
|
|
|
fc.deleteFile("foo")
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate foo and a file in it with some data
|
|
|
|
fc.addFile("foo", 0755, protocol.FileInfoTypeDirectory, nil)
|
|
|
|
fc.addFile("foo/test", 0644, protocol.FileInfoTypeFile, []byte("testtesttest"))
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the test file and see if it escaped
|
|
|
|
fc.deleteFile("foo/test")
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
2018-03-13 13:03:10 +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) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
tmpDir := createTmpDir()
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2018-03-13 13:03:10 +00:00
|
|
|
cfg := defaultCfgWrapper.RawCopy()
|
2018-02-25 08:39:00 +00:00
|
|
|
cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2"))
|
|
|
|
cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpDir)
|
|
|
|
cfg.Folders[0].Devices = []config.FolderDeviceConfiguration{
|
|
|
|
{DeviceID: device1},
|
|
|
|
{DeviceID: device2},
|
2017-11-11 19:18:17 +00:00
|
|
|
}
|
2018-02-25 08:39:00 +00:00
|
|
|
cfg.Folders[0].Type = ft
|
|
|
|
m, fc := setupModelWithConnectionManual(cfg)
|
|
|
|
defer m.Stop()
|
|
|
|
|
|
|
|
// 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(cfg.Folders[0].Filesystem(), ignore.WithChangeDetector(newAlwaysChanged()))
|
|
|
|
m.fmut.Unlock()
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, ignExisting), otherContents, 0644); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
done <- struct{}{}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
sub := events.Default.Subscribe(events.FolderErrors)
|
|
|
|
defer events.Default.Unsubscribe(sub)
|
|
|
|
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
timeout := time.NewTimer(5 * time.Second)
|
|
|
|
select {
|
|
|
|
case ev := <-sub.C():
|
|
|
|
t.Fatalf("Errors while pulling: %v", ev)
|
|
|
|
case <-timeout.C:
|
|
|
|
t.Fatalf("timed out before index was received")
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
expected := map[string]struct{}{ign: {}, ignExisting: {}}
|
|
|
|
for _, f := range fs {
|
|
|
|
if _, ok := expected[f.Name]; !ok {
|
|
|
|
t.Fatalf("Unexpected file %v was updated in index", f.Name)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
// The unignored files should only have a local version,
|
|
|
|
// to mark them as in conflict with any other existing versions.
|
|
|
|
ev := protocol.Vector{}.Update(device1.Short())
|
|
|
|
if v := f.Version; !v.Equal(ev) {
|
|
|
|
t.Errorf("File %v has version %v, expected %v", f.Name, v, ev)
|
|
|
|
}
|
|
|
|
if f.Name == ign {
|
|
|
|
if !f.Deleted {
|
|
|
|
t.Errorf("File %v was not marked as deleted", f.Name)
|
|
|
|
}
|
|
|
|
} else if f.Deleted {
|
|
|
|
t.Errorf("File %v is marked as deleted", f.Name)
|
|
|
|
}
|
|
|
|
delete(expected, f.Name)
|
|
|
|
}
|
|
|
|
for name := range expected {
|
|
|
|
t.Errorf("File %v wasn't updated in index", name)
|
|
|
|
}
|
|
|
|
done <- struct{}{}
|
|
|
|
}
|
|
|
|
// Make sure pulling doesn't interfere, as index updates are racy and
|
|
|
|
// thus we cannot distinguish between scan and pull results.
|
|
|
|
fc.requestFn = func(folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
if err := m.SetIgnores("default", []string{"*:ignored*"}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = time.NewTimer(5 * time.Second)
|
|
|
|
select {
|
|
|
|
case <-timeout.C:
|
|
|
|
t.Fatalf("timed out before index was received")
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-27 20:24:20 +00:00
|
|
|
func TestIssue4841(t *testing.T) {
|
|
|
|
m, fc, tmpDir := setupModelWithConnection()
|
|
|
|
defer m.Stop()
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
received := make(chan protocol.FileInfo)
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
received <- fs[0]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Setup file from remote that was ignored locally
|
|
|
|
m.updateLocals(defaultFolderConfig.ID, []protocol.FileInfo{{
|
2018-06-24 07:50:18 +00:00
|
|
|
Name: "foo",
|
|
|
|
Type: protocol.FileInfoTypeFile,
|
|
|
|
LocalFlags: protocol.FlagLocalIgnored,
|
|
|
|
Version: protocol.Vector{}.Update(device2.Short()),
|
2018-03-27 20:24:20 +00:00
|
|
|
}})
|
|
|
|
<-received
|
|
|
|
|
|
|
|
// Scan without ignore patterns with "foo" not existing locally
|
|
|
|
if err := m.ScanFolder("default"); err != nil {
|
|
|
|
t.Fatal("Failed scanning:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := <-received
|
|
|
|
if expected := (protocol.Vector{}.Update(device1.Short())); !f.Version.Equal(expected) {
|
|
|
|
t.Errorf("Got Version == %v, expected %v", f.Version, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 08:24:44 +00:00
|
|
|
func TestRescanIfHaveInvalidContent(t *testing.T) {
|
|
|
|
m, fc, tmpDir := setupModelWithConnection()
|
|
|
|
defer m.Stop()
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
payload := []byte("hello")
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
received := make(chan protocol.FileInfo)
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
received <- fs[0]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Scan without ignore patterns with "foo" not existing locally
|
|
|
|
if err := m.ScanFolder("default"); err != nil {
|
|
|
|
t.Fatal("Failed scanning:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := <-received
|
|
|
|
if f.Blocks[0].WeakHash != 103547413 {
|
|
|
|
t.Fatalf("unexpected weak hash: %d != 103547413", f.Blocks[0].WeakHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, len(payload))
|
|
|
|
|
|
|
|
err := m.Request(device2, "default", "foo", 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(buf, payload) {
|
|
|
|
t.Errorf("%s != %s", buf, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = []byte("bye")
|
|
|
|
buf = make([]byte, len(payload))
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, "foo"), payload, 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.Request(device2, "default", "foo", 0, f.Blocks[0].Hash, f.Blocks[0].WeakHash, false, buf)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case f := <-received:
|
|
|
|
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-02-25 08:39:00 +00:00
|
|
|
func setupModelWithConnection() (*Model, *fakeConnection, string) {
|
|
|
|
tmpDir := createTmpDir()
|
2018-03-13 13:03:10 +00:00
|
|
|
cfg := defaultCfgWrapper.RawCopy()
|
2018-02-25 08:39:00 +00:00
|
|
|
cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2"))
|
|
|
|
cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpDir)
|
2016-11-30 09:32:28 +00:00
|
|
|
cfg.Folders[0].Devices = []config.FolderDeviceConfiguration{
|
|
|
|
{DeviceID: device1},
|
|
|
|
{DeviceID: device2},
|
|
|
|
}
|
2018-02-25 08:39:00 +00:00
|
|
|
m, fc := setupModelWithConnectionManual(cfg)
|
|
|
|
return m, fc, tmpDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupModelWithConnectionManual(cfg config.Configuration) (*Model, *fakeConnection) {
|
2018-03-13 13:03:10 +00:00
|
|
|
w, path := createTmpWrapper(cfg)
|
|
|
|
defer os.Remove(path)
|
2016-11-30 09:32:28 +00:00
|
|
|
|
|
|
|
db := db.OpenMemory()
|
2017-05-22 19:58:33 +00:00
|
|
|
m := NewModel(w, device1, "syncthing", "dev", db, nil)
|
2016-11-30 09:32:28 +00:00
|
|
|
m.AddFolder(cfg.Folders[0])
|
|
|
|
m.ServeBackground()
|
|
|
|
m.StartFolder("default")
|
|
|
|
|
|
|
|
fc := addFakeConn(m, device2)
|
|
|
|
fc.folder = "default"
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
m.ScanFolder("default")
|
|
|
|
|
|
|
|
return m, fc
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTmpDir() string {
|
2018-03-13 13:03:10 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("testdata", "_request-")
|
2018-02-25 08:39:00 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("Failed to create temporary testing dir")
|
|
|
|
}
|
|
|
|
return tmpDir
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|