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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
// SPDX-License-Identifier: GPL-3.0-or-later
package kwcache
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
// allKeys is every keyword the tests store, for Save calls that keep them all.
var allKeys = []string{"k:acme", "k:faktura", "k:nip", "k:new", "k:x"}
var (
a = ID{Dev: 1, Ino: 10, Size: 100, MTime: 1000}
b = ID{Dev: 1, Ino: 11, Size: 200, MTime: 2000}
)
func saved(t *testing.T, c *Cache, present ...ID) string {
t.Helper()
path := filepath.Join(t.TempDir(), "sub", "dl.cache")
if err := c.Save(path, present, allKeys); err != nil {
t.Fatal(err)
}
return path
}
// TestLookupAfterReload: answers stored and saved come back after a Load
// with the same fingerprint, for any keywords the entry covers.
func TestLookupAfterReload(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true, "k:faktura": false, "k:nip": true})
path := saved(t, c, a)
got, err := Load(path, "fp")
if err != nil {
t.Fatal(err)
}
hits, ok := got.Lookup(a, []string{"k:faktura", "k:nip"})
if !ok || !reflect.DeepEqual(hits, []bool{false, true}) {
t.Errorf("Lookup = %v, %v; want [false true], true", hits, ok)
}
if _, ok := got.Lookup(a, []string{"k:acme", "k:new"}); ok {
t.Error("a keyword the entry was never checked against must be a miss")
}
if _, ok := got.Lookup(b, []string{"k:acme"}); ok {
t.Error("a file never stored must be a miss")
}
}
// TestLookupBeforeSave: what was stored in this run answers at once.
func TestLookupBeforeSave(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true})
if hits, ok := c.Lookup(a, []string{"k:acme"}); !ok || !hits[0] {
t.Errorf("Lookup = %v, %v", hits, ok)
}
}
// TestChangedFileMisses: a different size or modification time is a
// different ID, so nothing stored for the old one answers.
func TestChangedFileMisses(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true})
for _, id := range []ID{{Dev: 1, Ino: 10, Size: 101, MTime: 1000}, {Dev: 1, Ino: 10, Size: 100, MTime: 1001}} {
if _, ok := c.Lookup(id, []string{"k:acme"}); ok {
t.Errorf("%+v answered from %+v's entry", id, a)
}
}
}
// TestFingerprintMismatchDiscardsAll: a cache written under a different
// extractor fingerprint loads empty, without error.
func TestFingerprintMismatchDiscardsAll(t *testing.T) {
c := New("old")
c.Store(a, map[string]bool{"k:acme": true})
path := saved(t, c, a)
got, err := Load(path, "new")
if err != nil {
t.Fatal(err)
}
if _, ok := got.Lookup(a, []string{"k:acme"}); ok {
t.Error("entry survived a fingerprint change")
}
}
// TestSaveKeepsOnlyPresentFiles: entries for files not passed to Save, from
// this run or an earlier one, are dropped.
func TestSaveKeepsOnlyPresentFiles(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true})
c.Store(b, map[string]bool{"k:acme": false})
path := saved(t, c, a, b)
next, err := Load(path, "fp")
if err != nil {
t.Fatal(err)
}
if err := next.Save(path, []ID{b}, allKeys); err != nil {
t.Fatal(err)
}
last, err := Load(path, "fp")
if err != nil {
t.Fatal(err)
}
if _, ok := last.Lookup(a, []string{"k:acme"}); ok {
t.Error("a is gone from the directory but its entry was kept")
}
if hits, ok := last.Lookup(b, []string{"k:acme"}); !ok || hits[0] {
t.Errorf("b's entry lost across a reload: %v, %v", hits, ok)
}
}
// TestStoreReplacesLoadedEntry: a file read again this run replaces what
// an earlier run stored for it.
func TestStoreReplacesLoadedEntry(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true})
path := saved(t, c, a)
next, _ := Load(path, "fp")
next.Store(a, map[string]bool{"k:acme": false, "k:new": true})
if hits, ok := next.Lookup(a, []string{"k:acme", "k:new"}); !ok || hits[0] || !hits[1] {
t.Errorf("Lookup = %v, %v; want the new entry", hits, ok)
}
}
// TestSavePermissions: the directory is private and the file readable by
// its owner only, with no temporary file left behind.
func TestSavePermissions(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true})
path := saved(t, c, a)
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if perm := fi.Mode().Perm(); perm != 0o600 {
t.Errorf("file mode %o, want 600", perm)
}
di, err := os.Stat(filepath.Dir(path))
if err != nil {
t.Fatal(err)
}
if perm := di.Mode().Perm(); perm != 0o700 {
t.Errorf("directory mode %o, want 700", perm)
}
entries, _ := os.ReadDir(filepath.Dir(path))
if len(entries) != 1 {
t.Errorf("directory holds %d entries, want only the cache", len(entries))
}
}
// TestLoadMissingAndCorrupt: no file is an empty cache and no error; an
// unreadable one is an empty cache and an error saying so.
func TestLoadMissingAndCorrupt(t *testing.T) {
dir := t.TempDir()
c, err := Load(filepath.Join(dir, "none.cache"), "fp")
if err != nil || c == nil {
t.Fatalf("missing: %v, %v", c, err)
}
bad := filepath.Join(dir, "bad.cache")
os.WriteFile(bad, []byte("{not json"), 0o600)
c, err = Load(bad, "fp")
if err == nil || c == nil {
t.Fatalf("corrupt: want an empty cache and an error, got %v, %v", c, err)
}
if _, ok := c.Lookup(a, []string{"k:acme"}); ok {
t.Error("corrupt cache answered")
}
}
// TestSaveWithNothingWritesNothing: a cache that never held an entry, and
// has no file yet, creates none.
func TestSaveWithNothingWritesNothing(t *testing.T) {
path := filepath.Join(t.TempDir(), "sub", "dl.cache")
if err := New("fp").Save(path, []ID{a}, allKeys); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Dir(path)); !os.IsNotExist(err) {
t.Errorf("Save created %s for an empty cache", filepath.Dir(path))
}
}
// TestSaveTrimsToCurrentKeywords: a keyword no longer in the configuration
// is not kept in the cache file (review cache F6).
func TestSaveTrimsToCurrentKeywords(t *testing.T) {
c := New("fp")
c.Store(a, map[string]bool{"k:keep": true, "k:gone client": true})
path := filepath.Join(t.TempDir(), "dl.cache")
if err := c.Save(path, []ID{a}, []string{"k:keep"}); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(b), "gone client") {
t.Errorf("a removed keyword is still stored:\n%s", b)
}
got, _ := Load(path, "fp")
if hits, ok := got.Lookup(a, []string{"k:keep"}); !ok || !hits[0] {
t.Errorf("the kept keyword's answer was lost: %v %v", hits, ok)
}
}
// TestSaveTightensAnExistingDirectory: the cache directory is private even
// when it already existed with wider permissions (review cache F7).
func TestSaveTightensAnExistingDirectory(t *testing.T) {
dir := filepath.Join(t.TempDir(), "krino")
if err := os.Mkdir(dir, 0o755); err != nil {
t.Fatal(err)
}
c := New("fp")
c.Store(a, map[string]bool{"k:acme": true})
if err := c.Save(filepath.Join(dir, "dl.cache"), []ID{a}, []string{"k:acme"}); err != nil {
t.Fatal(err)
}
if fi, err := os.Stat(dir); err != nil || fi.Mode().Perm() != 0o700 {
t.Errorf("directory mode %v, %v; want 0700", fi.Mode().Perm(), err)
}
}
|