Debian Codegraph @

envelope_test.go

raw
1 package enmime_test
2
3 import (
4 "bytes"
5 "sort"
6 "strings"
7 "testing"
8
9 "github.com/go-test/deep"
10 "github.com/jhillyerd/enmime"
11 "github.com/jhillyerd/enmime/internal/test"
12 )
13
14 func TestParseHeaderOnly(t *testing.T) {
15 want := ""
16
17 msg := test.OpenTestData("mail", "header-only.raw")
18 e, err := enmime.ReadEnvelope(msg)
19
20 if err != nil {
21 t.Fatal("Failed to parse non-MIME:", err)
22 }
23 if !strings.Contains(e.Text, want) {
24 t.Errorf("Expected %q to contain %q", e.Text, want)
25 }
26 if e.HTML != "" {
27 t.Errorf("Expected no HTML body, got %q", e.HTML)
28 }
29 if e.Root == nil {
30 t.Errorf("Expected a root part")
31 }
32 if len(e.Root.Header) != 7 {
33 t.Errorf("Expected 7 headers, got %d", len(e.Root.Header))
34 }
35 }
36
37 func TestParseNonMime(t *testing.T) {
38 want := "This is a test mailing"
39 msg := test.OpenTestData("mail", "non-mime.raw")
40 e, err := enmime.ReadEnvelope(msg)
41
42 if err != nil {
43 t.Fatal("Failed to parse non-MIME:", err)
44 }
45 if !strings.Contains(e.Text, want) {
46 t.Errorf("Expected %q to contain %q", e.Text, want)
47 }
48 if e.HTML != "" {
49 t.Errorf("Expected no HTML body, got %q", e.HTML)
50 }
51 }
52
53 func TestParseNonMimeHTML(t *testing.T) {
54 msg := test.OpenTestData("mail", "non-mime-html.raw")
55 e, err := enmime.ReadEnvelope(msg)
56
57 if err != nil {
58 t.Fatal("Failed to parse non-MIME:", err)
59 }
60 if len(e.Errors) == 1 {
61 want := enmime.ErrorPlainTextFromHTML
62 got := e.Errors[0].Name
63 if got != want {
64 t.Errorf("e.Errors[0] got: %v, want: %v", got, want)
65 }
66 } else {
67 t.Errorf("len(e.Errors) got: %v, want: 1", len(e.Errors))
68 }
69
70 want := "This is *a* *test* mailing"
71 if !strings.Contains(e.Text, want) {
72 t.Errorf("Expected %q to contain %q", e.Text, want)
73 }
74
75 want = "<span>This</span>"
76 if !strings.Contains(e.HTML, want) {
77 t.Errorf("Expected %q to contain %q", e.HTML, want)
78 }
79 }
80
81 func TestParseMimeTree(t *testing.T) {
82 msg := test.OpenTestData("mail", "attachment.raw")
83 e, err := enmime.ReadEnvelope(msg)
84
85 if err != nil {
86 t.Fatal("Failed to parse MIME:", err)
87 }
88 if e.Root == nil {
89 t.Error("Message should have a root node")
90 }
91 }
92
93 func TestParseInlineText(t *testing.T) {
94 msg := test.OpenTestData("mail", "html-mime-inline.raw")
95 e, err := enmime.ReadEnvelope(msg)
96
97 if err != nil {
98 t.Fatal("Failed to parse MIME:", err)
99 }
100
101 want := "Test of text section"
102 if e.Text != want {
103 t.Error("got:", e.Text, "want:", want)
104 }
105 }
106
107 func TestParseInlineBadCharsetText(t *testing.T) {
108 msg := test.OpenTestData("mail", "html-mime-bad-charset-inline.raw")
109 e, err := enmime.ReadEnvelope(msg)
110
111 if err != nil {
112 t.Fatal("Failed to parse MIME:", err)
113 }
114
115 want := "Test of text section"
116 if e.Text != want {
117 t.Error("got:", e.Text, "want:", want)
118 }
119 }
120
121 func TestParseInlineBadUknownCharsetText(t *testing.T) {
122 msg := test.OpenTestData("mail", "html-mime-bad-unknown-charset-inline.raw")
123 e, err := enmime.ReadEnvelope(msg)
124
125 if err != nil {
126 t.Fatal("Failed to parse MIME:", err)
127 }
128
129 want := "Test of text section"
130 if e.Text != want {
131 t.Error("got:", e.Text, "want:", want)
132 }
133 }
134
135 func TestParseMultiAlernativeText(t *testing.T) {
136 msg := test.OpenTestData("mail", "mime-alternative.raw")
137 e, err := enmime.ReadEnvelope(msg)
138
139 if err != nil {
140 t.Fatal("Failed to parse MIME:", err)
141 }
142
143 want := "Section one\n"
144 if e.Text != want {
145 t.Error("Text parts should not concatenate, got:", e.Text, "want:", want)
146 }
147 }
148
149 func TestParseMultiMixedText(t *testing.T) {
150 msg := test.OpenTestData("mail", "mime-mixed.raw")
151 e, err := enmime.ReadEnvelope(msg)
152
153 if err != nil {
154 t.Fatal("Failed to parse MIME:", err)
155 }
156
157 want := "Section one\n\n--\nSection two"
158 if e.Text != want {
159 t.Error("Text parts should concatenate, got:", e.Text, "want:", want)
160 }
161 }
162
163 func TestParseMultiMixedRelatedHtml(t *testing.T) {
164 msg := test.OpenTestData("mail", "mime-mixed-related.raw")
165 e, err := enmime.ReadEnvelope(msg)
166
167 if err != nil {
168 t.Fatal("Failed to parse MIME:", err)
169 }
170
171 want := "<p>html1</p>"
172 if e.HTML != want {
173 t.Error("Text parts should concatenate, got:", e.HTML, "want:", want)
174 }
175 }
176
177 func TestParseMultiSignedText(t *testing.T) {
178 msg := test.OpenTestData("mail", "mime-signed.raw")
179 e, err := enmime.ReadEnvelope(msg)
180
181 if err != nil {
182 t.Fatal("Failed to parse MIME:", err)
183 }
184
185 want := "Section one\n\n--\nSection two"
186 if e.Text != want {
187 t.Error("Text parts should concatenate, got:", e.Text, "want:", want)
188 }
189 }
190
191 func TestParseQuotedPrintable(t *testing.T) {
192 msg := test.OpenTestData("mail", "quoted-printable.raw")
193 e, err := enmime.ReadEnvelope(msg)
194
195 if err != nil {
196 t.Fatal("Failed to parse MIME:", err)
197 }
198
199 want := "Phasellus sit amet arcu"
200 if !strings.Contains(e.Text, want) {
201 t.Errorf("Text: %q should contain: %q", e.Text, want)
202 }
203 }
204
205 func TestParseQuotedPrintableMime(t *testing.T) {
206 msg := test.OpenTestData("mail", "quoted-printable-mime.raw")
207 e, err := enmime.ReadEnvelope(msg)
208
209 if err != nil {
210 t.Fatal("Failed to parse MIME:", err)
211 }
212
213 want := "Nullam venenatis ante"
214 if !strings.Contains(e.Text, want) {
215 t.Errorf("Text: %q should contain: %q", e.Text, want)
216 }
217 }
218
219 func TestParseInlineHTML(t *testing.T) {
220 msg := test.OpenTestData("mail", "html-mime-inline.raw")
221 e, err := enmime.ReadEnvelope(msg)
222
223 if err != nil {
224 t.Fatal("Failed to parse MIME:", err)
225 }
226
227 want := "<html>"
228 if !strings.Contains(e.HTML, want) {
229 t.Errorf("HTML: %q should contain: %q", e.Text, want)
230 }
231
232 want = "Test of HTML section"
233 if !strings.Contains(e.HTML, want) {
234 t.Errorf("HTML: %q should contain: %q", e.Text, want)
235 }
236 }
237
238 func TestParseAttachment(t *testing.T) {
239 msg := test.OpenTestData("mail", "attachment.raw")
240 e, err := enmime.ReadEnvelope(msg)
241
242 if err != nil {
243 t.Fatal("Failed to parse MIME:", err)
244 }
245
246 want := "A text section"
247 if !strings.Contains(e.Text, want) {
248 t.Errorf("Text: %q should contain: %q", e.Text, want)
249 }
250 if e.HTML != "" {
251 t.Error("mime.HTML should be empty, attachment is not for display, got:", e.HTML)
252 }
253 if len(e.Inlines) > 0 {
254 t.Error("Should have no inlines, got:", len(e.Inlines))
255 }
256 if len(e.Attachments) != 1 {
257 t.Fatal("Should have a single attachment, got:", len(e.Attachments))
258 }
259
260 want = "test.html"
261 got := e.Attachments[0].FileName
262 if got != want {
263 t.Error("FileName got:", got, "want:", want)
264 }
265
266 want = "<html>"
267 test.ContentContainsString(t, e.Attachments[0].Content, want)
268 }
269
270 func TestParseAttachmentOctet(t *testing.T) {
271 msg := test.OpenTestData("mail", "attachment-octet.raw")
272 e, err := enmime.ReadEnvelope(msg)
273
274 if err != nil {
275 t.Fatal("Failed to parse MIME:", err)
276 }
277
278 want := "A text section"
279 if !strings.Contains(e.Text, want) {
280 t.Errorf("Text: %q should contain: %q", e.Text, want)
281 }
282 if e.HTML != "" {
283 t.Error("mime.HTML should be empty, attachment is not for display, got:", e.HTML)
284 }
285 if len(e.Inlines) > 0 {
286 t.Error("Should have no inlines, got:", len(e.Inlines))
287 }
288 if len(e.Attachments) != 1 {
289 t.Fatal("Should have a single attachment, got:", len(e.Attachments))
290 }
291
292 want = "ATTACHMENT.EXE"
293 got := e.Attachments[0].FileName
294 if got != want {
295 t.Error("FileName got:", got, "want:", want)
296 }
297
298 wantBytes := []byte{
299 0x3, 0x17, 0xe1, 0x7e, 0xe8, 0xeb, 0xa2, 0x96, 0x9d, 0x95, 0xa7, 0x67, 0x82, 0x9,
300 0xdf, 0x8e, 0xc, 0x2c, 0x6a, 0x2b, 0x9b, 0xbe, 0x79, 0xa4, 0x69, 0xd8, 0xae, 0x86,
301 0xd7, 0xab, 0xa8, 0x72, 0x52, 0x15, 0xfb, 0x80, 0x8e, 0x47, 0xe1, 0xae, 0xaa, 0x5e,
302 0xa2, 0xb2, 0xc0, 0x90, 0x59, 0xe3, 0x35, 0xf8, 0x60, 0xb7, 0xb1, 0x63, 0x77, 0xd7,
303 0x5f, 0x92, 0x58, 0xa8, 0x75,
304 }
305 if !bytes.Equal(e.Attachments[0].Content, wantBytes) {
306 t.Error("Attachment should have correct content")
307 }
308 }
309
310 func TestParseAttachmentApplication(t *testing.T) {
311 msg := test.OpenTestData("mail", "attachment-application.raw")
312 e, err := enmime.ReadEnvelope(msg)
313 if err != nil {
314 t.Fatal("Failed to parse MIME:", err)
315 }
316
317 if len(e.Inlines) > 0 {
318 t.Error("Should have no inlines, got:", len(e.Inlines))
319 }
320 if len(e.Attachments) != 1 {
321 t.Fatal("Should have a single attachment, got:", len(e.Attachments))
322 }
323
324 want := "some.doc"
325 got := e.Attachments[0].FileName
326 if got != want {
327 t.Error("FileName got:", got, "want:", want)
328 }
329 }
330
331 func TestParseOtherParts(t *testing.T) {
332 msg := test.OpenTestData("mail", "other-parts.raw")
333 e, err := enmime.ReadEnvelope(msg)
334 if err != nil {
335 t.Fatal("Failed to parse MIME:", err)
336 }
337
338 want := "A text section"
339 if !strings.Contains(e.Text, want) {
340 t.Errorf("Text: %q should contain: %q", e.Text, want)
341 }
342 if e.HTML != "" {
343 t.Error("mime.HTML should be empty, attachment is not for display, got:", e.HTML)
344 }
345 if len(e.Inlines) > 0 {
346 t.Error("Should have no inlines, got:", len(e.Inlines))
347 }
348 if len(e.Attachments) > 0 {
349 t.Error("Should have no attachments, got:", len(e.Attachments))
350 }
351 if len(e.OtherParts) != 1 {
352 t.Fatal("Should have one other part, got:", len(e.OtherParts))
353 }
354
355 want = "B05.gif"
356 got := e.OtherParts[0].FileName
357 if got != want {
358 t.Error("FileName got:", got, "want:", want)
359 }
360 wantBytes := []byte{
361 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0xf, 0x0, 0xf, 0x0, 0xa2, 0x5, 0x0, 0xde, 0xeb,
362 0xf3, 0x5b, 0xb0, 0xec, 0x0, 0x89, 0xe3, 0xa3, 0xd0, 0xed, 0x0, 0x46, 0x74, 0xdd,
363 0xed, 0xfa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x21, 0xf9, 0x4, 0x1, 0x0, 0x0, 0x5, 0x0,
364 0x2c, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x3, 0x40, 0x58, 0x25, 0xa4, 0x4b,
365 0xb0, 0x39, 0x1, 0x46, 0xa3, 0x23, 0x5b, 0x47, 0x46, 0x68, 0x9d, 0x20, 0x6, 0x9f,
366 0xd2, 0x95, 0x45, 0x44, 0x8, 0xe8, 0x29, 0x39, 0x69, 0xeb, 0xbd, 0xc, 0x41, 0x4a,
367 0xae, 0x82, 0xcd, 0x1c, 0x9f, 0xce, 0xaf, 0x1f, 0xc3, 0x34, 0x18, 0xc2, 0x42, 0xb8,
368 0x80, 0xf1, 0x18, 0x84, 0xc0, 0x9e, 0xd0, 0xe8, 0xf2, 0x1, 0xb5, 0x19, 0xad, 0x41,
369 0x53, 0x33, 0x9b, 0x0, 0x0, 0x3b,
370 }
371 if !bytes.Equal(e.OtherParts[0].Content, wantBytes) {
372 t.Error("Other part should have correct content")
373 }
374 }
375
376 func TestParseInline(t *testing.T) {
377 msg := test.OpenTestData("mail", "html-mime-inline.raw")
378 e, err := enmime.ReadEnvelope(msg)
379 if err != nil {
380 t.Fatal("Failed to parse MIME:", err)
381 }
382
383 want := "Test of text section"
384 if !strings.Contains(e.Text, want) {
385 t.Errorf("Text: %q should contain: %q", e.Text, want)
386 }
387
388 want = ">Test of HTML section<"
389 if !strings.Contains(e.HTML, want) {
390 t.Errorf("HTML: %q should contain %q", e.HTML, want)
391 }
392
393 if len(e.Inlines) != 1 {
394 t.Fatal("Should have one inline, got:", len(e.Inlines))
395 }
396 if len(e.Attachments) > 0 {
397 t.Error("Should have no attachments, got:", len(e.Attachments))
398 }
399
400 want = "favicon.png"
401 got := e.Inlines[0].FileName
402 if got != want {
403 t.Error("FileName got:", got, "want:", want)
404 }
405 if !bytes.HasPrefix(e.Inlines[0].Content, []byte{0x89, 'P', 'N', 'G'}) {
406 t.Error("Inline should have correct content")
407 }
408 }
409
410 func TestParseOtherPartsRelated(t *testing.T) {
411 msg := test.OpenTestData("mail", "other-multi-related.raw")
412 e, err := enmime.ReadEnvelope(msg)
413 if err != nil {
414 t.Fatal("Failed to parse MIME:", err)
415 }
416
417 want := "Plain text."
418 if !strings.Contains(e.Text, want) {
419 t.Errorf("Text: %q should contain: %q", e.Text, want)
420 }
421
422 want = "<i>HTML text.</i>"
423 if !strings.Contains(e.HTML, want) {
424 t.Errorf("HTML: %q should contain %q", e.HTML, want)
425 }
426
427 if len(e.Attachments) > 0 {
428 t.Error("Should have no attachments, got:", len(e.Attachments))
429 }
430 if len(e.Inlines) > 0 {
431 t.Error("Should have no inlines, got:", len(e.Inlines))
432 }
433 if len(e.OtherParts) != 2 {
434 t.Fatal("Should have two other parts, got:", len(e.Inlines))
435 }
436
437 want = "image001.png"
438 got := e.OtherParts[0].FileName
439 if got != want {
440 t.Error("FileName got:", got, "want:", want)
441 }
442 want = "[email protected]"
443 got = e.OtherParts[0].ContentID
444 if got != want {
445 t.Error("ContentID got:", got, "want:", want)
446 }
447 if !bytes.HasPrefix(e.OtherParts[0].Content, []byte{0x89, 'P', 'N', 'G'}) {
448 t.Error("Other part should have correct content")
449 }
450
451 want = "image002.png"
452 got = e.OtherParts[1].FileName
453 if got != want {
454 t.Error("FileName got:", got, "want:", want)
455 }
456 want = "[email protected]"
457 got = e.OtherParts[1].ContentID
458 if got != want {
459 t.Error("ContentID got:", got, "want:", want)
460 }
461 if !bytes.HasPrefix(e.OtherParts[1].Content, []byte{0x89, 'P', 'N', 'G'}) {
462 t.Error("Other part should have correct content")
463 }
464 }
465
466 func TestParseHTMLOnlyInline(t *testing.T) {
467 msg := test.OpenTestData("mail", "html-only-inline.raw")
468 e, err := enmime.ReadEnvelope(msg)
469 if err != nil {
470 t.Fatal("Failed to parse MIME:", err)
471 }
472
473 if len(e.Errors) == 1 {
474 want := enmime.ErrorPlainTextFromHTML
475 got := e.Errors[0].Name
476 if got != want {
477 t.Errorf("e.Errors[0] got: %v, want: %v", got, want)
478 }
479 } else {
480 t.Errorf("len(e.Errors) got: %v, want: 1", len(e.Errors))
481 }
482
483 want := "Test of HTML section"
484 if !strings.Contains(e.Text, want) {
485 t.Errorf("Downconverted Text: %q should contain: %q", e.Text, want)
486 }
487
488 want = ">Test of HTML section<"
489 if !strings.Contains(e.HTML, want) {
490 t.Errorf("HTML: %q should contain %q", e.HTML, want)
491 }
492
493 if len(e.Inlines) != 1 {
494 t.Error("Should one inline, got:", len(e.Inlines))
495 }
496 if len(e.Attachments) > 0 {
497 t.Fatal("Should have no attachments, got:", len(e.Attachments))
498 }
499
500 want = "favicon.png"
501 got := e.Inlines[0].FileName
502 if got != want {
503 t.Error("FileName got:", got, "want:", want)
504 }
505 if !bytes.HasPrefix(e.Inlines[0].Content, []byte{0x89, 'P', 'N', 'G'}) {
506 t.Error("Inline should have correct content")
507 }
508 }
509
510 func TestParseInlineMultipart(t *testing.T) {
511 msg := test.OpenTestData("mail", "inlinemultipart.raw")
512 e, err := enmime.ReadEnvelope(msg)
513 if err != nil {
514 t.Fatal("Failed to parse MIME:", err)
515 }
516
517 if len(e.Errors) != 0 {
518 t.Errorf("len(e.Errors) got: %v, want: 0", len(e.Errors))
519 }
520
521 want := "Simple text."
522 if !strings.Contains(e.Text, want) {
523 t.Errorf("Downconverted Text: %q should contain: %q", e.Text, want)
524 }
525
526 if len(e.Inlines) != 1 {
527 t.Error("Should have one inline, got:", len(e.Inlines))
528 }
529 if len(e.Attachments) != 1 {
530 t.Fatal("Should have one attachments, got:", len(e.Attachments))
531 }
532
533 want = "test.txt"
534 got := e.Inlines[0].FileName
535 if got != want {
536 t.Error("FileName got:", got, "want:", want)
537 }
538
539 if !bytes.HasPrefix(e.Inlines[0].Content, []byte("Text")) {
540 t.Error("Inline should have correct content")
541 }
542 }
543
544 func TestParseNestedHeaders(t *testing.T) {
545 msg := test.OpenTestData("mail", "html-mime-inline.raw")
546 e, err := enmime.ReadEnvelope(msg)
547
548 if err != nil {
549 t.Fatal("Failed to parse MIME:", err)
550 }
551 if len(e.Inlines) != 1 {
552 t.Error("Should one inline, got:", len(e.Inlines))
553 }
554
555 want := "favicon.png"
556 got := e.Inlines[0].FileName
557 if got != want {
558 t.Error("FileName got:", got, "want:", want)
559 }
560 want = "<8B8481A2-25CA-4886-9B5A-8EB9115DD064@skynet>"
561 got = e.Inlines[0].Header.Get("Content-Id")
562 if got != want {
563 t.Errorf("Content-Id header was: %q, want: %q", got, want)
564 }
565 }
566
567 func TestParseHTMLOnlyCharsetInHeaderOnly(t *testing.T) {
568 msg := test.OpenTestData("mail", "non-mime-html-charset-header-only.raw")
569 e, err := enmime.ReadEnvelope(msg)
570 if err != nil {
571 t.Fatal("Failed to parse non-MIME:", err)
572 }
573
574 if !strings.ContainsRune(e.HTML, 0xfc) {
575 t.Error("HTML body should contained German ü")
576 }
577
578 if !strings.Contains(e.HTML, "Müller") {
579 t.Error("HTML body should contained 'Müller'")
580 }
581 }
582
583 func TestEnvelopeGetHeader(t *testing.T) {
584 // Test empty header
585 e := &enmime.Envelope{}
586 want := ""
587 got := e.GetHeader("Subject")
588 if got != want {
589 t.Errorf("Subject was: %q, want: %q", got, want)
590 }
591
592 // Even non-MIME messages should support encoded-words in headers
593 // Also, encoded addresses should be suppored
594 r := test.OpenTestData("mail", "qp-ascii-header.raw")
595 e, err := enmime.ReadEnvelope(r)
596 if err != nil {
597 t.Fatal("Failed to parse non-MIME:", err)
598 }
599
600 want = "Test QP Subject!"
601 got = e.GetHeader("Subject")
602 if got != want {
603 t.Errorf("Subject was: %q, want: %q", got, want)
604 }
605
606 // Test UTF-8 subject line
607 r = test.OpenTestData("mail", "qp-utf8-header.raw")
608 e, err = enmime.ReadEnvelope(r)
609 if err != nil {
610 t.Fatal("Failed to parse MIME:", err)
611 }
612
613 want = "MIME UTF8 Test \u00a2 More Text"
614 got = e.GetHeader("Subject")
615 if got != want {
616 t.Errorf("Subject was: %q, want: %q", got, want)
617 }
618 }
619
620 func TestEnvelopeGetHeaderKeys(t *testing.T) {
621 // Test empty header
622 e := &enmime.Envelope{}
623 got := e.GetHeaderKeys()
624 if got != nil {
625 t.Errorf("Headers was: %q, want: nil", got)
626 }
627
628 // Even non-MIME messages should support encoded-words in headers
629 // Also, encoded addresses should be suppored
630 r := test.OpenTestData("mail", "qp-ascii-header.raw")
631 e, err := enmime.ReadEnvelope(r)
632 if err != nil {
633 t.Fatal("Failed to parse non-MIME:", err)
634 }
635
636 want := []string{"Date", "From", "Subject", "To", "X-Mailer"}
637 got = e.GetHeaderKeys()
638 sort.Strings(got)
639 test.DiffStrings(t, got, want)
640
641 // Test UTF-8 subject line
642 r = test.OpenTestData("mail", "qp-utf8-header.raw")
643 e, err = enmime.ReadEnvelope(r)
644 if err != nil {
645 t.Fatal("Failed to parse MIME:", err)
646 }
647
648 want = []string{"Content-Type", "Date", "From", "Message-Id", "Mime-Version", "Sender", "Subject", "To", "User-Agent"}
649 got = e.GetHeaderKeys()
650 sort.Strings(got)
651 test.DiffStrings(t, got, want)
652 }
653
654 func TestEnvelopeGetHeaderValues(t *testing.T) {
655 r := test.OpenTestData("mail", "ctype-bug.raw")
656 e, err := enmime.ReadEnvelope(r)
657 if err != nil {
658 t.Fatal("Failed to parse MIME:", err)
659 }
660
661 // test Received headers
662 want := []string{
663 "by 10.76.55.35 with SMTP id o3csp106612oap; Fri, 10 Jul 2015 13:12:34 -0700 (PDT)",
664 "from mail135-10.atl141.mandrillapp.com (mail135-10.atl141.mandrillapp.com. [198.2.135.10]) by mx.google.com with ESMTPS id k184si6630505ywf.180.2015.07.10.13.12.34 for <[email protected]> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Jul 2015 13:12:34 -0700 (PDT)",
665 "from pmta03.mandrill.prod.atl01.rsglab.com (127.0.0.1) by mail135-10.atl141.mandrillapp.com id hk0jj41sau80 for <[email protected]>; Fri, 10 Jul 2015 20:12:33 +0000 (envelope-from <bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com>)",
666 "from [67.214.212.122] by mandrillapp.com id 163e4a0faf244a2da6b0121cc7af1fe9; Fri, 10 Jul 2015 20:12:33 +0000",
667 }
668 got := e.GetHeaderValues("received")
669 diff := deep.Equal(got, want)
670 if diff != nil {
671 t.Errorf("Got: %+v, want: %+v", got, want)
672 }
673 }
674
675 func TestEnvelopeSetHeader(t *testing.T) {
676 r := test.OpenTestData("mail", "qp-utf8-header.raw")
677 e, err := enmime.ReadEnvelope(r)
678 if err != nil {
679 t.Fatal("Failed to parse MIME:", err)
680 }
681
682 // replace existing header
683 want := "André Pirard <[email protected]>"
684 e.SetHeader("To", []string{want})
685 got := e.GetHeader("To")
686 if got != want {
687 t.Errorf("Got: %q, want: %q", got, want)
688 }
689
690 // replace existing header with multiple values
691 wantSlice := []string{"Mirosław Marczak <[email protected]>", "James Hillyerd <[email protected]>"}
692 e.SetHeader("To", wantSlice)
693 gotSlice := e.GetHeaderValues("to")
694 diff := deep.Equal(gotSlice, wantSlice)
695 if diff != nil {
696 t.Errorf("Got: %+v, want: %+v", gotSlice, wantSlice)
697 }
698
699 // replace non-existing header
700 want = "foobar"
701 e.SetHeader("X-Foo-Bar", []string{want})
702 got = e.GetHeader("X-Foo-Bar")
703 if got != want {
704 t.Errorf("Got: %q, want: %q", got, want)
705 }
706 }
707
708 func TestEnvelopeAddHeader(t *testing.T) {
709 r := test.OpenTestData("mail", "qp-utf8-header.raw")
710 e, err := enmime.ReadEnvelope(r)
711 if err != nil {
712 t.Fatal("Failed to parse MIME:", err)
713 }
714
715 // add to existing header
716 to := "James Hillyerd <[email protected]>"
717 wantSlice := []string{"Mirosław Marczak <[email protected]>", "James Hillyerd <[email protected]>"}
718 e.AddHeader("To", to)
719 gotSlice := e.GetHeaderValues("To")
720 diff := deep.Equal(gotSlice, wantSlice)
721 if diff != nil {
722 t.Errorf("Got: %+v, want: %+v", gotSlice, wantSlice)
723 }
724
725 // add to non-existing header
726 want := "foobar"
727 e.AddHeader("X-Foo-Bar", want)
728 got := e.GetHeader("X-Foo-Bar")
729 if got != want {
730 t.Errorf("Got: %q, want: %q", got, want)
731 }
732 }
733
734 func TestEnvelopeDeleteHeader(t *testing.T) {
735 r := test.OpenTestData("mail", "qp-utf8-header.raw")
736 e, err := enmime.ReadEnvelope(r)
737 if err != nil {
738 t.Fatal("Failed to parse MIME:", err)
739 }
740
741 // delete user-agent header
742 e.DeleteHeader("User-Agent")
743 got := e.GetHeader("User-Agent")
744 want := ""
745 if got != want {
746 t.Errorf("Got: %q, want: %q", got, want)
747 }
748 }
749
750 func TestEnvelopeAddressList(t *testing.T) {
751 // Test empty header
752 e := &enmime.Envelope{}
753 _, err := e.AddressList("To")
754 if err == nil {
755 t.Error("AddressList(\"To\") should have returned err, got nil")
756 }
757
758 r := test.OpenTestData("mail", "qp-utf8-header.raw")
759 e, err = enmime.ReadEnvelope(r)
760 if err != nil {
761 t.Fatal("Failed to parse MIME:", err)
762 }
763
764 _, err = e.AddressList("BCC")
765 if err == nil {
766 t.Error("AddressList(\"BCC\") should have returned err, got nil")
767 }
768
769 _, err = e.AddressList("Subject")
770 if err == nil {
771 t.Error("AddressList(\"Subject\") should have returned err, got nil")
772 }
773
774 toAddresses, err := e.AddressList("To")
775 if err != nil {
776 t.Fatal("Failed to parse To list:", err)
777 }
778 if len(toAddresses) != 1 {
779 t.Fatalf("len(toAddresses) == %v, want: %v", len(toAddresses), 1)
780 }
781
782 // Confirm address name was decoded properly
783 want := "Mirosław Marczak"
784 got := toAddresses[0].Name
785 if got != want {
786 t.Errorf("To was: %q, want: %q", got, want)
787 }
788
789 senderAddresses, err := e.AddressList("Sender")
790 if err != nil {
791 t.Fatal("Failed to parse Sender list:", err)
792 }
793 if len(senderAddresses) != 1 {
794 t.Fatalf("len(senderAddresses) == %v, want: %v", len(senderAddresses), 1)
795 }
796
797 // Confirm address name was decoded properly
798 want = "André Pirard"
799 got = senderAddresses[0].Name
800 if got != want {
801 t.Errorf("Sender was: %q, want: %q", got, want)
802 }
803 }
804
805 func TestDetectCharacterSetInHTML(t *testing.T) {
806 msg := test.OpenTestData("mail", "non-mime-missing-charset.raw")
807 e, err := enmime.ReadEnvelope(msg)
808 if err != nil {
809 t.Fatal("Failed to parse non-MIME:", err)
810 }
811 if strings.ContainsRune(e.HTML, 0x80) {
812 t.Error("HTML body should not have contained a Windows CP1250 Euro Symbol")
813 }
814 if !strings.ContainsRune(e.HTML, 0x20ac) {
815 t.Error("HTML body should have contained a Unicode Euro Symbol")
816 }
817 }
818
819 func TestAttachmentOnly(t *testing.T) {
820 var aTests = []struct {
821 filename string
822 attachmentsLen int
823 inlinesLen int
824 }{
825 {filename: "attachment-only.raw", attachmentsLen: 1, inlinesLen: 0},
826 {filename: "attachment-only-inline.raw", attachmentsLen: 0, inlinesLen: 1},
827 }
828
829 for _, a := range aTests {
830 // Mail with disposition attachment
831 msg := test.OpenTestData("mail", a.filename)
832 e, err := enmime.ReadEnvelope(msg)
833 if err != nil {
834 t.Fatal("Failed to parse MIME:", err)
835 }
836 if len(e.Attachments) != a.attachmentsLen {
837 t.Fatal("len(Attachments) got:", len(e.Attachments), "want:", a.attachmentsLen)
838 }
839 if a.attachmentsLen > 0 {
840 got := e.Attachments[0].Content
841 if !bytes.HasPrefix(got, []byte{0x89, 'P', 'N', 'G'}) {
842 t.Errorf("Content should be PNG image, got: %v", got)
843 }
844 }
845 if len(e.Inlines) != a.inlinesLen {
846 t.Fatal("len(Inlines) got:", len(e.Inlines), "want:", a.inlinesLen)
847 }
848 if a.inlinesLen > 0 {
849 got := e.Inlines[0].Content
850 if !bytes.HasPrefix(got, []byte{0x89, 'P', 'N', 'G'}) {
851 t.Errorf("Content should be PNG image, got: %v", got)
852 }
853 }
854 // Check, if root header is set
855 if len(e.Root.Header) < 1 {
856 t.Errorf("No root header defined, but must be set from binary only part.")
857 }
858 // Check, that the root part has content
859 if len(e.Root.Content) == 0 {
860 t.Errorf("Root part of envelope has no content.")
861 }
862 }
863 }
864
865 func TestDuplicateParamsInMime(t *testing.T) {
866 msg := test.OpenTestData("mail", "mime-duplicate-param.raw")
867 e, err := enmime.ReadEnvelope(msg)
868
869 if err != nil {
870 t.Fatal("Failed to parse MIME:", err)
871 }
872
873 if e.Attachments[0].FileName != "Invoice_302232133150612.pdf" {
874 t.Fatal("Mail should have a part with filename Invoice_302232133150612.pdf")
875 }
876 }
877
878 func TestUnquotedSpecialCharParamsInMime(t *testing.T) {
879 msg := test.OpenTestData("mail", "mime-unquoted-tspecials-param.raw")
880 e, err := enmime.ReadEnvelope(msg)
881
882 if err != nil {
883 t.Fatal("Failed to parse MIME:", err)
884 }
885
886 if e.Attachments[0].FileName != "Invoice_(302232133150612).pdf" {
887 t.Fatal("Mail should have a part with filename Invoice_(302232133150612).pdf")
888 }
889 }
890
891 func TestBadAddressHeaderInMime(t *testing.T) {
892 msg := test.OpenTestData("mail", "malformed-multiple-address-header-values.raw")
893 e, err := enmime.ReadEnvelope(msg)
894
895 if err != nil {
896 t.Fatal("Failed to parse MIME:", err)
897 }
898
899 froms, err := e.AddressList("From")
900 if err != nil {
901 t.Log(err)
902 }
903
904 if len(froms) < 1 {
905 t.Fatal("From header should have at least one entry")
906 }
907 }
908
909 func TestBadContentTypeInMime(t *testing.T) {
910 msg := test.OpenTestData("mail", "mime-bad-content-type.raw")
911 e, err := enmime.ReadEnvelope(msg)
912
913 if err != nil {
914 t.Fatal("Failed to parse MIME:", err)
915 }
916
917 if e.Attachments[0].FileName != "Invoice_302232133150612.pdf" {
918 t.Fatal("Mail should have a part with filename Invoice_302232133150612.pdf")
919 }
920 }
921
922 func TestBadContentTransferEncodingInMime(t *testing.T) {
923 msg := test.OpenTestData("mail", "mime-bad-content-transfer-encoding.raw")
924 e, err := enmime.ReadEnvelope(msg)
925
926 if err != nil {
927 t.Fatal("Failed to parse MIME:", err)
928 }
929
930 var expectedErrorPresent bool
931 for _, v := range e.Errors {
932 if v.Name == enmime.ErrorMalformedBase64 && v.Severe {
933 expectedErrorPresent = true
934 }
935 }
936 if !expectedErrorPresent {
937 t.Fatal("Mail should have a severe malformed base64 error")
938 }
939 }
940
941 func TestBadMime8bitFilename(t *testing.T) {
942 msg := test.OpenTestData("mail", "mime-bad-8bit-filename.raw")
943 e, err := enmime.ReadEnvelope(msg)
944
945 if err != nil {
946 t.Fatal("Failed to parse MIME:", err)
947 }
948 if strings.TrimSpace(e.Text) != "Text part" {
949 t.Fatal("Text part not parsed correctly")
950 }
951 if len(e.Attachments) != 1 {
952 t.Fatal("Wrong number of attachments")
953 }
954 if e.Attachments[0].FileName != "管理.doc" {
955 t.Fatal("Wrong attachment name")
956 }
957 }
958
959 func TestBlankMediaName(t *testing.T) {
960 msg := test.OpenTestData("mail", "mime-blank-media-name.raw")
961 e, err := enmime.ReadEnvelope(msg)
962
963 if err != nil {
964 t.Fatal("Failed to parse MIME:", err)
965 }
966
967 if e.Attachments[0].FileName != "Invoice_302232133150612.pdf" {
968 t.Fatal("Mail should have a part with filename Invoice_302232133150612.pdf")
969 }
970 }
971
972 func TestEnvelopeHeaders(t *testing.T) {
973 headers := map[string]string{
974 "Received-Spf": "pass (google.com: domain of bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com designates 198.2.135.10 as permitted sender) client-ip=198.2.135.10;",
975 "To": "<[email protected]>",
976 "Domainkey-Signature": "a=rsa-sha1; c=nofws; q=dns; s=mandrill; d=papertrailapp.com; b=Cv9EE+3+CO+puDhpfQOsuwuP6YqJQBA/Z6OofPTXqWf/Asr/edsi7aoXIE+forQ/q8DjhhMMuMiD bQ1tlRXMFckw08GjqU7RN+ouwJEMXOpzxUgp6OwrITvddwhddEg6H3uYRva5pNJqonDDykshHyjA EVeAdcY4tjYQrcRxw/0=;",
977 "Dkim-Signature": "v=1; a=rsa-sha1; c=relaxed/relaxed; s=mandrill; d=papertrailapp.com; h=From:Subject:To:Message-Id:Date:MIME-Version:Content-Type; [email protected]; bh=2tw/BU7QN7gmFr2K2wnVpETYxbU=; b=T+PzWzjbOoKO3jNANsmqsnbM+gnbgT9EQBP8DOSno75iHQ9AuU6xcDCPctvJt50Exr6aTs9qJmEG baCa39danDRIx5zXsdaSy34+SKfDODdgmwEEfKFeULQGPwF1g73tXeX4k0kwt+bm6f0baWbaLwR1 RdhUd42jEMossTKuD9w= v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandrillapp.com; [email protected]; q=dns/txt; s=mandrill; t=1436559153; h=From : Subject : To : Message-Id : Date : MIME-Version : Content-Type : From : Subject : Date : X-Mandrill-User : List-Unsubscribe; bh=eW2QM8XcfLCwIBTvTJaT619pYOD3YrxBvxC9cZ2gxe0=; b=quxFFNbO04KKNNB8yMd9Zch6wogobVbNFlpGIOQI/jA9FuhdZvMxQwwZ2jeno7c17v2eXY Vp3c1vwvVERCboNaPwwxrKkrhqMxM8rb15n8xM3v0IplkQ3vs9G5agiTT1qqxErsrS6xAqmj UNUPKEXuSjr24HqmQzxPry0aIgHdI=",
978 "Message-Id": "<[email protected]>",
979 "X-Report-Abuse": "Please forward a copy of this message, including all headers, to [email protected] You can also report abuse here: http://mandrillapp.com/contact/abuse?id=30112948.163e4a0faf244a2da6b0121cc7af1fe9",
980 "Mime-Version": "1.0",
981 "Return-Path": "<bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com> <bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com>",
982 "Authentication-Results": "mx.google.com; spf=pass (google.com: domain of bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com designates 198.2.135.10 as permitted sender) smtp.mail=bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com; dkim=pass [email protected]; dkim=pass [email protected]",
983 "From": "Papertrail <[email protected]>",
984 "Subject": "Welcome to Papertrail",
985 "Content-Type": `multipart/alternative; boundary="_av-rPFkvS5QROAYLq2cQTUr1w"`,
986 "X-Mandrill-User": "md_30112948",
987 "Delivered-To": "[email protected]",
988 "Received": "by 10.76.55.35 with SMTP id o3csp106612oap; Fri, 10 Jul 2015 13:12:34 -0700 (PDT) from mail135-10.atl141.mandrillapp.com (mail135-10.atl141.mandrillapp.com. [198.2.135.10]) by mx.google.com with ESMTPS id k184si6630505ywf.180.2015.07.10.13.12.34 for <[email protected]> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Jul 2015 13:12:34 -0700 (PDT) from pmta03.mandrill.prod.atl01.rsglab.com (127.0.0.1) by mail135-10.atl141.mandrillapp.com id hk0jj41sau80 for <[email protected]>; Fri, 10 Jul 2015 20:12:33 +0000 (envelope-from <bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com>) from [67.214.212.122] by mandrillapp.com id 163e4a0faf244a2da6b0121cc7af1fe9; Fri, 10 Jul 2015 20:12:33 +0000",
989 "X-Received": "by 10.170.119.147 with SMTP id l141mr25507408ykb.89.1436559154116; Fri, 10 Jul 2015 13:12:34 -0700 (PDT)",
990 "Date": "Fri, 10 Jul 2015 20:12:33 +0000",
991 }
992
993 msg := test.OpenTestData("mail", "ctype-bug.raw")
994 e, err := enmime.ReadEnvelope(msg)
995
996 if err != nil {
997 t.Fatal("Failed to parse MIME:", err)
998 }
999
1000 if len(e.Root.Header) != len(headers) {
1001 t.Errorf("Failed to extract expected headers. Got %v headers, expected %v",
1002 len(e.Root.Header), len(headers))
1003 }
1004
1005 for k := range headers {
1006 if e.Root.Header[k] == nil {
1007 t.Errorf("Header named %q was missing, want it to exist", k)
1008 }
1009 }
1010
1011 for k, v := range e.Root.Header {
1012 if _, ok := headers[k]; !ok {
1013 t.Errorf("Got header named %q, did not expect it to exist", k)
1014 continue
1015 }
1016 for _, val := range v {
1017 if !strings.Contains(headers[k], val) {
1018 t.Errorf("Got header %q with value %q, wanted value contained in:\n%q",
1019 k, val, headers[k])
1020 }
1021 }
1022 }
1023 }
1024
1025 func TestInlineTextBody(t *testing.T) {
1026 headers := map[string]string{
1027 "To": "<[email protected]>",
1028 "Message-Id": "<[email protected]>",
1029 "Mime-Version": "1.0",
1030 "From": "Chris Garrett <[email protected]>",
1031 "Subject": "Text body only with disposition inline",
1032 "Content-Type": `text/html; charset="UTF-8"`,
1033 "Content-Disposition": "inline",
1034 "Content-Transfer-Encoding": "quoted-printable",
1035 "Date": "Wed, 8 Feb 2017 03:23:13 -0500",
1036 }
1037
1038 msg := test.OpenTestData("mail", "attachment-only-inline-quoted-printable.raw")
1039 e, err := enmime.ReadEnvelope(msg)
1040
1041 if err != nil {
1042 t.Fatal("Failed to parse MIME:", err)
1043 }
1044
1045 want := "Just some html content"
1046 if e.Text != want {
1047 t.Errorf("Got Text with value %s, wanted value:\n%s", e.Text, want)
1048 }
1049 if !strings.Contains(e.HTML, want) {
1050 t.Errorf("Expected %q to contain %q", e.HTML, want)
1051 }
1052
1053 if len(e.Root.Header) != len(headers) {
1054 t.Errorf("Failed to extract expected headers. Got %v headers, expected %v",
1055 len(e.Root.Header), len(headers))
1056 }
1057
1058 for k := range headers {
1059 if e.Root.Header[k] == nil {
1060 t.Errorf("Header named %q was missing, want it to exist", k)
1061 }
1062 }
1063
1064 for k, v := range e.Root.Header {
1065 if _, ok := headers[k]; !ok {
1066 t.Errorf("Got header named %q, did not expect it to exist", k)
1067 continue
1068 }
1069 for _, val := range v {
1070 if !strings.Contains(headers[k], val) {
1071 t.Errorf("Got header %q with value %q, wanted value contained in:\n%q",
1072 k, val, headers[k])
1073 }
1074 }
1075 }
1076 }
1077
1078 func TestBinaryOnlyBodyHeaders(t *testing.T) {
1079 headers := map[string]string{
1080 "To": "[email protected]",
1081 "From": "[email protected]",
1082 "Subject": "Test",
1083 "Message-Id": "<[email protected]>",
1084 "Date": "Thu, 21 Jan 2016 10:52:31 +0100",
1085 "Mime-Version": "1.0",
1086 "Content-Type": `image/jpeg; name="favicon.jpg"`,
1087 "Content-Transfer-Encoding": "base64",
1088 "Content-Disposition": `attachment; filename="favicon.jpg"`,
1089 }
1090
1091 msg := test.OpenTestData("mail", "attachment-only.raw")
1092 e, err := enmime.ReadEnvelope(msg)
1093
1094 if err != nil {
1095 t.Fatal("Failed to parse MIME:", err)
1096 }
1097
1098 if len(e.Root.Header) != len(headers) {
1099 t.Errorf("Failed to extract expected headers. Got %v headers, expected %v",
1100 len(e.Root.Header), len(headers))
1101 }
1102
1103 for k := range headers {
1104 if e.Root.Header[k] == nil {
1105 t.Errorf("Header named %q was missing, want it to exist", k)
1106 }
1107 }
1108
1109 for k, v := range e.Root.Header {
1110 if _, ok := headers[k]; !ok {
1111 t.Errorf("Got header named %q, did not expect it to exist", k)
1112 continue
1113 }
1114 for _, val := range v {
1115 if !strings.Contains(headers[k], val) {
1116 t.Errorf("Got header %q with value %q, wanted value contained in:\n%q",
1117 k, val, headers[k])
1118 }
1119 }
1120 }
1121 }
1122
1123 func TestEnvelopeEpilogue(t *testing.T) {
1124 msg := test.OpenTestData("mail", "epilogue-sample.raw")
1125 e, err := enmime.ReadEnvelope(msg)
1126 if err != nil {
1127 t.Fatal("Failed to parse MIME:", err)
1128 }
1129
1130 got := string(e.Root.Epilogue)
1131 want := "Potentially malicious content\n"
1132 if got != want {
1133 t.Errorf("Epilogue == %q, want: %q", got, want)
1134 }
1135 }
1136
1137 func TestCloneEnvelope(t *testing.T) {
1138 msg := test.OpenTestData("mail", "other-multi-related.raw")
1139 e, err := enmime.ReadEnvelope(msg)
1140 if err != nil {
1141 t.Fatal("Failed to parse MIME:", err)
1142 }
1143 clone := e.Clone()
1144 test.CompareEnvelope(t, clone, e)
1145 }
1146
1147 func TestNilHeaderValues(t *testing.T) {
1148 e := &enmime.Envelope{}
1149 values := e.GetHeaderValues("")
1150 if len(values) > 0 {
1151 t.Fatal("No header values should be available, failed")
1152 }
1153 }
1154
1155 func TestSetHeaderEmptyName(t *testing.T) {
1156 e := &enmime.Envelope{}
1157 err := e.SetHeader("", nil)
1158 if err == nil || err.Error() != "provide non-empty header name" {
1159 t.Fatal("Cannot set a header without a name, failed")
1160 }
1161 }
1162
1163 func TestAddHeaderEmptyName(t *testing.T) {
1164 e := &enmime.Envelope{}
1165 err := e.AddHeader("", "")
1166 if err == nil || err.Error() != "provide non-empty header name" {
1167 t.Fatal("Cannot add a header without a name, failed")
1168 }
1169 }
1170
1171 func TestDeleteHeaderEmptyName(t *testing.T) {
1172 e := &enmime.Envelope{}
1173 err := e.DeleteHeader("")
1174 if err == nil || err.Error() != "provide non-empty header name" {
1175 t.Fatal("Cannot add a header without a name, failed")
1176 }
1177 }
1178
1179 func TestNilEnvelopeClone(t *testing.T) {
1180 var e *enmime.Envelope
1181 if e.Clone() != nil {
1182 t.Fatal("Clone of nil envelope is not nil, failed")
1183 }
1184 }