summaryrefslogtreecommitdiffstats
path: root/lib/model/requests_test.go
blob: 33beca77d41af5699e62d718f53612c525fca052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package model

import (
	"context"
	"testing"
	"time"

	"github.com/syncthing/syncthing/lib/protocol"
)

// 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)

	folderIgnoresAlwaysReload(m, fcfg)

	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.Error("expected a single index entry, got", len(fs))
		} else if fs[0].Name != file {
			t.Errorf("expected a index entry for %v, got one for %v", file, fs[0].Name)
		}
		l.Infoln("received", fs[0])
	}

	done := make(chan struct{})
	fc.mut.Lock()
	fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
		basicCheck(fs)
		close(done)
	}
	fc.mut.Unlock()

	if err := writeFile(fss, file, contents, 0644); err != nil {
		t.Fatal(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()
	fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
		basicCheck(fs)
		f := fs[0]
		if !f.IsInvalid() {
			t.Errorf("Received non-invalid index update: %v", f)
		}
		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()
	fc.indexFn = func(_ context.Context, folder string, fs []protocol.FileInfo) {
		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:
	}
}