forked from emersion/go-mbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
509 lines (386 loc) · 12.1 KB
/
reader_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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package mbox
import (
"bytes"
"fmt"
"io"
"net/mail"
"strings"
"testing"
)
const mboxWithOneMessage = `From herp.derp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.
`
const mboxWithThreeMessages = `From herp.derp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: derp.herp@example.com (Derp Herp)
Date: Thu, 02 Jan 2015 00:00:01 +0100
Subject: Another test
This is another simple test.
Another line.
Bye.
From bernd.lauert@example.com Thu Jan 3 00:00:01 2015
From: bernd.lauert@example.com (Bernd Lauert)
Date: Thu, 03 Jan 2015 00:00:01 +0100
Subject: A last test
This is the last simple test.
Bye.
`
const mboxWithStartingLF = `
From herp.derp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: derp.herp@example.com (Derp Herp)
Date: Thu, 02 Jan 2015 00:00:01 +0100
Subject: Another test
This is another simple test.
Another line.
Bye.
From bernd.lauert@example.com Thu Jan 3 00:00:01 2015
From: bernd.lauert@example.com (Bernd Lauert)
Date: Thu, 03 Jan 2015 00:00:01 +0100
Subject: A last test
This is the last simple test.
Bye.
`
const mboxWithThreeMessagesMalformedButValid = `From herp.derp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: derp.herp@example.com (Derp Herp)
Date: Thu, 02 Jan 2015 00:00:01 +0100
Subject: Another test
This is another simple test.
Another line.
Bye.
From bernd.lauert@example.com Thu Jan 3 00:00:01 2015
From: bernd.lauert@example.com (Bernd Lauert)
Date: Thu, 03 Jan 2015 00:00:01 +0100
Subject: A last test
This is the last simple test.
Bye.
`
const mboxWithOneMessageMissingSeparator = `From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
>From Herp Derp with love.
Bye.
`
const mboxFirstMessage = `From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
And, by the way, this is how a "From" line is escaped in mboxo format:
From Herp Derp with love.
Bye.
`
func toCRLF(s string) string {
return strings.Replace(s, "\n", "\r\n", -1)
}
func testMboxMessage(t *testing.T, mbox string, count int) {
b := bytes.NewBufferString(mbox)
m := NewReader(b)
for i := 0; i < count; i++ {
r, err := m.NextMessage()
if err != nil {
t.Fatalf("Unexpected error after NextMessage(): %v", err)
}
var text bytes.Buffer
_, err = text.ReadFrom(r)
if err != nil {
t.Errorf("Unexpected error reading message body: %v", err)
}
want := toCRLF(mboxFirstMessage)
if i == 0 && text.String() != want {
t.Errorf("Expected:\n %q\ngot\n%q", want, text.String())
}
}
if _, err := m.NextMessage(); err != io.EOF {
t.Fatalf("Unexpected error after NextMessage(): %v", err)
}
}
func TestMboxMessageWithOneMessage(t *testing.T) {
testMboxMessage(t, mboxWithOneMessage, 1)
}
func TestMboxMessageWithThreeMessages(t *testing.T) {
testMboxMessage(t, mboxWithThreeMessages, 3)
}
func TestMboxMessageWithStartingLF(t *testing.T) {
testMboxMessage(t, mboxWithStartingLF, 3)
}
func TestMboxMessageWithThreeMessagesMalformedButValid(t *testing.T) {
testMboxMessage(t, mboxWithThreeMessagesMalformedButValid, 3)
}
func testMboxMessageInvalid(t *testing.T, mbox string) {
b := bytes.NewBufferString(mbox)
m := NewReader(b)
if _, err := m.NextMessage(); err == nil {
t.Errorf("Missing error after Next(): %v", err)
}
}
func TestMboxMessageWithOneMessageMissingSeparator(t *testing.T) {
testMboxMessageInvalid(t, mboxWithOneMessageMissingSeparator)
}
func TestMboxMessageNoRead(t *testing.T) {
b := bytes.NewBufferString(mboxWithThreeMessages)
m := NewReader(b)
n := 0
for {
_, err := m.NextMessage()
if err == io.EOF {
break
} else if err != nil {
t.Fatalf("Unexpected error after NextMessage(): %v", err)
}
n++
}
if _, err := m.NextMessage(); err != io.EOF {
t.Fatalf("Unexpected error after NextMessage(): %v", err)
}
if n != 3 {
t.Fatalf("Expected 3 mesages, got %v", n)
}
}
// TODO: decide whether we should keep this test or not
func DisabledTestScanMessageWithBoundaries(t *testing.T) {
sourceData := `
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
Content-Type: multipart/alternative;
boundary=Apple-Mail-D55D9B1A-A379-4D5C-BDA9-00D35DF424A0
This is a test of boundaries. Don't accept a new email via \nFrom until the boundary is done!'
And, by the way, this is how a "From" line is escaped in mboxo format:
From Herp Derp with love.
From Herp Derp with love.
Bye.
--Apple-Mail-D55D9B1A-A379-4D5C-BDA9-00D35DF424A0--
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is the second email in a test of boundaries.
`
expected := []string{
"This is a test of boundaries. Don't accept a new email via \\nFrom until the boundary is done!'\n\nAnd, by the way, this is how a \"From\" line is escaped in mboxo format:\nFrom Herp Derp with love.\n\nFrom Herp Derp with love.\n\nBye.\n--Apple-Mail-D55D9B1A-A379-4D5C-BDA9-00D35DF424A0--\n",
"This is the second email in a test of boundaries.\n",
}
b := bytes.NewBufferString(sourceData)
m := NewReader(b)
for i := range expected {
r, err := m.NextMessage()
if err != nil {
t.Fatalf("Unexpected error after NextMessage(): %v", err)
}
msg, err := mail.ReadMessage(r)
if err != nil {
t.Fatalf("mail.ReadMessage() = %v", err)
}
var body bytes.Buffer
_, err = body.ReadFrom(msg.Body)
if err != nil {
t.Errorf("%d - Unexpected error reading message body: %v", i, err)
continue
}
want := toCRLF(expected[i])
if body.String() != want {
t.Errorf("%d - Expected:\n %q\ngot\n%q", i, want, body.String())
}
}
if _, err := m.NextMessage(); err != io.EOF {
t.Errorf("Next() succeeded")
}
}
func TestScanMessageWithTextBoundary(t *testing.T) {
sourceData := `
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
Content-Type: text/html; charset="utf-8";
boundary="monkey_d3df4dc8-da5e-47dd-be15-f19c5ed55194"
This is a test of boundaries. Don't accept a new email via \nFrom until the boundary is done!'
And, by the way, this is how a "From" line is escaped in mboxo format:
Bye.
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is the second email in a test of boundaries.
`
expected := []string{
"This is a test of boundaries. Don't accept a new email via \\nFrom until the boundary is done!'\n\nAnd, by the way, this is how a \"From\" line is escaped in mboxo format:\n\nBye.\n",
"This is the second email in a test of boundaries.\n",
}
b := bytes.NewBufferString(sourceData)
m := NewReader(b)
for i := range expected {
r, err := m.NextMessage()
if err != nil {
t.Fatalf("Unexpected error after NextMessage(): %v", err)
}
msg, err := mail.ReadMessage(r)
if err != nil {
t.Fatalf("mail.ReadMessage() = %v", err)
}
var body bytes.Buffer
_, err = body.ReadFrom(msg.Body)
if err != nil {
t.Errorf("%d - Unexpected error reading message body: %v", i, err)
continue
}
want := toCRLF(expected[i])
if body.String() != want {
t.Errorf("%d - Expected:\n %q\ngot\n%q", i, want, body.String())
}
}
if _, err := m.NextMessage(); err != io.EOF {
t.Errorf("Next() succeeded")
}
}
func TestScanMessageWithBoundarySemicolon(t *testing.T) {
mbox := `From notifications@github.com Tue Jun 7 05:46:46 2016
From: Sender <notifications@github.com>
To: foo/bar <bar@noreply.github.com>
Message-ID: <foo/bar/1/0@github.com>
Subject: Re: [foo/bar] [question] Baz? (#1)
Content-Type: multipart/alternative; boundary="--==_mimepart_5755da228145a_38da3facdf97329c42987b";
MIME-Version: 1.0
----==_mimepart_5755da228145a_38da3facdf97329c42987b
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Blah blah
----==_mimepart_5755da228145a_38da3facdf97329c42987b
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 8bit
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<p>Blah blah</p>
----==_mimepart_5755da228145a_38da3facdf97329c42987b--
From notifications@github.com Tue Jun 7 05:52:15 2016
From: Author <notifications@github.com>
To: frob/blab <blab@noreply.github.com>
Message-ID: <frob/blab/1/0@github.com>
Subject: Re: [frob/blab] [question] Bling? (#1)
Content-Type: multipart/alternative; boundary="--==_mimepart_5755db739a819_79783f996b0172c04025ee";
MIME-Version: 1.0
----==_mimepart_5755db739a819_79783f996b0172c04025ee
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Blah blah
----==_mimepart_5755db739a819_79783f996b0172c04025ee
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 8bit
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<p>Blah blah</p>
----==_mimepart_5755db739a819_79783f996b0172c04025ee--
`
expected := 2
b := bytes.NewBufferString(mbox)
m := NewReader(b)
n := 0
for {
_, err := m.NextMessage()
if err == io.EOF {
break
} else if err != nil {
t.Fatalf("m.NextMessaage() = %v", err)
}
n += 1
}
if n != expected {
t.Errorf("Expected: %d; got: %d", expected, n)
}
}
func TestReadingMessageWithLongLine(t *testing.T) {
// We are testing longer line than what `bufio.MaxScanTokenSize` defines.
// RFC 5322 section 2.1.1 says line MUST be less than 998 bytes long, but
// some providers generates messages with such long lines.
want := strings.Repeat("This is very long line.", 5000) // Over 100k bytes.
mbox := fmt.Sprintf(`From herp.derp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
%s`, want)
b := bytes.NewBufferString(mbox)
m := NewReader(b)
r, err := m.NextMessage()
if err != nil {
t.Fatalf("m.NextMessaage() = %v", err)
}
msg, err := mail.ReadMessage(r)
if err != nil {
t.Fatalf("mail.ReadMessage() = %v", err)
}
var body bytes.Buffer
_, err = body.ReadFrom(msg.Body)
if err != nil {
t.Errorf("body.ReadFrom() = %v", err)
}
if body.String() != want+"\r\n" {
t.Errorf("Expected:\n %q\ngot\n%q", want, body.String())
}
_, err = m.NextMessage()
if err != io.EOF {
t.Fatalf("m.NextMessage() = %v", err)
}
}
func ExampleReader() {
r := strings.NewReader(`From herp.derp@example.com Thu Jan 1 00:00:01 2015
From: herp.derp@example.com (Herp Derp)
Date: Thu, 01 Jan 2015 00:00:01 +0100
Subject: Test
This is a simple test.
CU.
From derp.herp@example.com Thu Jan 1 00:00:01 2015
From: derp.herp@example.com (Derp Herp)
Date: Thu, 02 Jan 2015 00:00:01 +0100
Subject: Another test
This is another simple test.
Bye.
`)
mr := NewReader(r)
for {
r, err := mr.NextMessage()
if err == io.EOF {
break
} else if err != nil {
fmt.Print("Oops, something went wrong!", err)
return
}
msg, err := mail.ReadMessage(r)
if err != nil {
fmt.Print("Oops, something went wrong!", err)
return
}
fmt.Printf("Message from %v\n", msg.Header.Get("From"))
}
// Output:
// Message from herp.derp@example.com (Herp Derp)
// Message from derp.herp@example.com (Derp Herp)
}