use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class MimeTest method testFullContentType.
@Test
public void testFullContentType() throws Exception {
String content = "From: user1@example.com\r\n" + "To: user2@example.com\r\n" + "Subject: test\r\n" + "Content-Type: text/plain;param=foo\r\n" + "Content-Transfer-Encoding: base64\r\n\r\n" + //obviously not a real file
"R0a1231312ad124svsdsal==";
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
MimePart part = Mime.getMimePart(mm, "1");
Assert.assertEquals("text/plain;param=foo", part.getContentType());
List<MPartInfo> parts = Mime.getParts(mm);
Assert.assertNotNull(parts);
Assert.assertEquals(1, parts.size());
MPartInfo info = parts.get(0);
Assert.assertEquals("text/plain", info.getContentType());
Assert.assertEquals("text/plain;param=foo", info.getFullContentType());
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestMailSender method testRejectRecipient.
@Test
public void testRejectRecipient() throws Exception {
String errorMsg = "Sender address rejected: User unknown in relay recipient table";
String bogusAddress = TestUtil.getAddress("bogus");
startDummySmtpServer(bogusAddress, errorMsg);
Server server = Provisioning.getInstance().getLocalServer();
server.setSmtpPort(TEST_SMTP_PORT);
String content = TestUtil.getTestMessage(NAME_PREFIX + " testRejectSender", bogusAddress, SENDER_NAME, null);
MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
Mailbox mbox = TestUtil.getMailbox(SENDER_NAME);
// Test reject first recipient, get partial send value from LDAP.
boolean sendFailed = false;
server.setSmtpSendPartial(false);
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test reject first recipient, set partial send value explicitly.
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
server.setSmtpSendPartial(true);
MailSender sender = mbox.getMailSender().setSendPartial(false);
try {
sender.sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test reject second recipient, get partial send value from LDAP.
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
String validAddress = TestUtil.getAddress(RECIPIENT_NAME);
InternetAddress[] recipients = new InternetAddress[2];
recipients[0] = new JavaMailInternetAddress(validAddress);
recipients[1] = new JavaMailInternetAddress(bogusAddress);
msg.setRecipients(MimeMessage.RecipientType.TO, recipients);
server.setSmtpSendPartial(false);
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test partial send, get value from LDAP.
startDummySmtpServer(bogusAddress, errorMsg);
server.setSmtpSendPartial(true);
sendFailed = false;
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test partial send, specify value explicitly.
server.setSmtpSendPartial(false);
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
sender = mbox.getMailSender().setSendPartial(true);
try {
sender.sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
// Don't check error message. JavaMail does not give us the SMTP protocol error in the
// partial send case.
validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestMessageIntercept method compareContent.
/**
* Confirm that the message attached to the intercept message matches the original.
*/
private void compareContent(ZMailbox tappedMbox, ZMessage tappedMsg, ZMailbox interceptorMbox, ZMessage interceptMsg) throws Exception {
String relativeUrl = String.format("?id=%s&part=2", interceptMsg.getId());
InputStream in = interceptorMbox.getRESTResource(relativeUrl);
String interceptedMsgContent = new String(ByteUtil.getContent(in, -1)).trim();
String tappedMsgContent = TestUtil.getContent(tappedMbox, tappedMsg.getId()).trim();
Account account = TestUtil.getAccount(RECIPIENT_NAME);
// Compare headers
MimeMessage tappedMimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(tappedMsgContent.getBytes()));
MimeMessage interceptedMimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(interceptedMsgContent.getBytes()));
boolean headersOnly = account.getBooleanAttr(Provisioning.A_zimbraInterceptSendHeadersOnly, false);
Set<String> tappedHeaderLines = getHeaderLines(tappedMimeMsg);
Set<String> interceptedHeaderLines = getHeaderLines(interceptedMimeMsg);
tappedHeaderLines.removeAll(getHeaderLines(interceptedMimeMsg));
interceptedHeaderLines.removeAll(getHeaderLines(tappedMimeMsg));
String context = "Unexpected headers found. tapped: " + StringUtil.join(",", tappedHeaderLines) + ". intercepted: " + StringUtil.join(",", interceptedHeaderLines) + ".";
assertTrue(context, tappedHeaderLines.size() == 0 && interceptedHeaderLines.size() == 0);
// Compare body
if (headersOnly) {
String interceptedBody = new String(ByteUtil.getContent(interceptedMimeMsg.getInputStream(), 0));
if (interceptedBody != null) {
interceptedBody = interceptedBody.trim();
}
assertTrue("Unexpected body: '" + interceptedBody + "'", interceptedBody == null || interceptedBody.length() == 0);
} else {
TestUtil.assertMessageContains(tappedMsgContent, interceptedMsgContent);
}
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestParsedMessage method testParsedMessage.
@Test
public void testParsedMessage() throws Exception {
ExpectedResults expected = new ExpectedResults();
String subject = NAME_PREFIX + " testParsedMessage";
expected.convertedSubject = subject;
expected.rawContent = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
expected.wasMutated = false;
// Test ParsedMessage created from byte[]
ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
verifyParsedMessage(pm, expected);
pm = new ParsedMessage(expected.rawContent.getBytes(), true);
verifyParsedMessage(pm, expected);
// Test ParsedMessage created from File
mFile = File.createTempFile("TestParsedMessage", ".msg");
FileOutputStream out = new FileOutputStream(mFile);
out.write(expected.rawContent.getBytes());
pm = new ParsedMessage(mFile, null, false);
verifyParsedMessage(pm, expected);
pm = new ParsedMessage(mFile, null, true);
verifyParsedMessage(pm, expected);
// Test ParsedMessage created from MimeMessage.
MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
pm = new ParsedMessage(mimeMsg, false);
verifyParsedMessage(pm, expected);
pm = new ParsedMessage(mimeMsg, true);
verifyParsedMessage(pm, expected);
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestParsedMessage method testMimeConverter.
@Test
public void testMimeConverter() throws Exception {
MimeVisitor.registerConverter(TestMimeVisitor.class);
ExpectedResults expected = new ExpectedResults();
String subject = NAME_PREFIX + " testMimeConverter oldsubject";
expected.convertedSubject = NAME_PREFIX + " testMimeConverter newsubject";
expected.rawContent = TestUtil.getTestMessage(subject, RECIPIENT_NAME, SENDER_NAME, null);
expected.wasMutated = false;
// Test ParsedMessage created from byte[]
ParsedMessage pm = new ParsedMessage(expected.rawContent.getBytes(), false);
verifyParsedMessage(pm, expected);
pm = new ParsedMessage(expected.rawContent.getBytes(), true);
verifyParsedMessage(pm, expected);
// Test ParsedMessage created from File
mFile = File.createTempFile("TestParsedMessage", ".msg");
FileOutputStream out = new FileOutputStream(mFile);
out.write(expected.rawContent.getBytes());
out.close();
pm = new ParsedMessage(mFile, null, false);
verifyParsedMessage(pm, expected);
pm = new ParsedMessage(mFile, null, true);
verifyParsedMessage(pm, expected);
// Test ParsedMessage created from MimeMessage. Can't verify entire content
// because JavaMail mangles the headers.
MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(expected.rawContent.getBytes()));
pm = new ParsedMessage(mimeMsg, false);
Assert.assertTrue((new String(pm.getRawData()).contains("oldsubject")));
Assert.assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
Assert.assertTrue(pm.getSubject().contains("newsubject"));
pm = new ParsedMessage(mimeMsg, true);
Assert.assertTrue((new String(pm.getRawData()).contains("oldsubject")));
Assert.assertTrue(getContent(pm.getMimeMessage()).contains("newsubject"));
Assert.assertTrue(pm.getSubject().contains("newsubject"));
}
Aggregations