From 499fc762ee4aea0838655adb4bc4f065062ca866 Mon Sep 17 00:00:00 2001 From: Sang-Jun Park Date: Wed, 2 Feb 2011 08:16:13 +0900 Subject: [PATCH] Fix a decoding problem on the Email body When decoding the Email body, email application used google default base64 function. But it caused problem, so particular email did not sync any more. So we have changed decoding function as a decoder from apache in order not to occur problem Change-Id: I7581123f21fbb4015153ca6f4a0c14c0f6a769fc Signed-off-by: Sang-Jun Park --- .../email/mail/internet/MimeUtility.java | 5 ++-- .../email/mail/internet/MimeUtilityTest.java | 29 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/com/android/email/mail/internet/MimeUtility.java b/src/com/android/email/mail/internet/MimeUtility.java index 47cd6152e..008328a44 100644 --- a/src/com/android/email/mail/internet/MimeUtility.java +++ b/src/com/android/email/mail/internet/MimeUtility.java @@ -26,13 +26,12 @@ import com.android.email.mail.Part; import org.apache.commons.io.IOUtils; import org.apache.james.mime4j.codec.EncoderUtil; +import org.apache.james.mime4j.decoder.Base64InputStream; import org.apache.james.mime4j.decoder.DecoderUtil; import org.apache.james.mime4j.decoder.QuotedPrintableInputStream; import org.apache.james.mime4j.util.CharsetUtil; import android.util.Log; -import android.util.Base64; -import android.util.Base64InputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -361,7 +360,7 @@ public class MimeUtility { in = new QuotedPrintableInputStream(in); } else if ("base64".equalsIgnoreCase(contentTransferEncoding)) { - in = new Base64InputStream(in, Base64.DEFAULT); + in = new Base64InputStream(in); } } diff --git a/tests/src/com/android/email/mail/internet/MimeUtilityTest.java b/tests/src/com/android/email/mail/internet/MimeUtilityTest.java index 8052a62df..b0a82fe2b 100644 --- a/tests/src/com/android/email/mail/internet/MimeUtilityTest.java +++ b/tests/src/com/android/email/mail/internet/MimeUtilityTest.java @@ -24,6 +24,10 @@ import com.android.email.mail.Part; import com.android.email.mail.MessageTestUtils.MessageBuilder; import com.android.email.mail.MessageTestUtils.MultipartBuilder; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + import android.test.suitebuilder.annotation.SmallTest; import junit.framework.TestCase; @@ -49,6 +53,16 @@ public class MimeUtilityTest extends TestCase { /** a string without any unicode */ private final String SHORT_PLAIN = "abcd"; + + /** Good base64 string */ + private final String GOOD_BASE64 = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + /** Bad base64 string */ + private final String BAD_BASE64 = "CgpTZW50IGZyb20gU2Ftc3VuZyB0YWJsZXQ="; + + /** Encoding type */ + private final String BASE64 = "base64"; /** long subject which will be split into two MIME/Base64 chunks */ private final String LONG_UNICODE_SPLIT = @@ -501,7 +515,20 @@ public class MimeUtilityTest extends TestCase { assertTrue(MimeUtility.mimeTypeMatches("match/this", arrayTwo)); } - // TODO: tests for decodeBody(InputStream in, String contentTransferEncoding) + // TODO: tests for decodeBody(InputStream in, String contentTransferEncoding) + + /** + * Confirm that decodeBody() does not crash when + * given bad Base64 data. + */ + public void testDecodeBody_BadBase64() throws IOException { + InputStream in = new ByteArrayInputStream(GOOD_BASE64.getBytes("us-ascii")); + assertNotNull(MimeUtility.decodeBody(in, BASE64)); + + in = new ByteArrayInputStream(BAD_BASE64.getBytes("us-ascii")); + assertNotNull(MimeUtility.decodeBody(in, BASE64)); + } + // TODO: tests for collectParts(Part part, ArrayList viewables, ArrayList attachments) }