Search in sources :

Example 1 with MimeMessage

use of com.zimbra.common.mime.MimeMessage in project zm-mailbox by Zimbra.

the class TestFilter method testRedirect.

/**
 * Tests the redirect filter action and confirms that the X-ZimbraForwarded
 * header is set on the redirected message.
 */
@Test
public void testRedirect() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "testRedirect", redirect to user2
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testRedirect"));
    actions.add(new ZRedirectAction(TestUtil.getAddress(REMOTE_USER_NAME)));
    rules.add(new ZFilterRule("testRedirect", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message.  Set the From header to something bogus to make
    // sure we're not rewriting it
    String from = "joebob@mycompany.com";
    String subject = NAME_PREFIX + " testRedirect 1";
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    // Confirm that user1 did not receive it.
    List<ZMessage> messages = TestUtil.search(mMbox, "subject:\"" + subject + "\"");
    assertEquals(0, messages.size());
    // Confirm that user2 received it, and make sure X-ZimbraForwarded is set.
    ZMailbox remoteMbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
    ZMessage msg = TestUtil.waitForMessage(remoteMbox, "in:inbox subject:\"" + subject + "\"");
    byte[] content = TestUtil.getContent(remoteMbox, msg.getId()).getBytes();
    MimeMessage mimeMsg = new MimeMessage(new ByteArrayInputStream(content));
    assertEquals(user1.getName(), mimeMsg.getHeader(FilterUtil.HEADER_FORWARDED));
    assertEquals(from, mimeMsg.getHeader("From"));
    // Check zimbraMailRedirectSetEnvelopeSender=FALSE.
    int port = 6025;
    DummySmtpServer smtp = startSmtpServer(port);
    localServer.setSmtpPort(port);
    localServer.setMailRedirectSetEnvelopeSender(false);
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    assertEquals(from, smtp.getMailFrom());
    // Check zimbraMailRedirectSetEnvelopeSender=TRUE.
    smtp = startSmtpServer(port);
    localServer.setMailRedirectSetEnvelopeSender(true);
    subject = NAME_PREFIX + " testRedirect 2";
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    String userAddress = Strings.nullToEmpty(TestUtil.getAddress(USER_NAME)).toLowerCase();
    assertEquals("testRedirect 2 mail from", userAddress, smtp.getMailFrom());
    // Check empty envelope sender.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 3";
    String msgContent = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    String[] recipients = new String[] { USER_NAME };
    TestUtil.addMessageLmtp(recipients, null, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
    // Check Auto-Submitted=yes.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 4";
    msgContent = "Auto-Submitted: yes\r\n" + TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
    // Check Auto-Submitted=no.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 5";
    msgContent = "Auto-Submitted: no\r\n" + TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertEquals("testRedirect 5 mail from", userAddress, smtp.getMailFrom());
    // Check Content-Type=multipart/report.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 6";
    msgContent = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    msgContent = msgContent.replace("text/plain", "multipart/report");
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) ZMailbox(com.zimbra.client.ZMailbox) MimeMessage(com.zimbra.common.mime.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 2 with MimeMessage

use of com.zimbra.common.mime.MimeMessage in project zm-mailbox by Zimbra.

the class TestFilter method testRedirectMailLoop.

/**
 * Confirms that the message gets delivered even when a mail loop occurs.
 */
@Test
public void testRedirectMailLoop() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "testRedirectMailLoop", redirect to user1
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testRedirectMailLoop"));
    actions.add(new ZRedirectAction(TestUtil.getAddress(USER_NAME)));
    rules.add(new ZFilterRule("testRedirectMailLoop", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message.
    String subject = NAME_PREFIX + " testRedirectMailLoop";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    // Confirm that user1 received it.
    ZMessage msg = TestUtil.waitForMessage(mMbox, "subject:\"" + subject + "\"");
    byte[] content = TestUtil.getContent(mMbox, msg.getId()).getBytes();
    MimeMessage mimeMsg = new MimeMessage(new ByteArrayInputStream(content));
    assertEquals(user1.getName(), mimeMsg.getHeader(FilterUtil.HEADER_FORWARDED));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) MimeMessage(com.zimbra.common.mime.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Aggregations

ZFilterAction (com.zimbra.client.ZFilterAction)2 ZRedirectAction (com.zimbra.client.ZFilterAction.ZRedirectAction)2 ZFilterCondition (com.zimbra.client.ZFilterCondition)2 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)2 ZFilterRule (com.zimbra.client.ZFilterRule)2 ZFilterRules (com.zimbra.client.ZFilterRules)2 ZMessage (com.zimbra.client.ZMessage)2 MimeMessage (com.zimbra.common.mime.MimeMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ZMailbox (com.zimbra.client.ZMailbox)1