forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envelope_test.go
972 lines (847 loc) · 27.1 KB
/
envelope_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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
package enmime
import (
"bytes"
"io/ioutil"
"net/textproto"
"strings"
"testing"
)
func TestIdentifySinglePart(t *testing.T) {
r := openTestData("mail", "non-mime.raw")
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if isMultipartMessage(msg) {
t.Error("Failed to identify non-multipart message")
}
}
func TestIdentifyMultiPart(t *testing.T) {
r := openTestData("mail", "html-mime-inline.raw")
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !isMultipartMessage(msg) {
t.Error("Failed to identify multipart MIME message")
}
}
func TestIdentifyUnknownMultiPart(t *testing.T) {
r := openTestData("mail", "unknown-part-type.raw")
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !isMultipartMessage(msg) {
t.Error("Failed to identify multipart MIME message of unknown type")
}
}
func TestIdentifyBinaryBody(t *testing.T) {
ttable := []struct {
filename string
disposition string
}{
{filename: "attachment-only.raw", disposition: "attachment"},
{filename: "attachment-only-inline.raw", disposition: "inline"},
}
for _, tt := range ttable {
r := openTestData("mail", tt.filename)
root, err := ReadParts(r)
if err != nil {
t.Fatal(err)
}
if !isBinaryBody(root) {
t.Errorf("Failed to identify binary body %s in %q", tt.disposition, tt.filename)
}
}
}
func TestParseHeaderOnly(t *testing.T) {
want := ""
msg := openTestData("mail", "header-only.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse non-MIME:", err)
}
if !strings.Contains(e.Text, want) {
t.Errorf("Expected %q to contain %q", e.Text, want)
}
if e.HTML != "" {
t.Errorf("Expected no HTML body, got %q", e.HTML)
}
if e.Root == nil {
t.Errorf("Expected a root part")
}
if len(e.Root.Header) != 7 {
t.Errorf("Expected 7 headers, got %d", len(e.Root.Header))
}
}
func TestParseNonMime(t *testing.T) {
want := "This is a test mailing"
msg := openTestData("mail", "non-mime.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse non-MIME:", err)
}
if !strings.Contains(e.Text, want) {
t.Errorf("Expected %q to contain %q", e.Text, want)
}
if e.HTML != "" {
t.Errorf("Expected no HTML body, got %q", e.HTML)
}
}
func TestParseNonMimeHTML(t *testing.T) {
msg := openTestData("mail", "non-mime-html.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse non-MIME:", err)
}
if len(e.Errors) == 1 {
want := string(errorPlainTextFromHTML)
got := e.Errors[0].Name
if got != want {
t.Errorf("e.Errors[0] got: %v, want: %v", got, want)
}
} else {
t.Errorf("len(e.Errors) got: %v, want: 1", len(e.Errors))
}
want := "This is *a* *test* mailing"
if !strings.Contains(e.Text, want) {
t.Errorf("Expected %q to contain %q", e.Text, want)
}
want = "<span>This</span>"
if !strings.Contains(e.HTML, want) {
t.Errorf("Expected %q to contain %q", e.HTML, want)
}
}
func TestParseMimeTree(t *testing.T) {
msg := openTestData("mail", "attachment.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if e.Root == nil {
t.Error("Message should have a root node")
}
}
func TestParseInlineText(t *testing.T) {
msg := openTestData("mail", "html-mime-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Test of text section"
if e.Text != want {
t.Error("got:", e.Text, "want:", want)
}
}
func TestParseInlineBadCharsetText(t *testing.T) {
msg := openTestData("mail", "html-mime-bad-charset-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Test of text section"
if e.Text != want {
t.Error("got:", e.Text, "want:", want)
}
}
func TestParseInlineBadUknownCharsetText(t *testing.T) {
msg := openTestData("mail", "html-mime-bad-unknown-charset-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Test of text section"
if e.Text != want {
t.Error("got:", e.Text, "want:", want)
}
}
func TestParseMultiAlernativeText(t *testing.T) {
msg := openTestData("mail", "mime-alternative.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Section one\n"
if e.Text != want {
t.Error("Text parts should not concatenate, got:", e.Text, "want:", want)
}
}
func TestParseMultiMixedText(t *testing.T) {
msg := openTestData("mail", "mime-mixed.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Section one\n\n--\nSection two"
if e.Text != want {
t.Error("Text parts should concatenate, got:", e.Text, "want:", want)
}
}
func TestParseMultiSignedText(t *testing.T) {
msg := openTestData("mail", "mime-signed.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Section one\n\n--\nSection two"
if e.Text != want {
t.Error("Text parts should concatenate, got:", e.Text, "want:", want)
}
}
func TestParseQuotedPrintable(t *testing.T) {
msg := openTestData("mail", "quoted-printable.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Phasellus sit amet arcu"
if !strings.Contains(e.Text, want) {
t.Errorf("Text: %q should contain: %q", e.Text, want)
}
}
func TestParseQuotedPrintableMime(t *testing.T) {
msg := openTestData("mail", "quoted-printable-mime.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Nullam venenatis ante"
if !strings.Contains(e.Text, want) {
t.Errorf("Text: %q should contain: %q", e.Text, want)
}
}
func TestParseInlineHTML(t *testing.T) {
msg := openTestData("mail", "html-mime-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "<html>"
if !strings.Contains(e.HTML, want) {
t.Errorf("HTML: %q should contain: %q", e.Text, want)
}
want = "Test of HTML section"
if !strings.Contains(e.HTML, want) {
t.Errorf("HTML: %q should contain: %q", e.Text, want)
}
}
func TestParseAttachment(t *testing.T) {
msg := openTestData("mail", "attachment.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "A text section"
if !strings.Contains(e.Text, want) {
t.Errorf("Text: %q should contain: %q", e.Text, want)
}
if e.HTML != "" {
t.Error("mime.HTML should be empty, attachment is not for display, got:", e.HTML)
}
if len(e.Inlines) > 0 {
t.Error("Should have no inlines, got:", len(e.Inlines))
}
if len(e.Attachments) != 1 {
t.Fatal("Should have a single attachment, got:", len(e.Attachments))
}
want = "test.html"
got := e.Attachments[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
want = "<html>"
if ok, err := contentContainsString(e.Attachments[0], want); !ok {
t.Error("Part", err)
}
}
func TestParseAttachmentOctet(t *testing.T) {
msg := openTestData("mail", "attachment-octet.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "A text section"
if !strings.Contains(e.Text, want) {
t.Errorf("Text: %q should contain: %q", e.Text, want)
}
if e.HTML != "" {
t.Error("mime.HTML should be empty, attachment is not for display, got:", e.HTML)
}
if len(e.Inlines) > 0 {
t.Error("Should have no inlines, got:", len(e.Inlines))
}
if len(e.Attachments) != 1 {
t.Fatal("Should have a single attachment, got:", len(e.Attachments))
}
want = "ATTACHMENT.EXE"
got := e.Attachments[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
wantBytes := []byte{
0x3, 0x17, 0xe1, 0x7e, 0xe8, 0xeb, 0xa2, 0x96, 0x9d, 0x95, 0xa7, 0x67, 0x82, 0x9,
0xdf, 0x8e, 0xc, 0x2c, 0x6a, 0x2b, 0x9b, 0xbe, 0x79, 0xa4, 0x69, 0xd8, 0xae, 0x86,
0xd7, 0xab, 0xa8, 0x72, 0x52, 0x15, 0xfb, 0x80, 0x8e, 0x47, 0xe1, 0xae, 0xaa, 0x5e,
0xa2, 0xb2, 0xc0, 0x90, 0x59, 0xe3, 0x35, 0xf8, 0x60, 0xb7, 0xb1, 0x63, 0x77, 0xd7,
0x5f, 0x92, 0x58, 0xa8, 0x75,
}
allBytes, err := ioutil.ReadAll(e.Attachments[0])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(allBytes, wantBytes) {
t.Error("Attachment should have correct content")
}
}
func TestParseAttachmentApplication(t *testing.T) {
msg := openTestData("mail", "attachment-application.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if len(e.Inlines) > 0 {
t.Error("Should have no inlines, got:", len(e.Inlines))
}
if len(e.Attachments) != 1 {
t.Fatal("Should have a single attachment, got:", len(e.Attachments))
}
want := "some.doc"
got := e.Attachments[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
}
func TestParseOtherParts(t *testing.T) {
msg := openTestData("mail", "other-parts.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "A text section"
if !strings.Contains(e.Text, want) {
t.Errorf("Text: %q should contain: %q", e.Text, want)
}
if e.HTML != "" {
t.Error("mime.HTML should be empty, attachment is not for display, got:", e.HTML)
}
if len(e.Inlines) > 0 {
t.Error("Should have no inlines, got:", len(e.Inlines))
}
if len(e.Attachments) > 0 {
t.Fatal("Should have no attachments, got:", len(e.Attachments))
}
if len(e.OtherParts) != 1 {
t.Fatal("Should have one other part, got:", len(e.OtherParts))
}
want = "B05.gif"
got := e.OtherParts[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
wantBytes := []byte{
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0xf, 0x0, 0xf, 0x0, 0xa2, 0x5, 0x0, 0xde, 0xeb,
0xf3, 0x5b, 0xb0, 0xec, 0x0, 0x89, 0xe3, 0xa3, 0xd0, 0xed, 0x0, 0x46, 0x74, 0xdd,
0xed, 0xfa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x21, 0xf9, 0x4, 0x1, 0x0, 0x0, 0x5, 0x0,
0x2c, 0x0, 0x0, 0x0, 0x0, 0xf, 0x0, 0xf, 0x0, 0x0, 0x3, 0x40, 0x58, 0x25, 0xa4, 0x4b,
0xb0, 0x39, 0x1, 0x46, 0xa3, 0x23, 0x5b, 0x47, 0x46, 0x68, 0x9d, 0x20, 0x6, 0x9f,
0xd2, 0x95, 0x45, 0x44, 0x8, 0xe8, 0x29, 0x39, 0x69, 0xeb, 0xbd, 0xc, 0x41, 0x4a,
0xae, 0x82, 0xcd, 0x1c, 0x9f, 0xce, 0xaf, 0x1f, 0xc3, 0x34, 0x18, 0xc2, 0x42, 0xb8,
0x80, 0xf1, 0x18, 0x84, 0xc0, 0x9e, 0xd0, 0xe8, 0xf2, 0x1, 0xb5, 0x19, 0xad, 0x41,
0x53, 0x33, 0x9b, 0x0, 0x0, 0x3b,
}
allBytes, err := ioutil.ReadAll(e.OtherParts[0])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(allBytes, wantBytes) {
t.Error("Other part should have correct content")
}
}
func TestParseInline(t *testing.T) {
msg := openTestData("mail", "html-mime-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Test of text section"
if !strings.Contains(e.Text, want) {
t.Errorf("Text: %q should contain: %q", e.Text, want)
}
want = ">Test of HTML section<"
if !strings.Contains(e.HTML, want) {
t.Errorf("HTML: %q should contain %q", e.HTML, want)
}
if len(e.Inlines) != 1 {
t.Error("Should one inline, got:", len(e.Inlines))
}
if len(e.Attachments) > 0 {
t.Fatal("Should have no attachments, got:", len(e.Attachments))
}
want = "favicon.png"
got := e.Inlines[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
allBytes, err := ioutil.ReadAll(e.Inlines[0])
if err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(allBytes, []byte{0x89, 'P', 'N', 'G'}) {
t.Error("Inline should have correct content")
}
}
func TestParseHTMLOnlyInline(t *testing.T) {
msg := openTestData("mail", "html-only-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if len(e.Errors) == 1 {
want := string(errorPlainTextFromHTML)
got := e.Errors[0].Name
if got != want {
t.Errorf("e.Errors[0] got: %v, want: %v", got, want)
}
} else {
t.Errorf("len(e.Errors) got: %v, want: 1", len(e.Errors))
}
want := "Test of HTML section"
if !strings.Contains(e.Text, want) {
t.Errorf("Downconverted Text: %q should contain: %q", e.Text, want)
}
want = ">Test of HTML section<"
if !strings.Contains(e.HTML, want) {
t.Errorf("HTML: %q should contain %q", e.HTML, want)
}
if len(e.Inlines) != 1 {
t.Error("Should one inline, got:", len(e.Inlines))
}
if len(e.Attachments) > 0 {
t.Fatal("Should have no attachments, got:", len(e.Attachments))
}
want = "favicon.png"
got := e.Inlines[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
allBytes, err := ioutil.ReadAll(e.Inlines[0])
if err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(allBytes, []byte{0x89, 'P', 'N', 'G'}) {
t.Error("Inline should have correct content")
}
}
func TestParseNestedHeaders(t *testing.T) {
msg := openTestData("mail", "html-mime-inline.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if len(e.Inlines) != 1 {
t.Error("Should one inline, got:", len(e.Inlines))
}
want := "favicon.png"
got := e.Inlines[0].FileName
if got != want {
t.Error("FileName got:", got, "want:", want)
}
want = "<8B8481A2-25CA-4886-9B5A-8EB9115DD064@skynet>"
got = e.Inlines[0].Header.Get("Content-Id")
if got != want {
t.Errorf("Content-Id header was: %q, want: %q", got, want)
}
}
func TestEnvelopeGetHeader(t *testing.T) {
var e *Envelope
var got, want string
// Test empty header
e = &Envelope{}
want = ""
got = e.GetHeader("Subject")
if got != want {
t.Errorf("Subject was: %q, want: %q", got, want)
}
// Even non-MIME messages should support encoded-words in headers
// Also, encoded addresses should be suppored
r := openTestData("mail", "qp-ascii-header.raw")
e, err := ReadEnvelope(r)
if err != nil {
t.Fatal("Failed to parse non-MIME:", err)
}
want = "Test QP Subject!"
got = e.GetHeader("Subject")
if got != want {
t.Errorf("Subject was: %q, want: %q", got, want)
}
// Test UTF-8 subject line
r = openTestData("mail", "qp-utf8-header.raw")
e, err = ReadEnvelope(r)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want = "MIME UTF8 Test \u00a2 More Text"
got = e.GetHeader("Subject")
if got != want {
t.Errorf("Subject was: %q, want: %q", got, want)
}
}
func TestEnvelopeAddressList(t *testing.T) {
var e *Envelope
var got, want string
// Test empty header
e = &Envelope{}
_, err := e.AddressList("To")
if err == nil {
t.Error("AddressList(\"Subject\") should have returned err, got nil")
}
r := openTestData("mail", "qp-utf8-header.raw")
e, err = ReadEnvelope(r)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
_, err = e.AddressList("Subject")
if err == nil {
t.Error("AddressList(\"Subject\") should have returned err, got nil")
}
toAddresses, err := e.AddressList("To")
if err != nil {
t.Fatal("Failed to parse To list:", err)
}
if len(toAddresses) != 1 {
t.Fatalf("len(toAddresses) == %v, want: %v", len(toAddresses), 1)
}
// Confirm address name was decoded properly
want = "Mirosław Marczak"
got = toAddresses[0].Name
if got != want {
t.Errorf("To was: %q, want: %q", got, want)
}
senderAddresses, err := e.AddressList("Sender")
if err != nil {
t.Fatal("Failed to parse Sender list:", err)
}
if len(senderAddresses) != 1 {
t.Fatalf("len(senderAddresses) == %v, want: %v", len(senderAddresses), 1)
}
// Confirm address name was decoded properly
want = "André Pirard"
got = senderAddresses[0].Name
if got != want {
t.Errorf("Sender was: %q, want: %q", got, want)
}
}
func TestDetectCharacterSetInHTML(t *testing.T) {
msg := openTestData("mail", "non-mime-missing-charset.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse non-MIME:", err)
}
if strings.ContainsRune(e.HTML, 0x80) {
t.Error("HTML body should not have contained a Windows CP1250 Euro Symbol")
}
if !strings.ContainsRune(e.HTML, 0x20ac) {
t.Error("HTML body should have contained a Unicode Euro Symbol")
}
}
func TestIsAttachment(t *testing.T) {
var htests = []struct {
want bool
header textproto.MIMEHeader
}{
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"attachment; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"ATTACHMENT; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Type": []string{"image/jpg; name=\"test.jpg\""},
"Content-Disposition": []string{"attachment; filename=\"test.jpg\""},
},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Type": []string{"attachment; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"inline; filename=\"frog.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"non-attachment; filename=\"frog.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{},
},
}
for _, s := range htests {
got := isAttachment(s.header)
if got != s.want {
t.Errorf("IsAttachment(%v) == %v, want: %v", s.header, got, s.want)
}
}
}
func TestIsPlain(t *testing.T) {
var htests = []struct {
want bool
header textproto.MIMEHeader
emptyIsPlain bool
}{
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/plain"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/html"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/html; charset=utf-8"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{},
emptyIsPlain: true,
},
{
want: false,
header: textproto.MIMEHeader{},
emptyIsPlain: false,
},
{
want: false,
header: textproto.MIMEHeader{"Content-Type": []string{"image/jpeg;"}},
emptyIsPlain: true,
},
{
want: false,
header: textproto.MIMEHeader{"Content-Type": []string{"application/octet-stream"}},
emptyIsPlain: true,
},
}
for _, s := range htests {
got := isPlain(s.header, s.emptyIsPlain)
if got != s.want {
t.Errorf("IsPlain(%v, %v) == %v, want: %v", s.header, s.emptyIsPlain, got, s.want)
}
}
}
func TestAttachmentOnly(t *testing.T) {
var aTests = []struct {
filename string
attachmentsLen int
inlinesLen int
}{
{filename: "attachment-only.raw", attachmentsLen: 1, inlinesLen: 0},
{filename: "attachment-only-inline.raw", attachmentsLen: 0, inlinesLen: 1},
}
for _, a := range aTests {
// Mail with disposition attachment
msg := openTestData("mail", a.filename)
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if len(e.Attachments) != a.attachmentsLen {
t.Fatal("len(Attachments) got:", len(e.Attachments), "want:", a.attachmentsLen)
}
if a.attachmentsLen > 0 {
allBytes, err := ioutil.ReadAll(e.Attachments[0])
if err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(allBytes, []byte{0x89, 'P', 'N', 'G'}) {
t.Errorf("Content should be PNG image, got: %v", allBytes)
}
}
if len(e.Inlines) != a.inlinesLen {
t.Fatal("len(Inlines) got:", len(e.Inlines), "want:", a.inlinesLen)
}
if a.inlinesLen > 0 {
allBytes, err := ioutil.ReadAll(e.Inlines[0])
if err != nil {
t.Fatal(err)
}
if !bytes.HasPrefix(allBytes, []byte{0x89, 'P', 'N', 'G'}) {
t.Errorf("Content should be PNG image, got: %v", allBytes)
}
}
}
}
func TestEnvelopeParseMultiPartBody(t *testing.T) {
var want string
var err error
root := &Part{}
e := &Envelope{}
// Empty root part
err = parseMultiPartBody(root, e)
want = "Unable to parse media type"
if err == nil {
t.Fatalf("err was %v, wanted: %v", err, want)
}
if !strings.Contains(err.Error(), want) {
t.Fatalf("err was %v, wanted: %v", err, want)
}
// Unexpected content type
root.Header = make(textproto.MIMEHeader)
root.Header.Set("Content-Type", "text/plain")
err = parseMultiPartBody(root, e)
want = "Unknown mediatype"
if err == nil {
t.Fatalf("err was %v, wanted: %v", err, want)
}
if !strings.Contains(err.Error(), want) {
t.Fatalf("err was %v, wanted: %v", err, want)
}
// No boundary param
root.Header = make(textproto.MIMEHeader)
root.Header.Set("Content-Type", "multipart/mixed")
err = parseMultiPartBody(root, e)
want = "Unable to locate boundary param"
if err == nil {
t.Fatalf("err was %v, wanted: %v", err, want)
}
if !strings.Contains(err.Error(), want) {
t.Fatalf("err was %v, wanted: %v", err, want)
}
}
func TestDuplicateParamsInMime(t *testing.T) {
msg := openTestData("mail", "mime-duplicate-param.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if e.Attachments[0].FileName != "Invoice_302232133150612.pdf" {
t.Fatal("Mail should have a part with filename Invoice_302232133150612.pdf")
}
}
func TestBadContentTypeInMime(t *testing.T) {
msg := openTestData("mail", "mime-bad-content-type.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if e.Attachments[0].FileName != "Invoice_302232133150612.pdf" {
t.Fatal("Mail should have a part with filename Invoice_302232133150612.pdf")
}
}
func TestBlankMediaName(t *testing.T) {
msg := openTestData("mail", "mime-blank-media-name.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if e.Attachments[0].FileName != "Invoice_302232133150612.pdf" {
t.Fatal("Mail should have a part with filename Invoice_302232133150612.pdf")
}
}
func TestEnvelopeHeaders(t *testing.T) {
headers := map[string]string{
"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;",
"To": "<[email protected]>",
"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=;",
"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=",
"Message-Id": "<[email protected]>",
"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",
"Mime-Version": "1.0",
"Return-Path": "<bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com> <bounce-md_30112948.55a02731.v1-163e4a0faf244a2da6b0121cc7af1fe9@mandrill.papertrailapp.com>",
"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]",
"From": "Papertrail <[email protected]>",
"Subject": "Welcome to Papertrail",
"Content-Type": `multipart/alternative; boundary="_av-rPFkvS5QROAYLq2cQTUr1w"`,
"X-Mandrill-User": "md_30112948",
"Delivered-To": "[email protected]",
"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",
"X-Received": "by 10.170.119.147 with SMTP id l141mr25507408ykb.89.1436559154116; Fri, 10 Jul 2015 13:12:34 -0700 (PDT)",
"Date": "Fri, 10 Jul 2015 20:12:33 +0000",
}
msg := openTestData("mail", "ctype-bug.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if len(e.Root.Header) != len(headers) {
t.Errorf("Failed to extract expected headers. Got %v headers, expected %v",
len(e.Root.Header), len(headers))
}
for k, _ := range headers {
if e.Root.Header[k] == nil {
t.Errorf("Header named %q was missing, want it to exist", k)
}
}
for k, v := range e.Root.Header {
if _, ok := headers[k]; !ok {
t.Errorf("Got header named %q, did not expect it to exist", k)
continue
}
for _, val := range v {
if !strings.Contains(headers[k], val) {
t.Errorf("Got header %q with value %q, wanted value contained in:\n%q",
k, val, headers[k])
}
}
}
}
func TestInlineTextBody(t *testing.T) {
headers := map[string]string{
"To": "<[email protected]>",
"Message-Id": "<[email protected]>",
"Mime-Version": "1.0",
"From": "Chris Garrett <[email protected]>",
"Subject": "Text body only with disposition inline",
"Content-Type": `text/html; charset="UTF-8"`,
"Content-Disposition": "inline",
"Content-Transfer-Encoding": "quoted-printable",
"Date": "Wed, 8 Feb 2017 03:23:13 -0500",
}
msg := openTestData("mail", "attachment-only-inline-quoted-printable.raw")
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
want := "Just some html content"
if e.Text != want {
t.Errorf("Got Text with value %s, wanted value:\n%s", e.Text, want)
}
if !strings.Contains(e.HTML, want) {
t.Errorf("Expected %q to contain %q", e.HTML, want)
}
if len(e.Root.Header) != len(headers) {
t.Errorf("Failed to extract expected headers. Got %v headers, expected %v",
len(e.Root.Header), len(headers))
}
for k, _ := range headers {
if e.Root.Header[k] == nil {
t.Errorf("Header named %q was missing, want it to exist", k)
}
}
for k, v := range e.Root.Header {
if _, ok := headers[k]; !ok {
t.Errorf("Got header named %q, did not expect it to exist", k)
continue
}
for _, val := range v {
if !strings.Contains(headers[k], val) {
t.Errorf("Got header %q with value %q, wanted value contained in:\n%q",
k, val, headers[k])
}
}
}
}