lib/model: Improve filesystem operations during tests (fixes #5422)
* lib/fs, lib/model: Improve filesystem operations during tests (fixes #5422) Introduces MustFilesystem that panics on errors and should be used for operations during testing which must never fail. Create temporary directories outside of testdata. * don't do a filesystem, just a wrapper around os for testing * fix copyright
This commit is contained in:
committed by
Audrius Butkevicius
parent
24ffd8be99
commit
0b03b6a9ec
@@ -26,14 +26,16 @@ import (
|
||||
)
|
||||
|
||||
func TestRequestSimple(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
// Verify that the model performs a request and creates a file based on
|
||||
// an incoming index update.
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
// We listen for incoming index updates and trigger when we see one for
|
||||
@@ -63,6 +65,8 @@ func TestRequestSimple(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSymlinkTraversalRead(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
// Verify that a symlink can not be traversed for reading.
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
@@ -73,8 +77,8 @@ func TestSymlinkTraversalRead(t *testing.T) {
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
// We listen for incoming index updates and trigger when we see one for
|
||||
@@ -105,6 +109,8 @@ func TestSymlinkTraversalRead(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSymlinkTraversalWrite(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
// Verify that a symlink can not be traversed for writing.
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
@@ -115,8 +121,8 @@ func TestSymlinkTraversalWrite(t *testing.T) {
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
// We listen for incoming index updates and trigger when we see one for
|
||||
@@ -173,13 +179,15 @@ func TestSymlinkTraversalWrite(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequestCreateTmpSymlink(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
// Test that an update for a temporary file is invalidated
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
// We listen for incoming index updates and trigger when we see one for
|
||||
@@ -213,6 +221,8 @@ func TestRequestCreateTmpSymlink(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequestVersioningSymlinkAttack(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("no symlink support on Windows")
|
||||
}
|
||||
@@ -221,7 +231,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
|
||||
// deleted symlink to escape
|
||||
|
||||
tmpDir := createTmpDir()
|
||||
defer os.RemoveAll(tmpDir)
|
||||
defer testOs.RemoveAll(tmpDir)
|
||||
|
||||
cfg := defaultCfgWrapper.RawCopy()
|
||||
cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2"))
|
||||
@@ -234,7 +244,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
|
||||
Type: "trashcan",
|
||||
}
|
||||
w := createTmpWrapper(cfg)
|
||||
defer os.Remove(w.ConfigPath())
|
||||
defer testOs.Remove(w.ConfigPath())
|
||||
|
||||
db := db.OpenMemory()
|
||||
m := NewModel(w, device1, "syncthing", "dev", db, nil)
|
||||
@@ -243,7 +253,7 @@ func TestRequestVersioningSymlinkAttack(t *testing.T) {
|
||||
m.StartFolder("default")
|
||||
defer m.Stop()
|
||||
|
||||
defer os.RemoveAll(tmpDir)
|
||||
defer testOs.RemoveAll(tmpDir)
|
||||
|
||||
fc := addFakeConn(m, device2)
|
||||
fc.folder = "default"
|
||||
@@ -309,6 +319,8 @@ func TestPullInvalidIgnoredSR(t *testing.T) {
|
||||
func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
|
||||
t.Helper()
|
||||
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
tmpDir := createTmpDir()
|
||||
cfg := defaultCfgWrapper.RawCopy()
|
||||
cfg.Devices = append(cfg.Devices, config.NewDeviceConfiguration(device2, "device2"))
|
||||
@@ -321,8 +333,8 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
|
||||
m, fc, w := setupModelWithConnectionManual(cfg)
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
// Reach in and update the ignore matcher to one that always does
|
||||
@@ -440,11 +452,13 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
|
||||
}
|
||||
|
||||
func TestIssue4841(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
received := make(chan protocol.FileInfo)
|
||||
@@ -482,11 +496,13 @@ func TestIssue4841(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRescanIfHaveInvalidContent(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
payload := []byte("hello")
|
||||
@@ -551,11 +567,13 @@ func TestRescanIfHaveInvalidContent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParentDeletion(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
parent := "foo"
|
||||
@@ -637,11 +655,13 @@ func TestRequestSymlinkWindows(t *testing.T) {
|
||||
t.Skip("windows specific test")
|
||||
}
|
||||
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
|
||||
first := make(chan struct{})
|
||||
@@ -724,7 +744,7 @@ func setupModelWithConnectionManual(cfg config.Configuration) (*Model, *fakeConn
|
||||
}
|
||||
|
||||
func createTmpDir() string {
|
||||
tmpDir, err := ioutil.TempDir("testdata", "_request-")
|
||||
tmpDir, err := ioutil.TempDir("", "_request-")
|
||||
if err != nil {
|
||||
panic("Failed to create temporary testing dir")
|
||||
}
|
||||
@@ -741,11 +761,13 @@ func equalContents(path string, contents []byte) error {
|
||||
}
|
||||
|
||||
func TestRequestRemoteRenameChanged(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
tfs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
|
||||
|
||||
@@ -823,11 +845,13 @@ func TestRequestRemoteRenameChanged(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequestRemoteRenameConflict(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
tfs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
|
||||
|
||||
@@ -919,11 +943,13 @@ func TestRequestRemoteRenameConflict(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequestDeleteChanged(t *testing.T) {
|
||||
testOs := &fatalOs{t}
|
||||
|
||||
m, fc, tmpDir, w := setupModelWithConnection()
|
||||
defer func() {
|
||||
m.Stop()
|
||||
os.RemoveAll(tmpDir)
|
||||
os.Remove(w.ConfigPath())
|
||||
testOs.RemoveAll(tmpDir)
|
||||
testOs.Remove(w.ConfigPath())
|
||||
}()
|
||||
tfs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
|
||||
|
||||
@@ -967,7 +993,7 @@ func TestRequestDeleteChanged(t *testing.T) {
|
||||
|
||||
// Check outcome
|
||||
if _, err := tfs.Lstat(a); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
if fs.IsNotExist(err) {
|
||||
t.Error(`Modified file "a" was removed`)
|
||||
} else {
|
||||
t.Error(`Error stating file "a":`, err)
|
||||
|
||||
Reference in New Issue
Block a user