File: maildir_test.go

package info (click to toggle)
golang-github-emersion-go-maildir 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 116 kB
  • sloc: makefile: 2
file content (393 lines) | stat: -rw-r--r-- 6,903 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package maildir

import (
	"fmt"
	"io"
	"io/ioutil"
	"math/rand"
	"os"
	"testing"
)

// cleanup removes a Dir's directory structure
func cleanup(tb testing.TB, d Dir) {
	err := os.RemoveAll(string(d))
	if err != nil {
		tb.Error(err)
	}
}

// exists checks if the given path exists
func exists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	panic(err)
}

// cat returns the content of a file as a string
func cat(t *testing.T, path string) string {
	f, err := os.Open(path)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	c, err := ioutil.ReadAll(f)
	if err != nil {
		t.Fatal(err)
	}
	return string(c)
}

// makeDelivery creates a new message
func makeDelivery(tb testing.TB, d Dir, msg string) {
	del, err := NewDelivery(string(d))
	if err != nil {
		tb.Fatal(err)
	}
	_, err = del.Write([]byte(msg))
	if err != nil {
		tb.Fatal(err)
	}
	err = del.Close()
	if err != nil {
		tb.Fatal(err)
	}
}

func TestInit(t *testing.T) {
	t.Parallel()

	var d Dir = "test_init"
	err := d.Init()
	if err != nil {
		t.Fatal(err)
	}

	f, err := os.Open("test_init")
	if err != nil {
		t.Fatal(err)
	}

	fis, err := f.Readdir(0)
	subdirs := make(map[string]os.FileInfo)
	for _, fi := range fis {
		if !fi.IsDir() {
			t.Errorf("%s was not a directory", fi.Name())
			continue
		}
		subdirs[fi.Name()] = fi
	}

	// Verify the directories have been created.
	if _, ok := subdirs["tmp"]; !ok {
		t.Error("'tmp' directory was not created")
	}
	if _, ok := subdirs["new"]; !ok {
		t.Error("'new' directory was not created")
	}
	if _, ok := subdirs["cur"]; !ok {
		t.Error("'cur' directory was not created")
	}

	// Make sure no error is returned if the directories already exist.
	err = d.Init()
	if err != nil {
		t.Fatal(err)
	}

	defer cleanup(t, d)
}

func TestDelivery(t *testing.T) {
	t.Parallel()

	var d Dir = "test_delivery"
	err := d.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d)

	var msg = "this is a message"
	makeDelivery(t, d, msg)

	keys, err := d.Unseen()
	if err != nil {
		t.Fatal(err)
	}
	path, err := d.Filename(keys[0])
	if err != nil {
		t.Fatal(err)
	}
	if !exists(path) {
		t.Fatal("File doesn't exist")
	}

	if cat(t, path) != msg {
		t.Fatal("Content doesn't match")
	}
}

func TestDir_Create(t *testing.T) {
	t.Parallel()

	var d Dir = "test_create"
	err := d.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d)

	var msg = "this is a message"
	key, w, err := d.Create([]Flag{FlagFlagged})
	if err != nil {
		t.Fatal(err)
	}
	defer w.Close()
	if _, err := io.WriteString(w, msg); err != nil {
		t.Fatal(err)
	}
	if err := w.Close(); err != nil {
		t.Fatal(err)
	}

	flags, err := d.Flags(key)
	if err != nil {
		t.Fatal(err)
	} else if len(flags) != 1 || flags[0] != FlagFlagged {
		t.Errorf("Dir.Flags() = %v, want {FlagFlagged}", flags)
	}

	path, err := d.Filename(key)
	if err != nil {
		t.Fatal(err)
	}
	if !exists(path) {
		t.Fatal("File doesn't exist")
	}

	if cat(t, path) != msg {
		t.Fatal("Content doesn't match")
	}
}

func TestPurge(t *testing.T) {
	t.Parallel()

	var d Dir = "test_purge"
	err := d.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d)

	makeDelivery(t, d, "foo")

	keys, err := d.Unseen()
	if err != nil {
		t.Fatal(err)
	}
	path, err := d.Filename(keys[0])
	if err != nil {
		t.Fatal(err)
	}
	err = d.Remove(keys[0])
	if err != nil {
		t.Fatal(err)
	}

	if exists(path) {
		t.Fatal("File still exists")
	}
}

