@Test public void migrateHtmlWithRelatedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertHtmlWithRelatedMessage(db); db.close(); LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application); LocalMessage msg = localStore.getFolder("dev").getMessage("10"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(9, msg.getId()); Assert.assertEquals(11, msg.getHeaderNames().size()); Assert.assertEquals("multipart/mixed", msg.getMimeType()); Assert.assertEquals(1, msg.getAttachmentCount()); Multipart msgBody = (Multipart) msg.getBody(); Assert.assertEquals("------------050707070308090509030605", msgBody.getBoundary()); Multipart multipartAlternativePart = (Multipart) msgBody.getBodyPart(0).getBody(); BodyPart htmlPart = multipartAlternativePart.getBodyPart(1); String msgTextContent = MessageExtractor.getTextFromPart(htmlPart); Assert.assertNotNull(msgTextContent); Assert.assertTrue(msgTextContent.contains("cid:[email protected]")); Assert.assertEquals("image/jpeg", msgBody.getBodyPart(1).getMimeType()); }
@Test public void migrateTextPlain() throws Exception { SQLiteDatabase db = createV50Database(); insertSimplePlaintextMessage(db); db.close(); LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application); LocalMessage msg = localStore.getFolder("dev").getMessage("3"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals("text/plain", msg.getMimeType()); Assert.assertEquals(2, msg.getId()); Assert.assertEquals(13, msg.getHeaderNames().size()); Assert.assertEquals(0, msg.getAttachmentCount()); Assert.assertEquals(1, msg.getHeader("User-Agent").length); Assert.assertEquals("Mutt/1.5.24 (2015-08-30)", msg.getHeader("User-Agent")[0]); Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length); Assert.assertEquals( "text/plain", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null)); Assert.assertEquals( "utf-8", MimeUtility.getHeaderParameter( msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "charset")); Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody); String msgTextContent = MessageExtractor.getTextFromPart(msg); Assert.assertEquals("nothing special here.\r\n", msgTextContent); }
@Test public void testShouldSanitizeOutputHtml() throws MessagingException { // Create text/plain body TextBody body = new TextBody(BODY_TEXT); // Create message MimeMessage message = new MimeMessage(); MimeMessageHelper.setBody(message, body); // Prepare fixture HtmlSanitizer htmlSanitizer = mock(HtmlSanitizer.class); MessageViewInfoExtractor messageViewInfoExtractor = new MessageViewInfoExtractor(context, null, htmlSanitizer); String value = "--sanitized html--"; when(htmlSanitizer.sanitize(any(String.class))).thenReturn(value); // Extract text List<Part> outputNonViewableParts = new ArrayList<>(); ArrayList<Viewable> outputViewableParts = new ArrayList<>(); MessageExtractor.findViewablesAndAttachments( message, outputViewableParts, outputNonViewableParts); ViewableExtractedText viewableExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts); assertSame(value, viewableExtractedText.html); }
@Test public void testSimpleHtmlMessage() throws MessagingException { String bodyText = "<strong>K-9 Mail</strong> rocks :>"; // Create text/plain body TextBody body = new TextBody(bodyText); // Create message MimeMessage message = new MimeMessage(); message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); MimeMessageHelper.setBody(message, body); // Extract text ArrayList<Viewable> outputViewableParts = new ArrayList<>(); MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, null); assertEquals(outputViewableParts.size(), 1); ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts); String expectedText = BODY_TEXT; String expectedHtml = bodyText; assertEquals(expectedText, container.text); assertEquals(expectedHtml, getHtmlBodyText(container.html)); }
private boolean containsInlinePgpEncryptedText(Message message) { Part textPart = textPartFinder.findFirstTextPart(message); if (!isUsableTextPart(textPart)) { return false; } String text = MessageExtractor.getTextFromPart(textPart); if (text == null) { return false; } return PGP_MESSAGE_PATTERN.matcher(text).find(); }
@Test public void testMultipartPlainTextMessage() throws MessagingException { String bodyText1 = "text body 1"; String bodyText2 = "text body 2"; // Create text/plain bodies TextBody body1 = new TextBody(bodyText1); TextBody body2 = new TextBody(bodyText2); // Create multipart/mixed part MimeMultipart multipart = MimeMultipart.newInstance(); MimeBodyPart bodyPart1 = new MimeBodyPart(body1, "text/plain"); MimeBodyPart bodyPart2 = new MimeBodyPart(body2, "text/plain"); multipart.addBodyPart(bodyPart1); multipart.addBodyPart(bodyPart2); // Create message MimeMessage message = new MimeMessage(); MimeMessageHelper.setBody(message, multipart); // Extract text List<Part> outputNonViewableParts = new ArrayList<>(); ArrayList<Viewable> outputViewableParts = new ArrayList<>(); MessageExtractor.findViewablesAndAttachments( message, outputViewableParts, outputNonViewableParts); ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts); String expectedText = bodyText1 + "\r\n\r\n" + "------------------------------------------------------------------------\r\n\r\n" + bodyText2; String expectedHtml = "<pre class=\"k9mail\">" + bodyText1 + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; " + "border-bottom: 1px solid #000\"></p>" + "<pre class=\"k9mail\">" + bodyText2 + "</pre>"; assertEquals(expectedText, container.text); assertEquals(expectedHtml, getHtmlBodyText(container.html)); }
@Test public void migratePgpInlineClearsignedMessage() throws Exception { SQLiteDatabase db = createV50Database(); insertPgpInlineClearsignedMessage(db); db.close(); LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application); LocalMessage msg = localStore.getFolder("dev").getMessage("8"); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null); Assert.assertEquals(7, msg.getId()); Assert.assertEquals(12, msg.getHeaderNames().size()); Assert.assertEquals("text/plain", msg.getMimeType()); Assert.assertEquals(0, msg.getAttachmentCount()); Assert.assertTrue(msg.getBody() instanceof BinaryMemoryBody); String msgTextContent = MessageExtractor.getTextFromPart(msg); Assert.assertEquals( OpenPgpUtils.PARSE_RESULT_SIGNED_MESSAGE, OpenPgpUtils.parseMessage(msgTextContent)); }
@Test public void testSimplePlainTextMessage() throws MessagingException { // Create text/plain body TextBody body = new TextBody(BODY_TEXT); // Create message MimeMessage message = new MimeMessage(); message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); MimeMessageHelper.setBody(message, body); // Extract text List<Part> outputNonViewableParts = new ArrayList<>(); ArrayList<Viewable> outputViewableParts = new ArrayList<>(); MessageExtractor.findViewablesAndAttachments( message, outputViewableParts, outputNonViewableParts); ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts); String expectedText = BODY_TEXT; String expectedHtml = "<pre class=\"k9mail\">" + "K-9 Mail rocks :>" + "</pre>"; assertEquals(expectedText, container.text); assertEquals(expectedHtml, getHtmlBodyText(container.html)); }
@Test public void testMultipartDigestWithMessages() throws Exception { String data = "Content-Type: multipart/digest; boundary=\"bndry\"\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "--bndry\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "Content-Type: multipart/alternative; boundary=\"bndry2\"\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "text part of second message\r\n" + "\r\n" + "--bndry2\r\n" + "Content-Type: text/html\"\r\n" + "\r\n" + "html part of second message\r\n" + "\r\n" + "--bndry2--\r\n" + "\r\n" + "--bndry--\r\n"; MimeMessage message = MimeMessage.parseMimeMessage(new ByteArrayInputStream(data.getBytes()), false); // Extract text List<Part> outputNonViewableParts = new ArrayList<>(); ArrayList<Viewable> outputViewableParts = new ArrayList<>(); MessageExtractor.findViewablesAndAttachments( message, outputViewableParts, outputNonViewableParts); String expectedExtractedText = "Subject: (No subject)\r\n" + "\r\n" + "text body of first message\r\n" + "\r\n" + "\r\n" + "------------------------------------------------------------------------\r\n" + "\r\n" + "Subject: subject of second message\r\n" + "\r\n" + "text part of second message\r\n"; String expectedHtmlText = "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>(No subject)</td></tr>" + "</table>" + "<pre class=\"k9mail\">text body of first message<br /></pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: 1px solid #000\"></p>" + "<table style=\"border: 0\">" + "<tr><th style=\"text-align: left; vertical-align: top;\">Subject:</th><td>subject of second message</td></tr>" + "</table>" + "<pre class=\"k9mail\">text part of second message<br /></pre>"; assertEquals(4, outputViewableParts.size()); assertEquals( "subject of second message", ((MessageHeader) outputViewableParts.get(2)).getMessage().getSubject()); ViewableExtractedText firstMessageExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts); assertEquals(expectedExtractedText, firstMessageExtractedText.text); assertEquals(expectedHtmlText, getHtmlBodyText(firstMessageExtractedText.html)); }
@Test public void testTextPlusRfc822Message() throws MessagingException { K9ActivityCommon.setLanguage(context, "en"); Locale.setDefault(Locale.US); TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00")); String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment."; // Create text/plain body TextBody textBody = new TextBody(BODY_TEXT); // Create inner text/plain body TextBody innerBody = new TextBody(innerBodyText); // Create message/rfc822 body MimeMessage innerMessage = new MimeMessage(); innerMessage.addSentDate(new Date(112, 02, 17), false); innerMessage.setRecipients(RecipientType.TO, new Address[] {new Address("*****@*****.**")}); innerMessage.setSubject("Subject"); innerMessage.setFrom(new Address("*****@*****.**")); MimeMessageHelper.setBody(innerMessage, innerBody); // Create multipart/mixed part MimeMultipart multipart = MimeMultipart.newInstance(); MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain"); MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822"); bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\""); multipart.addBodyPart(bodyPart1); multipart.addBodyPart(bodyPart2); // Create message MimeMessage message = new MimeMessage(); MimeMessageHelper.setBody(message, multipart); // Extract text List<Part> outputNonViewableParts = new ArrayList<Part>(); ArrayList<Viewable> outputViewableParts = new ArrayList<>(); MessageExtractor.findViewablesAndAttachments( message, outputViewableParts, outputNonViewableParts); ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts); String expectedText = BODY_TEXT + "\r\n\r\n" + "----- message.eml ------------------------------------------------------" + "\r\n\r\n" + "From: [email protected]" + "\r\n" + "To: [email protected]" + "\r\n" + "Sent: Sat Mar 17 00:00:00 GMT+01:00 2012" + "\r\n" + "Subject: Subject" + "\r\n" + "\r\n" + innerBodyText; String expectedHtml = "<pre class=\"k9mail\">" + BODY_TEXT_HTML + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: " + "1px solid #000\">message.eml</p>" + "<table style=\"border: 0\">" + "<tr>" + "<th style=\"text-align: left; vertical-align: top;\">From:</th>" + "<td>[email protected]</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">To:</th>" + "<td>[email protected]</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Sent:</th>" + "<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Subject:</th>" + "<td>Subject</td>" + "</tr>" + "</table>" + "<pre class=\"k9mail\">" + innerBodyText + "</pre>"; assertEquals(expectedText, container.text); assertEquals(expectedHtml, getHtmlBodyText(container.html)); }