Search in sources :

Example 6 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class MessageDecryptVerifierTest method findEncryptedPartsShouldReturnEmptyListForSimpleMessage.

@Test
public void findEncryptedPartsShouldReturnEmptyListForSimpleMessage() throws Exception {
    MimeMessage message = new MimeMessage();
    message.setBody(new TextBody("message text"));
    List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
    assertEquals(0, encryptedParts.size());
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 7 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class MessageDecryptVerifierTest method findEncryptedPartsShouldReturnMultipleEncryptedParts.

@Test
public void findEncryptedPartsShouldReturnMultipleEncryptedParts() throws Exception {
    MimeMessage message = new MimeMessage();
    MimeMultipart multipartMixed = MimeMultipart.newInstance();
    multipartMixed.setSubType("mixed");
    MimeMessageHelper.setBody(message, multipartMixed);
    MimeMultipart multipartEncryptedOne = MimeMultipart.newInstance();
    multipartEncryptedOne.setSubType("encrypted");
    MimeBodyPart bodyPartOne = new MimeBodyPart(multipartEncryptedOne);
    setContentTypeWithProtocol(bodyPartOne, MIME_TYPE_MULTIPART_ENCRYPTED, PROTCOL_PGP_ENCRYPTED);
    multipartMixed.addBodyPart(bodyPartOne);
    MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain");
    multipartMixed.addBodyPart(bodyPartTwo);
    MimeMultipart multipartEncryptedThree = MimeMultipart.newInstance();
    multipartEncryptedThree.setSubType("encrypted");
    MimeBodyPart bodyPartThree = new MimeBodyPart(multipartEncryptedThree);
    setContentTypeWithProtocol(bodyPartThree, MIME_TYPE_MULTIPART_ENCRYPTED, PROTCOL_PGP_ENCRYPTED);
    multipartMixed.addBodyPart(bodyPartThree);
    List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
    assertEquals(2, encryptedParts.size());
    assertSame(bodyPartOne, encryptedParts.get(0));
    assertSame(bodyPartThree, encryptedParts.get(1));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 8 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testMultipartDigestWithMessages.

@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));
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) MessageHeader(com.fsck.k9.mail.internet.Viewable.MessageHeader) Test(org.junit.Test)

Example 9 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class PgpMessageBuilderTest method buildOpportunisticEncrypt__withNoKeysAndNoSignOnly__shouldNotBeSigned.

@Test
public void buildOpportunisticEncrypt__withNoKeysAndNoSignOnly__shouldNotBeSigned() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key"))).setCryptoMode(CryptoMode.OPPORTUNISTIC).build();
    pgpMessageBuilder.setCryptoStatus(cryptoStatus);
    Intent returnIntent = new Intent();
    returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
    returnIntent.putExtra(OpenPgpApi.RESULT_ERROR, new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "Missing keys"));
    when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent);
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
    verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
    verifyNoMoreInteractions(mockCallback);
    MimeMessage message = captor.getValue();
    Assert.assertEquals("text/plain", message.getMimeType());
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ComposeCryptoStatus(com.fsck.k9.activity.compose.ComposeCryptoStatus) Recipient(com.fsck.k9.view.RecipientSelectView.Recipient) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) OpenPgpError(org.openintents.openpgp.OpenPgpError) Test(org.junit.Test)

Example 10 with MimeMessage

use of com.fsck.k9.mail.internet.MimeMessage in project k-9 by k9mail.

the class PgpMessageBuilderTest method buildSign__withInlineEnabled__shouldSucceed.

@Test
public void buildSign__withInlineEnabled__shouldSucceed() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY).setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key"))).setEnablePgpInline(true).build();
    pgpMessageBuilder.setCryptoStatus(cryptoStatus);
    ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class);
    Intent returnIntent = new Intent();
    returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
    when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent);
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    Intent expectedApiIntent = new Intent(OpenPgpApi.ACTION_SIGN);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    assertIntentEqualsActionAndExtras(expectedApiIntent, capturedApiIntent.getValue());
    ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class);
    verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false));
    verifyNoMoreInteractions(mockCallback);
    MimeMessage message = captor.getValue();
    Assert.assertEquals("message must be text/plain", "text/plain", message.getMimeType());
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ComposeCryptoStatus(com.fsck.k9.activity.compose.ComposeCryptoStatus) Recipient(com.fsck.k9.view.RecipientSelectView.Recipient) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) OpenPgpDataSource(org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource) Test(org.junit.Test)

Aggregations

MimeMessage (com.fsck.k9.mail.internet.MimeMessage)44 Test (org.junit.Test)35 TextBody (com.fsck.k9.mail.internet.TextBody)16 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)14 Part (com.fsck.k9.mail.Part)11 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 ArrayList (java.util.ArrayList)9 BodyPart (com.fsck.k9.mail.BodyPart)8 Message (com.fsck.k9.mail.Message)7 Viewable (com.fsck.k9.mail.internet.Viewable)7 ViewableExtractedText (com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText)7 Intent (android.content.Intent)6 OutputStream (java.io.OutputStream)6 PendingIntent (android.app.PendingIntent)5 Callback (com.fsck.k9.message.MessageBuilder.Callback)5 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)5 ComposeCryptoStatus (com.fsck.k9.activity.compose.ComposeCryptoStatus)4 Address (com.fsck.k9.mail.Address)4 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)4