diff --git a/src/com/android/email/mail/internet/MimeMessage.java b/src/com/android/email/mail/internet/MimeMessage.java
index 27d7aadb4..371c5b1de 100644
--- a/src/com/android/email/mail/internet/MimeMessage.java
+++ b/src/com/android/email/mail/internet/MimeMessage.java
@@ -247,7 +247,8 @@ public class MimeMessage extends Message {
}
public void setSubject(String subject) throws MessagingException {
- setHeader("Subject", subject);
+ final int HEADER_NAME_LENGTH = 9; // "Subject: "
+ setHeader("Subject", MimeUtility.foldAndEncode2(subject, HEADER_NAME_LENGTH));
}
public Address[] getFrom() throws MessagingException {
diff --git a/src/com/android/email/mail/internet/MimeUtility.java b/src/com/android/email/mail/internet/MimeUtility.java
index 7aa740221..008b60adf 100644
--- a/src/com/android/email/mail/internet/MimeUtility.java
+++ b/src/com/android/email/mail/internet/MimeUtility.java
@@ -25,6 +25,7 @@ import com.android.email.mail.Multipart;
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;
@@ -37,14 +38,27 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MimeUtility {
+
+ private final static Pattern PATTERN_CR_OR_LF = Pattern.compile("\r|\n");
+
+ /**
+ * Replace sequences of CRLF+WSP with WSP. Tries to preserve original string
+ * object whenever possible.
+ */
public static String unfold(String s) {
if (s == null) {
return null;
}
- return s.replaceAll("\r|\n", "");
+ Matcher patternMatcher = PATTERN_CR_OR_LF.matcher(s);
+ if (patternMatcher.find()) {
+ patternMatcher.reset();
+ s = patternMatcher.replaceAll("");
+ }
+ return s;
}
public static String decode(String s) {
@@ -59,9 +73,99 @@ public class MimeUtility {
}
// TODO implement proper foldAndEncode
+ // NOTE: When this really works, we *must* remove all calls to foldAndEncode2() to prevent
+ // duplication of encoding.
public static String foldAndEncode(String s) {
return s;
}
+
+ /**
+ * INTERIM version of foldAndEncode that will be used only by Subject: headers.
+ * This is safer than implementing foldAndEncode() (see above) and risking unknown damage
+ * to other headers.
+ *
+ * TODO: Copy this code to foldAndEncode(), get rid of this function, confirm all working OK.
+ *
+ * @param s original string to encode and fold
+ * @param usedCharacters number of characters already used up by header name
+
+ * @return the String ready to be transmitted
+ */
+ public static String foldAndEncode2(String s, int usedCharacters) {
+ // james.mime4j.codec.EncoderUtil.java
+ // encode: encodeIfNecessary(text, usage, numUsedInHeaderName)
+ // Usage.TEXT_TOKENlooks like the right thing for subjects
+ // use WORD_ENTITY for address/names
+
+ String encoded = EncoderUtil.encodeIfNecessary(s, EncoderUtil.Usage.TEXT_TOKEN,
+ usedCharacters);
+
+ return fold(encoded, usedCharacters);
+ }
+
+ /**
+ * INTERIM: From newer version of org.apache.james (but we don't want to import
+ * the entire MimeUtil class).
+ *
+ * Splits the specified string into a multiple-line representation with
+ * lines no longer than 76 characters (because the line might contain
+ * encoded words; see RFC
+ * 2047 section 2). If the string contains non-whitespace sequences
+ * longer than 76 characters a line break is inserted at the whitespace
+ * character following the sequence resulting in a line longer than 76
+ * characters.
+ *
+ * @param s
+ * string to split.
+ * @param usedCharacters
+ * number of characters already used up. Usually the number of
+ * characters for header field name plus colon and one space.
+ * @return a multiple-line representation of the given string.
+ */
+ public static String fold(String s, int usedCharacters) {
+ final int maxCharacters = 76;
+
+ final int length = s.length();
+ if (usedCharacters + length <= maxCharacters)
+ return s;
+
+ StringBuilder sb = new StringBuilder();
+
+ int lastLineBreak = -usedCharacters;
+ int wspIdx = indexOfWsp(s, 0);
+ while (true) {
+ if (wspIdx == length) {
+ sb.append(s.substring(Math.max(0, lastLineBreak)));
+ return sb.toString();
+ }
+
+ int nextWspIdx = indexOfWsp(s, wspIdx + 1);
+
+ if (nextWspIdx - lastLineBreak > maxCharacters) {
+ sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx));
+ sb.append("\r\n");
+ lastLineBreak = wspIdx;
+ }
+
+ wspIdx = nextWspIdx;
+ }
+ }
+
+ /**
+ * INTERIM: From newer version of org.apache.james (but we don't want to import
+ * the entire MimeUtil class).
+ *
+ * Search for whitespace.
+ */
+ private static int indexOfWsp(String s, int fromIndex) {
+ final int len = s.length();
+ for (int index = fromIndex; index < len; index++) {
+ char c = s.charAt(index);
+ if (c == ' ' || c == '\t')
+ return index;
+ }
+ return len;
+ }
/**
* Returns the named parameter of a header field. If name is null the first
@@ -69,6 +173,11 @@ public class MimeUtility {
* field the entire field is returned. Otherwise the named parameter is
* searched for in a case insensitive fashion and returned. If the parameter
* cannot be found the method returns null.
+ *
+ * TODO: quite inefficient with the inner trimming & splitting.
+ * TODO: Also has a latent bug: uses "startsWith" to match the name, which can false-positive.
+ * TODO: The doc says that for a null name you get the first param, but you get the header.
+ * Should probably just fix the doc, but if other code assumes that behavior, fix the code.
*
* @param header
* @param name
@@ -78,13 +187,13 @@ public class MimeUtility {
if (header == null) {
return null;
}
- header = header.replaceAll("\r|\n", "");
- String[] parts = header.split(";");
+ String[] parts = unfold(header).split(";");
if (name == null) {
return parts[0];
}
+ String lowerCaseName = name.toLowerCase();
for (String part : parts) {
- if (part.trim().toLowerCase().startsWith(name.toLowerCase())) {
+ if (part.trim().toLowerCase().startsWith(lowerCaseName)) {
String parameter = part.split("=", 2)[1].trim();
if (parameter.startsWith("\"") && parameter.endsWith("\"")) {
return parameter.substring(1, parameter.length() - 1);
diff --git a/src/org/apache/james/mime4j/codec/EncoderUtil.java b/src/org/apache/james/mime4j/codec/EncoderUtil.java
new file mode 100644
index 000000000..d6f3998d4
--- /dev/null
+++ b/src/org/apache/james/mime4j/codec/EncoderUtil.java
@@ -0,0 +1,626 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mime4j.codec;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.util.BitSet;
+import java.util.Locale;
+
+import org.apache.james.mime4j.util.CharsetUtil;
+
+/**
+ * ANDROID: THIS CLASS IS COPIED FROM A NEWER VERSION OF MIME4J
+ */
+
+/**
+ * Static methods for encoding header field values. This includes encoded-words
+ * as defined in RFC 2047
+ * or display-names of an e-mail address, for example.
+ *
+ */
+public class EncoderUtil {
+
+ // This array is a lookup table that translates 6-bit positive integer index
+ // values into their "Base64 Alphabet" equivalents as specified in Table 1
+ // of RFC 2045.
+ // ANDROID: THIS TABLE IS COPIED FROM BASE64OUTPUTSTREAM
+ static final byte[] BASE64_TABLE = { 'A', 'B', 'C', 'D', 'E', 'F',
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
+ 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
+ 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
+ '6', '7', '8', '9', '+', '/' };
+
+ // Byte used to pad output.
+ private static final byte BASE64_PAD = '=';
+
+ private static final BitSet Q_REGULAR_CHARS = initChars("=_?");
+
+ private static final BitSet Q_RESTRICTED_CHARS = initChars("=_?\"#$%&'(),.:;<>@[\\]^`{|}~");
+
+ private static final int MAX_USED_CHARACTERS = 50;
+
+ private static final String ENC_WORD_PREFIX = "=?";
+ private static final String ENC_WORD_SUFFIX = "?=";
+
+ private static final int ENCODED_WORD_MAX_LENGTH = 75; // RFC 2047
+
+ private static final BitSet TOKEN_CHARS = initChars("()<>@,;:\\\"/[]?=");
+
+ private static final BitSet ATEXT_CHARS = initChars("()<>@.,;:\\\"[]");
+
+ private static BitSet initChars(String specials) {
+ BitSet bs = new BitSet(128);
+ for (char ch = 33; ch < 127; ch++) {
+ if (specials.indexOf(ch) == -1) {
+ bs.set(ch);
+ }
+ }
+ return bs;
+ }
+
+ /**
+ * Selects one of the two encodings specified in RFC 2047.
+ */
+ public enum Encoding {
+ /** The B encoding (identical to base64 defined in RFC 2045). */
+ B,
+ /** The Q encoding (similar to quoted-printable defined in RFC 2045). */
+ Q
+ }
+
+ /**
+ * Indicates the intended usage of an encoded word.
+ */
+ public enum Usage {
+ /**
+ * Encoded word is used to replace a 'text' token in any Subject or
+ * Comments header field.
+ */
+ TEXT_TOKEN,
+ /**
+ * Encoded word is used to replace a 'word' entity within a 'phrase',
+ * for example, one that precedes an address in a From, To, or Cc
+ * header.
+ */
+ WORD_ENTITY
+ }
+
+ private EncoderUtil() {
+ }
+
+ /**
+ * Encodes the display-name portion of an address. See RFC 5322 section 3.4
+ * and RFC 2047 section
+ * 5.3. The specified string should not be folded.
+ *
+ * @param displayName
+ * display-name to encode.
+ * @return encoded display-name.
+ */
+ public static String encodeAddressDisplayName(String displayName) {
+ // display-name = phrase
+ // phrase = 1*( encoded-word / word )
+ // word = atom / quoted-string
+ // atom = [CFWS] 1*atext [CFWS]
+ // CFWS = comment or folding white space
+
+ if (isAtomPhrase(displayName)) {
+ return displayName;
+ } else if (hasToBeEncoded(displayName, 0)) {
+ return encodeEncodedWord(displayName, Usage.WORD_ENTITY);
+ } else {
+ return quote(displayName);
+ }
+ }
+
+ /**
+ * Encodes the local part of an address specification as described in RFC
+ * 5322 section 3.4.1. Leading and trailing CFWS should have been removed
+ * before calling this method. The specified string should not contain any
+ * illegal (control or non-ASCII) characters.
+ *
+ * @param localPart
+ * the local part to encode
+ * @return the encoded local part.
+ */
+ public static String encodeAddressLocalPart(String localPart) {
+ // local-part = dot-atom / quoted-string
+ // dot-atom = [CFWS] dot-atom-text [CFWS]
+ // CFWS = comment or folding white space
+
+ if (isDotAtomText(localPart)) {
+ return localPart;
+ } else {
+ return quote(localPart);
+ }
+ }
+
+ /**
+ * Encodes the specified strings into a header parameter as described in RFC
+ * 2045 section 5.1 and RFC 2183 section 2. The specified strings should not
+ * contain any illegal (control or non-ASCII) characters.
+ *
+ * @param name
+ * parameter name.
+ * @param value
+ * parameter value.
+ * @return encoded result.
+ */
+ public static String encodeHeaderParameter(String name, String value) {
+ name = name.toLowerCase(Locale.US);
+
+ // value := token / quoted-string
+ if (isToken(value)) {
+ return name + "=" + value;
+ } else {
+ return name + "=" + quote(value);
+ }
+ }
+
+ /**
+ * Shortcut method that encodes the specified text into an encoded-word if
+ * the text has to be encoded.
+ *
+ * @param text
+ * text to encode.
+ * @param usage
+ * whether the encoded-word is to be used to replace a text token
+ * or a word entity (see RFC 822).
+ * @param usedCharacters
+ * number of characters already used up (0 <= usedCharacters <= 50
).
+ * @return the specified text if encoding is not necessary or an encoded
+ * word or a sequence of encoded words otherwise.
+ */
+ public static String encodeIfNecessary(String text, Usage usage,
+ int usedCharacters) {
+ if (hasToBeEncoded(text, usedCharacters))
+ return encodeEncodedWord(text, usage, usedCharacters);
+ else
+ return text;
+ }
+
+ /**
+ * Determines if the specified string has to encoded into an encoded-word.
+ * Returns true
if the text contains characters that don't
+ * fall into the printable ASCII character set or if the text contains a
+ * 'word' (sequence of non-whitespace characters) longer than 77 characters
+ * (including characters already used up in the line).
+ *
+ * @param text
+ * text to analyze.
+ * @param usedCharacters
+ * number of characters already used up (0 <= usedCharacters <= 50
).
+ * @return true
if the specified text has to be encoded into
+ * an encoded-word, false
otherwise.
+ */
+ public static boolean hasToBeEncoded(String text, int usedCharacters) {
+ if (text == null)
+ throw new IllegalArgumentException();
+ if (usedCharacters < 0 || usedCharacters > MAX_USED_CHARACTERS)
+ throw new IllegalArgumentException();
+
+ int nonWhiteSpaceCount = usedCharacters;
+
+ for (int idx = 0; idx < text.length(); idx++) {
+ char ch = text.charAt(idx);
+ if (ch == '\t' || ch == ' ') {
+ nonWhiteSpaceCount = 0;
+ } else {
+ nonWhiteSpaceCount++;
+ if (nonWhiteSpaceCount > 77) {
+ // Line cannot be folded into multiple lines with no more
+ // than 78 characters each. Encoding as encoded-words makes
+ // that possible. One character has to be reserved for
+ // folding white space; that leaves 77 characters.
+ return true;
+ }
+
+ if (ch < 32 || ch >= 127) {
+ // non-printable ascii character has to be encoded
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Encodes the specified text into an encoded word or a sequence of encoded
+ * words separated by space. The text is separated into a sequence of
+ * encoded words if it does not fit in a single one.
+ *
+ * The charset to encode the specified text into a byte array and the + * encoding to use for the encoded-word are detected automatically. + *
+ * This method assumes that zero characters have already been used up in the + * current line. + * + * @param text + * text to encode. + * @param usage + * whether the encoded-word is to be used to replace a text token + * or a word entity (see RFC 822). + * @return the encoded word (or sequence of encoded words if the given text + * does not fit in a single encoded word). + * @see #hasToBeEncoded(String, int) + */ + public static String encodeEncodedWord(String text, Usage usage) { + return encodeEncodedWord(text, usage, 0, null, null); + } + + /** + * Encodes the specified text into an encoded word or a sequence of encoded + * words separated by space. The text is separated into a sequence of + * encoded words if it does not fit in a single one. + *
+ * The charset to encode the specified text into a byte array and the
+ * encoding to use for the encoded-word are detected automatically.
+ *
+ * @param text
+ * text to encode.
+ * @param usage
+ * whether the encoded-word is to be used to replace a text token
+ * or a word entity (see RFC 822).
+ * @param usedCharacters
+ * number of characters already used up (0 <= usedCharacters <= 50
).
+ * @return the encoded word (or sequence of encoded words if the given text
+ * does not fit in a single encoded word).
+ * @see #hasToBeEncoded(String, int)
+ */
+ public static String encodeEncodedWord(String text, Usage usage,
+ int usedCharacters) {
+ return encodeEncodedWord(text, usage, usedCharacters, null, null);
+ }
+
+ /**
+ * Encodes the specified text into an encoded word or a sequence of encoded
+ * words separated by space. The text is separated into a sequence of
+ * encoded words if it does not fit in a single one.
+ *
+ * @param text
+ * text to encode.
+ * @param usage
+ * whether the encoded-word is to be used to replace a text token
+ * or a word entity (see RFC 822).
+ * @param usedCharacters
+ * number of characters already used up (0 <= usedCharacters <= 50
).
+ * @param charset
+ * the Java charset that should be used to encode the specified
+ * string into a byte array. A suitable charset is detected
+ * automatically if this parameter is null
.
+ * @param encoding
+ * the encoding to use for the encoded-word (either B or Q). A
+ * suitable encoding is automatically chosen if this parameter is
+ * null
.
+ * @return the encoded word (or sequence of encoded words if the given text
+ * does not fit in a single encoded word).
+ * @see #hasToBeEncoded(String, int)
+ */
+ public static String encodeEncodedWord(String text, Usage usage,
+ int usedCharacters, Charset charset, Encoding encoding) {
+ if (text == null)
+ throw new IllegalArgumentException();
+ if (usedCharacters < 0 || usedCharacters > MAX_USED_CHARACTERS)
+ throw new IllegalArgumentException();
+
+ if (charset == null)
+ charset = determineCharset(text);
+
+ String mimeCharset = CharsetUtil.toMimeCharset(charset.name());
+ if (mimeCharset == null) {
+ // cannot happen if charset was originally null
+ throw new IllegalArgumentException("Unsupported charset");
+ }
+
+ byte[] bytes = encode(text, charset);
+
+ if (encoding == null)
+ encoding = determineEncoding(bytes, usage);
+
+ if (encoding == Encoding.B) {
+ String prefix = ENC_WORD_PREFIX + mimeCharset + "?B?";
+ return encodeB(prefix, text, usedCharacters, charset, bytes);
+ } else {
+ String prefix = ENC_WORD_PREFIX + mimeCharset + "?Q?";
+ return encodeQ(prefix, text, usage, usedCharacters, charset, bytes);
+ }
+ }
+
+ /**
+ * Encodes the specified byte array using the B encoding defined in RFC
+ * 2047.
+ *
+ * @param bytes
+ * byte array to encode.
+ * @return encoded string.
+ */
+ public static String encodeB(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+
+ int idx = 0;
+ final int end = bytes.length;
+ for (; idx < end - 2; idx += 3) {
+ int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8
+ | bytes[idx + 2] & 0xff;
+ sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]);
+ sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]);
+ sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]);
+ sb.append((char) BASE64_TABLE[data & 0x3f]);
+ }
+
+ if (idx == end - 2) {
+ int data = (bytes[idx] & 0xff) << 16 | (bytes[idx + 1] & 0xff) << 8;
+ sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]);
+ sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]);
+ sb.append((char) BASE64_TABLE[data >> 6 & 0x3f]);
+ sb.append(BASE64_PAD);
+
+ } else if (idx == end - 1) {
+ int data = (bytes[idx] & 0xff) << 16;
+ sb.append((char) BASE64_TABLE[data >> 18 & 0x3f]);
+ sb.append((char) BASE64_TABLE[data >> 12 & 0x3f]);
+ sb.append(BASE64_PAD);
+ sb.append(BASE64_PAD);
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Encodes the specified byte array using the Q encoding defined in RFC
+ * 2047.
+ *
+ * @param bytes
+ * byte array to encode.
+ * @param usage
+ * whether the encoded-word is to be used to replace a text token
+ * or a word entity (see RFC 822).
+ * @return encoded string.
+ */
+ public static String encodeQ(byte[] bytes, Usage usage) {
+ BitSet qChars = usage == Usage.TEXT_TOKEN ? Q_REGULAR_CHARS
+ : Q_RESTRICTED_CHARS;
+
+ StringBuilder sb = new StringBuilder();
+
+ final int end = bytes.length;
+ for (int idx = 0; idx < end; idx++) {
+ int v = bytes[idx] & 0xff;
+ if (v == 32) {
+ sb.append('_');
+ } else if (!qChars.get(v)) {
+ sb.append('=');
+ sb.append(hexDigit(v >>> 4));
+ sb.append(hexDigit(v & 0xf));
+ } else {
+ sb.append((char) v);
+ }
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Tests whether the specified string is a token as defined in RFC 2045
+ * section 5.1.
+ *
+ * @param str
+ * string to test.
+ * @return true
if the specified string is a RFC 2045 token,
+ * false
otherwise.
+ */
+ public static boolean isToken(String str) {
+ // token := 1*true
if the specified character is a whitespace
+ * character (CR, LF, SP or HT).
+ *
+ * ANDROID: COPIED FROM A NEWER VERSION OF MIME4J
+ *
+ * @param ch
+ * character to test.
+ * @return true
if the specified character is a whitespace
+ * character, false
otherwise.
+ */
+ public static boolean isWhitespace(char ch) {
+ return ch == SP || ch == HT || ch == CR || ch == LF;
+ }
+
+ /**
+ * Returns true
if the specified string consists entirely of
+ * whitespace characters.
+ *
+ * ANDROID: COPIED FROM A NEWER VERSION OF MIME4J
+ *
+ * @param s
+ * string to test.
+ * @return true
if the specified string consists entirely of
+ * whitespace characters, false
otherwise.
+ */
+ public static boolean isWhitespace(final String s) {
+ if (s == null) {
+ throw new IllegalArgumentException("String may not be null");
+ }
+ final int len = s.length();
+ for (int i = 0; i < len; i++) {
+ if (!isWhitespace(s.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Determines if the VM supports encoding (chars to bytes) the
* specified character set. NOTE: the given character set name may
diff --git a/tests/src/com/android/email/mail/internet/MimeMessageTest.java b/tests/src/com/android/email/mail/internet/MimeMessageTest.java
index d1fde4a6e..45f223e4c 100644
--- a/tests/src/com/android/email/mail/internet/MimeMessageTest.java
+++ b/tests/src/com/android/email/mail/internet/MimeMessageTest.java
@@ -35,6 +35,26 @@ import junit.framework.TestCase;
*/
@SmallTest
public class MimeMessageTest extends TestCase {
+
+ /** up arrow, down arrow, left arrow, right arrow */
+ private final String SHORT_UNICODE = "\u2191\u2193\u2190\u2192";
+ private final String SHORT_UNICODE_ENCODED = "=?UTF-8?B?4oaR4oaT4oaQ4oaS?=";
+
+ /** a string without any unicode */
+ private final String SHORT_PLAIN = "abcd";
+
+ /** longer unicode strings */
+ private final String LONG_UNICODE_16 = SHORT_UNICODE + SHORT_UNICODE +
+ SHORT_UNICODE + SHORT_UNICODE;
+ private final String LONG_UNICODE_64 = LONG_UNICODE_16 + LONG_UNICODE_16 +
+ LONG_UNICODE_16 + LONG_UNICODE_16;
+
+ /** longer plain strings (with fold points) */
+ private final String LONG_PLAIN_16 = "abcdefgh ijklmno";
+ private final String LONG_PLAIN_64 =
+ LONG_PLAIN_16 + LONG_PLAIN_16 + LONG_PLAIN_16 + LONG_PLAIN_16;
+ private final String LONG_PLAIN_256 =
+ LONG_PLAIN_64 + LONG_PLAIN_64 + LONG_PLAIN_64 + LONG_PLAIN_64;
// TODO: more tests.
@@ -98,7 +118,7 @@ public class MimeMessageTest extends TestCase {
assertEquals("set and get Message-ID", testId2, message2.getMessageId());
}
- /*
+ /**
* Confirm getContentID() correctly works.
*/
public void testGetContentId() throws MessagingException {
@@ -116,4 +136,80 @@ public class MimeMessageTest extends TestCase {
message.setHeader(MimeHeader.HEADER_CONTENT_ID, "<" + cid1 + ">");
assertEquals(cid1, message.getContentId());
}
+
+ /**
+ * Confirm that setSubject() works with plain strings
+ */
+ public void testSetSubjectPlain() throws MessagingException {
+ MimeMessage message = new MimeMessage();
+
+ message.setSubject(SHORT_PLAIN);
+
+ // test 1: readback
+ assertEquals("plain subjects", SHORT_PLAIN, message.getSubject());
+
+ // test 2: raw readback is not escaped
+ String rawHeader = message.getFirstHeader("Subject");
+ assertEquals("plain subject not encoded", -1, rawHeader.indexOf("=?"));
+
+ // test 3: long subject (shouldn't fold)
+ message.setSubject(LONG_PLAIN_64);
+ rawHeader = message.getFirstHeader("Subject");
+ String[] split = rawHeader.split("\r\n");
+ assertEquals("64 shouldn't fold", 1, split.length);
+
+ // test 4: very long subject (should fold)
+ message.setSubject(LONG_PLAIN_256);
+ rawHeader = message.getFirstHeader("Subject");
+ split = rawHeader.split("\r\n");
+ assertTrue("long subject should fold", split.length > 1);
+ for (String s : split) {
+ assertTrue("split lines max length 78", s.length() <= 76); // 76+\r\n = 78
+ String trimmed = s.trim();
+ assertFalse("split lines are not encoded", trimmed.startsWith("=?"));
+ }
+ }
+
+ /**
+ * Confirm that setSubject() works with unicode strings
+ */
+ public void testSetSubject() throws MessagingException {
+ MimeMessage message = new MimeMessage();
+
+ message.setSubject(SHORT_UNICODE);
+
+ // test 1: readback in unicode
+ assertEquals("unicode readback", SHORT_UNICODE, message.getSubject());
+
+ // test 2: raw readback is escaped
+ String rawHeader = message.getFirstHeader("Subject");
+ assertEquals("raw readback", SHORT_UNICODE_ENCODED, rawHeader);
+ }
+
+ /**
+ * Confirm folding operations on unicode subjects
+ */
+ public void testSetLongSubject() throws MessagingException {
+ MimeMessage message = new MimeMessage();
+
+ // test 1: long unicode - readback in unicode
+ message.setSubject(LONG_UNICODE_16);
+ assertEquals("unicode readback 16", LONG_UNICODE_16, message.getSubject());
+
+ // test 2: longer unicode (will fold)
+ message.setSubject(LONG_UNICODE_64);
+ assertEquals("unicode readback 64", LONG_UNICODE_64, message.getSubject());
+
+ // test 3: check folding & encoding
+ String rawHeader = message.getFirstHeader("Subject");
+ String[] split = rawHeader.split("\r\n");
+ assertTrue("long subject should fold", split.length > 1);
+ for (String s : split) {
+ assertTrue("split lines max length 78", s.length() <= 76); // 76+\r\n = 78
+ String trimmed = s.trim();
+ assertTrue("split lines are encoded",
+ trimmed.startsWith("=?") && trimmed.endsWith("?="));
+ }
+ }
+
}
\ No newline at end of file
diff --git a/tests/src/com/android/email/mail/internet/MimeUtilityTest.java b/tests/src/com/android/email/mail/internet/MimeUtilityTest.java
index e894161be..b39e4cf0d 100644
--- a/tests/src/com/android/email/mail/internet/MimeUtilityTest.java
+++ b/tests/src/com/android/email/mail/internet/MimeUtilityTest.java
@@ -35,11 +35,120 @@ import junit.framework.TestCase;
@SmallTest
public class MimeUtilityTest extends TestCase {
- // TODO: tests for unfold(String s)
+ /** up arrow, down arrow, left arrow, right arrow */
+ private final String SHORT_UNICODE = "\u2191\u2193\u2190\u2192";
+ private final String SHORT_UNICODE_ENCODED = "=?UTF-8?B?4oaR4oaT4oaQ4oaS?=";
+
+ /** a string without any unicode */
+ private final String SHORT_PLAIN = "abcd";
+
+ /** a typical no-param header */
+ private final String HEADER_NO_PARAMETER =
+ "header";
+ /** a typical multi-param header */
+ private final String HEADER_MULTI_PARAMETER =
+ "header; Param1Name=Param1Value; Param2Name=Param2Value";
+
+ /**
+ * Test that decode/unfold is efficient when it can be
+ */
+ public void testEfficientUnfoldAndDecode() {
+ String result1 = MimeUtility.unfold(SHORT_PLAIN);
+ String result2 = MimeUtility.decode(SHORT_PLAIN);
+ String result3 = MimeUtility.unfoldAndDecode(SHORT_PLAIN);
+
+ assertSame(SHORT_PLAIN, result1);
+ assertSame(SHORT_PLAIN, result2);
+ assertSame(SHORT_PLAIN, result3);
+ }
+
+ // TODO: more tests for unfold(String s)
+
+ /**
+ * Test that decode is working for simple strings
+ */
+ public void testDecodeSimple() {
+ String result1 = MimeUtility.decode(SHORT_UNICODE_ENCODED);
+ assertEquals(SHORT_UNICODE, result1);
+ }
+
// TODO: tests for decode(String s)
+
+ /**
+ * Test that unfoldAndDecode is working for simple strings
+ */
+ public void testUnfoldAndDecodeSimple() {
+ String result1 = MimeUtility.unfoldAndDecode(SHORT_UNICODE_ENCODED);
+ assertEquals(SHORT_UNICODE, result1);
+ }
+
// TODO: tests for unfoldAndDecode(String s)
- // TODO: tests for foldAndEncode(String s)
- // TODO: tests for getHeaderParameter(String header, String name)
+
+ /**
+ * Test that fold/encode is efficient when it can be
+ */
+ public void testEfficientFoldAndEncode() {
+ String result1 = MimeUtility.foldAndEncode(SHORT_PLAIN);
+ String result2 = MimeUtility.foldAndEncode2(SHORT_PLAIN, 10);
+ String result3 = MimeUtility.fold(SHORT_PLAIN, 10);
+
+ assertSame(SHORT_PLAIN, result1);
+ assertSame(SHORT_PLAIN, result2);
+ assertSame(SHORT_PLAIN, result3);
+ }
+
+ // TODO: more tests for foldAndEncode(String s)
+
+ /**
+ * Test that foldAndEncode2 is working for simple strings
+ */
+ public void testFoldAndEncode2() {
+ String result1 = MimeUtility.foldAndEncode2(SHORT_UNICODE, 10);
+ assertEquals(SHORT_UNICODE_ENCODED, result1);
+ }
+
+ // TODO: more tests for foldAndEncode2(String s)
+ // TODO: more tests for fold(String s, int usedCharacters)
+
+ /**
+ * Basic tests of getHeaderParameter()
+ *
+ * Typical header value: multipart/mixed; boundary="----E5UGTXUQQJV80DR8SJ88F79BRA4S8K"
+ *
+ * Function spec says:
+ * if header is null: return null
+ * if name is null: if params, return first param. else return full field
+ * else: if param is found (case insensitive) return it
+ * else return null
+ */
+ public void testGetHeaderParameter() {
+ // if header is null, return null
+ assertNull("null header check", MimeUtility.getHeaderParameter(null, "name"));
+
+ // if name is null, return first param or full header
+ // NOTE: The docs are wrong - it returns the header (no params) in that case
+// assertEquals("null name first param per docs", "Param1Value",
+// MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null));
+ assertEquals("null name first param per code", "header",
+ MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null));
+ assertEquals("null name full header", HEADER_NO_PARAMETER,
+ MimeUtility.getHeaderParameter(HEADER_NO_PARAMETER, null));
+
+ // find name
+ assertEquals("get 1st param", "Param1Value",
+ MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param1Name"));
+ assertEquals("get 2nd param", "Param2Value",
+ MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param2Name"));
+ assertEquals("get missing param", null,
+ MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param3Name"));
+
+ // case insensitivity
+ assertEquals("get 2nd param all LC", "Param2Value",
+ MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "param2name"));
+ assertEquals("get 2nd param all UC", "Param2Value",
+ MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "PARAM2NAME"));
+ }
+
// TODO: tests for findFirstPartByMimeType(Part part, String mimeType)
/** Tests for findPartByContentId(Part part, String contentId) */