diff options
| author | test@dangofactory.cos <you@example.com> | 2026-08-20 16:57:18 +0000 |
|---|---|---|
| committer | test@dangofactory.cos <you@example.com> | 2026-08-20 16:57:18 +0000 |
| commit | 50ce5d5e6cd0d720ff437d35eace545816848428 (patch) | |
| tree | b59af206a36265cf081945e434900b32b0c43edd /app/crypto-openpgp/src/test/java | |
| download | monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.tar.gz monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.tar.bz2 monocles_mail-50ce5d5e6cd0d720ff437d35eace545816848428.zip | |
Inital
Diffstat (limited to 'app/crypto-openpgp/src/test/java')
| -rw-r--r-- | app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/EncryptionDetectorTest.java | 109 | ||||
| -rw-r--r-- | app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/MessageCreationHelper.java | 51 |
2 files changed, 160 insertions, 0 deletions
diff --git a/app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/EncryptionDetectorTest.java b/app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/EncryptionDetectorTest.java new file mode 100644 index 0000000..992b2d1 --- /dev/null +++ b/app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/EncryptionDetectorTest.java @@ -0,0 +1,109 @@ +package com.fsck.k9.crypto.openpgp; + + +import com.fsck.k9.mail.Message; +import com.fsck.k9.message.extractors.TextPartFinder; +import org.junit.Before; +import org.junit.Test; + +import static com.fsck.k9.crypto.openpgp.MessageCreationHelper.createMessage; +import static com.fsck.k9.crypto.openpgp.MessageCreationHelper.createMultipartMessage; +import static com.fsck.k9.crypto.openpgp.MessageCreationHelper.createPart; +import static com.fsck.k9.crypto.openpgp.MessageCreationHelper.createTextMessage; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + + +public class EncryptionDetectorTest { + private static final String CRLF = "\r\n"; + + + private EncryptionDetector encryptionDetector; + + + @Before + public void setUp() { + encryptionDetector = new EncryptionDetector(new TextPartFinder()); + } + + @Test + public void isEncrypted_withTextPlain_shouldReturnFalse() { + Message message = createTextMessage("text/plain", "plain text"); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertFalse(encrypted); + } + + @Test + public void isEncrypted_withMultipartEncrypted_shouldReturnTrue() throws Exception { + Message message = createMultipartMessage("multipart/encrypted", + createPart("application/octet-stream"), createPart("application/octet-stream")); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertTrue(encrypted); + } + + @Test + public void isEncrypted_withSMimePart_shouldReturnTrue() { + Message message = createMessage("application/pkcs7-mime"); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertTrue(encrypted); + } + + @Test + public void isEncrypted_withMultipartMixedContainingSMimePart_shouldReturnTrue() throws Exception { + Message message = createMultipartMessage("multipart/mixed", + createPart("application/pkcs7-mime"), createPart("text/plain")); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertTrue(encrypted); + } + + @Test + public void isEncrypted_withInlinePgp_shouldReturnTrue() { + Message message = createTextMessage("text/plain", "" + + "-----BEGIN PGP MESSAGE-----" + CRLF + + "some encrypted stuff here" + CRLF + + "-----END PGP MESSAGE-----"); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertTrue(encrypted); + } + + @Test + public void isEncrypted_withPlainTextAndPreambleWithInlinePgp_shouldReturnFalse() { + Message message = createTextMessage("text/plain", "" + + "preamble" + CRLF + + "-----BEGIN PGP MESSAGE-----" + CRLF + + "some encrypted stuff here" + CRLF + + "-----END PGP MESSAGE-----" + CRLF + + "epilogue"); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertFalse(encrypted); + } + + @Test + public void isEncrypted_withQuotedInlinePgp_shouldReturnFalse() { + Message message = createTextMessage("text/plain", "" + + "good talk!" + CRLF + + CRLF + + "> -----BEGIN PGP MESSAGE-----" + CRLF + + "> some encrypted stuff here" + CRLF + + "> -----END PGP MESSAGE-----" + CRLF + + CRLF + + "-- " + CRLF + + "my signature"); + + boolean encrypted = encryptionDetector.isEncrypted(message); + + assertFalse(encrypted); + } +} diff --git a/app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/MessageCreationHelper.java b/app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/MessageCreationHelper.java new file mode 100644 index 0000000..c174718 --- /dev/null +++ b/app/crypto-openpgp/src/test/java/com/fsck/k9/crypto/openpgp/MessageCreationHelper.java @@ -0,0 +1,51 @@ +package com.fsck.k9.crypto.openpgp; + + +import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.BodyPart; +import com.fsck.k9.mail.Message; +import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.internet.MimeBodyPart; +import com.fsck.k9.mail.internet.MimeHeader; +import com.fsck.k9.mail.internet.MimeMessage; +import com.fsck.k9.mail.internet.MimeMultipart; +import com.fsck.k9.mail.internet.TextBody; +import com.fsck.k9.mailstore.BinaryMemoryBody; + + +public class MessageCreationHelper { + public static BodyPart createPart(String mimeType) throws MessagingException { + BinaryMemoryBody body = new BinaryMemoryBody(new byte[0], "utf-8"); + return new MimeBodyPart(body, mimeType); + } + + public static Message createTextMessage(String mimeType, String text) { + TextBody body = new TextBody(text); + return createMessage(mimeType, body); + } + + public static Message createMultipartMessage(String mimeType, BodyPart... parts) { + MimeMultipart body = createMultipartBody(mimeType, parts); + return createMessage(mimeType, body); + } + + public static Message createMessage(String mimeType) { + return createMessage(mimeType, null); + } + + private static Message createMessage(String mimeType, Body body) { + MimeMessage message = new MimeMessage(); + message.setBody(body); + message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType); + + return message; + } + + private static MimeMultipart createMultipartBody(String mimeType, BodyPart[] parts) { + MimeMultipart multipart = new MimeMultipart(mimeType, "boundary"); + for (BodyPart part : parts) { + multipart.addBodyPart(part); + } + return multipart; + } +} |
