1   /**
2    *
3    * Copyright 2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package javax.mail.internet;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.util.Properties;
26  
27  import javax.activation.DataHandler;
28  import javax.activation.DataSource;
29  import javax.mail.Session;
30  
31  import junit.framework.TestCase;
32  
33  public class MimeUtilityTest extends TestCase {
34  
35      private byte[] encodeBytes = new byte[] { 32, 104, -61, -87, 33, 32, -61, -96, -61, -88, -61, -76, 117, 32, 33, 33, 33 };
36  
37      public void testEncodeDecode() throws Exception {
38  
39          byte [] data = new byte[256];
40          for (int i = 0; i < data.length; i++) {
41              data[i] = (byte)i;
42          }
43  
44          // different lengths test boundary conditions
45          doEncodingTest(data, 256, "uuencode");
46          doEncodingTest(data, 255, "uuencode");
47          doEncodingTest(data, 254, "uuencode");
48  
49          doEncodingTest(data, 256, "binary");
50          doEncodingTest(data, 256, "7bit");
51          doEncodingTest(data, 256, "8bit");
52          doEncodingTest(data, 256, "base64");
53          doEncodingTest(data, 255, "base64");
54          doEncodingTest(data, 254, "base64");
55  
56          doEncodingTest(data, 256, "x-uuencode");
57          doEncodingTest(data, 256, "x-uue");
58          doEncodingTest(data, 256, "quoted-printable");
59          doEncodingTest(data, 255, "quoted-printable");
60          doEncodingTest(data, 254, "quoted-printable");
61      }
62  
63  
64      public void doEncodingTest(byte[] data, int length, String encoding) throws Exception {
65          ByteArrayOutputStream out = new ByteArrayOutputStream();
66          OutputStream encoder = MimeUtility.encode(out, encoding);
67  
68          encoder.write(data, 0, length);
69          encoder.flush();
70  
71          byte[] encodedData = out.toByteArray();
72  
73          ByteArrayInputStream in = new ByteArrayInputStream(encodedData);
74  
75          InputStream decoder = MimeUtility.decode(in, encoding);
76  
77          byte[] decodedData = new byte[length];
78  
79          int count = decoder.read(decodedData);
80  
81          assertEquals(length, count);
82  
83          for (int i = 0; i < length; i++) {
84              assertEquals(data[i], decodedData[i]);
85          }
86      }
87  
88  
89      public void testEncodeWord() throws Exception {
90          assertEquals("abc", MimeUtility.encodeWord("abc"));
91  
92          String encodeString = new String(encodeBytes, "UTF-8");
93          // default code page dependent, hard to directly test the encoded results
94          assertEquals(encodeString, MimeUtility.decodeWord(MimeUtility.encodeWord(encodeString)));
95  
96          String encoded = MimeUtility.encodeWord(encodeString, "UTF-8", "Q");
97          assertEquals("=?UTF-8?Q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?=", encoded);
98          assertEquals(encodeString, MimeUtility.decodeWord(encoded));
99  
100         encoded = MimeUtility.encodeWord(encodeString, "UTF-8", "B");
101         assertEquals("=?UTF-8?B?IGjDqSEgw6DDqMO0dSAhISE=?=", encoded);
102         assertEquals(encodeString, MimeUtility.decodeWord(encoded));
103     }
104 
105 
106     public void testEncodeText() throws Exception {
107         assertEquals("abc", MimeUtility.encodeWord("abc"));
108 
109         String encodeString = new String(encodeBytes, "UTF-8");
110         // default code page dependent, hard to directly test the encoded results
111         assertEquals(encodeString, MimeUtility.decodeText(MimeUtility.encodeText(encodeString)));
112 
113         String encoded = MimeUtility.encodeText(encodeString, "UTF-8", "Q");
114         assertEquals("=?UTF-8?Q?_h=C3=A9!_=C3=A0=C3=A8=C3=B4u_!!!?=", encoded);
115         assertEquals(encodeString, MimeUtility.decodeText(encoded));
116 
117         encoded = MimeUtility.encodeText(encodeString, "UTF-8", "B");
118         assertEquals("=?UTF-8?B?IGjDqSEgw6DDqMO0dSAhISE=?=", encoded);
119         assertEquals(encodeString, MimeUtility.decodeText(encoded));
120     }
121 
122 
123     public void testQuote() throws Exception {
124         assertEquals("abc", MimeUtility.quote("abc", "&*%"));
125         assertEquals("\"abc&\"", MimeUtility.quote("abc&", "&*%"));
126         assertEquals("\"abc\\\"\"", MimeUtility.quote("abc\"", "&*%"));
127         assertEquals("\"abc\\\\\"", MimeUtility.quote("abc\\", "&*%"));
128         assertEquals("\"abc\\\r\"", MimeUtility.quote("abc\r", "&*%"));
129         assertEquals("\"abc\\\n\"", MimeUtility.quote("abc\n", "&*%"));
130     }
131 }