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

Commit d220b29a authored by Philip Whitehouse's avatar Philip Whitehouse
Browse files

Handle e-mails with unsupported encoding

parent 4d61ca8f
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -59,6 +59,8 @@ public class MessageExtractor {
            } else {
                throw new MessagingException("Provided invalid part: " + part);
            }
        } catch (UnsupportedEncodingException e) {
            Log.e(LOG_TAG, "Unable to getTextFromPart", e);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Unable to getTextFromPart", e);
        } catch (MessagingException e) {
@@ -68,7 +70,7 @@ public class MessageExtractor {
    }

    private static String getTextFromTextPart(Part part, Body body, String mimeType, long textSizeLimit)
            throws IOException, MessagingException {
            throws IOException, MessagingException, UnsupportedEncodingException {
        /*
         * We've got a text part, so let's see if it needs to be processed further.
         */
+2 −2
Original line number Diff line number Diff line
@@ -1018,7 +1018,7 @@ public class MimeUtility {
     * The ultimate goal is to get to a point where all classes retain the original data and {@code RawDataBody} can be
     * merged into {@link Body}.
     */
    public static InputStream decodeBody(Body body) throws MessagingException {
    public static InputStream decodeBody(Body body) throws MessagingException, UnsupportedEncodingException {
        InputStream inputStream;
        if (body instanceof RawDataBody) {
            RawDataBody rawDataBody = (RawDataBody) body;
@@ -1044,7 +1044,7 @@ public class MimeUtility {
                    }
                };
            } else {
                throw new UnsupportedOperationException("Encoding for RawDataBody not supported: " + encoding);
                throw new UnsupportedEncodingException(encoding);
            }
        } else {
            inputStream = body.getInputStream();
+7 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.internet;

public class UnsupportedEncodingException extends Exception {
    public UnsupportedEncodingException(String encoding) {
        super("Unsupported encoding: "+encoding);
    }
}
+17 −0
Original line number Diff line number Diff line
@@ -128,6 +128,23 @@ public class MimeMessageParseTest {
                        "");
    }

    @Test(expected = UnsupportedEncodingException.class)
    public void testSinglePartUnknownEncoding_throwsUnsupportedEncodingException() throws Exception {
        MimeMessage msg = parseWithoutRecurse(toStream(
                "From: <adam@example.org>\r\n" +
                        "To: <eva@example.org>\r\n" +
                        "Subject: Testmail\r\n" +
                        "MIME-Version: 1.0\r\n" +
                        "Content-type: text/plain\r\n" +
                        "Content-Transfer-Encoding: utf-8\r\n" +
                        "\r\n" +
                        "dGhpcyBpcyBzb21lIG1vcmUgdGVzdCB0ZXh0Lg==\r\n"));

        checkAddresses(msg.getFrom(), "adam@example.org");
        checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org");
        streamToString(MimeUtility.decodeBody(msg.getBody()));
    }

    @Test
    public void testMultipartSingleLayerRecurse() throws Exception {
        MimeMessage msg = parseWithRecurse(toStream(
+2 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMultipart;
import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mail.internet.TextBody;
import com.fsck.k9.mail.internet.UnsupportedEncodingException;
import com.fsck.k9.message.MessageBuilder.Callback;
import com.fsck.k9.view.RecipientSelectView.Recipient;
import org.apache.commons.io.IOUtils;
@@ -507,7 +508,7 @@ public class PgpMessageBuilderTest {
            InputStream inputStream = MimeUtility.decodeBody(signatureBodyPart.getBody());
            IOUtils.copy(inputStream, bos);
            Assert.assertEquals(reason, expected, new String(bos.toByteArray()));
        } catch (IOException | MessagingException e) {
        } catch (IOException | MessagingException | UnsupportedEncodingException e) {
            Assert.fail();
        }
    }