package enmime_test import ( "bytes" "sort" "strings" "testing" "github.com/go-test/deep" "github.com/jhillyerd/enmime" "github.com/jhillyerd/enmime/internal/test" ) func TestParseHeaderOnly(t *testing.T) { want := "" msg := test.OpenTestData("mail", "header-only.raw") e, err := enmime.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 := test.OpenTestData("mail", "non-mime.raw") e, err := enmime.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 := test.OpenTestData("mail", "non-mime-html.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse non-MIME:", err) } if len(e.Errors) == 1 { want := enmime.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 = "This" if !strings.Contains(e.HTML, want) { t.Errorf("Expected %q to contain %q", e.HTML, want) } } func TestParseMimeTree(t *testing.T) { msg := test.OpenTestData("mail", "attachment.raw") e, err := enmime.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 := test.OpenTestData("mail", "html-mime-inline.raw") e, err := enmime.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 := test.OpenTestData("mail", "html-mime-bad-charset-inline.raw") e, err := enmime.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 := test.OpenTestData("mail", "html-mime-bad-unknown-charset-inline.raw") e, err := enmime.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 := test.OpenTestData("mail", "mime-alternative.raw") e, err := enmime.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 := test.OpenTestData("mail", "mime-mixed.raw") e, err := enmime.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 TestParseMultiMixedRelatedHtml(t *testing.T) { msg := test.OpenTestData("mail", "mime-mixed-related.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse MIME:", err) } want := "
html1
" if e.HTML != want { t.Error("Text parts should concatenate, got:", e.HTML, "want:", want) } } func TestParseMultiSignedText(t *testing.T) { msg := test.OpenTestData("mail", "mime-signed.raw") e, err := enmime.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 := test.OpenTestData("mail", "quoted-printable.raw") e, err := enmime.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 := test.OpenTestData("mail", "quoted-printable-mime.raw") e, err := enmime.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 := test.OpenTestData("mail", "html-mime-inline.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse MIME:", err) } want := "" 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 := test.OpenTestData("mail", "attachment.raw") e, err := enmime.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 = "" test.ContentContainsString(t, e.Attachments[0].Content, want) } func TestParseAttachmentOctet(t *testing.T) { msg := test.OpenTestData("mail", "attachment-octet.raw") e, err := enmime.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, } if !bytes.Equal(e.Attachments[0].Content, wantBytes) { t.Error("Attachment should have correct content") } } func TestParseAttachmentApplication(t *testing.T) { msg := test.OpenTestData("mail", "attachment-application.raw") e, err := enmime.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 := test.OpenTestData("mail", "other-parts.raw") e, err := enmime.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.Error("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, } if !bytes.Equal(e.OtherParts[0].Content, wantBytes) { t.Error("Other part should have correct content") } } func TestParseInline(t *testing.T) { msg := test.OpenTestData("mail", "html-mime-inline.raw") e, err := enmime.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.Fatal("Should have one inline, got:", len(e.Inlines)) } if len(e.Attachments) > 0 { t.Error("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) } if !bytes.HasPrefix(e.Inlines[0].Content, []byte{0x89, 'P', 'N', 'G'}) { t.Error("Inline should have correct content") } } func TestParseOtherPartsRelated(t *testing.T) { msg := test.OpenTestData("mail", "other-multi-related.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse MIME:", err) } want := "Plain text." if !strings.Contains(e.Text, want) { t.Errorf("Text: %q should contain: %q", e.Text, want) } want = "HTML text." if !strings.Contains(e.HTML, want) { t.Errorf("HTML: %q should contain %q", e.HTML, want) } if len(e.Attachments) > 0 { t.Error("Should have no attachments, got:", len(e.Attachments)) } if len(e.Inlines) > 0 { t.Error("Should have no inlines, got:", len(e.Inlines)) } if len(e.OtherParts) != 2 { t.Fatal("Should have two other parts, got:", len(e.Inlines)) } want = "image001.png" got := e.OtherParts[0].FileName if got != want { t.Error("FileName got:", got, "want:", want) } want = "image001.png@01D3BA12.F6C6AEB0" got = e.OtherParts[0].ContentID if got != want { t.Error("ContentID got:", got, "want:", want) } if !bytes.HasPrefix(e.OtherParts[0].Content, []byte{0x89, 'P', 'N', 'G'}) { t.Error("Other part should have correct content") } want = "image002.png" got = e.OtherParts[1].FileName if got != want { t.Error("FileName got:", got, "want:", want) } want = "image002.png@01D3BA12.F6C6AEB0" got = e.OtherParts[1].ContentID if got != want { t.Error("ContentID got:", got, "want:", want) } if !bytes.HasPrefix(e.OtherParts[1].Content, []byte{0x89, 'P', 'N', 'G'}) { t.Error("Other part should have correct content") } } func TestParseHTMLOnlyInline(t *testing.T) { msg := test.OpenTestData("mail", "html-only-inline.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse MIME:", err) } if len(e.Errors) == 1 { want := enmime.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) } if !bytes.HasPrefix(e.Inlines[0].Content, []byte{0x89, 'P', 'N', 'G'}) { t.Error("Inline should have correct content") } } func TestParseInlineMultipart(t *testing.T) { msg := test.OpenTestData("mail", "inlinemultipart.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse MIME:", err) } if len(e.Errors) != 0 { t.Errorf("len(e.Errors) got: %v, want: 0", len(e.Errors)) } want := "Simple text." if !strings.Contains(e.Text, want) { t.Errorf("Downconverted Text: %q should contain: %q", e.Text, want) } if len(e.Inlines) != 1 { t.Error("Should have one inline, got:", len(e.Inlines)) } if len(e.Attachments) != 1 { t.Fatal("Should have one attachments, got:", len(e.Attachments)) } want = "test.txt" got := e.Inlines[0].FileName if got != want { t.Error("FileName got:", got, "want:", want) } if !bytes.HasPrefix(e.Inlines[0].Content, []byte("Text")) { t.Error("Inline should have correct content") } } func TestParseNestedHeaders(t *testing.T) { msg := test.OpenTestData("mail", "html-mime-inline.raw") e, err := enmime.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 TestParseHTMLOnlyCharsetInHeaderOnly(t *testing.T) { msg := test.OpenTestData("mail", "non-mime-html-charset-header-only.raw") e, err := enmime.ReadEnvelope(msg) if err != nil { t.Fatal("Failed to parse non-MIME:", err) } if !strings.ContainsRune(e.HTML, 0xfc) { t.Error("HTML body should contained German ü") } if !strings.Contains(e.HTML, "Müller") { t.Error("HTML body should contained 'Müller'") } } func TestEnvelopeGetHeader(t *testing.T) { // Test empty header e := &enmime.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 := test.OpenTestData("mail", "qp-ascii-header.raw") e, err := enmime.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 = test.OpenTestData("mail", "qp-utf8-header.raw") e, err = enmime.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 TestEnvelopeGetHeaderKeys(t *testing.T) { // Test empty header e := &enmime.Envelope{} got := e.GetHeaderKeys() if got != nil { t.Errorf("Headers was: %q, want: nil", got) } // Even non-MIME messages should support encoded-words in headers // Also, encoded addresses should be suppored r := test.OpenTestData("mail", "qp-ascii-header.raw") e, err := enmime.ReadEnvelope(r) if err != nil { t.Fatal("Failed to parse non-MIME:", err) } want := []string{"Date", "From", "Subject", "To", "X-Mailer"} got = e.GetHeaderKeys() sort.Strings(got) test.DiffStrings(t, got, want) // Test UTF-8 subject line r = test.OpenTestData("mail", "qp-utf8-header.raw") e, err = enmime.ReadEnvelope(r) if err != nil { t.Fatal("Failed to parse MIME:", err) } want = []string{"Content-Type", "Date", "From", "Message-Id", "Mime-Version", "Sender", "Subject", "To", "User-Agent"} got = e.GetHeaderKeys() sort.Strings(got) test.DiffStrings(t, got, want) } func TestEnvelopeGetHeaderValues(t *testing.T) { r := test.OpenTestData("mail", "ctype-bug.raw") e, err := enmime.ReadEnvelope(r) if err != nil { t.Fatal("Failed to parse MIME:", err) } // test Received headers want := []string{ "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