2018-07-12 01:15:57 -07:00
|
|
|
// Copyright (C) 2018 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 (
|
2018-08-19 14:34:26 -07:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-08-27 06:54:00 -07:00
|
|
|
"path/filepath"
|
2018-07-12 01:15:57 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2021-06-03 06:08:56 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2018-07-12 01:15:57 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2018-08-19 14:34:26 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/scanner"
|
2018-07-12 01:15:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecvOnlyRevertDeletes(t *testing.T) {
|
|
|
|
// Make sure that we delete extraneous files and directories when we hit
|
|
|
|
// Revert.
|
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
// Get us a model up and running
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2019-04-07 04:29:17 -07:00
|
|
|
ffs := f.Filesystem()
|
2020-02-11 23:47:05 -07:00
|
|
|
defer cleanupModel(m)
|
2021-06-03 05:58:50 -07:00
|
|
|
addFakeConn(m, device1, f.ID)
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
// Create some test data
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
for _, dir := range []string{".stfolder", "ignDir", "unknownDir"} {
|
|
|
|
must(t, ffs.MkdirAll(dir, 0755))
|
|
|
|
}
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, writeFile(ffs, "ignDir/ignFile", []byte("hello\n"), 0644))
|
|
|
|
must(t, writeFile(ffs, "unknownDir/unknownFile", []byte("hello\n"), 0644))
|
|
|
|
must(t, writeFile(ffs, ".stignore", []byte("ignDir\n"), 0644))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
knownFiles := setupKnownFiles(t, ffs, []byte("hello\n"))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
// Send and index update for the known stuff
|
|
|
|
|
2021-05-16 08:23:27 -07:00
|
|
|
must(t, m.Index(device1, "ro", knownFiles))
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2020-07-30 04:49:14 -07:00
|
|
|
size := globalSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
// Scan, should discover the other stuff in the folder
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2020-07-30 04:49:14 -07:00
|
|
|
// We should now have two files and two directories, with global state unchanged.
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size = globalSize(t, m, "ro")
|
2020-07-30 04:49:14 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
2018-07-12 01:15:57 -07:00
|
|
|
t.Fatalf("Global: expected 2 files and 2 directories: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 2 || size.Directories != 2 {
|
|
|
|
t.Fatalf("Local: expected 2 files and 2 directories: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files+size.Directories == 0 {
|
|
|
|
t.Fatalf("ROChanged: expected something: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revert should delete the unknown stuff
|
|
|
|
|
|
|
|
m.Revert("ro")
|
|
|
|
|
|
|
|
// These should still exist
|
2019-03-20 10:38:03 -07:00
|
|
|
for _, p := range []string{"knownDir/knownFile", "ignDir/ignFile"} {
|
|
|
|
if _, err := ffs.Stat(p); err != nil {
|
2018-07-12 01:15:57 -07:00
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These should have been removed
|
2019-03-20 10:38:03 -07:00
|
|
|
for _, p := range []string{"unknownDir", "unknownDir/unknownFile"} {
|
|
|
|
if _, err := ffs.Stat(p); !fs.IsNotExist(err) {
|
2018-07-12 01:15:57 -07:00
|
|
|
t.Error("Unexpected existing thing:", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should now have one file and directory again.
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size = globalSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 files and 1 directories: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 files and 1 directories: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecvOnlyRevertNeeds(t *testing.T) {
|
|
|
|
// Make sure that a new file gets picked up and considered latest, then
|
|
|
|
// gets considered old when we hit Revert.
|
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2019-04-07 04:29:17 -07:00
|
|
|
ffs := f.Filesystem()
|
2020-02-11 23:47:05 -07:00
|
|
|
defer cleanupModel(m)
|
2021-06-03 05:58:50 -07:00
|
|
|
addFakeConn(m, device1, f.ID)
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0755))
|
2018-07-12 01:15:57 -07:00
|
|
|
oldData := []byte("hello\n")
|
2019-03-20 10:38:03 -07:00
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
// Send and index update for the known stuff
|
|
|
|
|
2021-05-16 08:23:27 -07:00
|
|
|
must(t, m.Index(device1, "ro", knownFiles))
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
// Scan the folder.
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size := globalSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 08:01:23 -07:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the file.
|
|
|
|
|
|
|
|
newData := []byte("totally different data\n")
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, writeFile(ffs, "knownDir/knownFile", newData, 0644))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
// Rescan.
|
|
|
|
|
2019-03-09 11:45:36 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
// We now have a newer file than the rest of the cluster. Global state should reflect this.
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size = globalSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
const sizeOfDir = 128
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(oldData)) {
|
|
|
|
t.Fatalf("Global: expected no change due to the new file: %+v", size)
|
2018-07-12 01:15:57 -07:00
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(newData)) {
|
|
|
|
t.Fatalf("Local: expected the new file to be reflected: %+v", size)
|
|
|
|
}
|
2021-06-05 08:01:23 -07:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files+size.Directories == 0 {
|
|
|
|
t.Fatalf("ROChanged: expected something: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We hit the Revert button. The file that was new should become old.
|
|
|
|
|
|
|
|
m.Revert("ro")
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size = globalSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(oldData)) {
|
|
|
|
t.Fatalf("Global: expected the global size to revert: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = localSize(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Bytes != sizeOfDir+int64(len(newData)) {
|
|
|
|
t.Fatalf("Local: expected the local size to remain: %+v", size)
|
|
|
|
}
|
2021-06-05 08:01:23 -07:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-07-12 01:15:57 -07:00
|
|
|
if size.Files != 1 || size.Bytes != int64(len(oldData)) {
|
|
|
|
t.Fatalf("Local: expected to need the old file data: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:43:07 -07:00
|
|
|
func TestRecvOnlyUndoChanges(t *testing.T) {
|
2019-03-20 10:38:03 -07:00
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2019-04-07 04:29:17 -07:00
|
|
|
ffs := f.Filesystem()
|
2020-02-11 23:47:05 -07:00
|
|
|
defer cleanupModel(m)
|
2021-06-03 05:58:50 -07:00
|
|
|
addFakeConn(m, device1, f.ID)
|
2018-10-10 03:43:07 -07:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0755))
|
2018-10-10 03:43:07 -07:00
|
|
|
oldData := []byte("hello\n")
|
2019-03-20 10:38:03 -07:00
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
// Send an index update for the known stuff
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2021-05-16 08:23:27 -07:00
|
|
|
must(t, m.Index(device1, "ro", knownFiles))
|
2019-04-07 04:29:17 -07:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
// Scan the folder.
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-10-10 03:43:07 -07:00
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size := globalSize(t, m, "ro")
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = localSize(t, m, "ro")
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 08:01:23 -07:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
2020-01-21 10:23:08 -07:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file and modify another
|
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
const file = "foo"
|
|
|
|
must(t, writeFile(ffs, file, []byte("hello\n"), 0644))
|
|
|
|
must(t, writeFile(ffs, "knownDir/knownFile", []byte("bye\n"), 0644))
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files != 2 {
|
|
|
|
t.Fatalf("Receive only: expected 2 files: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the file again and undo the modification
|
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, ffs.Remove(file))
|
|
|
|
must(t, writeFile(ffs, "knownDir/knownFile", oldData, 0644))
|
|
|
|
must(t, ffs.Chtimes("knownDir/knownFile", knownFiles[1].ModTime(), knownFiles[1].ModTime()))
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-10-10 03:43:07 -07:00
|
|
|
|
2020-01-21 10:23:08 -07:00
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
2018-10-10 03:43:07 -07:00
|
|
|
if size.Files+size.Directories+size.Deleted != 0 {
|
|
|
|
t.Fatalf("Receive only: expected all zero: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 07:14:25 -07:00
|
|
|
func TestRecvOnlyDeletedRemoteDrop(t *testing.T) {
|
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2020-04-02 07:14:25 -07:00
|
|
|
ffs := f.Filesystem()
|
|
|
|
defer cleanupModel(m)
|
2021-06-03 05:58:50 -07:00
|
|
|
addFakeConn(m, device1, f.ID)
|
2020-04-02 07:14:25 -07:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0755))
|
|
|
|
oldData := []byte("hello\n")
|
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
|
|
|
|
|
|
|
// Send an index update for the known stuff
|
|
|
|
|
2021-05-16 08:23:27 -07:00
|
|
|
must(t, m.Index(device1, "ro", knownFiles))
|
2020-04-02 07:14:25 -07:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
|
|
|
|
|
|
|
// Scan the folder.
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
|
|
|
size := globalSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
size = localSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 08:01:23 -07:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2020-04-02 07:14:25 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete our file
|
|
|
|
|
|
|
|
must(t, ffs.Remove(knownFiles[1].Name))
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Deleted != 1 {
|
|
|
|
t.Fatalf("Receive only: expected 1 deleted: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the remote
|
|
|
|
|
|
|
|
f.fset.Drop(device1)
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Deleted != 0 {
|
|
|
|
t.Fatalf("Receive only: expected no deleted: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 06:54:00 -07:00
|
|
|
func TestRecvOnlyRemoteUndoChanges(t *testing.T) {
|
|
|
|
// Get us a model up and running
|
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
2020-08-27 06:54:00 -07:00
|
|
|
ffs := f.Filesystem()
|
|
|
|
defer cleanupModel(m)
|
2021-06-03 05:58:50 -07:00
|
|
|
addFakeConn(m, device1, f.ID)
|
2020-08-27 06:54:00 -07:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0755))
|
|
|
|
oldData := []byte("hello\n")
|
|
|
|
knownFiles := setupKnownFiles(t, ffs, oldData)
|
|
|
|
|
|
|
|
// Send an index update for the known stuff
|
|
|
|
|
2021-05-16 08:23:27 -07:00
|
|
|
must(t, m.Index(device1, "ro", knownFiles))
|
2020-08-27 06:54:00 -07:00
|
|
|
f.updateLocalsFromScanning(knownFiles)
|
|
|
|
|
|
|
|
// Scan the folder.
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
// Everything should be in sync.
|
|
|
|
|
|
|
|
size := globalSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Global: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
|
|
|
size = localSize(t, m, "ro")
|
|
|
|
if size.Files != 1 || size.Directories != 1 {
|
|
|
|
t.Fatalf("Local: expected 1 file and 1 directory: %+v", size)
|
|
|
|
}
|
2021-06-05 08:01:23 -07:00
|
|
|
size = needSizeLocal(t, m, "ro")
|
2020-08-27 06:54:00 -07:00
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("Need: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories > 0 {
|
|
|
|
t.Fatalf("ROChanged: expected nothing: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file and modify another
|
|
|
|
|
|
|
|
const file = "foo"
|
|
|
|
knownFile := filepath.Join("knownDir", "knownFile")
|
|
|
|
must(t, writeFile(ffs, file, []byte("hello\n"), 0644))
|
|
|
|
must(t, writeFile(ffs, knownFile, []byte("bye\n"), 0644))
|
|
|
|
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files != 2 {
|
|
|
|
t.Fatalf("Receive only: expected 2 files: %+v", size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the same changes on the remote
|
|
|
|
|
|
|
|
files := make([]protocol.FileInfo, 0, 2)
|
2021-03-07 05:43:22 -07:00
|
|
|
snap := fsetSnapshot(t, f.fset)
|
2020-08-27 07:20:31 -07:00
|
|
|
snap.WithHave(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
|
2020-08-27 06:54:00 -07:00
|
|
|
if n := fi.FileName(); n != file && n != knownFile {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
f := fi.(protocol.FileInfo)
|
|
|
|
f.LocalFlags = 0
|
|
|
|
f.Version = protocol.Vector{}.Update(device1.Short())
|
|
|
|
files = append(files, f)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
snap.Release()
|
2021-05-16 08:23:27 -07:00
|
|
|
must(t, m.IndexUpdate(device1, "ro", files))
|
2020-08-27 06:54:00 -07:00
|
|
|
|
2020-12-20 10:13:35 -07:00
|
|
|
// Ensure the pull to resolve conflicts (content identical) happened
|
|
|
|
must(t, f.doInSync(func() error {
|
|
|
|
f.pull()
|
|
|
|
return nil
|
|
|
|
}))
|
2020-08-27 06:54:00 -07:00
|
|
|
|
|
|
|
size = receiveOnlyChangedSize(t, m, "ro")
|
|
|
|
if size.Files+size.Directories+size.Deleted != 0 {
|
|
|
|
t.Fatalf("Receive only: expected all zero: %+v", size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 06:08:56 -07:00
|
|
|
func TestRecvOnlyRevertOwnID(t *testing.T) {
|
|
|
|
// If the folder was receive-only in the past, the global item might have
|
|
|
|
// only our id in the version vector and be valid. There was a bug based on
|
|
|
|
// the incorrect assumption that this can never happen.
|
|
|
|
|
|
|
|
// Get us a model up and running
|
|
|
|
|
|
|
|
m, f, wcfgCancel := setupROFolder(t)
|
|
|
|
defer wcfgCancel()
|
|
|
|
ffs := f.Filesystem()
|
|
|
|
defer cleanupModel(m)
|
2021-06-04 06:32:47 -07:00
|
|
|
addFakeConn(m, device1, f.ID)
|
2021-06-03 06:08:56 -07:00
|
|
|
|
|
|
|
// Create some test data
|
|
|
|
|
|
|
|
must(t, ffs.MkdirAll(".stfolder", 0755))
|
|
|
|
data := []byte("hello\n")
|
|
|
|
name := "foo"
|
|
|
|
must(t, writeFile(ffs, name, data, 0644))
|
|
|
|
|
|
|
|
// Make sure the file is scanned and locally changed
|
|
|
|
must(t, m.ScanFolder("ro"))
|
|
|
|
fi, ok := m.testCurrentFolderFile(f.ID, name)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("File missing")
|
|
|
|
} else if !fi.IsReceiveOnlyChanged() {
|
|
|
|
t.Fatal("File should be receiveonly changed")
|
|
|
|
}
|
|
|
|
fi.LocalFlags = 0
|
|
|
|
v := fi.Version.Counters[0].Value
|
|
|
|
fi.Version.Counters[0].Value = uint64(time.Unix(int64(v), 0).Add(-10 * time.Second).Unix())
|
|
|
|
|
|
|
|
// Monitor the outcome
|
|
|
|
sub := f.evLogger.Subscribe(events.LocalIndexUpdated)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sub.C():
|
|
|
|
if file, _ := m.testCurrentFolderFile(f.ID, name); file.Deleted {
|
|
|
|
t.Error("local file was deleted")
|
|
|
|
cancel()
|
|
|
|
} else if file.IsEquivalent(fi, f.modTimeWindow) {
|
|
|
|
cancel() // That's what we are waiting for
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Receive an index update with an older version, but valid and then revert
|
|
|
|
must(t, m.Index(device1, f.ID, []protocol.FileInfo{fi}))
|
|
|
|
f.Revert()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
func setupKnownFiles(t *testing.T, ffs fs.Filesystem, data []byte) []protocol.FileInfo {
|
|
|
|
t.Helper()
|
2019-01-11 05:56:05 -07:00
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
must(t, ffs.MkdirAll("knownDir", 0755))
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, writeFile(ffs, "knownDir/knownFile", data, 0644))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
|
|
|
t0 := time.Now().Add(-1 * time.Minute)
|
2019-03-20 10:38:03 -07:00
|
|
|
must(t, ffs.Chtimes("knownDir/knownFile", t0, t0))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2019-03-20 10:38:03 -07:00
|
|
|
fi, err := ffs.Stat("knownDir/knownFile")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-08-19 14:34:26 -07:00
|
|
|
blocks, _ := scanner.Blocks(context.TODO(), bytes.NewReader(data), protocol.BlockSize(int64(len(data))), int64(len(data)), nil, true)
|
2018-07-12 01:15:57 -07:00
|
|
|
knownFiles := []protocol.FileInfo{
|
|
|
|
{
|
|
|
|
Name: "knownDir",
|
|
|
|
Type: protocol.FileInfoTypeDirectory,
|
|
|
|
Permissions: 0755,
|
|
|
|
Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 42}}},
|
|
|
|
Sequence: 42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "knownDir/knownFile",
|
|
|
|
Type: protocol.FileInfoTypeFile,
|
|
|
|
Permissions: 0644,
|
|
|
|
Size: fi.Size(),
|
|
|
|
ModifiedS: fi.ModTime().Unix(),
|
2020-10-01 23:07:05 -07:00
|
|
|
ModifiedNs: int(fi.ModTime().UnixNano() % 1e9),
|
2018-07-12 01:15:57 -07:00
|
|
|
Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 42}}},
|
|
|
|
Sequence: 42,
|
2018-08-19 14:34:26 -07:00
|
|
|
Blocks: blocks,
|
2018-07-12 01:15:57 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return knownFiles
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
func setupROFolder(t *testing.T) (*testModel, *receiveOnlyFolder, context.CancelFunc) {
|
2020-02-11 23:47:05 -07:00
|
|
|
t.Helper()
|
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
w, cancel := createTmpWrapper(defaultCfg)
|
2020-04-02 07:14:25 -07:00
|
|
|
cfg := w.RawCopy()
|
2020-02-11 23:47:05 -07:00
|
|
|
fcfg := testFolderConfigFake()
|
2019-03-20 10:38:03 -07:00
|
|
|
fcfg.ID = "ro"
|
2020-02-11 23:47:05 -07:00
|
|
|
fcfg.Label = "ro"
|
2018-07-12 01:15:57 -07:00
|
|
|
fcfg.Type = config.FolderTypeReceiveOnly
|
2020-04-02 07:14:25 -07:00
|
|
|
cfg.Folders = []config.FolderConfiguration{fcfg}
|
2021-01-15 07:43:34 -07:00
|
|
|
replace(t, w, cfg)
|
2019-11-29 01:29:59 -07:00
|
|
|
|
2020-12-21 04:59:22 -07:00
|
|
|
m := newModel(t, w, myID, "syncthing", "dev", nil)
|
2019-11-22 13:30:16 -07:00
|
|
|
m.ServeBackground()
|
2020-11-17 05:19:04 -07:00
|
|
|
<-m.started
|
2020-02-11 23:47:05 -07:00
|
|
|
must(t, m.ScanFolder("ro"))
|
2018-07-12 01:15:57 -07:00
|
|
|
|
2019-11-22 13:30:16 -07:00
|
|
|
m.fmut.RLock()
|
2020-02-11 23:47:05 -07:00
|
|
|
defer m.fmut.RUnlock()
|
|
|
|
f := m.folderRunners["ro"].(*receiveOnlyFolder)
|
2019-03-20 10:38:03 -07:00
|
|
|
|
2021-01-15 07:43:34 -07:00
|
|
|
return m, f, cancel
|
2019-11-29 01:29:59 -07:00
|
|
|
}
|
2020-02-11 23:47:05 -07:00
|
|
|
|
|
|
|
func writeFile(fs fs.Filesystem, filename string, data []byte, perm fs.FileMode) error {
|
|
|
|
fd, err := fs.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fd.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fs.Chmod(filename, perm)
|
|
|
|
}
|