func TestMove(t *testing.T) {
	t.Parallel()

	var d1 Dir = "test_move1"
	err := d1.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d1)
	var d2 Dir = "test_move2"
	err = d2.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d2)

	const msg = "a moving message"
	makeDelivery(t, d1, msg)
	keys, err := d1.Unseen()
	if err != nil {
		t.Fatal(err)
	}
	err = d1.Move(d2, keys[0])
	if err != nil {
		t.Fatal(err)
	}

	keys, err = d2.Keys()
	if err != nil {
		t.Fatal(err)
	}
	path, err := d2.Filename(keys[0])
	if err != nil {
		t.Fatal(err)
	}
	if cat(t, path) != msg {
		t.Fatal("Content doesn't match")
	}

}

func TestCopy(t *testing.T) {
	t.Parallel()
	var d1 Dir = "test_copy1"
	err := d1.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d1)
	var d2 Dir = "test_copy2"
	err = d2.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d2)
	const msg = "a moving message"
	makeDelivery(t, d1, msg)
	keys, err := d1.Unseen()
	if err != nil {
		t.Fatal(err)
	}
	if err = d1.SetFlags(keys[0], []Flag{FlagSeen}); err != nil {
		t.Fatal(err)
	}
	key2, err := d1.Copy(d2, keys[0])
	if err != nil {
		t.Fatal(err)
	}
	path, err := d1.Filename(keys[0])
	if err != nil {
		t.Fatal(err)
	}
	if cat(t, path) != msg {
		t.Error("original content has changed")
	}
	path, err = d2.Filename(key2)
	if err != nil {
		t.Fatal(err)
	}
	if cat(t, path) != msg {
		t.Error("target content doesn't match source")
	}
	flags, err := d2.Flags(key2)
	if err != nil {
		t.Fatal(err)
	}
	if len(flags) != 1 {
		t.Fatal("no flags on target")
	}
	if flags[0] != FlagSeen {
		t.Error("seen flag not present on target")
	}
}

func TestIllegal(t *testing.T) {
	t.Parallel()
	var d1 Dir = "test_illegal"
	err := d1.Init()
	if err != nil {
		t.Fatal(err)
	}
	defer cleanup(t, d1)
	const msg = "an illegal message"
	makeDelivery(t, d1, msg)
	keys, err := d1.Unseen()
	if err != nil {
		t.Fatal(err)
	}
	if err = d1.SetFlags(keys[0], []Flag{FlagSeen}); err != nil {
		t.Fatal(err)
	}
	path, err := d1.Filename(keys[0])
	if err != nil {
		t.Fatal(err)
	}
	os.Rename(path, "test_illegal/cur/"+keys[0])
	_, err = d1.Flags(keys[0])
	if err != nil {
		if _, ok := err.(*MailfileError); !ok {
			t.Fatal(err)
		}
	}
}

func BenchmarkFilename(b *testing.B) {
	// set up test maildir
	d := Dir("benchmark_filename")
	if err := d.Init(); err != nil {
		b.Fatalf("could not set up benchmark: %v", err)
	}
	defer cleanup(b, d)

	// make 5000 deliveries
	for i := 0; i < 5000; i++ {
		makeDelivery(b, d, fmt.Sprintf("here is message number %d", i))
	}

	// grab keys
	keys, err := d.Unseen()
	if err != nil {
		b.Fatal(err)
	}

	// shuffle keys
	rand.Shuffle(len(keys), func(i, j int) {
		keys[i], keys[j] = keys[j], keys[i]
	})

	// set some flags
	for i, key := range keys {
		var err error
		switch i % 5 {
		case 0:
			// no flags
			fallthrough
		case 1:
			err = d.SetFlags(key, []Flag{FlagSeen})
		case 2:
			err = d.SetFlags(key, []Flag{FlagSeen, FlagReplied})
		case 3:
			err = d.SetFlags(key, []Flag{FlagReplied})
		case 4:
			err = d.SetFlags(key, []Flag{FlagFlagged})
		}
		if err != nil {
			b.Fatal(err)
		}
	}

	// run benchmark for the first N shuffled keys
	keyIdx := 0
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StartTimer()
		_, err := d.Filename(keys[keyIdx])
		b.StopTimer()
		if err != nil {
			b.Errorf("could not get filename for key %s", keys[keyIdx])
		}
		keyIdx++
		if keyIdx >= len(keys) {
			keyIdx = 0
		}
	}
}