Search in sources :

Example 6 with ComposeCryptoStatus

use of com.fsck.k9.activity.compose.ComposeCryptoStatus in project k-9 by k9mail.

the class PgpMessageBuilderTest method buildSignWithAttach__withInlineEnabled__shouldThrow.

@Test
public void buildSignWithAttach__withInlineEnabled__shouldThrow() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY).setEnablePgpInline(true).build();
    pgpMessageBuilder.setCryptoStatus(cryptoStatus);
    pgpMessageBuilder.setAttachments(Collections.singletonList(Attachment.createAttachment(null, 0, null)));
    Callback mockCallback = mock(Callback.class);
    pgpMessageBuilder.buildAsync(mockCallback);
    verify(mockCallback).onMessageBuildException(any(MessagingException.class));
    verifyNoMoreInteractions(mockCallback);
    verifyNoMoreInteractions(openPgpApi);
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) MessagingException(com.fsck.k9.mail.MessagingException) ComposeCryptoStatus(com.fsck.k9.activity.compose.ComposeCryptoStatus) Test(org.junit.Test)

Example 7 with ComposeCryptoStatus

use of com.fsck.k9.activity.compose.ComposeCryptoStatus in project k-9 by k9mail.

the class PgpMessageBuilderTest method buildEncrypt__shouldSucceed.

@Test
public void buildEncrypt__shouldSucceed() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setCryptoMode(CryptoMode.PRIVATE).setRecipients(Collections.singletonList(new Recipient("test", "test@example.org", "labru", -1, "key"))).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_AND_ENCRYPT);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, new long[] { TEST_SELF_ENCRYPT_KEY_ID });
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_ENCRYPT_OPPORTUNISTIC, false);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_USER_IDS, cryptoStatus.getRecipientAddresses());
    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 multipart/encrypted", "multipart/encrypted", message.getMimeType());
    MimeMultipart multipart = (MimeMultipart) message.getBody();
    Assert.assertEquals("multipart/encrypted must consist of two parts", 2, multipart.getCount());
    BodyPart dummyBodyPart = multipart.getBodyPart(0);
    Assert.assertEquals("first part must be pgp encrypted dummy part", "application/pgp-encrypted", dummyBodyPart.getContentType());
    assertContentOfBodyPartEquals("content must match the supplied detached signature", dummyBodyPart, "Version: 1");
    BodyPart encryptedBodyPart = multipart.getBodyPart(1);
    Assert.assertEquals("second part must be octet-stream of encrypted data", "application/octet-stream; name=\"encrypted.asc\"", encryptedBodyPart.getContentType());
    Assert.assertTrue("message body must be BinaryTempFileBody", encryptedBodyPart.getBody() instanceof BinaryTempFileBody);
    Assert.assertEquals(MimeUtil.ENC_7BIT, ((BinaryTempFileBody) encryptedBodyPart.getBody()).getEncoding());
}
Also used : BodyPart(com.fsck.k9.mail.BodyPart) Callback(com.fsck.k9.message.MessageBuilder.Callback) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) 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)

Example 8 with ComposeCryptoStatus

use of com.fsck.k9.activity.compose.ComposeCryptoStatus in project k-9 by k9mail.

the class PgpMessageBuilderTest method buildEncrypt__withInlineEnabled__shouldSucceed.

@Test
public void buildEncrypt__withInlineEnabled__shouldSucceed() throws MessagingException {
    ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder.setCryptoMode(CryptoMode.PRIVATE).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_AND_ENCRYPT);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_SIGN_KEY_ID);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, new long[] { TEST_SELF_ENCRYPT_KEY_ID });
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_ENCRYPT_OPPORTUNISTIC, false);
    expectedApiIntent.putExtra(OpenPgpApi.EXTRA_USER_IDS, cryptoStatus.getRecipientAddresses());
    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("text/plain", message.getMimeType());
    Assert.assertTrue("message body must be BinaryTempFileBody", message.getBody() instanceof BinaryTempFileBody);
    Assert.assertEquals(MimeUtil.ENC_7BIT, ((BinaryTempFileBody) message.getBody()).getEncoding());
}
Also used : Callback(com.fsck.k9.message.MessageBuilder.Callback) BinaryTempFileBody(com.fsck.k9.mail.internet.BinaryTempFileBody) 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)

