-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmaildir_test.go
310 lines (273 loc) · 8.24 KB
/
maildir_test.go
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
package maildir
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
paths "path"
"strings"
"testing"
)
type encodingTestData struct {
decoded, encoded string
}
var encodingTests = []encodingTestData{
{"&2[foo]", "&-2[foo]"}, // Folder name starting with a special character
{"foo&", "foo&-"}, // Folder name ending with a special character
{"A./B", "A&AC4ALw-B"}, // "." and "/" are special
{"Lesson:日本語", "Lesson:&ZeVnLIqe-"}, // long sequence of characters
{"Résumé&Écritures", "R&AOk-sum&AOk-&-&AMk-critures"}, // "&" in the middle of a sequence of special characters
{"Hello world", "Hello world"}, // spaces are not encoded
}
func TestCreate(t *testing.T) {
if err := os.RemoveAll("_obj/Maildir"); err != nil {
panic(fmt.Sprintf("Can't remove old test data: %v", err))
}
// Opening non-existing maildir
md, err := New("_obj/Maildir", false)
if md != nil {
t.Error("I shouldn't be able to open a non-existent maildir")
return
}
// Creating new maildir
md, err = New("_obj/Maildir", true)
defer os.RemoveAll("_obj/Maildir")
if err != nil {
t.Errorf("Error while creating maildir: %v", err)
return
}
if md == nil {
t.Error("No error, but nil maildir when creating a maildir")
return
}
// Chek that cur/, tmp/ and new/ have been created
for _, subdir := range []string{"cur", "tmp", "new"} {
fi, err := os.Stat("_obj/Maildir/" + subdir)
if err != nil {
t.Errorf("Can't open %v of maildir _obj/Maildir: %v", subdir, err)
continue
}
if !fi.IsDir() {
t.Errorf("%v of maildir _obj/Maildir is not a directory", subdir)
continue
}
}
}
func TestCreateWithPerms(t *testing.T) {
if err := os.RemoveAll("_obj/Maildir"); err != nil {
panic(fmt.Sprintf("Can't remove old test data: %v", err))
}
// Creating new maildir
md, err := NewWithPerm("_obj/Maildir", true, 0644, DoNotSetOwner, DoNotSetOwner)
defer os.RemoveAll("_obj/Maildir")
if err != nil {
t.Errorf("Error while creating maildir: %v", err)
return
}
if md == nil {
t.Error("No error, but nil maildir when creating a maildir")
return
}
// check correct permissions
if fi, statErr := os.Stat("_obj/Maildir"); statErr != nil {
t.Error("could not stat _obj/Maildir", statErr)
} else if perm := fi.Mode().Perm(); perm != 0755 {
t.Errorf("expected permissions of _obj/Maildir 0755, but got %o", perm)
}
// Chek that cur/, tmp/ and new/ have correct perms
for _, subdir := range []string{"cur", "tmp", "new"} {
fi, err := os.Stat("_obj/Maildir/" + subdir)
if err != nil {
t.Errorf("Can't open %v of maildir _obj/Maildir: %v", subdir, err)
continue
}
if !fi.IsDir() {
t.Errorf("%v of maildir _obj/Maildir is not a directory", subdir)
continue
}
// for every r permission for u,g,o it should add an x permission
if perm := fi.Mode().Perm(); perm != 0755 {
t.Errorf("expected permissions %v of maildir of _obj/Maildir 0755, but got %o", subdir, perm)
}
}
}
func TestEncode(t *testing.T) {
if err := os.RemoveAll("_obj/Maildir"); err != nil {
panic(fmt.Sprintf("Can't remove old test data: %v", err))
}
maildir, err := New("_obj/Maildir", true)
if maildir == nil {
t.Errorf("Can't create maildir: %v", err)
return
}
defer os.RemoveAll("_obj/Maildir")
for _, testData := range encodingTests {
child, err := maildir.Child(testData.decoded, true)
if err != nil {
t.Errorf("Can't create sub-maildir %v: %v", testData.decoded, err)
continue
}
if child.Path != "_obj/Maildir/."+testData.encoded {
t.Logf("Sub-maildir %v has an invalid path", testData.decoded)
t.Logf(" Expected result: %s", "_obj/Maildir/."+testData.encoded)
t.Logf(" Actual result: %s", child.Path)
t.Fail()
continue
}
}
// Separator between sub-maildir and sub-sub-maildir should not be encoded
child, err := maildir.Child("foo", true)
if err != nil {
t.Errorf("Can't create sub-maildir foo: %v", err)
return
}
child, err = child.Child("bar", true)
if err != nil {
t.Errorf("Can't create sub-maildir foo/bar: %v", err)
return
}
if child.Path != "_obj/Maildir/.foo.bar" {
t.Logf("Sub-maildir %v has an invalid path", "foo/bar")
t.Logf(" Expected result: %s", "_obj/Maildir/.foo.bar")
t.Logf(" Actual result: %s", child.Path)
t.Fail()
}
}
func readdirnames(dir string) ([]string, error) {
d, err := os.Open(dir)
if err != nil {
return nil, err
}
list, err := d.Readdirnames(-1)
if err != nil {
return nil, err
}
res := make([]string, 0, len(list))
for _, entry := range list {
if entry != "." && entry != ".." {
res = append(res, entry)
}
}
return res, nil
}
func TestWritePerms(t *testing.T) {
if err := os.RemoveAll("_obj/Maildir"); err != nil {
panic(fmt.Sprintf("Can't remove old test data: %v", err))
}
maildir, err := NewWithPerm("_obj/Maildir", true, 0644, DoNotSetOwner, DoNotSetOwner)
if maildir == nil {
t.Errorf("Can't create maildir: %v", err)
return
}
defer os.RemoveAll("_obj/Maildir")
testData := []byte("Hello, world !")
// write a mail
fullName, err := maildir.CreateMail(bytes.NewBuffer(testData))
if err != nil {
t.Errorf("Can't create mail: %v", err)
}
// fullName should have our pid
if strings.Index(fullName, fmt.Sprintf("%d_", os.Getpid())) == -1 {
t.Error("fullName does not contain the pid, it was:", fullName)
}
// check perms
if fi, err := os.Stat(fullName); err != nil {
t.Error("could not stat", fullName)
} else {
if perm := fi.Mode().Perm(); perm != 0644 {
t.Errorf("expected permissions %v 600, 0644 got %o", fullName, perm)
}
}
}
func TestWrite(t *testing.T) {
if err := os.RemoveAll("_obj/Maildir"); err != nil {
panic(fmt.Sprintf("Can't remove old test data: %v", err))
}
maildir, err := New("_obj/Maildir", true)
if maildir == nil {
t.Errorf("Can't create maildir: %v", err)
return
}
defer os.RemoveAll("_obj/Maildir")
testData := []byte("Hello, world !")
// write a mail
fullName, err := maildir.CreateMail(bytes.NewBuffer(testData))
if err != nil {
t.Errorf("Can't create mail: %v", err)
}
// tmp/ and cur/ must be empty
names, err := readdirnames("_obj/Maildir/tmp")
if err != nil {
t.Errorf("Can't read tmp/: %v", err)
return
}
if len(names) > 0 {
t.Errorf("Expected no element in tmp/, got %v", names)
}
names, err = readdirnames("_obj/Maildir/cur")
if err != nil {
t.Errorf("Can't read cur/: %v", err)
return
}
if len(names) > 0 {
t.Errorf("Expected no element in cur/, got %v", names)
}
// new/ must contain only one file, which must contain the written data
names, err = readdirnames("_obj/Maildir/new")
if err != nil {
t.Errorf("Can't read new/: %v", err)
return
}
if len(names) != 1 {
t.Errorf("Expected one element in new/, got %v", names)
}
f, err := os.Open(fullName)
if err != nil {
t.Errorf("Can't open %v: %v", fullName, err)
return
}
data, err := ioutil.ReadAll(f)
if err != nil {
t.Errorf("Can't read %v: %v", fullName, err)
return
}
if bytes.Compare(data, testData) != 0 {
t.Errorf("File contains %#v, expected %#v", string(data), string(testData))
}
// filename must end with ,S=(mail size)
name := names[0]
if !strings.HasSuffix(name, fmt.Sprintf(",S=%d", len(testData))) {
t.Errorf("Filename %#v must end with %#v", name, fmt.Sprintf(",S=%d", len(testData)))
}
if path.Base(fullName) != name {
t.Errorf("Returned name %#v does not match #%v", path.Base(fullName), name)
}
}
// For folder with no sub folders, it should create the sub-folders
func TestFolderWithNoSubFolders(t *testing.T) {
dir := "_obj/Maildir"
if err := os.RemoveAll(dir); err != nil {
panic(fmt.Sprintf("Can't remove old test data: %v", err))
}
// create a maildir folder without any sub-dirs
dirPerm := os.FileMode(DefaultFilePerm | ((DefaultFilePerm & 0444) >> 2))
if err := os.MkdirAll(dir, dirPerm); err != nil {
t.Errorf("Can't create maildir: %v", err)
return
}
// this will create the sub-folders
maildir, err := New(dir, true)
if maildir == nil {
t.Errorf("Can't create maildir: %v", err)
return
}
// sub-dirs should be there
for _, subdir := range []string{"tmp", "cur", "new"} {
ps := paths.Join(dir, subdir)
if _, err = os.Stat(ps); os.IsNotExist(err) {
t.Error("sub folder does not exist. Expected ", ps)
}
}
defer os.RemoveAll("_obj/Maildir")
}