Home | History | Annotate | Download | only in internet
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.emailcommon.internet;
     18 
     19 import com.android.emailcommon.internet.MimeBodyPart;
     20 import com.android.emailcommon.internet.MimeHeader;
     21 import com.android.emailcommon.internet.MimeUtility;
     22 import com.android.emailcommon.internet.TextBody;
     23 import com.android.emailcommon.mail.BodyPart;
     24 import com.android.emailcommon.mail.Message;
     25 import com.android.emailcommon.mail.MessageTestUtils;
     26 import com.android.emailcommon.mail.MessagingException;
     27 import com.android.emailcommon.mail.Part;
     28 import com.android.emailcommon.mail.MessageTestUtils.MessageBuilder;
     29 import com.android.emailcommon.mail.MessageTestUtils.MultipartBuilder;
     30 
     31 import android.test.suitebuilder.annotation.SmallTest;
     32 
     33 import java.util.ArrayList;
     34 
     35 import junit.framework.TestCase;
     36 
     37 /**
     38  * This is a series of unit tests for the MimeUtility class.  These tests must be locally
     39  * complete - no server(s) required.
     40  */
     41 @SmallTest
     42 public class MimeUtilityTest extends TestCase {
     43 
     44     /** up arrow, down arrow, left arrow, right arrow */
     45     private final String SHORT_UNICODE = "\u2191\u2193\u2190\u2192";
     46     private final String SHORT_UNICODE_ENCODED = "=?UTF-8?B?4oaR4oaT4oaQ4oaS?=";
     47 
     48     /** dollar and euro sign */
     49     private final String PADDED2_UNICODE = "$\u20AC";
     50     private final String PADDED2_UNICODE_ENCODED = "=?UTF-8?B?JOKCrA==?=";
     51     private final String PADDED1_UNICODE = "$$\u20AC";
     52     private final String PADDED1_UNICODE_ENCODED = "=?UTF-8?B?JCTigqw=?=";
     53     private final String PADDED0_UNICODE = "$$$\u20AC";
     54     private final String PADDED0_UNICODE_ENCODED = "=?UTF-8?B?JCQk4oKs?=";
     55 
     56     /** a string without any unicode */
     57     private final String SHORT_PLAIN = "abcd";
     58 
     59     /** long subject which will be split into two MIME/Base64 chunks */
     60     private final String LONG_UNICODE_SPLIT =
     61         "$" +
     62         "\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC" +
     63         "\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC\u20AC";
     64     private final String LONG_UNICODE_SPLIT_ENCODED =
     65         "=?UTF-8?B?JOKCrOKCrOKCrOKCrOKCrOKCrOKCrOKCrA==?=" + "\r\n " +
     66         "=?UTF-8?B?4oKs4oKs4oKs4oKs4oKs4oKs4oKs4oKs4oKs4oKs4oKs4oKs?=";
     67 
     68     /** strings that use supplemental characters and really stress encode/decode */
     69     // actually it's U+10400
     70     private final String SHORT_SUPPLEMENTAL = "\uD801\uDC00";
     71     private final String SHORT_SUPPLEMENTAL_ENCODED = "=?UTF-8?B?8JCQgA==?=";
     72     private final String LONG_SUPPLEMENTAL = SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL +
     73         SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL +
     74         SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL;
     75     private final String LONG_SUPPLEMENTAL_ENCODED =
     76         "=?UTF-8?B?8JCQgPCQkIDwkJCA8JCQgA==?=" + "\r\n " +
     77         "=?UTF-8?B?8JCQgPCQkIDwkJCA8JCQgPCQkIDwkJCA?=";
     78     private final String LONG_SUPPLEMENTAL_2 = "a" + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL +
     79         SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL +
     80         SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL + SHORT_SUPPLEMENTAL;
     81     private final String LONG_SUPPLEMENTAL_ENCODED_2 =
     82         "=?UTF-8?B?YfCQkIDwkJCA8JCQgPCQkIA=?=" + "\r\n " +
     83         "=?UTF-8?B?8JCQgPCQkIDwkJCA8JCQgPCQkIDwkJCA?=";
     84     // Earth is U+1D300.
     85     private final String LONG_SUPPLEMENTAL_QP =
     86         "*Monogram for Earth \uD834\uDF00. Monogram for Human \u268b.";
     87     private final String LONG_SUPPLEMENTAL_QP_ENCODED =
     88         "=?UTF-8?Q?*Monogram_for_Earth_?=" + "\r\n " +
     89         "=?UTF-8?Q?=F0=9D=8C=80._Monogram_for_Human_=E2=9A=8B.?=";
     90 
     91     /** a typical no-param header */
     92     private final String HEADER_NO_PARAMETER =
     93             "header";
     94     /** a typical multi-param header */
     95     private final String HEADER_MULTI_PARAMETER =
     96             "header; Param1Name=Param1Value; Param2Name=Param2Value";
     97     /** a multi-param header with quoting */
     98     private final String HEADER_QUOTED_MULTI_PARAMETER =
     99             "header; Param1Name=\"Param1Value\"; Param2Name=\"Param2Value\"";
    100     /** a malformed header we're seeing in production servers */
    101     private final String HEADER_MALFORMED_PARAMETER =
    102             "header; Param1Name=Param1Value; filename";
    103 
    104     /**
    105      * a string generated by google calendar that contains two interesting gotchas:
    106      * 1.  Uses windows-1252 encoding, and en-dash recoded appropriately (\u2013 / =96)
    107      * 2.  Because the first encoded char requires '=XX' encoding, we create an "internal"
    108      *     "?=" that the decoder must correctly skip over.
    109      **/
    110     private final String CALENDAR_SUBJECT_UNICODE =
    111         "=?windows-1252?Q?=5BReminder=5D_test_=40_Fri_Mar_20_10=3A30am_=96_11am_=28andro?=" +
    112         "\r\n\t" +
    113         "=?windows-1252?Q?id=2Etr=40gmail=2Ecom=29?=";
    114     private final String CALENDAR_SUBJECT_PLAIN =
    115         "[Reminder] test @ Fri Mar 20 10:30am \u2013 11am (android.tr (at) gmail.com)";
    116 
    117     /**
    118      * Some basic degenerate strings designed to exercise error handling in the decoder
    119      */
    120     private final String CALENDAR_DEGENERATE_UNICODE_1 =
    121         "=?windows-1252?Q=5B?=";
    122     private final String CALENDAR_DEGENERATE_UNICODE_2 =
    123         "=?windows-1252Q?=5B?=";
    124     private final String CALENDAR_DEGENERATE_UNICODE_3 =
    125         "=?windows-1252?=";
    126     private final String CALENDAR_DEGENERATE_UNICODE_4 =
    127         "=?windows-1252";
    128 
    129     /**
    130      * Test that decode/unfold is efficient when it can be
    131      */
    132     public void testEfficientUnfoldAndDecode() {
    133         String result1 = MimeUtility.unfold(SHORT_PLAIN);
    134         String result2 = MimeUtility.decode(SHORT_PLAIN);
    135         String result3 = MimeUtility.unfoldAndDecode(SHORT_PLAIN);
    136 
    137         assertSame(SHORT_PLAIN, result1);
    138         assertSame(SHORT_PLAIN, result2);
    139         assertSame(SHORT_PLAIN, result3);
    140     }
    141 
    142     // TODO:  more tests for unfold(String s)
    143 
    144     /**
    145      * Test that decode is working for simple strings
    146      */
    147     public void testDecodeSimple() {
    148         String result1 = MimeUtility.decode(SHORT_UNICODE_ENCODED);
    149         assertEquals(SHORT_UNICODE, result1);
    150     }
    151 
    152     // TODO:  tests for decode(String s)
    153 
    154     /**
    155      * Test that unfoldAndDecode is working for simple strings
    156      */
    157     public void testUnfoldAndDecodeSimple() {
    158         String result1 = MimeUtility.unfoldAndDecode(SHORT_UNICODE_ENCODED);
    159         assertEquals(SHORT_UNICODE, result1);
    160     }
    161 
    162     /**
    163      * test decoding complex string from google calendar that has two gotchas for the decoder.
    164      * also tests a couple of degenerate cases that should "fail" decoding and pass through.
    165      */
    166     public void testComplexDecode() {
    167         String result1 = MimeUtility.unfoldAndDecode(CALENDAR_SUBJECT_UNICODE);
    168         assertEquals(CALENDAR_SUBJECT_PLAIN, result1);
    169 
    170         // These degenerate cases should "fail" and return the same string
    171         String degenerate1 = MimeUtility.unfoldAndDecode(CALENDAR_DEGENERATE_UNICODE_1);
    172         assertEquals("degenerate case 1", CALENDAR_DEGENERATE_UNICODE_1, degenerate1);
    173         String degenerate2 = MimeUtility.unfoldAndDecode(CALENDAR_DEGENERATE_UNICODE_2);
    174         assertEquals("degenerate case 2", CALENDAR_DEGENERATE_UNICODE_2, degenerate2);
    175         String degenerate3 = MimeUtility.unfoldAndDecode(CALENDAR_DEGENERATE_UNICODE_3);
    176         assertEquals("degenerate case 3", CALENDAR_DEGENERATE_UNICODE_3, degenerate3);
    177         String degenerate4 = MimeUtility.unfoldAndDecode(CALENDAR_DEGENERATE_UNICODE_4);
    178         assertEquals("degenerate case 4", CALENDAR_DEGENERATE_UNICODE_4, degenerate4);
    179     }
    180 
    181     // TODO:  more tests for unfoldAndDecode(String s)
    182 
    183     /**
    184      * Test that fold/encode is efficient when it can be
    185      */
    186     public void testEfficientFoldAndEncode() {
    187         String result1 = MimeUtility.foldAndEncode(SHORT_PLAIN);
    188         String result2 = MimeUtility.foldAndEncode2(SHORT_PLAIN, 10);
    189         String result3 = MimeUtility.fold(SHORT_PLAIN, 10);
    190 
    191         assertSame(SHORT_PLAIN, result1);
    192         assertSame(SHORT_PLAIN, result2);
    193         assertSame(SHORT_PLAIN, result3);
    194     }
    195 
    196     /**
    197      * Test about base64 padding variety.
    198      */
    199     public void testPaddingOfFoldAndEncode2() {
    200         String result1 = MimeUtility.foldAndEncode2(PADDED2_UNICODE, 0);
    201         String result2 = MimeUtility.foldAndEncode2(PADDED1_UNICODE, 0);
    202         String result3 = MimeUtility.foldAndEncode2(PADDED0_UNICODE, 0);
    203 
    204         assertEquals("padding 2", PADDED2_UNICODE_ENCODED, result1);
    205         assertEquals("padding 1", PADDED1_UNICODE_ENCODED, result2);
    206         assertEquals("padding 0", PADDED0_UNICODE_ENCODED, result3);
    207     }
    208 
    209     // TODO:  more tests for foldAndEncode(String s)
    210 
    211     /**
    212      * Test that foldAndEncode2 is working for simple strings
    213      */
    214     public void testFoldAndEncode2() {
    215         String result1 = MimeUtility.foldAndEncode2(SHORT_UNICODE, 10);
    216         assertEquals(SHORT_UNICODE_ENCODED, result1);
    217     }
    218 
    219     /**
    220      * Test that foldAndEncode2 is working for long strings which needs splitting.
    221      */
    222     public void testFoldAndEncode2WithLongSplit() {
    223         String result = MimeUtility.foldAndEncode2(LONG_UNICODE_SPLIT, "Subject: ".length());
    224 
    225         assertEquals("long string", LONG_UNICODE_SPLIT_ENCODED, result);
    226     }
    227 
    228     /**
    229      * Tests of foldAndEncode2 that involve supplemental characters (UTF-32)
    230      *
    231      * Note that the difference between LONG_SUPPLEMENTAL and LONG_SUPPLEMENTAL_2 is the
    232      * insertion of a single character at the head of the string. This is intended to disrupt
    233      * the code that splits the long string into multiple encoded words, and confirm that it
    234      * properly applies the breaks between UTF-32 code points.
    235      */
    236     public void testFoldAndEncode2Supplemental() {
    237         String result1 = MimeUtility.foldAndEncode2(SHORT_SUPPLEMENTAL, "Subject: ".length());
    238         String result2 = MimeUtility.foldAndEncode2(LONG_SUPPLEMENTAL, "Subject: ".length());
    239         String result3 = MimeUtility.foldAndEncode2(LONG_SUPPLEMENTAL_2, "Subject: ".length());
    240         assertEquals("short supplemental", SHORT_SUPPLEMENTAL_ENCODED, result1);
    241         assertEquals("long supplemental", LONG_SUPPLEMENTAL_ENCODED, result2);
    242         assertEquals("long supplemental 2", LONG_SUPPLEMENTAL_ENCODED_2, result3);
    243     }
    244 
    245     /**
    246      * Tests of foldAndEncode2 that involve supplemental characters (UTF-32)
    247      *
    248      * Note that the difference between LONG_SUPPLEMENTAL and LONG_SUPPLEMENTAL_QP is that
    249      * the former will be encoded as base64 but the latter will be encoded as quoted printable.
    250      */
    251     public void testFoldAndEncode2SupplementalQuotedPrintable() {
    252         String result = MimeUtility.foldAndEncode2(LONG_SUPPLEMENTAL_QP, "Subject: ".length());
    253         assertEquals("long supplement quoted printable",
    254                      LONG_SUPPLEMENTAL_QP_ENCODED, result);
    255     }
    256 
    257     // TODO:  more tests for foldAndEncode2(String s)
    258     // TODO:  more tests for fold(String s, int usedCharacters)
    259 
    260     /**
    261      * Basic tests of getHeaderParameter()
    262      *
    263      * Typical header value:  multipart/mixed; boundary="----E5UGTXUQQJV80DR8SJ88F79BRA4S8K"
    264      *
    265      * Function spec says:
    266      *  if header is null:  return null
    267      *  if name is null:    if params, return first param.  else return full field
    268      *  else:               if param is found (case insensitive) return it
    269      *                        else return null
    270      */
    271     public void testGetHeaderParameter() {
    272         // if header is null, return null
    273         assertNull("null header check", MimeUtility.getHeaderParameter(null, "name"));
    274 
    275         // if name is null, return first param or full header
    276         // NOTE:  The docs are wrong - it returns the header (no params) in that case
    277 //      assertEquals("null name first param per docs", "Param1Value",
    278 //              MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null));
    279         assertEquals("null name first param per code", "header",
    280                 MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null));
    281         assertEquals("null name full header", HEADER_NO_PARAMETER,
    282                 MimeUtility.getHeaderParameter(HEADER_NO_PARAMETER, null));
    283 
    284         // find name
    285         assertEquals("get 1st param", "Param1Value",
    286                 MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param1Name"));
    287         assertEquals("get 2nd param", "Param2Value",
    288                 MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param2Name"));
    289         assertEquals("get missing param", null,
    290                 MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param3Name"));
    291 
    292         // case insensitivity
    293         assertEquals("get 2nd param all LC", "Param2Value",
    294                 MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "param2name"));
    295         assertEquals("get 2nd param all UC", "Param2Value",
    296                 MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "PARAM2NAME"));
    297 
    298         // quoting
    299         assertEquals("get 1st param", "Param1Value",
    300                 MimeUtility.getHeaderParameter(HEADER_QUOTED_MULTI_PARAMETER, "Param1Name"));
    301         assertEquals("get 2nd param", "Param2Value",
    302                 MimeUtility.getHeaderParameter(HEADER_QUOTED_MULTI_PARAMETER, "Param2Name"));
    303 
    304         // Don't fail when malformed
    305         assertEquals("malformed filename param", null,
    306                 MimeUtility.getHeaderParameter(HEADER_MALFORMED_PARAMETER, "filename"));
    307     }
    308 
    309     // TODO:  tests for findFirstPartByMimeType(Part part, String mimeType)
    310 
    311     /** Tests for findPartByContentId(Part part, String contentId) */
    312     public void brokentestCollectParts() throws MessagingException, Exception {
    313         // golden cases; these will marked as attachments
    314         final String cid1 = "<i_12e8248b4f0874cb>";
    315         final Part cid1bp = MessageTestUtils.bodyPart("image/gif; name=\"im1.gif\"", cid1);
    316         final String cid2 = "<ii_12e8248b4f0874cb>";
    317         final Part cid2bp = MessageTestUtils.bodyPart("image/gif", cid2);
    318         cid2bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline; filename=\"im2.gif\"");
    319         final String cid3 = "<iii_12e8248b4f0874cb>";
    320         final Part cid3bp = MessageTestUtils.bodyPart("image/gif", cid3);
    321         cid3bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"im3.gif\"");
    322         // error cases; these will NOT be marked as attachments
    323         final String cid4 = "<iv_12e8248b4f0874cb>";
    324         final Part cid4bp = MessageTestUtils.bodyPart("image/gif", cid4);  // no name attr
    325         final String cid5 = "<v_12e8248b4f0874cb>";
    326         final Part cid5bp = MessageTestUtils.bodyPart("image/gif", cid5);
    327         cid5bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline"); // no filename attr
    328 
    329         // Default content disposition
    330         final ArrayList<Part> view1 = new ArrayList<Part>();
    331         final ArrayList<Part> attach1 = new ArrayList<Part>();
    332         MimeUtility.collectParts(cid1bp, view1, attach1);
    333         assertEquals(1, attach1.size());
    334         assertEquals(attach1.get(0), cid1bp);
    335 
    336         // Explicit content disposition of "inline"
    337         final ArrayList<Part> view2 = new ArrayList<Part>();
    338         final ArrayList<Part> attach2 = new ArrayList<Part>();
    339         MimeUtility.collectParts(cid2bp, view2, attach2);
    340         assertEquals(1, attach2.size());
    341         assertEquals(attach2.get(0), cid2bp);
    342 
    343         // Explicit content disposition of "attachment"
    344         final ArrayList<Part> view3 = new ArrayList<Part>();
    345         final ArrayList<Part> attach3 = new ArrayList<Part>();
    346         MimeUtility.collectParts(cid3bp, view3, attach3);
    347         assertEquals(1, attach3.size());
    348         assertEquals(attach3.get(0), cid3bp);
    349 
    350         // Default content disposition; missing name attribute on content-type
    351         final ArrayList<Part> view4 = new ArrayList<Part>();
    352         final ArrayList<Part> attach4 = new ArrayList<Part>();
    353         MimeUtility.collectParts(cid4bp, view4, attach4);
    354         assertEquals(0, attach4.size());
    355 
    356         // Content disposition of "inline"; missing filename attribute
    357         final ArrayList<Part> view5 = new ArrayList<Part>();
    358         final ArrayList<Part> attach5 = new ArrayList<Part>();
    359         MimeUtility.collectParts(cid5bp, view5, attach5);
    360         assertEquals(0, attach5.size());
    361     }
    362 
    363     /** Tests for getTextFromPart(Part part) */
    364     public void testGetTextFromPartContentTypeCase() throws MessagingException {
    365         final String theText = "This is the text of the part";
    366         TextBody tb = new TextBody(theText);
    367         MimeBodyPart p = new MimeBodyPart();
    368 
    369         // 1. test basic text/plain mode
    370         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
    371         p.setBody(tb);
    372         String gotText = MimeUtility.getTextFromPart(p);
    373         assertEquals(theText, gotText);
    374 
    375         // 2. mixed case is OK
    376         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "TEXT/PLAIN");
    377         p.setBody(tb);
    378         gotText = MimeUtility.getTextFromPart(p);
    379         assertEquals(theText, gotText);
    380 
    381         // 3. wildcards OK
    382         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/other");
    383         p.setBody(tb);
    384         gotText = MimeUtility.getTextFromPart(p);
    385         assertEquals(theText, gotText);
    386     }
    387 
    388     /** Test for usage of Content-Type in getTextFromPart(Part part).
    389      *
    390      * For example 'Content-Type: text/html; charset=utf-8'
    391      *
    392      *  If the body part has no mime-type, refuses to parse content as text.
    393      *  If the mime-type does not match text/*, it will not get parsed.
    394      *  Then, the charset parameter is used, with a default of ASCII.
    395      *
    396      *  This test works by using a string that is valid Unicode, and is also
    397      *  valid when decoded from UTF-8 bytes into Windows-1252 (so that
    398      *  auto-detection is not possible), and checks that the correct conversion
    399      *  was made, based on the Content-Type header.
    400      *
    401      */
    402     public void testContentTypeCharset() throws MessagingException {
    403         final String UNICODE_EXPECT = "This is some happy unicode text \u263a";
    404         // What you get if you encode to UTF-8 (\xe2\x98\xba) and reencode with Windows-1252
    405         final String WINDOWS1252_EXPECT = "This is some happy unicode text \u00e2\u02dc\u00ba";
    406         TextBody tb = new TextBody(UNICODE_EXPECT);
    407         MimeBodyPart p = new MimeBodyPart();
    408 
    409         String gotText, mimeType, charset;
    410         // TEST 0: Standard Content-Type header; no extraneous spaces or fields
    411         p.setBody(tb);
    412         // We call setHeader after setBody, since setBody overwrites Content-Type
    413         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=utf-8");
    414         gotText = MimeUtility.getTextFromPart(p);
    415         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    416         assertEquals(UNICODE_EXPECT, gotText);
    417 
    418         p.setBody(tb);
    419         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=windows-1252");
    420         gotText = MimeUtility.getTextFromPart(p);
    421         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    422         assertEquals(WINDOWS1252_EXPECT, gotText);
    423 
    424         // TEST 1: Extra fields and quotes in Content-Type (from RFC 2045)
    425         p.setBody(tb);
    426         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
    427                     "text/html; prop1 = \"test\"; charset = \"utf-8\"; prop2 = \"test\"");
    428         gotText = MimeUtility.getTextFromPart(p);
    429         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    430         assertEquals(UNICODE_EXPECT, gotText);
    431 
    432         p.setBody(tb);
    433         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
    434                     "text/html; prop1 = \"test\"; charset = \"windows-1252\"; prop2 = \"test\"");
    435         gotText = MimeUtility.getTextFromPart(p);
    436         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    437         assertEquals(WINDOWS1252_EXPECT, gotText);
    438 
    439         // TEST 2: Mixed case in Content-Type header:
    440         // RFC 2045 says that content types, subtypes and parameter names
    441         // are case-insensitive.
    442 
    443         p.setBody(tb);
    444         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "TEXT/HtmL ; CHARseT=utf-8");
    445         gotText = MimeUtility.getTextFromPart(p);
    446         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    447         assertEquals(UNICODE_EXPECT, gotText);
    448 
    449         p.setBody(tb);
    450         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "TEXT/HtmL ; CHARseT=windows-1252");
    451         gotText = MimeUtility.getTextFromPart(p);
    452         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    453         assertEquals(WINDOWS1252_EXPECT, gotText);
    454 
    455         // TEST 3: Comments in Content-Type header field (from RFC 2045)
    456         // Thunderbird permits comments after the end of a parameter, as in this example.
    457         // Not something that I have seen in the real world outside RFC 2045.
    458 
    459         p.setBody(tb);
    460         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
    461                     "text/html; charset=utf-8 (Plain text)");
    462         gotText = MimeUtility.getTextFromPart(p);
    463         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    464         // Note: This test does not pass.
    465         //assertEquals(UNICODE_EXPECT, gotText);
    466 
    467         p.setBody(tb);
    468         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
    469                     "text/html; charset=windows-1252 (Plain text)");
    470         gotText = MimeUtility.getTextFromPart(p);
    471         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
    472         // Note: These tests does not pass.
    473         //assertEquals(WINDOWS1252_EXPECT, gotText);
    474     }
    475 
    476     /** Tests for various aspects of mimeTypeMatches(String mimeType, String matchAgainst) */
    477     public void testMimeTypeMatches() {
    478         // 1. No match
    479         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", "TEXT/PLAIN"));
    480 
    481         // 2. Match
    482         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "text/plain"));
    483 
    484         // 3. Match (mixed case)
    485         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "TEXT/PLAIN"));
    486         assertTrue(MimeUtility.mimeTypeMatches("TEXT/PLAIN", "text/plain"));
    487 
    488         // 4. Match (wildcards)
    489         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "*/plain"));
    490         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "text/*"));
    491         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "*/*"));
    492 
    493         // 5. No Match (wildcards)
    494         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", "*/plain"));
    495         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", "text/*"));
    496     }
    497 
    498     /** Tests for various aspects of mimeTypeMatches(String mimeType, String[] matchAgainst) */
    499     public void testMimeTypeMatchesArray() {
    500         // 1. Zero-length array
    501         String[] arrayZero = new String[0];
    502         assertFalse(MimeUtility.mimeTypeMatches("text/plain", arrayZero));
    503 
    504         // 2. Single entry, no match
    505         String[] arrayOne = new String[] { "text/plain" };
    506         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", arrayOne));
    507 
    508         // 3. Single entry, match
    509         assertTrue(MimeUtility.mimeTypeMatches("text/plain", arrayOne));
    510 
    511         // 4. Multi entry, no match
    512         String[] arrayTwo = new String[] { "text/plain", "match/this" };
    513         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", arrayTwo));
    514 
    515         // 5. Multi entry, match first
    516         assertTrue(MimeUtility.mimeTypeMatches("text/plain", arrayTwo));
    517 
    518         // 6. Multi entry, match not first
    519         assertTrue(MimeUtility.mimeTypeMatches("match/this", arrayTwo));
    520     }
    521 
    522     // TODO:  tests for decodeBody(InputStream in, String contentTransferEncoding)
    523     // TODO:  tests for collectParts(Part part, ArrayList<Part> viewables, ArrayList<Part> attachments)
    524 
    525 }
    526