Example 9 with ComposeCryptoStatus

use of com.fsck.k9.activity.compose.ComposeCryptoStatus in project k-9 by k9mail.

the class MessageCompose method createMessageBuilder.

@Nullable
private MessageBuilder createMessageBuilder(boolean isDraft) {
    MessageBuilder builder;
    recipientPresenter.updateCryptoStatus();
    ComposeCryptoStatus cryptoStatus = recipientPresenter.getCurrentCryptoStatus();
    // TODO encrypt drafts for storage
    if (!isDraft && cryptoStatus.shouldUsePgpMessageBuilder()) {
        SendErrorState maybeSendErrorState = cryptoStatus.getSendErrorStateOrNull();
        if (maybeSendErrorState != null) {
            recipientPresenter.showPgpSendError(maybeSendErrorState);
            return null;
        }
        PgpMessageBuilder pgpBuilder = PgpMessageBuilder.newInstance();
        recipientPresenter.builderSetProperties(pgpBuilder);
        builder = pgpBuilder;
    } else {
        builder = SimpleMessageBuilder.newInstance();
    }
    builder.setSubject(Utility.stripNewLines(subjectView.getText().toString())).setSentDate(new Date()).setHideTimeZone(K9.hideTimeZone()).setTo(recipientPresenter.getToAddresses()).setCc(recipientPresenter.getCcAddresses()).setBcc(recipientPresenter.getBccAddresses()).setInReplyTo(repliedToMessageId).setReferences(referencedMessageIds).setRequestReadReceipt(requestReadReceipt).setIdentity(identity).setMessageFormat(currentMessageFormat).setText(messageContentView.getCharacters()).setAttachments(attachmentPresenter.createAttachmentList()).setSignature(signatureView.getCharacters()).setSignatureBeforeQuotedText(account.isSignatureBeforeQuotedText()).setIdentityChanged(identityChanged).setSignatureChanged(signatureChanged).setCursorPosition(messageContentView.getSelectionStart()).setMessageReference(relatedMessageReference).setDraft(isDraft).setIsPgpInlineEnabled(cryptoStatus.isPgpInlineModeEnabled());
    quotedMessagePresenter.builderSetProperties(builder);
    return builder;
}
Also used : SendErrorState(com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState) PgpMessageBuilder(com.fsck.k9.message.PgpMessageBuilder) SimpleMessageBuilder(com.fsck.k9.message.SimpleMessageBuilder) MessageBuilder(com.fsck.k9.message.MessageBuilder) PgpMessageBuilder(com.fsck.k9.message.PgpMessageBuilder) ComposeCryptoStatus(com.fsck.k9.activity.compose.ComposeCryptoStatus) Date(java.util.Date) Nullable(android.support.annotation.Nullable)

Aggregations

ComposeCryptoStatus (com.fsck.k9.activity.compose.ComposeCryptoStatus)8 Callback (com.fsck.k9.message.MessageBuilder.Callback)7 Test (org.junit.Test)7 PendingIntent (android.app.PendingIntent)5 Intent (android.content.Intent)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 OpenPgpDataSource (org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource)5 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)4 Recipient (com.fsck.k9.view.RecipientSelectView.Recipient)4 MessagingException (com.fsck.k9.mail.MessagingException)3 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)2 Nullable (android.support.annotation.Nullable)1 ComposeCryptoStatusBuilder (com.fsck.k9.activity.compose.ComposeCryptoStatus.ComposeCryptoStatusBuilder)1 SendErrorState (com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState)1 BodyPart (com.fsck.k9.mail.BodyPart)1 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)1 MessageBuilder (com.fsck.k9.message.MessageBuilder)1 PgpMessageBuilder (com.fsck.k9.message.PgpMessageBuilder)1 SimpleMessageBuilder (com.fsck.k9.message.SimpleMessageBuilder)1