2015-06-12 11:04:00 +00:00
|
|
|
// Copyright (C) 2015 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/.
|
2015-06-12 11:04:00 +00:00
|
|
|
|
|
|
|
package versioner
|
|
|
|
|
|
|
|
import (
|
2021-11-22 07:59:47 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2015-06-12 11:04:00 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2020-07-14 08:48:50 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-06-12 11:04:00 +00:00
|
|
|
)
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
func TestTrashcanArchiveRestoreSwitcharoo(t *testing.T) {
|
|
|
|
// This tests that trashcan versioner restoration correctly archives existing file, because trashcan versioner
|
|
|
|
// files are untagged, archiving existing file to replace with a restored version technically should collide in
|
|
|
|
// in names.
|
2021-11-22 07:59:47 +00:00
|
|
|
tmpDir1, err := os.MkdirTemp("", "")
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-11-22 07:59:47 +00:00
|
|
|
tmpDir2, err := os.MkdirTemp("", "")
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
cfg := config.FolderConfiguration{
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: tmpDir1,
|
|
|
|
Versioning: config.VersioningConfiguration{
|
2021-02-26 11:04:05 +00:00
|
|
|
FSType: fs.FilesystemTypeBasic,
|
|
|
|
FSPath: tmpDir2,
|
2020-06-18 06:15:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
folderFs := cfg.Filesystem()
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
versionsFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir2)
|
|
|
|
|
|
|
|
writeFile(t, folderFs, "file", "A")
|
|
|
|
|
2020-06-18 06:15:47 +00:00
|
|
|
versioner := newTrashcan(cfg)
|
2019-04-28 22:30:16 +00:00
|
|
|
|
|
|
|
if err := versioner.Archive("file"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := folderFs.Stat("file"); !fs.IsNotExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
// Check versions
|
|
|
|
versions, err := versioner.GetVersions()
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
fileVersions := versions["file"]
|
|
|
|
if len(fileVersions) != 1 {
|
|
|
|
t.Fatalf("unexpected number of versions: %d != 1", len(fileVersions))
|
|
|
|
}
|
|
|
|
|
|
|
|
fileVersion := fileVersions[0]
|
|
|
|
|
|
|
|
if !fileVersion.ModTime.Equal(fileVersion.VersionTime) {
|
|
|
|
t.Error("time mismatch")
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:30:16 +00:00
|
|
|
if content := readFile(t, versionsFs, "file"); content != "A" {
|
|
|
|
t.Errorf("expected A got %s", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(t, folderFs, "file", "B")
|
|
|
|
|
2019-06-25 05:56:11 +00:00
|
|
|
versionInfo, err := versionsFs.Stat("file")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !versionInfo.ModTime().Truncate(time.Second).Equal(fileVersion.ModTime) {
|
|
|
|
t.Error("time mismatch")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := versioner.Restore("file", fileVersion.VersionTime); err != nil {
|
2019-04-28 22:30:16 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if content := readFile(t, folderFs, "file"); content != "A" {
|
|
|
|
t.Errorf("expected A got %s", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
if content := readFile(t, versionsFs, "file"); content != "B" {
|
|
|
|
t.Errorf("expected B got %s", content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(t *testing.T, filesystem fs.Filesystem, name string) string {
|
2021-02-26 11:04:05 +00:00
|
|
|
t.Helper()
|
2019-04-28 22:30:16 +00:00
|
|
|
fd, err := filesystem.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
2021-11-22 07:59:47 +00:00
|
|
|
buf, err := io.ReadAll(fd)
|
2019-04-28 22:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeFile(t *testing.T, filesystem fs.Filesystem, name, content string) {
|
|
|
|
fd, err := filesystem.OpenFile(name, fs.OptReadWrite|fs.OptCreate, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
if err := fd.Truncate(int64(len(content))); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n, err := fd.Write([]byte(content)); err != nil || n != len(content) {
|
|
|
|
t.Fatal(n, len(content), err)
|
|
|
|
}
|
|
|
|
}
|