Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5c638dd2 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

use MessageDecryptVerifier methods in EncryptionDetector

parent 22b72a58
Loading
Loading
Loading
Loading
+16 −12
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.fsck.k9.mail.Body;
@@ -128,7 +129,7 @@ public class MessageDecryptVerifier {
            Part part = partsToCheck.pop();
            Body body = part.getBody();

            if (isPartPgpInline(part)) {
            if (isPartPgpInlineEncryptedOrSigned(part)) {
                inlineParts.add(part);
                continue;
            }
@@ -162,17 +163,8 @@ public class MessageDecryptVerifier {
        return null;
    }

    private static boolean isPartPgpInline(Part part) {
        if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) {
            return false;
        }
        String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK);
        return !TextUtils.isEmpty(text) &&
                (text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER));
    }

    private static boolean isPartEncryptedOrSigned(Part part) {
        return isPartMultipartEncrypted(part) || isPartMultipartSigned(part) || isPartPgpInline(part);
        return isPartMultipartEncrypted(part) || isPartMultipartSigned(part) || isPartPgpInlineEncryptedOrSigned(part);
    }

    private static boolean isPartMultipartSigned(Part part) {
@@ -196,7 +188,19 @@ public class MessageDecryptVerifier {
        return isPgpEncrypted || isPgpSigned;
    }

    public static boolean isPartPgpInlineEncrypted(Part part) {
    private static boolean isPartPgpInlineEncryptedOrSigned(Part part) {
        if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) {
            return false;
        }
        String text = MessageExtractor.getTextFromPart(part, TEXT_LENGTH_FOR_INLINE_CHECK);
        return !TextUtils.isEmpty(text) &&
                (text.startsWith(PGP_INLINE_START_MARKER) || text.startsWith(PGP_INLINE_SIGNED_START_MARKER));
    }

    public static boolean isPartPgpInlineEncrypted(@Nullable Part part) {
        if (part == null) {
            return false;
        }
        if (!part.isMimeType(TEXT_PLAIN) && !part.isMimeType(APPLICATION_PGP)) {
            return false;
        }
+2 −21
Original line number Diff line number Diff line
package com.fsck.k9.message.extractors;


import java.util.regex.Pattern;

import android.support.annotation.NonNull;

import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor;

import static com.fsck.k9.mail.internet.MimeUtility.isSameMimeType;


class EncryptionDetector {
    private static final Pattern PGP_MESSAGE_PATTERN = Pattern.compile(
            "(^|\\r\\n)-----BEGIN PGP MESSAGE-----\\r\\n.*?\\r\\n-----END PGP MESSAGE-----(\\r\\n|$)", Pattern.DOTALL);


    private final TextPartFinder textPartFinder;


@@ -37,20 +31,7 @@ class EncryptionDetector {

    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();
    }

    private boolean isUsableTextPart(Part textPart) {
        return textPart != null && textPart.getBody() != null;
        return MessageDecryptVerifier.isPartPgpInlineEncrypted(textPart);
    }

    private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) {
+7 −3
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@ package com.fsck.k9.message.extractors;
import com.fsck.k9.mail.Message;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static com.fsck.k9.message.MessageCreationHelper.createMessage;
import static com.fsck.k9.message.MessageCreationHelper.createMultipartMessage;
@@ -14,7 +17,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


@RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml", sdk = 21)
public class EncryptionDetectorTest {
    private static final String CRLF = "\r\n";

@@ -82,7 +86,7 @@ public class EncryptionDetectorTest {
    }

    @Test
    public void isEncrypted_withPlainTextAndInlinePgp_shouldReturnTrue() throws Exception {
    public void isEncrypted_withPlainTextAndPreambleWithInlinePgp_shouldReturnFalse() throws Exception {
        Message message = createTextMessage("text/plain", "" +
                "preamble" + CRLF +
                "-----BEGIN PGP MESSAGE-----" + CRLF +
@@ -93,7 +97,7 @@ public class EncryptionDetectorTest {

        boolean encrypted = encryptionDetector.isEncrypted(message);

        assertTrue(encrypted);
        assertFalse(encrypted);
    }

    @Test