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.ByteArrayInputStream; |
---|
23 | import java.io.ByteArrayOutputStream; |
---|
24 | import java.io.IOException; |
---|
25 | import java.io.InputStream; |
---|
26 | import java.io.UnsupportedEncodingException; |
---|
27 | import java.util.Random; |
---|
28 | |
---|
29 | import org.apache.commons.io.output.NullOutputStream; |
---|
30 | |
---|
31 | import junit.framework.TestCase; |
---|
32 | |
---|
33 | public class Base64InputStreamTest extends TestCase { |
---|
34 | |
---|
35 | public void testDecode() throws IOException { |
---|
36 | ByteArrayInputStream bis = null; |
---|
37 | Base64InputStream decoder = null; |
---|
38 | byte[] bytes = null; |
---|
39 | |
---|
40 | /* |
---|
41 | * Simple initial test. |
---|
42 | */ |
---|
43 | bis = new ByteArrayInputStream( |
---|
44 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); |
---|
45 | decoder = new Base64InputStream(bis); |
---|
46 | assertEquals("This is the plain text message!", toString(read(decoder))); |
---|
47 | |
---|
48 | /* |
---|
49 | * Test encoded text padded once, twice and not at all. |
---|
50 | */ |
---|
51 | bis = new ByteArrayInputStream( |
---|
52 | fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl" |
---|
53 | + "IHBhZGRlZCBvbmNlLi4=")); |
---|
54 | decoder = new Base64InputStream(bis); |
---|
55 | assertEquals("This is a text which has to be padded once..", toString(read(decoder))); |
---|
56 | bis = new ByteArrayInputStream( |
---|
57 | fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggaGFzIHRvIGJl" |
---|
58 | + "IHBhZGRlZCB0d2ljZQ==")); |
---|
59 | decoder = new Base64InputStream(bis); |
---|
60 | assertEquals("This is a text which has to be padded twice", toString(read(decoder))); |
---|
61 | bis = new ByteArrayInputStream( |
---|
62 | fromString("VGhpcyBpcyBhIHRleHQgd2hpY2ggd2lsbCBub3Qg" |
---|
63 | + "YmUgcGFkZGVk")); |
---|
64 | decoder = new Base64InputStream(bis); |
---|
65 | assertEquals("This is a text which will not be padded", toString(read(decoder))); |
---|
66 | |
---|
67 | /* |
---|
68 | * Test that non base64 characters are ignored. |
---|
69 | */ |
---|
70 | bis = new ByteArrayInputStream( |
---|
71 | fromString(" &% VGhp\r\ncyBp\r\ncyB0aGUgcGxhaW4g " |
---|
72 | + " \tdGV4dCBtZ?!XNzY*WdlIQ==")); |
---|
73 | decoder = new Base64InputStream(bis); |
---|
74 | assertEquals("This is the plain text message!", toString(read(decoder))); |
---|
75 | |
---|
76 | /* |
---|
77 | * Test that the bytes 0-255 shifted 0, 1 and 2 positions are |
---|
78 | * decoded properly. |
---|
79 | */ |
---|
80 | String s1 = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCU" |
---|
81 | + "mJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0" |
---|
82 | + "xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bxc" |
---|
83 | + "nN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeY" |
---|
84 | + "mZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6" |
---|
85 | + "/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5O" |
---|
86 | + "Xm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="; |
---|
87 | |
---|
88 | String s2 = "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSY" |
---|
89 | + "nKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE" |
---|
90 | + "1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc" |
---|
91 | + "3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZ" |
---|
92 | + "mpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/" |
---|
93 | + "AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5e" |
---|
94 | + "bn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AA=="; |
---|
95 | |
---|
96 | String s3 = "AgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJic" |
---|
97 | + "oKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU" |
---|
98 | + "5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzd" |
---|
99 | + "HV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJma" |
---|
100 | + "m5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8D" |
---|
101 | + "BwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5u" |
---|
102 | + "fo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQ=="; |
---|
103 | |
---|
104 | bis = new ByteArrayInputStream(fromString(s1)); |
---|
105 | decoder = new Base64InputStream(bis); |
---|
106 | bytes = read(decoder); |
---|
107 | |
---|
108 | for (int i = 0; i < bytes.length; i++) { |
---|
109 | assertEquals("Position " + i, bytes[i], (byte) i); |
---|
110 | } |
---|
111 | |
---|
112 | bis = new ByteArrayInputStream(fromString(s2)); |
---|
113 | decoder = new Base64InputStream(bis); |
---|
114 | bytes = read(decoder); |
---|
115 | |
---|
116 | for (int i = 0; i < bytes.length; i++) { |
---|
117 | assertEquals("Position " + i, bytes[i], (byte) (i + 1)); |
---|
118 | } |
---|
119 | |
---|
120 | bis = new ByteArrayInputStream(fromString(s3)); |
---|
121 | decoder = new Base64InputStream(bis); |
---|
122 | bytes = read(decoder); |
---|
123 | |
---|
124 | for (int i = 0; i < bytes.length; i++) { |
---|
125 | assertEquals("Position " + i, bytes[i], (byte) (i + 2)); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public void testDecodePrematureClose() throws IOException { |
---|
130 | ByteArrayInputStream bis = null; |
---|
131 | Base64InputStream decoder = null; |
---|
132 | |
---|
133 | bis = new ByteArrayInputStream( |
---|
134 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); |
---|
135 | decoder = new Base64InputStream(bis); |
---|
136 | assertEquals('T', decoder.read()); |
---|
137 | assertEquals('h', decoder.read()); |
---|
138 | decoder.close(); |
---|
139 | |
---|
140 | try { |
---|
141 | decoder.read(); |
---|
142 | fail(); |
---|
143 | } catch (IOException expected) { |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | public void testRoundtripWithVariousBufferSizes() throws Exception { |
---|
148 | byte[] data = new byte[3719]; |
---|
149 | new Random(0).nextBytes(data); |
---|
150 | |
---|
151 | ByteArrayOutputStream eOut = new ByteArrayOutputStream(); |
---|
152 | CodecUtil.encodeBase64(new ByteArrayInputStream(data), eOut); |
---|
153 | byte[] encoded = eOut.toByteArray(); |
---|
154 | |
---|
155 | for (int bufferSize = 1; bufferSize <= 1009; bufferSize++) { |
---|
156 | ByteArrayInputStream bis = new ByteArrayInputStream(encoded); |
---|
157 | Base64InputStream decoder = new Base64InputStream(bis); |
---|
158 | ByteArrayOutputStream dOut = new ByteArrayOutputStream(); |
---|
159 | |
---|
160 | final byte[] buffer = new byte[bufferSize]; |
---|
161 | int inputLength; |
---|
162 | while (-1 != (inputLength = decoder.read(buffer))) { |
---|
163 | dOut.write(buffer, 0, inputLength); |
---|
164 | } |
---|
165 | |
---|
166 | byte[] decoded = dOut.toByteArray(); |
---|
167 | |
---|
168 | assertEquals(data.length, decoded.length); |
---|
169 | for (int i = 0; i < data.length; i++) { |
---|
170 | assertEquals(data[i], decoded[i]); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Tests {@link InputStream#read()} |
---|
177 | */ |
---|
178 | public void testReadInt() throws Exception { |
---|
179 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
180 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); |
---|
181 | Base64InputStream decoder = new Base64InputStream(bis); |
---|
182 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
183 | |
---|
184 | while (true) { |
---|
185 | int x = decoder.read(); |
---|
186 | if (x == -1) |
---|
187 | break; |
---|
188 | out.write(x); |
---|
189 | } |
---|
190 | |
---|
191 | assertEquals("This is the plain text message!", toString(out |
---|
192 | .toByteArray())); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * Tests {@link InputStream#read(byte[], int, int)} with various offsets |
---|
197 | */ |
---|
198 | public void testReadOffset() throws Exception { |
---|
199 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
200 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlIQ==")); |
---|
201 | Base64InputStream decoder = new Base64InputStream(bis); |
---|
202 | |
---|
203 | byte[] data = new byte[36]; |
---|
204 | for (int i = 0;;) { |
---|
205 | int bytes = decoder.read(data, i, 5); |
---|
206 | if (bytes == -1) |
---|
207 | break; |
---|
208 | i += bytes; |
---|
209 | } |
---|
210 | |
---|
211 | assertEquals("This is the plain text message!\0\0\0\0\0", |
---|
212 | toString(data)); |
---|
213 | } |
---|
214 | |
---|
215 | public void testStrictUnexpectedEof() throws Exception { |
---|
216 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
217 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI")); |
---|
218 | Base64InputStream decoder = new Base64InputStream(bis, true); |
---|
219 | try { |
---|
220 | CodecUtil.copy(decoder, new NullOutputStream()); |
---|
221 | fail(); |
---|
222 | } catch (IOException expected) { |
---|
223 | assertTrue(expected.getMessage().toLowerCase().contains( |
---|
224 | "end of base64 stream")); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | public void testLenientUnexpectedEof() throws Exception { |
---|
229 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
230 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI")); |
---|
231 | Base64InputStream decoder = new Base64InputStream(bis, false); |
---|
232 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
233 | CodecUtil.copy(decoder, out); |
---|
234 | assertEquals("This is the plain text message", toString(out |
---|
235 | .toByteArray())); |
---|
236 | } |
---|
237 | |
---|
238 | public void testStrictUnexpectedPad() throws Exception { |
---|
239 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
240 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI=")); |
---|
241 | Base64InputStream decoder = new Base64InputStream(bis, true); |
---|
242 | try { |
---|
243 | CodecUtil.copy(decoder, new NullOutputStream()); |
---|
244 | fail(); |
---|
245 | } catch (IOException expected) { |
---|
246 | assertTrue(expected.getMessage().toLowerCase().contains("pad")); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | public void testLenientUnexpectedPad() throws Exception { |
---|
251 | ByteArrayInputStream bis = new ByteArrayInputStream( |
---|
252 | fromString("VGhpcyBpcyB0aGUgcGxhaW4gdGV4dCBtZXNzYWdlI=")); |
---|
253 | Base64InputStream decoder = new Base64InputStream(bis, false); |
---|
254 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
255 | CodecUtil.copy(decoder, out); |
---|
256 | assertEquals("This is the plain text message", toString(out |
---|
257 | .toByteArray())); |
---|
258 | } |
---|
259 | |
---|
260 | private byte[] read(InputStream is) throws IOException { |
---|
261 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
---|
262 | int b; |
---|
263 | while ((b = is.read()) != -1) { |
---|
264 | bos.write(b); |
---|
265 | } |
---|
266 | return bos.toByteArray(); |
---|
267 | } |
---|
268 | |
---|
269 | private byte[] fromString(String s) { |
---|
270 | try { |
---|
271 | return s.getBytes("US-ASCII"); |
---|
272 | } catch (UnsupportedEncodingException e) { |
---|
273 | fail(e.getMessage()); |
---|
274 | return null; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | private String toString(byte[] b) { |
---|
279 | try { |
---|
280 | return new String(b, "US-ASCII"); |
---|
281 | } catch (UnsupportedEncodingException e) { |
---|
282 | fail(e.getMessage()); |
---|
283 | return null; |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|