1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mime4j.codec;
21  
22  import java.io.UnsupportedEncodingException;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.log4j.BasicConfigurator;
27  
28  public class DecoderUtilTest extends TestCase {
29  
30      @Override
31      public void setUp() {
32          BasicConfigurator.resetConfiguration();
33          BasicConfigurator.configure();
34      }
35      /*
36      public void testDecodeEncodedWords() {
37          String s = "=?ISO-2022-JP?B?GyRCTCQbKEobJEI+NRsoShskQkJ6GyhKGyRCOS0bKEo=?= "
38                   + "=?ISO-2022-JP?B?GyRCOXAbKEobJEIiKBsoShskQiU1GyhKGyRCJSQbKEo=?= "
39                   + "=?ISO-2022-JP?B?GyRCJUkbKEobJEIlUxsoShskQiU4GyhKGyRCJU0bKEo=?= "  
40                   + "=?ISO-2022-JP?B?GyRCJTkbKEobJEIkThsoShskQjdoGyhKGyRCRGobKEo=?= "
41                   + "=?ISO-2022-JP?B?GyRCSEcbKEobJEIkRxsoShskQiQ5GyhKGyRCISobKEo=?=";      
42          
43          s = DecoderUtil.decodeEncodedWords(s);
44          System.out.println(s);
45      }*/
46      
47      public void testDecodeB() throws UnsupportedEncodingException {
48          String s = DecoderUtil.decodeB("VGhpcyBpcyB0aGUgcGxhaW4gd"
49                      + "GV4dCBtZXNzYWdlIQ==", "ISO8859-1");
50          assertEquals("This is the plain text message!", s);
51      }
52      
53  
54      public void testDecodeQ() throws UnsupportedEncodingException {
55          String s = DecoderUtil.decodeQ("=e1_=e2=09=E3_=E4_", 
56                                                           "ISO8859-1");
57          assertEquals("\u00e1 \u00e2\t\u00e3 \u00e4 ", s);
58      }
59      
60      public void testDecodeEncodedWords() {
61          assertEquals("", DecoderUtil.decodeEncodedWords(""));
62          assertEquals("Yada yada", DecoderUtil.decodeEncodedWords("Yada yada"));
63          assertEquals("  \u00e1\u00e2\u00e3\t\u00e4", 
64                  DecoderUtil.decodeEncodedWords("=?iso-8859-1?Q?_=20=e1=e2=E3=09=E4?="));
65          assertEquals("Word 1 '  \u00e2\u00e3\t\u00e4'. Word 2 '  \u00e2\u00e3\t\u00e4'", 
66                  DecoderUtil.decodeEncodedWords("Word 1 '=?iso-8859-1?Q?_=20=e2=E3=09=E4?="
67                          + "'. Word 2 '=?iso-8859-1?q?_=20=e2=E3=09=E4?='"));
68          assertEquals("=?iso-8859-YADA?Q?_=20=t1=e2=E3=09=E4?=", 
69                  DecoderUtil.decodeEncodedWords("=?iso-8859-YADA?Q?_=20=t1=e2=E3=09=E4?="));
70          assertEquals("A short text", 
71                  DecoderUtil.decodeEncodedWords("=?US-ASCII?B?QSBzaG9ydCB0ZXh0?="));
72          assertEquals("A short text again!", 
73                  DecoderUtil.decodeEncodedWords("=?US-ASCII?b?QSBzaG9ydCB0ZXh0IGFnYWluIQ==?="));
74  
75          // invalid encoded words should be returned unchanged
76          assertEquals("=?iso8859-1?Q?=", DecoderUtil.decodeEncodedWords("=?iso8859-1?Q?="));
77          assertEquals("=?iso8859-1?b?=", DecoderUtil.decodeEncodedWords("=?iso8859-1?b?="));
78          assertEquals("=?ISO-8859-1?Q?", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?"));
79          assertEquals("=?ISO-8859-1?R?abc?=", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?R?abc?="));
80  
81          // encoded-text requires at least one character according to rfc 2047
82          assertEquals("=?ISO-8859-1?Q??=", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q??="));
83          assertEquals("=?ISO-8859-1?B??=", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?B??="));
84          
85          // white space between encoded words should be removed (MIME4J-104)
86          assertEquals("a", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?="));
87          assertEquals("a b", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= b"));
88          assertEquals("ab", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?="));
89          assertEquals("ab", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?="));
90          assertEquals("ab", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?=\r\n  =?ISO-8859-1?Q?b?="));
91          assertEquals("a b", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a_b?="));
92          assertEquals("a b", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= =?ISO-8859-2?Q?_b?="));
93  
94          // non white space between encoded words should be retained
95          assertEquals("a b c", DecoderUtil.decodeEncodedWords("=?ISO-8859-1?Q?a?= b =?ISO-8859-1?Q?c?="));
96  
97          // text before and after encoded words should be retained
98          assertEquals(" a b c ", DecoderUtil.decodeEncodedWords(" =?ISO-8859-1?Q?a?= b =?ISO-8859-1?Q?c?= "));
99          assertEquals("! a b c !", DecoderUtil.decodeEncodedWords("! =?ISO-8859-1?Q?a?= b =?ISO-8859-1?Q?c?= !"));
100         
101         // Bug detected on June 7, 2005. Decoding the following string caused
102         // OutOfMemoryError.
103         assertEquals("=3?!!\\=?\"!g6P\"!Xp:\"!", DecoderUtil.decodeEncodedWords("=3?!!\\=?\"!g6P\"!Xp:\"!"));
104     }    
105 }