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