• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@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      */
testEfficientUnfoldAndDecode()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      */
testDecodeSimple()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      */
testUnfoldAndDecodeSimple()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      */
testComplexDecode()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      */
testEfficientFoldAndEncode()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      */
testPaddingOfFoldAndEncode2()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      */
testFoldAndEncode2()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      */
testFoldAndEncode2WithLongSplit()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      */
testFoldAndEncode2Supplemental()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      */
testFoldAndEncode2SupplementalQuotedPrintable()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      */
testGetHeaderParameter()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) */
testFindPartByContentIdTestCase()312     public void testFindPartByContentIdTestCase() throws MessagingException, Exception {
313         final String cid1 = "cid.1@android.com";
314         final Part cid1bp = MessageTestUtils.bodyPart("image/gif", cid1);
315         final String cid2 = "cid.2@android.com";
316         final Part cid2bp = MessageTestUtils.bodyPart("image/gif", "<" + cid2 + ">");
317 
318         final Message msg1 = new MessageBuilder()
319             .setBody(new MultipartBuilder("multipart/related")
320                  .addBodyPart(MessageTestUtils.bodyPart("text/html", null))
321                  .addBodyPart((BodyPart)cid1bp)
322                  .build())
323             .build();
324         // found cid1 part
325         final Part actual1_1 = MimeUtility.findPartByContentId(msg1, cid1);
326         assertEquals("could not found expected content-id part", cid1bp, actual1_1);
327 
328         final Message msg2 = new MessageBuilder()
329             .setBody(new MultipartBuilder("multipart/mixed")
330                 .addBodyPart(MessageTestUtils.bodyPart("image/tiff", "cid.4@android.com"))
331                 .addBodyPart(new MultipartBuilder("multipart/related")
332                     .addBodyPart(new MultipartBuilder("multipart/alternative")
333                         .addBodyPart(MessageTestUtils.bodyPart("text/plain", null))
334                         .addBodyPart(MessageTestUtils.bodyPart("text/html", null))
335                         .buildBodyPart())
336                     .addBodyPart((BodyPart)cid1bp)
337                     .buildBodyPart())
338                 .addBodyPart(MessageTestUtils.bodyPart("image/gif", "cid.3@android.com"))
339                 .addBodyPart((BodyPart)cid2bp)
340                 .build())
341             .build();
342         // found cid1 part
343         final Part actual2_1 = MimeUtility.findPartByContentId(msg2, cid1);
344         assertEquals("found part from related multipart", cid1bp, actual2_1);
345 
346         // found cid2 part
347         final Part actual2_2 = MimeUtility.findPartByContentId(msg2, cid2);
348         assertEquals("found part from mixed multipart", cid2bp, actual2_2);
349     }
350 
351     /** Tests for findPartByContentId(Part part, String contentId) */
testCollectParts()352     public void testCollectParts() throws MessagingException, Exception {
353         // golden cases; these will marked as attachments
354         final String cid1 = "<i_12e8248b4f0874cb>";
355         final Part cid1bp = MessageTestUtils.bodyPart("image/gif; name=\"im1.gif\"", cid1);
356         final String cid2 = "<ii_12e8248b4f0874cb>";
357         final Part cid2bp = MessageTestUtils.bodyPart("image/gif", cid2);
358         cid2bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline; filename=\"im2.gif\"");
359         final String cid3 = "<iii_12e8248b4f0874cb>";
360         final Part cid3bp = MessageTestUtils.bodyPart("image/gif", cid3);
361         cid3bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"im3.gif\"");
362         // error cases; these will NOT be marked as attachments
363         final String cid4 = "<iv_12e8248b4f0874cb>";
364         final Part cid4bp = MessageTestUtils.bodyPart("image/gif", cid4);  // no name attr
365         final String cid5 = "<v_12e8248b4f0874cb>";
366         final Part cid5bp = MessageTestUtils.bodyPart("image/gif", cid5);
367         cid5bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "inline"); // no filename attr
368 
369         // Default content disposition
370         final ArrayList<Part> view1 = new ArrayList<Part>();
371         final ArrayList<Part> attach1 = new ArrayList<Part>();
372         MimeUtility.collectParts(cid1bp, view1, attach1);
373         assertEquals(1, attach1.size());
374         assertEquals(attach1.get(0), cid1bp);
375 
376         // Explicit content disposition of "inline"
377         final ArrayList<Part> view2 = new ArrayList<Part>();
378         final ArrayList<Part> attach2 = new ArrayList<Part>();
379         MimeUtility.collectParts(cid2bp, view2, attach2);
380         assertEquals(1, attach2.size());
381         assertEquals(attach2.get(0), cid2bp);
382 
383         // Explicit content disposition of "attachment"
384         final ArrayList<Part> view3 = new ArrayList<Part>();
385         final ArrayList<Part> attach3 = new ArrayList<Part>();
386         MimeUtility.collectParts(cid3bp, view3, attach3);
387         assertEquals(1, attach3.size());
388         assertEquals(attach3.get(0), cid3bp);
389 
390         // Default content disposition; missing name attribute on content-type
391         final ArrayList<Part> view4 = new ArrayList<Part>();
392         final ArrayList<Part> attach4 = new ArrayList<Part>();
393         MimeUtility.collectParts(cid4bp, view4, attach4);
394         assertEquals(0, attach4.size());
395 
396         // Content disposition of "inline"; missing filename attribute
397         final ArrayList<Part> view5 = new ArrayList<Part>();
398         final ArrayList<Part> attach5 = new ArrayList<Part>();
399         MimeUtility.collectParts(cid5bp, view5, attach5);
400         assertEquals(0, attach5.size());
401     }
402 
403     /** Tests for getTextFromPart(Part part) */
testGetTextFromPartContentTypeCase()404     public void testGetTextFromPartContentTypeCase() throws MessagingException {
405         final String theText = "This is the text of the part";
406         TextBody tb = new TextBody(theText);
407         MimeBodyPart p = new MimeBodyPart();
408 
409         // 1. test basic text/plain mode
410         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
411         p.setBody(tb);
412         String gotText = MimeUtility.getTextFromPart(p);
413         assertEquals(theText, gotText);
414 
415         // 2. mixed case is OK
416         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "TEXT/PLAIN");
417         p.setBody(tb);
418         gotText = MimeUtility.getTextFromPart(p);
419         assertEquals(theText, gotText);
420 
421         // 3. wildcards OK
422         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/other");
423         p.setBody(tb);
424         gotText = MimeUtility.getTextFromPart(p);
425         assertEquals(theText, gotText);
426     }
427 
428     /** Test for usage of Content-Type in getTextFromPart(Part part).
429      *
430      * For example 'Content-Type: text/html; charset=utf-8'
431      *
432      *  If the body part has no mime-type, refuses to parse content as text.
433      *  If the mime-type does not match text/*, it will not get parsed.
434      *  Then, the charset parameter is used, with a default of ASCII.
435      *
436      *  This test works by using a string that is valid Unicode, and is also
437      *  valid when decoded from UTF-8 bytes into Windows-1252 (so that
438      *  auto-detection is not possible), and checks that the correct conversion
439      *  was made, based on the Content-Type header.
440      *
441      */
testContentTypeCharset()442     public void testContentTypeCharset() throws MessagingException {
443         final String UNICODE_EXPECT = "This is some happy unicode text \u263a";
444         // What you get if you encode to UTF-8 (\xe2\x98\xba) and reencode with Windows-1252
445         final String WINDOWS1252_EXPECT = "This is some happy unicode text \u00e2\u02dc\u00ba";
446         TextBody tb = new TextBody(UNICODE_EXPECT);
447         MimeBodyPart p = new MimeBodyPart();
448 
449         String gotText, mimeType, charset;
450         // TEST 0: Standard Content-Type header; no extraneous spaces or fields
451         p.setBody(tb);
452         // We call setHeader after setBody, since setBody overwrites Content-Type
453         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=utf-8");
454         gotText = MimeUtility.getTextFromPart(p);
455         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
456         assertEquals(UNICODE_EXPECT, gotText);
457 
458         p.setBody(tb);
459         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=windows-1252");
460         gotText = MimeUtility.getTextFromPart(p);
461         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
462         assertEquals(WINDOWS1252_EXPECT, gotText);
463 
464         // TEST 1: Extra fields and quotes in Content-Type (from RFC 2045)
465         p.setBody(tb);
466         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
467                     "text/html; prop1 = \"test\"; charset = \"utf-8\"; prop2 = \"test\"");
468         gotText = MimeUtility.getTextFromPart(p);
469         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
470         assertEquals(UNICODE_EXPECT, gotText);
471 
472         p.setBody(tb);
473         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
474                     "text/html; prop1 = \"test\"; charset = \"windows-1252\"; prop2 = \"test\"");
475         gotText = MimeUtility.getTextFromPart(p);
476         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
477         assertEquals(WINDOWS1252_EXPECT, gotText);
478 
479         // TEST 2: Mixed case in Content-Type header:
480         // RFC 2045 says that content types, subtypes and parameter names
481         // are case-insensitive.
482 
483         p.setBody(tb);
484         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "TEXT/HtmL ; CHARseT=utf-8");
485         gotText = MimeUtility.getTextFromPart(p);
486         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
487         assertEquals(UNICODE_EXPECT, gotText);
488 
489         p.setBody(tb);
490         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "TEXT/HtmL ; CHARseT=windows-1252");
491         gotText = MimeUtility.getTextFromPart(p);
492         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
493         assertEquals(WINDOWS1252_EXPECT, gotText);
494 
495         // TEST 3: Comments in Content-Type header field (from RFC 2045)
496         // Thunderbird permits comments after the end of a parameter, as in this example.
497         // Not something that I have seen in the real world outside RFC 2045.
498 
499         p.setBody(tb);
500         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
501                     "text/html; charset=utf-8 (Plain text)");
502         gotText = MimeUtility.getTextFromPart(p);
503         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
504         // Note: This test does not pass.
505         //assertEquals(UNICODE_EXPECT, gotText);
506 
507         p.setBody(tb);
508         p.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
509                     "text/html; charset=windows-1252 (Plain text)");
510         gotText = MimeUtility.getTextFromPart(p);
511         assertTrue(MimeUtility.mimeTypeMatches(p.getMimeType(), "text/html"));
512         // Note: These tests does not pass.
513         //assertEquals(WINDOWS1252_EXPECT, gotText);
514     }
515 
516     /** Tests for various aspects of mimeTypeMatches(String mimeType, String matchAgainst) */
testMimeTypeMatches()517     public void testMimeTypeMatches() {
518         // 1. No match
519         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", "TEXT/PLAIN"));
520 
521         // 2. Match
522         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "text/plain"));
523 
524         // 3. Match (mixed case)
525         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "TEXT/PLAIN"));
526         assertTrue(MimeUtility.mimeTypeMatches("TEXT/PLAIN", "text/plain"));
527 
528         // 4. Match (wildcards)
529         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "*/plain"));
530         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "text/*"));
531         assertTrue(MimeUtility.mimeTypeMatches("text/plain", "*/*"));
532 
533         // 5. No Match (wildcards)
534         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", "*/plain"));
535         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", "text/*"));
536     }
537 
538     /** Tests for various aspects of mimeTypeMatches(String mimeType, String[] matchAgainst) */
testMimeTypeMatchesArray()539     public void testMimeTypeMatchesArray() {
540         // 1. Zero-length array
541         String[] arrayZero = new String[0];
542         assertFalse(MimeUtility.mimeTypeMatches("text/plain", arrayZero));
543 
544         // 2. Single entry, no match
545         String[] arrayOne = new String[] { "text/plain" };
546         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", arrayOne));
547 
548         // 3. Single entry, match
549         assertTrue(MimeUtility.mimeTypeMatches("text/plain", arrayOne));
550 
551         // 4. Multi entry, no match
552         String[] arrayTwo = new String[] { "text/plain", "match/this" };
553         assertFalse(MimeUtility.mimeTypeMatches("foo/bar", arrayTwo));
554 
555         // 5. Multi entry, match first
556         assertTrue(MimeUtility.mimeTypeMatches("text/plain", arrayTwo));
557 
558         // 6. Multi entry, match not first
559         assertTrue(MimeUtility.mimeTypeMatches("match/this", arrayTwo));
560     }
561 
562     // TODO:  tests for decodeBody(InputStream in, String contentTransferEncoding)
563     // TODO:  tests for collectParts(Part part, ArrayList<Part> viewables, ArrayList<Part> attachments)
564 
565 }
566