From a41e75f5039e05bdddbcb1c02ebbf3d99f3ec394 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 29 Aug 2017 04:14:09 +0200 Subject: [PATCH] add autocrypt checks to MessageCryptoHelper tests --- .../fsck/k9/activity/MessageLoaderHelper.java | 4 +- .../k9/autocrypt/AutocryptOperations.java | 11 ++++- .../k9/ui/crypto/MessageCryptoHelper.java | 5 ++- .../k9/ui/crypto/MessageCryptoHelperTest.java | 45 ++++++++++++++++++- 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java index 70248f61c..54787eb63 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageLoaderHelper.java @@ -14,6 +14,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.UiThread; +import com.fsck.k9.autocrypt.AutocryptOperations; import com.fsck.k9.ui.crypto.OpenPgpApiFactory; import timber.log.Timber; @@ -270,7 +271,8 @@ public class MessageLoaderHelper { messageCryptoHelper = retainCryptoHelperFragment.getData(); } if (messageCryptoHelper == null || messageCryptoHelper.isConfiguredForOutdatedCryptoProvider()) { - messageCryptoHelper = new MessageCryptoHelper(context, new OpenPgpApiFactory()); + messageCryptoHelper = new MessageCryptoHelper( + context, new OpenPgpApiFactory(), AutocryptOperations.getInstance()); retainCryptoHelperFragment.setData(messageCryptoHelper); } messageCryptoHelper.asyncStartOrResumeProcessingMessage( diff --git a/k9mail/src/main/java/com/fsck/k9/autocrypt/AutocryptOperations.java b/k9mail/src/main/java/com/fsck/k9/autocrypt/AutocryptOperations.java index 6cdc6baa5..fc578d8f1 100644 --- a/k9mail/src/main/java/com/fsck/k9/autocrypt/AutocryptOperations.java +++ b/k9mail/src/main/java/com/fsck/k9/autocrypt/AutocryptOperations.java @@ -13,8 +13,15 @@ import org.openintents.openpgp.util.OpenPgpApi; public class AutocryptOperations { private final AutocryptHeaderParser autocryptHeaderParser; - public AutocryptOperations() { - this.autocryptHeaderParser = AutocryptHeaderParser.getInstance(); + + public static AutocryptOperations getInstance() { + AutocryptHeaderParser autocryptHeaderParser = AutocryptHeaderParser.getInstance(); + return new AutocryptOperations(autocryptHeaderParser); + } + + + private AutocryptOperations(AutocryptHeaderParser autocryptHeaderParser) { + this.autocryptHeaderParser = autocryptHeaderParser; } public boolean addAutocryptPeerUpdateToIntentIfPresent(Message currentMessage, Intent intent) { diff --git a/k9mail/src/main/java/com/fsck/k9/ui/crypto/MessageCryptoHelper.java b/k9mail/src/main/java/com/fsck/k9/ui/crypto/MessageCryptoHelper.java index 05f587aab..751c76853 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/crypto/MessageCryptoHelper.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/crypto/MessageCryptoHelper.java @@ -90,15 +90,16 @@ public class MessageCryptoHelper { private OpenPgpApiFactory openPgpApiFactory; - public MessageCryptoHelper(Context context, OpenPgpApiFactory openPgpApiFactory) { + public MessageCryptoHelper(Context context, OpenPgpApiFactory openPgpApiFactory, + AutocryptOperations autocryptOperations) { this.context = context.getApplicationContext(); if (!K9.isOpenPgpProviderConfigured()) { throw new IllegalStateException("MessageCryptoHelper must only be called with a OpenPGP provider!"); } + this.autocryptOperations = autocryptOperations; this.openPgpApiFactory = openPgpApiFactory; - autocryptOperations = new AutocryptOperations(); openPgpProviderPackage = K9.getOpenPgpProvider(); } diff --git a/k9mail/src/test/java/com/fsck/k9/ui/crypto/MessageCryptoHelperTest.java b/k9mail/src/test/java/com/fsck/k9/ui/crypto/MessageCryptoHelperTest.java index 8915b62e3..6f102d79c 100644 --- a/k9mail/src/test/java/com/fsck/k9/ui/crypto/MessageCryptoHelperTest.java +++ b/k9mail/src/test/java/com/fsck/k9/ui/crypto/MessageCryptoHelperTest.java @@ -1,6 +1,7 @@ package com.fsck.k9.ui.crypto; +import java.io.InputStream; import java.io.OutputStream; import android.app.PendingIntent; @@ -8,6 +9,7 @@ import android.content.Context; import android.content.Intent; import com.fsck.k9.K9; +import com.fsck.k9.autocrypt.AutocryptOperations; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; @@ -27,6 +29,7 @@ import org.openintents.openpgp.IOpenPgpService2; import org.openintents.openpgp.OpenPgpDecryptionResult; import org.openintents.openpgp.OpenPgpSignatureResult; import org.openintents.openpgp.util.OpenPgpApi; +import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback; import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpSinkResultCallback; import org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSink; import org.openintents.openpgp.util.OpenPgpApi.OpenPgpDataSource; @@ -38,6 +41,8 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertSame; import static junit.framework.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.same; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -54,17 +59,21 @@ public class MessageCryptoHelperTest { private Intent capturedApiIntent; private IOpenPgpSinkResultCallback capturedCallback; private MessageCryptoCallback messageCryptoCallback; + private AutocryptOperations autocryptOperations; @Before public void setUp() throws Exception { openPgpApi = mock(OpenPgpApi.class); + autocryptOperations = mock(AutocryptOperations.class); K9.setOpenPgpProvider("org.example.dummy"); OpenPgpApiFactory openPgpApiFactory = mock(OpenPgpApiFactory.class); when(openPgpApiFactory.createOpenPgpApi(any(Context.class), any(IOpenPgpService2.class))).thenReturn(openPgpApi); - messageCryptoHelper = new MessageCryptoHelper(RuntimeEnvironment.application, openPgpApiFactory); + + messageCryptoHelper = new MessageCryptoHelper(RuntimeEnvironment.application, openPgpApiFactory, + autocryptOperations); messageCryptoCallback = mock(MessageCryptoCallback.class); } @@ -82,6 +91,35 @@ public class MessageCryptoHelperTest { MessageCryptoAnnotations annotations = captor.getValue(); assertTrue(annotations.isEmpty()); verifyNoMoreInteractions(messageCryptoCallback); + + verify(autocryptOperations).hasAutocryptHeader(message); + verifyNoMoreInteractions(autocryptOperations); + } + + @Test + public void textPlain_withAutocrypt() throws Exception { + MimeMessage message = new MimeMessage(); + message.setUid("msguid"); + message.setHeader("Content-Type", "text/plain"); + + when(autocryptOperations.hasAutocryptHeader(message)).thenReturn(true); + when(autocryptOperations.addAutocryptPeerUpdateToIntentIfPresent(same(message), any(Intent.class))).thenReturn(true); + + + MessageCryptoCallback messageCryptoCallback = mock(MessageCryptoCallback.class); + messageCryptoHelper.asyncStartOrResumeProcessingMessage(message, messageCryptoCallback, null); + + + ArgumentCaptor captor = ArgumentCaptor.forClass(MessageCryptoAnnotations.class); + verify(messageCryptoCallback).onCryptoOperationsFinished(captor.capture()); + MessageCryptoAnnotations annotations = captor.getValue(); + assertTrue(annotations.isEmpty()); + verifyNoMoreInteractions(messageCryptoCallback); + + ArgumentCaptor intentCaptor = ArgumentCaptor.forClass(Intent.class); + verify(autocryptOperations).addAutocryptPeerUpdateToIntentIfPresent(same(message), intentCaptor.capture()); + verify(openPgpApi).executeApiAsync(same(intentCaptor.getValue()), same((InputStream) null), + same((OutputStream) null), any(IOpenPgpCallback.class)); } @Test @@ -161,6 +199,9 @@ public class MessageCryptoHelperTest { assertEquals(OpenPgpApi.ACTION_DECRYPT_VERIFY, capturedApiIntent.getAction()); assertEquals("test@example.org", capturedApiIntent.getStringExtra(OpenPgpApi.EXTRA_SENDER_ADDRESS)); + + verify(autocryptOperations).addAutocryptPeerUpdateToIntentIfPresent(message, capturedApiIntent); + verifyNoMoreInteractions(autocryptOperations); } @Test @@ -201,6 +242,8 @@ public class MessageCryptoHelperTest { assertEquals("test@example.org", capturedApiIntent.getStringExtra(OpenPgpApi.EXTRA_SENDER_ADDRESS)); assertPartAnnotationHasState(message, messageCryptoCallback, CryptoError.OPENPGP_OK, decryptedPart, decryptionResult, signatureResult, pendingIntent); + verify(autocryptOperations).addAutocryptPeerUpdateToIntentIfPresent(message, capturedApiIntent); + verifyNoMoreInteractions(autocryptOperations); } private void processEncryptedMessageAndCaptureMocks(Message message, Body encryptedBody, OutputStream outputStream)