Debian Codegraph @

part_test.go

raw
1 package enmime_test
2
3 import (
4 "testing"
5
6 "github.com/jhillyerd/enmime"
7 "github.com/jhillyerd/enmime/internal/test"
8 )
9
10 func TestPlainTextPart(t *testing.T) {
11 var want, got string
12 var wantp *enmime.Part
13 r := test.OpenTestData("parts", "textplain.raw")
14 p, err := enmime.ReadParts(r)
15 if err != nil {
16 t.Fatalf("Unexpected parse error: %+v", err)
17 }
18 if p == nil {
19 t.Fatal("Root node should not be nil")
20 }
21
22 wantp = &enmime.Part{
23 ContentType: "text/plain",
24 Charset: "us-ascii",
25 PartID: "0",
26 }
27 test.ComparePart(t, p, wantp)
28
29 want = "7bit"
30 got = p.Header.Get("Content-Transfer-Encoding")
31 if got != want {
32 t.Errorf("Content-Transfer-Encoding got: %q, want: %q", got, want)
33 }
34
35 want = "Test of text/plain section"
36 test.ContentContainsString(t, p.Content, want)
37 }
38
39 func TestAddChildInfiniteLoops(t *testing.T) {
40 // Part adds itself
41 parentPart := &enmime.Part{
42 ContentType: "text/plain",
43 Charset: "us-ascii",
44 PartID: "0",
45 }
46 parentPart.AddChild(parentPart)
47
48 // Part adds its own FirstChild
49 childPart := &enmime.Part{
50 ContentType: "text/plain",
51 Charset: "us-ascii",
52 PartID: "1",
53 }
54 parentPart.FirstChild = childPart
55 parentPart.AddChild(childPart)
56
57 parentPart.FirstChild = nil
58 // Part adds a child that is its own NextSibling
59 childPart.NextSibling = childPart
60 parentPart.AddChild(childPart)
61 }
62
63 func TestQuotedPrintablePart(t *testing.T) {
64 var want, got string
65 var wantp *enmime.Part
66 r := test.OpenTestData("parts", "quoted-printable.raw")
67 p, err := enmime.ReadParts(r)
68 if err != nil {
69 t.Fatalf("Unexpected parse error: %+v", err)
70 }
71 if p == nil {
72 t.Fatal("Root node should not be nil")
73 }
74
75 wantp = &enmime.Part{
76 ContentType: "text/plain",
77 Charset: "us-ascii",
78 PartID: "0",
79 }
80 test.ComparePart(t, p, wantp)
81
82 want = "quoted-printable"
83 got = p.Header.Get("Content-Transfer-Encoding")
84 if got != want {
85 t.Errorf("Content-Transfer-Encoding got: %q, want: %q", got, want)
86 }
87
88 want = "Start=ABC=Finish"
89 test.ContentEqualsString(t, p.Content, want)
90 }
91
92 func TestQuotedPrintableInvalidPart(t *testing.T) {
93 var want, got string
94 var wantp *enmime.Part
95 r := test.OpenTestData("parts", "quoted-printable-invalid.raw")
96 p, err := enmime.ReadParts(r)
97 if err != nil {
98 t.Fatalf("Unexpected parse error: %+v", err)
99 }
100 if p == nil {
101 t.Fatal("Root node should not be nil")
102 }
103
104 wantp = &enmime.Part{
105 ContentType: "text/plain",
106 Charset: "utf-8",
107 Disposition: "inline",
108 PartID: "0",
109 }
110 test.ComparePart(t, p, wantp)
111
112 want = "quoted-printable"
113 got = p.Header.Get("Content-Transfer-Encoding")
114 if got != want {
115 t.Errorf("Content-Transfer-Encoding got: %q, want: %q", got, want)
116 }
117
118 want = "Stuffs’s Weekly Summary"
119 test.ContentContainsString(t, p.Content, want)
120 }
121
122 func TestMultiAlternParts(t *testing.T) {
123 var want string
124 var wantp *enmime.Part
125 r := test.OpenTestData("parts", "multialtern.raw")
126 p, err := enmime.ReadParts(r)
127
128 // Examine root
129 if err != nil {
130 t.Fatalf("Unexpected parse error: %+v", err)
131 }
132 if p == nil {
133 t.Fatal("Root node should not be nil")
134 }
135
136 wantp = &enmime.Part{
137 FirstChild: test.PartExists,
138 ContentType: "multipart/alternative",
139 PartID: "0",
140 }
141 test.ComparePart(t, p, wantp)
142
143 test.ContentEqualsString(t, p.Content, "")
144
145 // Examine first child
146 p = p.FirstChild
147 wantp = &enmime.Part{
148 Parent: test.PartExists,
149 NextSibling: test.PartExists,
150 ContentType: "text/plain",
151 Charset: "us-ascii",
152 PartID: "1",
153 }
154 test.ComparePart(t, p, wantp)
155
156 want = "A text section"
157 test.ContentContainsString(t, p.Content, want)
158
159 // Examine sibling
160 p = p.NextSibling
161 wantp = &enmime.Part{
162 Parent: test.PartExists,
163 ContentType: "text/html",
164 Charset: "us-ascii",
165 PartID: "2",
166 }
167 test.ComparePart(t, p, wantp)
168
169 want = "An HTML section"
170 test.ContentContainsString(t, p.Content, want)
171 }
172
173 // TestRootMissingContentType expects a default content type to be set for the root if not specified
174 func TestRootMissingContentType(t *testing.T) {
175 var want string
176 r := test.OpenTestData("parts", "missing-ctype-root.raw")
177 p, err := enmime.ReadParts(r)
178
179 // Examine root
180 if err != nil {
181 t.Fatalf("Unexpected parse error: %+v", err)
182 }
183 if p == nil {
184 t.Fatal("Root node should not be nil")
185 }
186 want = "text/plain"
187 if p.ContentType != want {
188 t.Errorf("Content-Type got: %q, want: %q", p.ContentType, want)
189 }
190 want = "us-ascii"
191 if p.Charset != want {
192 t.Errorf("Charset got: %q, want: %q", p.Charset, want)
193 }
194 }
195
196 func TestPartMissingContentType(t *testing.T) {
197 var want string
198 var wantp *enmime.Part
199 r := test.OpenTestData("parts", "missing-ctype.raw")
200 p, err := enmime.ReadParts(r)
201
202 // Examine root
203 if err != nil {
204 t.Fatalf("Unexpected parse error: %+v", err)
205 }
206 if p == nil {
207 t.Fatal("Root node should not be nil")
208 }
209
210 wantp = &enmime.Part{
211 FirstChild: test.PartExists,
212 ContentType: "multipart/alternative",
213 PartID: "0",
214 }
215 test.ComparePart(t, p, wantp)
216
217 test.ContentEqualsString(t, p.Content, "")
218
219 // Examine first child
220 p = p.FirstChild
221 wantp = &enmime.Part{
222 Parent: test.PartExists,
223 NextSibling: test.PartExists,
224 // No ContentType
225 PartID: "1",
226 }
227 test.ComparePart(t, p, wantp)
228
229 want = "A text section"
230 test.ContentContainsString(t, p.Content, want)
231
232 // Examine sibling
233 p = p.NextSibling
234 wantp = &enmime.Part{
235 Parent: test.PartExists,
236 ContentType: "text/html",
237 Charset: "us-ascii",
238 PartID: "2",
239 }
240 test.ComparePart(t, p, wantp)
241
242 want = "An HTML section"
243 test.ContentContainsString(t, p.Content, want)
244 }
245
246 func TestPartEmptyHeader(t *testing.T) {
247 var want string
248 var wantp *enmime.Part
249 r := test.OpenTestData("parts", "empty-header.raw")
250 p, err := enmime.ReadParts(r)
251
252 // Examine root
253 if err != nil {
254 t.Fatalf("Unexpected parse error: %+v", err)
255 }
256 if p == nil {
257 t.Fatal("Root node should not be nil")
258 }
259
260 wantp = &enmime.Part{
261 FirstChild: test.PartExists,
262 ContentType: "multipart/alternative",
263 PartID: "0",
264 }
265 test.ComparePart(t, p, wantp)
266
267 test.ContentEqualsString(t, p.Content, "")
268
269 // Examine first child
270 p = p.FirstChild
271
272 wantp = &enmime.Part{
273 Parent: test.PartExists,
274 NextSibling: test.PartExists,
275 // No ContentType
276 PartID: "1",
277 }
278 test.ComparePart(t, p, wantp)
279
280 want = "A text section"
281 test.ContentContainsString(t, p.Content, want)
282
283 // Examine sibling
284 p = p.NextSibling
285 wantp = &enmime.Part{
286 Parent: test.PartExists,
287 ContentType: "text/html",
288 Charset: "us-ascii",
289 PartID: "2",
290 }
291 test.ComparePart(t, p, wantp)
292
293 want = "An HTML section"
294 test.ContentContainsString(t, p.Content, want)
295 }
296
297 func TestPartHeaders(t *testing.T) {
298 r := test.OpenTestData("parts", "header-only.raw")
299 p, err := enmime.ReadParts(r)
300 if err != nil {
301 t.Fatal(err)
302 }
303
304 want := "text/html"
305 if p.ContentType != want {
306 t.Errorf("ContentType %q, want %q", p.ContentType, want)
307 }
308 want = "file.html"
309 if p.FileName != want {
310 t.Errorf("FileName %q, want %q", p.FileName, want)
311 }
312 want = "utf-8"
313 if p.Charset != want {
314 t.Errorf("Charset %q, want %q", p.Charset, want)
315 }
316 want = "inline"
317 if p.Disposition != want {
318 t.Errorf("Disposition %q, want %q", p.Disposition, want)
319 }
320 want = "[email protected]"
321 if p.ContentID != want {
322 t.Errorf("ContentID %q, want %q", p.ContentID, want)
323 }
324 }
325
326 func TestMultiMixedParts(t *testing.T) {
327 var want string
328 var wantp *enmime.Part
329 r := test.OpenTestData("parts", "multimixed.raw")
330 p, err := enmime.ReadParts(r)
331
332 // Examine root
333 if err != nil {
334 t.Fatalf("Unexpected parse error: %+v", err)
335 }
336 if p == nil {
337 t.Fatal("Root node should not be nil")
338 }
339
340 wantp = &enmime.Part{
341 FirstChild: test.PartExists,
342 ContentType: "multipart/mixed",
343 PartID: "0",
344 }
345 test.ComparePart(t, p, wantp)
346
347 test.ContentEqualsString(t, p.Content, "")
348
349 // Examine first child
350 p = p.FirstChild
351 wantp = &enmime.Part{
352 Parent: test.PartExists,
353 NextSibling: test.PartExists,
354 ContentType: "text/plain",
355 Charset: "us-ascii",
356 PartID: "1",
357 }
358 test.ComparePart(t, p, wantp)
359
360 want = "Section one"
361 test.ContentContainsString(t, p.Content, want)
362
363 // Examine sibling
364 p = p.NextSibling
365 wantp = &enmime.Part{
366 Parent: test.PartExists,
367 ContentType: "text/plain",
368 Charset: "us-ascii",
369 PartID: "2",
370 }
371 test.ComparePart(t, p, wantp)
372
373 want = "Section two"
374 test.ContentContainsString(t, p.Content, want)
375 }
376
377 func TestMultiOtherParts(t *testing.T) {
378 var want string
379 var wantp *enmime.Part
380 r := test.OpenTestData("parts", "multiother.raw")
381 p, err := enmime.ReadParts(r)
382
383 // Examine root
384 if err != nil {
385 t.Fatalf("Unexpected parse error: %+v", err)
386 }
387 if p == nil {
388 t.Fatal("Root node should not be nil")
389 }
390
391 wantp = &enmime.Part{
392 FirstChild: test.PartExists,
393 ContentType: "multipart/x-enmime",
394 PartID: "0",
395 }
396 test.ComparePart(t, p, wantp)
397
398 test.ContentEqualsString(t, p.Content, "")
399
400 // Examine first child
401 p = p.FirstChild
402 wantp = &enmime.Part{
403 Parent: test.PartExists,
404 NextSibling: test.PartExists,
405 ContentType: "text/plain",
406 Charset: "us-ascii",
407 PartID: "1",
408 }
409 test.ComparePart(t, p, wantp)
410
411 want = "Section one"
412 test.ContentContainsString(t, p.Content, want)
413
414 // Examine sibling
415 p = p.NextSibling
416 wantp = &enmime.Part{
417 Parent: test.PartExists,
418 ContentType: "text/plain",
419 Charset: "us-ascii",
420 PartID: "2",
421 }
422 test.ComparePart(t, p, wantp)
423
424 want = "Section two"
425 test.ContentContainsString(t, p.Content, want)
426 }
427
428 func TestNestedAlternParts(t *testing.T) {
429 var want string
430 var wantp *enmime.Part
431 r := test.OpenTestData("parts", "nestedmulti.raw")
432 p, err := enmime.ReadParts(r)
433
434 // Examine root
435 if err != nil {
436 t.Fatalf("Unexpected parse error: %+v", err)
437 }
438 if p == nil {
439 t.Fatal("Root node should not be nil")
440 }
441
442 wantp = &enmime.Part{
443 ContentType: "multipart/alternative",
444 FirstChild: test.PartExists,
445 PartID: "0",
446 }
447 test.ComparePart(t, p, wantp)
448
449 test.ContentEqualsString(t, p.Content, "")
450
451 // Examine first child
452 p = p.FirstChild
453 wantp = &enmime.Part{
454 Parent: test.PartExists,
455 NextSibling: test.PartExists,
456 ContentType: "text/plain",
457 Charset: "us-ascii",
458 PartID: "1",
459 }
460 test.ComparePart(t, p, wantp)
461
462 want = "A text section"
463 test.ContentContainsString(t, p.Content, want)
464
465 // Examine sibling
466 p = p.NextSibling
467 wantp = &enmime.Part{
468 Parent: test.PartExists,
469 FirstChild: test.PartExists,
470 ContentType: "multipart/related",
471 PartID: "2.0",
472 }
473 test.ComparePart(t, p, wantp)
474
475 test.ContentEqualsString(t, p.Content, "")
476
477 // First nested
478 p = p.FirstChild
479 wantp = &enmime.Part{
480 Parent: test.PartExists,
481 NextSibling: test.PartExists,
482 ContentType: "text/html",
483 Charset: "us-ascii",
484 PartID: "2.1",
485 }
486 test.ComparePart(t, p, wantp)
487
488 want = "An HTML section"
489 test.ContentContainsString(t, p.Content, want)
490
491 // Second nested
492 p = p.NextSibling
493 wantp = &enmime.Part{
494 Parent: test.PartExists,
495 NextSibling: test.PartExists,
496 ContentType: "text/plain",
497 Disposition: "inline",
498 FileName: "attach.txt",
499 PartID: "2.2",
500 }
501 test.ComparePart(t, p, wantp)
502
503 want = "An inline text attachment"
504 test.ContentContainsString(t, p.Content, want)
505
506 // Third nested
507 p = p.NextSibling
508 wantp = &enmime.Part{
509 Parent: test.PartExists,
510 ContentType: "text/plain",
511 Disposition: "inline",
512 FileName: "attach2.txt",
513 PartID: "2.3",
514 }
515 test.ComparePart(t, p, wantp)
516
517 want = "Another inline text attachment"
518 test.ContentContainsString(t, p.Content, want)
519 }
520
521 func TestPartSimilarBoundary(t *testing.T) {
522 var want string
523 var wantp *enmime.Part
524 r := test.OpenTestData("parts", "similar-boundary.raw")
525 p, err := enmime.ReadParts(r)
526
527 // Examine root
528 if err != nil {
529 t.Fatalf("Unexpected parse error: %+v", err)
530 }
531 if p == nil {
532 t.Fatal("Root node should not be nil")
533 }
534
535 wantp = &enmime.Part{
536 ContentType: "multipart/mixed",
537 FirstChild: test.PartExists,
538 PartID: "0",
539 }
540 test.ComparePart(t, p, wantp)
541
542 test.ContentEqualsString(t, p.Content, "")
543
544 // Examine first child
545 p = p.FirstChild
546 wantp = &enmime.Part{
547 Parent: test.PartExists,
548 NextSibling: test.PartExists,
549 ContentType: "text/plain",
550 Charset: "us-ascii",
551 PartID: "1",
552 }
553 test.ComparePart(t, p, wantp)
554
555 want = "Section one"
556 test.ContentContainsString(t, p.Content, want)
557
558 // Examine sibling
559 p = p.NextSibling
560 wantp = &enmime.Part{
561 Parent: test.PartExists,
562 FirstChild: test.PartExists,
563 ContentType: "multipart/alternative",
564 PartID: "2.0",
565 }
566 test.ComparePart(t, p, wantp)
567
568 test.ContentEqualsString(t, p.Content, "")
569
570 // First nested
571 p = p.FirstChild
572 wantp = &enmime.Part{
573 Parent: test.PartExists,
574 NextSibling: test.PartExists,
575 ContentType: "text/plain",
576 Charset: "us-ascii",
577 PartID: "2.1",
578 }
579 test.ComparePart(t, p, wantp)
580
581 want = "A text section"
582 test.ContentContainsString(t, p.Content, want)
583
584 // Second nested
585 p = p.NextSibling
586 wantp = &enmime.Part{
587 Parent: test.PartExists,
588 ContentType: "text/html",
589 Charset: "us-ascii",
590 PartID: "2.2",
591 }
592 test.ComparePart(t, p, wantp)
593
594 want = "An HTML section"
595 test.ContentContainsString(t, p.Content, want)
596 }
597
598 // Check we don't UTF-8 decode attachments
599 func TestBinaryDecode(t *testing.T) {
600 var want string
601 var wantp *enmime.Part
602 r := test.OpenTestData("parts", "bin-attach.raw")
603 p, err := enmime.ReadParts(r)
604
605 // Examine root
606 if err != nil {
607 t.Fatalf("Unexpected parse error: %+v", err)
608 }
609 if p == nil {
610 t.Fatal("Root node should not be nil")
611 }
612
613 wantp = &enmime.Part{
614 FirstChild: test.PartExists,
615 ContentType: "multipart/mixed",
616 PartID: "0",
617 }
618 test.ComparePart(t, p, wantp)
619
620 test.ContentEqualsString(t, p.Content, "")
621
622 // Examine first child
623 p = p.FirstChild
624 wantp = &enmime.Part{
625 Parent: test.PartExists,
626 NextSibling: test.PartExists,
627 ContentType: "text/plain",
628 Charset: "us-ascii",
629 PartID: "1",
630 }
631 test.ComparePart(t, p, wantp)
632
633 want = "A text section"
634 test.ContentContainsString(t, p.Content, want)
635
636 // Examine sibling
637 p = p.NextSibling
638 wantp = &enmime.Part{
639 Parent: test.PartExists,
640 ContentType: "application/octet-stream",
641 Charset: "us-ascii",
642 Disposition: "attachment",
643 FileName: "test.bin",
644 PartID: "2",
645 }
646 test.ComparePart(t, p, wantp)
647
648 wantBytes := []byte{
649 0x50, 0x4B, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
650 0x08, 0x00, 0xC2, 0x02, 0x29, 0x4A, 0x00, 0x00}
651 test.ContentEqualsBytes(t, p.Content, wantBytes)
652 }
653
654 func TestMultiBase64Parts(t *testing.T) {
655 var want string
656 var wantp *enmime.Part
657 r := test.OpenTestData("parts", "multibase64.raw")
658 p, err := enmime.ReadParts(r)
659
660 // Examine root
661 if err != nil {
662 t.Fatalf("Unexpected parse error: %+v", err)
663 }
664 if p == nil {
665 t.Fatal("Root node should not be nil")
666 }
667
668 wantp = &enmime.Part{
669 FirstChild: test.PartExists,
670 ContentType: "multipart/mixed",
671 PartID: "0",
672 }
673 test.ComparePart(t, p, wantp)
674
675 test.ContentEqualsString(t, p.Content, "")
676
677 // Examine first child
678 p = p.FirstChild
679 wantp = &enmime.Part{
680 Parent: test.PartExists,
681 NextSibling: test.PartExists,
682 ContentType: "text/plain",
683 Charset: "us-ascii",
684 PartID: "1",
685 }
686 test.ComparePart(t, p, wantp)
687
688 want = "A text section"
689 test.ContentContainsString(t, p.Content, want)
690
691 // Examine sibling
692 p = p.NextSibling
693 wantp = &enmime.Part{
694 Parent: test.PartExists,
695 ContentType: "text/html",
696 Disposition: "attachment",
697 FileName: "test.html",
698 PartID: "2",
699 }
700 test.ComparePart(t, p, wantp)
701
702 want = "<html>"
703 test.ContentContainsString(t, p.Content, want)
704 }
705
706 func TestBadBoundaryTerm(t *testing.T) {
707 var want string
708 var wantp *enmime.Part
709 r := test.OpenTestData("parts", "badboundary.raw")
710 p, err := enmime.ReadParts(r)
711
712 // Examine root
713 if err != nil {
714 t.Fatalf("Unexpected parse error: %+v", err)
715 }
716 if p == nil {
717 t.Fatal("Root node should not be nil")
718 }
719
720 wantp = &enmime.Part{
721 FirstChild: test.PartExists,
722 ContentType: "multipart/alternative",
723 PartID: "0",
724 }
725 test.ComparePart(t, p, wantp)
726
727 // Examine first child
728 p = p.FirstChild
729 wantp = &enmime.Part{
730 Parent: test.PartExists,
731 NextSibling: test.PartExists,
732 ContentType: "text/plain",
733 Charset: "us-ascii",
734 PartID: "1",
735 }
736 test.ComparePart(t, p, wantp)
737
738 // Examine sibling
739 p = p.NextSibling
740 wantp = &enmime.Part{
741 Parent: test.PartExists,
742 NextSibling: test.PartExists,
743 ContentType: "text/html",
744 Charset: "us-ascii",
745 PartID: "2",
746 }
747 test.ComparePart(t, p, wantp)
748
749 want = "An HTML section"
750 test.ContentContainsString(t, p.Content, want)
751 }
752
753 func TestClonePart(t *testing.T) {
754 r := test.OpenTestData("parts", "multiother.raw")
755 p, err := enmime.ReadParts(r)
756
757 // Examine root
758 if err != nil {
759 t.Fatalf("Unexpected parse error: %+v", err)
760 }
761 if p == nil {
762 t.Fatal("Root node should not be nil")
763 }
764
765 clone := p.Clone(nil)
766 test.ComparePart(t, clone, p)
767 }
768
769 func TestBarrenContentType(t *testing.T) {
770 r := test.OpenTestData("parts", "barren-content-type.raw")
771 p, err := enmime.ReadParts(r)
772 if err != nil {
773 t.Fatal(err)
774 }
775
776 wantp := &enmime.Part{
777 PartID: "0",
778 Disposition: "attachment",
779 }
780 test.ComparePart(t, p, wantp)
781
782 expected := enmime.ErrorMissingContentType
783 satisfied := false
784 for _, perr := range p.Errors {
785 if perr.Name == expected {
786 satisfied = true
787 if perr.Severe {
788 t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
789 }
790 }
791 }
792 if !satisfied {
793 t.Errorf(
794 "Did not find expected error on part. Expected %q, but had: %v",
795 expected,
796 p.Errors)
797 }
798 }
799
800 func TestEmptyContentTypeBadContent(t *testing.T) {
801 r := test.OpenTestData("parts", "empty-ctype-bad-content.raw")
802 p, err := enmime.ReadParts(r)
803 if err != nil {
804 t.Fatal(err)
805 }
806
807 wantp := &enmime.Part{
808 PartID: "1",
809 Parent: test.PartExists,
810 Disposition: "",
811 }
812 test.ComparePart(t, p.FirstChild, wantp)
813
814 expected := enmime.ErrorMissingContentType
815 satisfied := false
816 for _, perr := range p.FirstChild.Errors {
817 if perr.Name == expected {
818 satisfied = true
819 if perr.Severe {
820 t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
821 }
822 }
823 }
824 if !satisfied {
825 t.Errorf(
826 "Did not find expected error on part. Expected %q, but had: %v",
827 expected,
828 p.Errors)
829 }
830 }
831
832 func TestMalformedContentTypeParams(t *testing.T) {
833 r := test.OpenTestData("parts", "malformed-content-type-params.raw")
834 p, err := enmime.ReadParts(r)
835 if err != nil {
836 t.Fatalf("%+v", err)
837 }
838 wantp := &enmime.Part{
839 PartID: "0",
840 ContentType: "text/html",
841 }
842 test.ComparePart(t, p, wantp)
843
844 expected := enmime.ErrorMalformedHeader
845 satisfied := false
846 for _, perr := range p.Errors {
847 if perr.Name == expected {
848 satisfied = true
849 if perr.Severe {
850 t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
851 }
852 }
853 }
854 if !satisfied {
855 t.Errorf(
856 "Did not find expected error on part. Expected %q, but had: %v",
857 expected,
858 p.Errors)
859 }
860 }
861
862 func TestContentTypeParamUnquotedSpecial(t *testing.T) {
863 r := test.OpenTestData("parts", "unquoted-ctype-param-special.raw")
864 p, err := enmime.ReadParts(r)
865 if err != nil {
866 t.Fatalf("%+v", err)
867 }
868
869 wantp := &enmime.Part{
870 PartID: "0",
871 ContentType: "text/calendar",
872 Disposition: "attachment",
873 FileName: "calendar.ics",
874 }
875 test.ComparePart(t, p, wantp)
876 }
877
878 func TestNoClosingBoundary(t *testing.T) {
879 r := test.OpenTestData("parts", "multimixed-no-closing-boundary.raw")
880 p, err := enmime.ReadParts(r)
881 if err != nil {
882 t.Errorf("%+v", err)
883 }
884 if p == nil {
885 t.Fatal("Expected part but got nil")
886 }
887
888 wantp := &enmime.Part{
889 Parent: test.PartExists,
890 PartID: "1",
891 ContentType: "text/html",
892 Charset: "UTF-8",
893 }
894 test.ComparePart(t, p.FirstChild, wantp)
895
896 expected := "Missing Boundary"
897 hasCorrectError := false
898 for _, v := range p.Errors {
899 if v.Severe {
900 t.Errorf("Expected Severe to be false, got true for %q", v.Name)
901 }
902 if v.Name == expected {
903 hasCorrectError = true
904 }
905 }
906 if !hasCorrectError {
907 t.Fatalf("Did not find expected error on part. Expected %q but got %v", expected, p.Errors)
908 }
909 }
910
911 func TestContentTypeParamMissingClosingQuote(t *testing.T) {
912 r := test.OpenTestData("parts", "missing-closing-param-quote.raw")
913 p, err := enmime.ReadParts(r)
914 if err != nil {
915 t.Fatalf("%+v", err)
916 }
917
918 wantp := &enmime.Part{
919 PartID: "0",
920 ContentType: "text/html",
921 Charset: "UTF-8Return-Path: bounce-810_HTML-769869545-477063-1070564-43@bounce.email.oflce57578375.com",
922 }
923 test.ComparePart(t, p, wantp)
924
925 expected := enmime.ErrorCharsetConversion
926 satisfied := false
927 for _, perr := range p.Errors {
928 if perr.Name == expected {
929 satisfied = true
930 if perr.Severe {
931 t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
932 }
933 }
934 }
935 if !satisfied {
936 t.Errorf(
937 "Did not find expected error on part. Expected %q, but had: %v",
938 expected,
939 p.Errors)
940 }
941 }
942
943 func TestChardetFailure(t *testing.T) {
944 const expectedContent = "GIF89ad\x00\x04\x00\x80\x00\x00\x00f\xccf\xff\x99!\xf9\x04\x00\x00\x00\x00\x00,\x00\x00\x00\x00d\x00\x04\x00\x00\x02\x1a\x8c\x8f\xa9\xcb\xed\x0f\xa3\x9c\xb4\xda\xeb\x80\u07bc\xfb\x0f\x86\xe2H\x96扦\xea*\x16\x00;"
945
946 t.Run("text part", func(t *testing.T) {
947 r := test.OpenTestData("parts", "chardet-fail.raw")
948 p, err := enmime.ReadParts(r)
949 if err != nil {
950 t.Fatal(err)
951 }
952 wantp := &enmime.Part{
953 PartID: "0",
954 ContentType: "text/plain",
955 ContentID: "[email protected]",
956 FileName: "rzkly.txt",
957 }
958 test.ComparePart(t, p, wantp)
959 expected := enmime.ErrorCharsetDeclaration
960 satisfied := false
961 for _, perr := range p.Errors {
962 if perr.Name == expected {
963 satisfied = true
964 if perr.Severe {
965 t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
966 }
967 }
968 }
969 if !satisfied {
970 t.Errorf(
971 "Did not find expected error on part. Expected %q, but had: %v",
972 expected,
973 p.Errors)
974 }
975 test.ContentEqualsString(t, p.Content, expectedContent)
976 })
977
978 t.Run("non-text part", func(t *testing.T) {
979 r := test.OpenTestData("parts", "chardet-fail-non-txt.raw")
980 p, err := enmime.ReadParts(r)
981 if err != nil {
982 t.Fatal(err)
983 }
984 if len(p.Errors) > 0 {
985 t.Errorf("Errors encountered while processing part: %v", p.Errors)
986 }
987 wantp := &enmime.Part{
988 PartID: "0",
989 ContentType: "image/gif",
990 ContentID: "[email protected]",
991 FileName: "rzkly.gif",
992 }
993 test.ComparePart(t, p, wantp)
994 test.ContentEqualsString(t, p.Content, expectedContent)
995 })
996
997 t.Run("not enough characters part", func(t *testing.T) {
998 r := test.OpenTestData("parts", "chardet-fail-not-long-enough.raw")
999 p, err := enmime.ReadParts(r)
1000 if err != nil {
1001 t.Fatal(err)
1002 }
1003 if len(p.Errors) > 0 {
1004 t.Errorf("Errors encountered while processing part: %v", p.Errors)
1005 }
1006 wantp := &enmime.Part{
1007 PartID: "0",
1008 ContentType: "text/plain",
1009 Charset: "UTF-8",
1010 }
1011 test.ComparePart(t, p, wantp)
1012 test.ContentEqualsString(t, p.Content, "和弟弟\r\n")
1013 })
1014 }
1015
1016 func TestChardetSuccess(t *testing.T) {
1017 // Testdata in these tests licensed under CC0: Public Domain
1018 t.Run("big-5 data in us-ascii part", func(t *testing.T) {
1019 r := test.OpenTestData("parts", "chardet-success-big-5.raw")
1020 p, err := enmime.ReadParts(r)
1021 if err != nil {
1022 t.Fatal(err)
1023 }
1024 expectedErr := enmime.Error{
1025 Name: "Character Set Declaration Mismatch",
1026 Detail: "declared charset \"us-ascii\", detected \"Big5\", confidence 100",
1027 Severe: false,
1028 }
1029 foundExpectedErr := false
1030 if len(p.Errors) > 0 {
1031 for _, v := range p.Errors {
1032 if *v == expectedErr {
1033 foundExpectedErr = true
1034 } else {
1035 t.Errorf("Error encountered while processing part: %v", v)
1036 }
1037 }
1038 }
1039 if !foundExpectedErr {
1040 t.Errorf("Expected to find %v warning", expectedErr)
1041 }
1042 wantp := &enmime.Part{
1043 PartID: "0",
1044 ContentType: "text/plain",
1045 Charset: "Big5",
1046 }
1047 test.ComparePart(t, p, wantp)
1048 })
1049
1050 t.Run("iso-8859-1 data in us-ascii part", func(t *testing.T) {
1051 r := test.OpenTestData("parts", "chardet-success-iso-8859-1.raw")
1052 p, err := enmime.ReadParts(r)
1053 if err != nil {
1054 t.Fatal(err)
1055 }
1056 expectedErr := enmime.Error{
1057 Name: "Character Set Declaration Mismatch",
1058 Detail: "declared charset \"us-ascii\", detected \"ISO-8859-1\", confidence 90",
1059 Severe: false,
1060 }
1061 foundExpectedErr := false
1062 if len(p.Errors) > 0 {
1063 for _, v := range p.Errors {
1064 if *v == expectedErr {
1065 foundExpectedErr = true
1066 } else {
1067 t.Errorf("Error encountered while processing part: %v", v)
1068 }
1069 }
1070 }
1071 if !foundExpectedErr {
1072 t.Errorf("Expected to find %v warning", expectedErr)
1073 }
1074 wantp := &enmime.Part{
1075 PartID: "0",
1076 ContentType: "text/plain",
1077 Charset: "ISO-8859-1",
1078 }
1079 test.ComparePart(t, p, wantp)
1080 })
1081 }