-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
379 lines (321 loc) · 8.55 KB
/
writer_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
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
package mbox
import (
"bytes"
"fmt"
"io"
"testing"
"github.com/kylelemons/godebug/diff"
)
var email1 string = `From: [email protected]
Subject: To interpretation
From X: WackyHeader
From all of us, to all of you, be happy!
`
var email2 string = `From: [email protected]
Subject: Bestest offer in the universe!!11!!
You won't believe these prices!
From 1 cent to 11 cents, we carry the least expensive
line of jets this side of the Gobi Desert!
`
var email3 string = `From: [email protected]
Subject: Mysterious Jenkins
`
var email4 string = `From: [email protected]
Subject: Ah, ha ha ha ha!
I remember when you wrote:
>From then on, I was a genius.
Do you remember?
`
var from1 = "[email protected]"
var from2 = "[email protected]"
var from3 = "[email protected]"
var from4 = "[email protected]"
func CompareBodies(expected string, received string, t *testing.T) {
if expected != received {
check := diff.Diff(expected, received)
t.Errorf("expected data doesn't match received data:\n%s\n", check)
}
}
type BrokenFS struct {
BreakWriter bool
BreakReader bool
BreakRemove bool
}
type Discarder struct{}
func (d *Discarder) Read([]byte) (n int, err error) {
return 0, nil
}
func (d *Discarder) Write([]byte) (n int, err error) {
return 0, nil
}
func (d *Discarder) Close() (err error) { return nil }
var Discard *Discarder = &Discarder{}
func (b *BrokenFS) OpenWriter(from string) (result io.WriteCloser, err error) {
if b.BreakWriter {
err = fmt.Errorf("never gonna give you up")
}
return Discard, err
}
func (b *BrokenFS) OpenReader(from string) (result io.ReadCloser, err error) {
if b.BreakReader {
err = fmt.Errorf("never gonna let you down")
}
return Discard, err
}
func (b *BrokenFS) Remove(from string) (err error) {
if b.BreakRemove {
err = fmt.Errorf("never gonna run around and desert you")
}
return err
}
func TestWriteMBOXO(t *testing.T) {
var err error
result := bytes.NewBuffer([]byte{})
mbox := NewWriter(result)
err = mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from2, bytes.NewBuffer([]byte(email2)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from3, bytes.NewBuffer([]byte(email3)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from4, bytes.NewBuffer([]byte(email4)))
if err != nil {
t.Error(err)
}
expectedFile := `From [email protected]
From: [email protected]
Subject: To interpretation
>From X: WackyHeader
>From all of us, to all of you, be happy!
From [email protected]
From: [email protected]
Subject: Bestest offer in the universe!!11!!
You won't believe these prices!
>From 1 cent to 11 cents, we carry the least expensive
line of jets this side of the Gobi Desert!
From [email protected]
From: [email protected]
Subject: Mysterious Jenkins
From [email protected]
From: [email protected]
Subject: Ah, ha ha ha ha!
I remember when you wrote:
>From then on, I was a genius.
Do you remember?
`
found := result.Bytes()
CompareBodies(expectedFile, string(found), t)
}
func TestWriteMBOXRD(t *testing.T) {
var err error
result := bytes.NewBuffer([]byte{})
mbox := NewWriter(result)
mbox.Type = MBOXRD
err = mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from2, bytes.NewBuffer([]byte(email2)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from3, bytes.NewBuffer([]byte(email3)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from4, bytes.NewBuffer([]byte(email4)))
if err != nil {
t.Error(err)
}
expectedFile := `From [email protected]
From: [email protected]
Subject: To interpretation
>From X: WackyHeader
>From all of us, to all of you, be happy!
From [email protected]
From: [email protected]
Subject: Bestest offer in the universe!!11!!
You won't believe these prices!
>From 1 cent to 11 cents, we carry the least expensive
line of jets this side of the Gobi Desert!
From [email protected]
From: [email protected]
Subject: Mysterious Jenkins
From [email protected]
From: [email protected]
Subject: Ah, ha ha ha ha!
I remember when you wrote:
>>From then on, I was a genius.
Do you remember?
`
found := result.Bytes()
CompareBodies(expectedFile, string(found), t)
}
func TestWriteMBOXCL(t *testing.T) {
var err error
result := bytes.NewBuffer([]byte{})
mbox := NewWriter(result)
mbox.Type = MBOXCL
err = mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from2, bytes.NewBuffer([]byte(email2)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from3, bytes.NewBuffer([]byte(email3)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from4, bytes.NewBuffer([]byte(email4)))
if err != nil {
t.Error(err)
}
expectedFile := `From [email protected]
From: [email protected]
Subject: To interpretation
>From X: WackyHeader
Content-Length: 42
>From all of us, to all of you, be happy!
From [email protected]
From: [email protected]
Subject: Bestest offer in the universe!!11!!
Content-Length: 130
You won't believe these prices!
>From 1 cent to 11 cents, we carry the least expensive
line of jets this side of the Gobi Desert!
From [email protected]
From: [email protected]
Subject: Mysterious Jenkins
Content-Length: 0
From [email protected]
From: [email protected]
Subject: Ah, ha ha ha ha!
Content-Length: 78
I remember when you wrote:
>>From then on, I was a genius.
Do you remember?
`
found := result.Bytes()
CompareBodies(expectedFile, string(found), t)
}
func TestWriteMBOXCL2(t *testing.T) {
var err error
result := bytes.NewBuffer([]byte{})
mbox := NewWriter(result)
mbox.Type = MBOXCL2
err = mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from2, bytes.NewBuffer([]byte(email2)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from3, bytes.NewBuffer([]byte(email3)))
if err != nil {
t.Error(err)
}
err = mbox.WriteMail(from4, bytes.NewBuffer([]byte(email4)))
if err != nil {
t.Error(err)
}
expectedFile := `From [email protected]
From: [email protected]
Subject: To interpretation
From X: WackyHeader
Content-Length: 41
From all of us, to all of you, be happy!
From [email protected]
From: [email protected]
Subject: Bestest offer in the universe!!11!!
Content-Length: 129
You won't believe these prices!
From 1 cent to 11 cents, we carry the least expensive
line of jets this side of the Gobi Desert!
From [email protected]
From: [email protected]
Subject: Mysterious Jenkins
Content-Length: 0
From [email protected]
From: [email protected]
Subject: Ah, ha ha ha ha!
Content-Length: 77
I remember when you wrote:
>From then on, I was a genius.
Do you remember?
`
found := result.Bytes()
CompareBodies(expectedFile, string(found), t)
}
func TestBrokenWriteFS(t *testing.T) {
result := bytes.NewBuffer([]byte{})
mbox := NewWriter(result)
mbox.FS = &BrokenFS{BreakWriter: true}
mbox.Type = MBOXCL2
err := mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err == nil {
t.Errorf("expected error, but it succeeded.")
}
mbox.Type = MBOXCL
err = mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err == nil {
t.Errorf("expected error, but it succeeded.")
}
}
func TestBrokenReadFS(t *testing.T) {
result := bytes.NewBuffer([]byte{})
mbox := NewWriter(result)
mbox.FS = &BrokenFS{BreakReader: true}
mbox.Type = MBOXCL2
err := mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err == nil {
t.Errorf("expected error, but it succeeded.")
}
mbox.FS = &BrokenFS{BreakReader: true}
mbox.Type = MBOXCL
err = mbox.WriteMail(from1, bytes.NewBuffer([]byte(email1)))
if err == nil {
t.Errorf("expected error, but it succeeded.")
}
}
func TestFileFromFSOrder(t *testing.T) {
fs := NewFileFromFS("")
reader, err := fs.OpenReader("moo")
if err == nil {
t.Errorf("expected error, but succeeded")
reader.Close()
fs.Remove("moo")
}
err = fs.Remove("arf")
if err == nil {
t.Errorf("expected error, but succeeded")
}
}