Search in sources :

Example 56 with Body

use of com.fsck.k9.mail.Body 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 57 with Body

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

the class MigrationTest method migrateMixedWithAttachments.

@Test
public void migrateMixedWithAttachments() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertMixedWithAttachments(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("4");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(3, msg.getId());
    Assert.assertEquals(8, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/mixed", msg.getMimeType());
    Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
    Assert.assertEquals("multipart/mixed", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], null));
    Assert.assertEquals("----5D6OUTIYLNN2X63O0R2M0V53TOUAQP", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary"));
    Assert.assertEquals(2, msg.getAttachmentCount());
    Multipart body = (Multipart) msg.getBody();
    Assert.assertEquals(3, body.getCount());
    Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
    LocalBodyPart attachmentPart = (LocalBodyPart) body.getBodyPart(1);
    Assert.assertEquals("image/png", attachmentPart.getMimeType());
    Assert.assertEquals("2", attachmentPart.getServerExtra());
    Assert.assertEquals("attachment", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), null));
    Assert.assertEquals("k9small.png", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), "filename"));
    Assert.assertEquals("2250", MimeUtility.getHeaderParameter(attachmentPart.getDisposition(), "size"));
    FileBackedBody attachmentBody = (FileBackedBody) attachmentPart.getBody();
    Assert.assertEquals(2250, attachmentBody.getSize());
    Assert.assertEquals(MimeUtil.ENC_BINARY, attachmentBody.getEncoding());
    Assert.assertEquals("application/whatevs", body.getBodyPart(2).getMimeType());
    Assert.assertNull(body.getBodyPart(2).getBody());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 58 with Body

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

the class MigrationTest method migratePgpMimeSignedMessage.

@Test
public void migratePgpMimeSignedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertPgpMimeSignedMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("5");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(4, msg.getId());
    Assert.assertEquals(8, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/mixed", msg.getMimeType());
    Assert.assertEquals(2, msg.getAttachmentCount());
    Multipart body = (Multipart) msg.getBody();
    Assert.assertEquals(3, body.getCount());
    Assert.assertEquals("multipart/alternative", body.getBodyPart(0).getMimeType());
    Assert.assertEquals("image/png", body.getBodyPart(1).getMimeType());
    Assert.assertEquals("application/pgp-signature", body.getBodyPart(2).getMimeType());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 59 with Body

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

the class MigrationTest method migratePgpMimeEncryptedMessage.

@Test
public void migratePgpMimeEncryptedMessage() throws Exception {
    SQLiteDatabase db = createV50Database();
    insertPgpMimeEncryptedMessage(db);
    db.close();
    LocalStore localStore = LocalStore.getInstance(account, RuntimeEnvironment.application);
    LocalMessage msg = localStore.getFolder("dev").getMessage("6");
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.BODY);
    localStore.getFolder("dev").fetch(Collections.singletonList(msg), fp, null);
    Assert.assertEquals(5, msg.getId());
    Assert.assertEquals(13, msg.getHeaderNames().size());
    Assert.assertEquals("multipart/encrypted", msg.getMimeType());
    Assert.assertEquals(2, msg.getAttachmentCount());
    Multipart body = (Multipart) msg.getBody();
    Assert.assertEquals(1, msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE).length);
    Assert.assertEquals("application/pgp-encrypted", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "protocol"));
    Assert.assertEquals("UoPmpPX/dBe4BELn", MimeUtility.getHeaderParameter(msg.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0], "boundary"));
    Assert.assertEquals("UoPmpPX/dBe4BELn", body.getBoundary());
    Assert.assertEquals(2, body.getCount());
    Assert.assertEquals("application/pgp-encrypted", body.getBodyPart(0).getMimeType());
    Assert.assertEquals("application/octet-stream", body.getBodyPart(1).getMimeType());
}
Also used : FetchProfile(com.fsck.k9.mail.FetchProfile) Multipart(com.fsck.k9.mail.Multipart) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Test(org.junit.Test)

Example 60 with Body

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

the class SmtpTransport method sendMessageTo.

private void sendMessageTo(List<String> addresses, Message message) throws MessagingException {
    close();
    open();
    if (!m8bitEncodingAllowed) {
        Log.d(LOG_TAG, "Server does not support 8bit transfer encoding");
    }
    // the size of messages, count the message's size before sending it
    if (mLargestAcceptableMessage > 0 && message.hasAttachments()) {
        if (message.calculateSize() > mLargestAcceptableMessage) {
            throw new MessagingException("Message too large for server", true);
        }
    }
    boolean entireMessageSent = false;
    Address[] from = message.getFrom();
    try {
        String fromAddress = from[0].getAddress();
        if (m8bitEncodingAllowed) {
            executeCommand("MAIL FROM:<%s> BODY=8BITMIME", fromAddress);
        } else {
            executeCommand("MAIL FROM:<%s>", fromAddress);
        }
        for (String address : addresses) {
            executeCommand("RCPT TO:<%s>", address);
        }
        executeCommand("DATA");
        EOLConvertingOutputStream msgOut = new EOLConvertingOutputStream(new LineWrapOutputStream(new SmtpDataStuffing(mOut), 1000));
        message.writeTo(msgOut);
        msgOut.endWithCrLfAndFlush();
        // After the "\r\n." is attempted, we may have sent the message
        entireMessageSent = true;
        executeCommand(".");
    } catch (NegativeSmtpReplyException e) {
        throw e;
    } catch (Exception e) {
        MessagingException me = new MessagingException("Unable to send message", e);
        me.setPermanentFailure(entireMessageSent);
        throw me;
    } finally {
        close();
    }
}
Also used : EOLConvertingOutputStream(com.fsck.k9.mail.filter.EOLConvertingOutputStream) SmtpDataStuffing(com.fsck.k9.mail.filter.SmtpDataStuffing) SocketAddress(java.net.SocketAddress) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Address(com.fsck.k9.mail.Address) Inet6Address(java.net.Inet6Address) LineWrapOutputStream(com.fsck.k9.mail.filter.LineWrapOutputStream) MessagingException(com.fsck.k9.mail.MessagingException) URISyntaxException(java.net.URISyntaxException) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) GeneralSecurityException(java.security.GeneralSecurityException) SSLException(javax.net.ssl.SSLException) SocketException(java.net.SocketException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException)

Aggregations

Body (com.fsck.k9.mail.Body)44 BodyPart (com.fsck.k9.mail.BodyPart)35 Multipart (com.fsck.k9.mail.Multipart)32 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)32 Part (com.fsck.k9.mail.Part)29 Test (org.junit.Test)29 TextBody (com.fsck.k9.mail.internet.TextBody)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)21 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)19 ArrayList (java.util.ArrayList)16 MessagingException (com.fsck.k9.mail.MessagingException)14 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Message (com.fsck.k9.mail.Message)9 OutputStream (java.io.OutputStream)9 Stack (java.util.Stack)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)7 InputStream (java.io.InputStream)7