use of com.zimbra.cs.lmtpserver.LmtpAddress in project zm-mailbox by Zimbra.
the class ErejectTest method testThatSenderRcdUnDeliveredEmail.
/*
* applyRulesToIncomingMessage() should throw an exception to cancel the message delivery.
* No message is delivered.
*
* The following error will be logged:
* ERROR - Evaluation failed. Reason: 'ereject' action refuses delivery of a message. Sieve rule evaluation is cancelled
*/
@Test
public void testThatSenderRcdUnDeliveredEmail() {
Account acct1 = null;
Mailbox mbox1 = null;
Account acct2 = null;
Mailbox mbox2 = null;
try {
acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
} catch (DeliveryServiceException e) {
if (e.getCause() instanceof ErejectException) {
try {
List<Integer> items = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE);
Assert.assertEquals(null, items);
} catch (Exception ex) {
ex.printStackTrace();
fail("No exception should be thrown: " + ex.getMessage());
}
} else {
fail("No exception other than DeliveryServiceException/ErejectException should be thrown: " + e.getMessage());
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.lmtpserver.LmtpAddress in project zm-mailbox by Zimbra.
the class LmtpAddressTester method test.
private static boolean test(String line, String[] allowedParams) {
System.out.println("==>" + line + "<==");
LmtpAddress addr = new LmtpAddress(line, allowedParams, "+");
System.out.println(" valid=" + addr.isValid());
if (addr.isValid()) {
System.out.println(" local-part=/" + addr.getLocalPart() + "/");
System.out.println(" normalized-local=/" + addr.getNormalizedLocalPart() + "/");
System.out.println(" domain-part=/" + addr.getDomainPart() + "/");
Map params = addr.getParameters();
int i = 0;
for (Iterator it = params.entrySet().iterator(); it.hasNext(); ) {
Map.Entry e = (Map.Entry) it.next();
String key = (String) e.getKey();
String val = (String) e.getValue();
System.out.println(" [" + i + "] key=/" + key + "/ val=/" + val + "/");
i++;
}
}
return addr.isValid();
}
use of com.zimbra.cs.lmtpserver.LmtpAddress in project zm-mailbox by Zimbra.
the class EnvelopeTest method testInvalidHeaderName2.
@Test
public void testInvalidHeaderName2() {
String filterScript = "require \"envelope\";\n" + "if anyof envelope :matches \"from123\" \"t1@zimbra.com\" {\n" + " fileinto \"Junk\";\n" + "}\n";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<t1@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
account.setMail("xyz@zimbra.com");
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, msg.getFolderId());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.lmtpserver.LmtpAddress in project zm-mailbox by Zimbra.
the class EnvelopeTest method testCountForEmptyFromHeader.
@Test
public void testCountForEmptyFromHeader() {
String filterScript = "require [\"envelope\", \"relational\", \"comparator-i;ascii-numeric\"];\n" + "if envelope :count \"eq\" :comparator \"i;ascii-numeric\" :all \"FROM\" \"0\" {\n" + "tag \"0\";\n" + "}\n" + "if envelope :all :matches \"from\" \"\" {\n" + " tag \"empty\";\n" + "}\n" + "if envelope :count \"eq\" :comparator \"i;ascii-numeric\" :all \"to\" \"1\" {\n" + "tag \"1\";\n" + "}";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals("0", ArrayUtil.getFirstElement(msg.getTags()));
Assert.assertEquals("empty", msg.getTags()[1]);
Assert.assertEquals("1", msg.getTags()[2]);
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.lmtpserver.LmtpAddress in project zm-mailbox by Zimbra.
the class EnvelopeTest method testHeaderNameWithLeadingSpace.
@Test
public void testHeaderNameWithLeadingSpace() {
String filterScript = "require \"envelope\";\n" + "if envelope :matches \" TO\" \"*@zimbra.com\" {\n" + " tag \"t1\";\n" + "}\n";
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
try {
Provisioning prov = Provisioning.getInstance();
Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
account.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals(0, msg.getTags().length);
} catch (Exception e) {
fail("No exception should be thrown");
}
}
Aggregations