Search in sources :

Example 1 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class ZMimeMultipartTest method encoded.

@Test
public void encoded() throws Exception {
    final String boundary = "dfghjkl";
    final String preamble = "when in the course of human events...\r\n";
    final String plain = "The Rain in Spain.";
    final String html = "The <u>Rain</u> in <em>Spain</em>.";
    ByteBuilder bbheader = new ByteBuilder();
    bbheader.append("From: test@example.com\r\n");
    bbheader.append("To: rcpt@example.com\r\n");
    bbheader.append("Subject: message subject\r\n");
    bbheader.append("Message-ID: <11e1-b0c4-0800200c9a66@example.com>\r\n");
    bbheader.append("Content-Transfer-Encoding: base64\r\n");
    bbheader.append("Content-Type: multipart/alternative; boundary=").append(boundary).append("\r\n");
    bbheader.append("\r\n");
    ByteBuilder bbmulti = new ByteBuilder();
    bbmulti.append(preamble);
    bbmulti.append("--").append(boundary).append("\r\n");
    bbmulti.append("Content-Type: text/plain\r\n");
    bbmulti.append("\r\n");
    bbmulti.append(plain).append("\r\n");
    bbmulti.append("--").append(boundary).append("\r\n");
    bbmulti.append("Content-Type: text/html\r\n");
    bbmulti.append("\r\n");
    bbmulti.append(html).append("\r\n");
    bbmulti.append("--").append(boundary).append("--\r\n");
    // message with CTE header and base64-encoded body
    ByteBuilder bb = new ByteBuilder();
    bb.append(bbheader);
    bb.append(ByteUtil.getContent(new Base64EncoderStream(new ByteArrayInputStream(bbmulti.toByteArray())), -1));
    Session s = Session.getDefaultInstance(new Properties());
    ZMimeMessage mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
    Object o = mm.getContent();
    Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
    ZMimeMultipart multi = (ZMimeMultipart) o;
    Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
    Assert.assertEquals("2 subparts", 2, multi.getCount());
    Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
    Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
    // message with CTE header and nonencoded body
    bb = new ByteBuilder();
    bb.append(bbheader);
    bb.append(bbmulti);
    mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
    o = mm.getContent();
    Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
    multi = (ZMimeMultipart) o;
    Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
    Assert.assertEquals("2 subparts", 2, multi.getCount());
    Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
    Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Base64EncoderStream(com.zimbra.common.zmime.ZTransferEncoding.Base64EncoderStream) Properties(java.util.Properties) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 2 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class ZMimeParserTest method detectBoundary.

@Test
public void detectBoundary() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    bb.append("From: <foo@example.com\r\n");
    bb.append("Subject: sample\r\n");
    appendMultipartWithoutBoundary(bb);
    MimeMessage mm = new ZMimeMessage(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
    Assert.assertTrue("content is multipart", mm.getContent() instanceof ZMimeMultipart);
    testMultipartWithoutBoundary((ZMimeMultipart) mm.getContent());
    bb.reset();
    bb.append("From: <foo@example.com\r\n");
    bb.append("Subject: sample\r\n");
    bb.append("Content-Type: multipart/alternative\r\n");
    bb.append("\r\n");
    bb.append("prologue text goes here\r\n");
    bb.append("--").append(BOUNDARY2).append("\r\n");
    appendMultipartWithoutBoundary(bb);
    bb.append("--").append(BOUNDARY2).append("--\r\n");
    mm = new ZMimeMessage(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
    Assert.assertTrue("content is multipart", mm.getContent() instanceof ZMimeMultipart);
    ZMimeMultipart mmp = (ZMimeMultipart) mm.getContent();
    Assert.assertEquals("multipart/alternative", "alternative", new ZContentType(mmp.getContentType()).getSubType());
    Assert.assertEquals("toplevel multipart has 1 subpart", 1, mmp.getCount());
    Assert.assertEquals("implicit boundary detection", BOUNDARY2, mmp.getBoundary());
    Assert.assertEquals("first part is multipart/mixed", "multipart/mixed", new ZContentType(mmp.getBodyPart(0).getContentType()).getBaseType());
    testMultipartWithoutBoundary((ZMimeMultipart) mmp.getBodyPart(0).getContent());
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 3 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class ZMimeParserTest method recursion.

@Test
public void recursion() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    String boundary = BOUNDARY1;
    bb.append("From: <foo@example.com\r\n");
    bb.append("Subject: sample\r\n");
    bb.append("Content-Type: multipart/mixed; boundary=").append(boundary).append("\r\n");
    bb.append("Content-Type: text/plain\r\n");
    bb.append("\r\n");
    bb.append("foo!  bar!  loud noises\r\n\r\n");
    bb.append("--").append(boundary).append("\r\n");
    addChildren(bb, 0);
    bb.append("--").append(boundary).append("--\r\n");
    try {
        MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
        Object content = mm.getContent();
        Assert.assertTrue("content is multipart", content instanceof ZMimeMultipart);
        ZMimeMultipart zmp = (ZMimeMultipart) content;
        Assert.assertEquals("top count", 1, zmp.getCount());
        traverseChildren((ZMimeMultipart) zmp.getBodyPart(0).getContent(), LC.mime_max_recursion.intValue() - 1);
    } catch (ClassCastException e) {
        Assert.fail("mishandled double Content-Type headers");
    }
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 4 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class ZMimeParserTest method parse.

@Test
public void parse() throws Exception {
    ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
    bb.append("Content-Type: text/plain\r\n");
    bb.append("From: <foo@example.com\r\n");
    bb.append("Subject: sample\r\n");
    bb.append("Content-Type: multipart/alternative; boundary=").append(BOUNDARY1).append("\r\n");
    bb.append("\r\n");
    bb.append("--").append(BOUNDARY1).append("\r\n");
    bb.append("Content-Type: text/plain\r\n");
    bb.append("\r\n");
    bb.append("foo!  bar!  loud noises\r\n\r\n");
    bb.append("--").append(BOUNDARY1).append("--\r\n");
    try {
        MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
        Assert.assertFalse("content isn't multipart", mm.getContent() instanceof MimeMultipart);
        Assert.assertEquals("text/plain", "text/plain", new ZContentType(mm.getContentType()).getBaseType());
    } catch (ClassCastException e) {
        Assert.fail("mishandled double Content-Type headers");
    }
}
Also used : ByteBuilder(com.zimbra.common.zmime.ZMimeUtility.ByteBuilder) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 5 with SharedByteArrayInputStream

use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.

the class SpamTest method whitelist.

/**
     * Tests whitelisting takes precedence over marking spam.
     */
@Test
public void whitelist() throws Exception {
    String raw = "From: sender@zimbra.com\n" + "To: recipient@zimbra.com\n" + "X-Spam-Flag: YES\n" + "Subject: test\n" + "\n" + "Hello World.";
    MimeMessage msg = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw.getBytes()));
    Assert.assertTrue(SpamHandler.isSpam(msg));
    // add a whitelist header to the previous message
    raw = "From: sender@zimbra.com\n" + "To: recipient@zimbra.com\n" + "X-Whitelist-Flag: YES\n" + "X-Spam-Flag: YES\n" + "Subject: test\n" + "\n" + "Hello World.";
    msg = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(raw.getBytes()));
    Assert.assertFalse(SpamHandler.isSpam(msg));
}
Also used : Mime(com.zimbra.cs.mime.Mime) MimeMessage(javax.mail.internet.MimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)61 MimeMessage (javax.mail.internet.MimeMessage)53 Test (org.junit.Test)47 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)31 Session (javax.mail.Session)15 JMSession (com.zimbra.cs.util.JMSession)14 Transport (javax.mail.Transport)14 InputStream (java.io.InputStream)9 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)7 ByteBuilder (com.zimbra.common.zmime.ZMimeUtility.ByteBuilder)6 Mailbox (com.zimbra.cs.mailbox.Mailbox)6 FixedMimeMessage (com.zimbra.cs.mime.Mime.FixedMimeMessage)6 MessagingException (javax.mail.MessagingException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)4 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)4 Message (com.zimbra.cs.mailbox.Message)4 ParseMimeMessage (com.zimbra.cs.service.mail.ParseMimeMessage)4 SendMsgRequest (com.zimbra.soap.mail.message.SendMsgRequest)4