From 025502ceb533878585cf34bc6a927109ae1ecf8b Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 12 Jan 2017 20:27:52 +0100 Subject: [PATCH 1/2] Don't crash on unknown content transfer encoding --- .../main/java/com/fsck/k9/mail/internet/MimeUtility.java | 6 +++++- .../UnsupportedContentTransferEncodingException.java | 9 --------- .../com/fsck/k9/mail/internet/MessageExtractorTest.java | 5 +++-- .../com/fsck/k9/mail/internet/MimeMessageParseTest.java | 6 ++++-- 4 files changed, 12 insertions(+), 14 deletions(-) delete mode 100644 k9mail-library/src/main/java/com/fsck/k9/mail/internet/UnsupportedContentTransferEncodingException.java diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java index 85544abec..a966af987 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java @@ -9,6 +9,7 @@ import java.util.Locale; import java.util.regex.Pattern; import android.support.annotation.NonNull; +import android.util.Log; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; @@ -21,6 +22,8 @@ import org.apache.james.mime4j.codec.Base64InputStream; import org.apache.james.mime4j.codec.QuotedPrintableInputStream; import org.apache.james.mime4j.util.MimeUtil; +import static com.fsck.k9.mail.K9MailLib.LOG_TAG; + public class MimeUtility { public static final String DEFAULT_ATTACHMENT_MIME_TYPE = "application/octet-stream"; @@ -1047,7 +1050,8 @@ public class MimeUtility { } }; } else { - throw new UnsupportedContentTransferEncodingException(encoding); + Log.w(LOG_TAG, "Unsupported encoding: " + encoding); + inputStream = rawInputStream; } } else { inputStream = body.getInputStream(); diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/UnsupportedContentTransferEncodingException.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/UnsupportedContentTransferEncodingException.java deleted file mode 100644 index 808d29194..000000000 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/UnsupportedContentTransferEncodingException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.fsck.k9.mail.internet; - -import com.fsck.k9.mail.MessagingException; - -public class UnsupportedContentTransferEncodingException extends MessagingException { - public UnsupportedContentTransferEncodingException(String encoding) { - super("Unsupported encoding: "+encoding); - } -} diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java index cb60c5316..e5c22cd6b 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java @@ -75,12 +75,13 @@ public class MessageExtractorTest { @Test public void getTextFromPart_withUnknownEncoding_shouldReturnNull() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); - BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), "unknown encoding"); + String bodyText = "Sample text body"; + BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), "unknown encoding"); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); - assertNull(result); + assertEquals(bodyText, result); } @Test diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java index 07363861e..e6152f1dd 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java @@ -138,7 +138,7 @@ public class MimeMessageParseTest { ""); } - @Test(expected = UnsupportedContentTransferEncodingException.class) + @Test public void testSinglePartUnknownEncoding_throwsUnsupportedEncodingException() throws Exception { MimeMessage msg = parseWithoutRecurse(toStream( "From: \r\n" + @@ -150,7 +150,9 @@ public class MimeMessageParseTest { "\r\n" + "dGhpcyBpcyBzb21lIG1vcmUgdGVzdCB0ZXh0Lg==\r\n")); - MimeUtility.decodeBody(msg.getBody()); + InputStream inputStream = MimeUtility.decodeBody(msg.getBody()); + + assertEquals("dGhpcyBpcyBzb21lIG1vcmUgdGVzdCB0ZXh0Lg==\r\n", streamToString(inputStream)); } @Test From 5f4fc19f1ded788728178697aa4e8e0967cc7709 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 13 Jan 2017 13:14:11 +0100 Subject: [PATCH 2/2] Fix test names --- .../java/com/fsck/k9/mail/internet/MessageExtractorTest.java | 2 +- .../java/com/fsck/k9/mail/internet/MimeMessageParseTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java index e5c22cd6b..5fa528b3d 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MessageExtractorTest.java @@ -73,7 +73,7 @@ public class MessageExtractorTest { } @Test - public void getTextFromPart_withUnknownEncoding_shouldReturnNull() throws Exception { + public void getTextFromPart_withUnknownEncoding_shouldReturnUnmodifiedBodyContents() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); String bodyText = "Sample text body"; BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), "unknown encoding"); diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java index e6152f1dd..0ac57dae6 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/internet/MimeMessageParseTest.java @@ -139,7 +139,7 @@ public class MimeMessageParseTest { } @Test - public void testSinglePartUnknownEncoding_throwsUnsupportedEncodingException() throws Exception { + public void decodeBody_withUnknownEncoding_shouldReturnUnmodifiedBodyContents() throws Exception { MimeMessage msg = parseWithoutRecurse(toStream( "From: \r\n" + "To: \r\n" +