KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > codec > binary > Base64Test


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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 org.apache.commons.codec.binary;
18
19
20 import java.util.Arrays JavaDoc;
21 import java.util.Random JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * @version $Id: Base64Test.java,v 1.15 2004/04/19 01:14:29 ggregory Exp $
27  * @author Apache Software Foundation
28  */

29 public class Base64Test extends TestCase {
30
31     /**
32      * Construct a new instance of this test case.
33      *
34      * @param name Name of the test case
35      */

36     public Base64Test(String JavaDoc name) {
37         super(name);
38     }
39
40
41     /**
42      * Test the Base64 implementation
43      */

44     public void testBase64() {
45         String JavaDoc content = "Hello World";
46         String JavaDoc encodedContent;
47         encodedContent = new String JavaDoc(Base64.encodeBase64(content.getBytes()));
48         assertTrue("encoding hello world", encodedContent.equals("SGVsbG8gV29ybGQ="));
49     }
50
51     /**
52      * Tests conditional true branch for "marker0" test.
53      */

54     public void testDecodePadMarkerIndex2() {
55         assertEquals("A", new String JavaDoc(Base64.decodeBase64("QQ==".getBytes())));
56     }
57     
58     /**
59      * Tests conditional branches for "marker1" test.
60      */

61     public void testDecodePadMarkerIndex3() {
62         assertEquals("AA", new String JavaDoc(Base64.decodeBase64("QUE=".getBytes())));
63         assertEquals("AAA", new String JavaDoc(Base64.decodeBase64("QUFB".getBytes())));
64     }
65     
66     public void testDecodePadOnly() {
67         assertTrue(Base64.decodeBase64("====".getBytes()).length == 0);
68         assertEquals("", new String JavaDoc(Base64.decodeBase64("====".getBytes())));
69     }
70     
71     public void testDecodePadOnlyChunked() {
72         assertTrue(Base64.decodeBase64("====\n".getBytes()).length == 0);
73         assertEquals("", new String JavaDoc(Base64.decodeBase64("====\n".getBytes())));
74     }
75     
76     // encode/decode random arrays from size 0 to size 11
77
public void testEncodeDecodeSmall() {
78         for(int i=0;i<12;i++) {
79             byte[] data = new byte[i];
80             this.getRandom().nextBytes(data);
81             byte[] enc = Base64.encodeBase64(data);
82             assertTrue("\"" + (new String JavaDoc(enc)) + "\" is Base64 data.",Base64.isArrayByteBase64(enc) );
83             byte[] data2 = Base64.decodeBase64(enc);
84             assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data,data2));
85         }
86     }
87
88     // encode/decode a large random array
89
public void testEncodeDecodeRandom() {
90         for(int i=1;i<5;i++) {
91             byte[] data = new byte[this.getRandom().nextInt(10000)+1];
92             this.getRandom().nextBytes(data);
93             byte[] enc = Base64.encodeBase64(data);
94             assertTrue(Base64.isArrayByteBase64(enc));
95             byte[] data2 = Base64.decodeBase64(enc);
96             assertTrue(Arrays.equals(data,data2));
97         }
98     }
99
100     /**
101      * Tests RFC 2045 section 2.1 CRLF definition.
102      */

103     public void testRfc2045Section2Dot1CrLfDefinition() {
104         assertTrue(Arrays.equals(new byte[] {13, 10}, Base64.CHUNK_SEPARATOR));
105     }
106
107     /**
108      * Tests RFC 2045 section 6.8 chuck size definition.
109      */

110     public void testRfc2045Section6Dot8ChunkSizeDefinition() {
111         assertEquals(76, Base64.CHUNK_SIZE);
112     }
113
114     public void testSingletons() {
115         assertEquals("AA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0 })));
116         assertEquals("AQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)1 })));
117         assertEquals("Ag==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)2 })));
118         assertEquals("Aw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)3 })));
119         assertEquals("BA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)4 })));
120         assertEquals("BQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)5 })));
121         assertEquals("Bg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)6 })));
122         assertEquals("Bw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)7 })));
123         assertEquals("CA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)8 })));
124         assertEquals("CQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)9 })));
125         assertEquals("Cg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)10 })));
126         assertEquals("Cw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)11 })));
127         assertEquals("DA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)12 })));
128         assertEquals("DQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)13 })));
129         assertEquals("Dg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)14 })));
130         assertEquals("Dw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)15 })));
131         assertEquals("EA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)16 })));
132         assertEquals("EQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)17 })));
133         assertEquals("Eg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)18 })));
134         assertEquals("Ew==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)19 })));
135         assertEquals("FA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)20 })));
136         assertEquals("FQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)21 })));
137         assertEquals("Fg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)22 })));
138         assertEquals("Fw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)23 })));
139         assertEquals("GA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)24 })));
140         assertEquals("GQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)25 })));
141         assertEquals("Gg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)26 })));
142         assertEquals("Gw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)27 })));
143         assertEquals("HA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)28 })));
144         assertEquals("HQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)29 })));
145         assertEquals("Hg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)30 })));
146         assertEquals("Hw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)31 })));
147         assertEquals("IA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)32 })));
148         assertEquals("IQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)33 })));
149         assertEquals("Ig==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)34 })));
150         assertEquals("Iw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)35 })));
151         assertEquals("JA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)36 })));
152         assertEquals("JQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)37 })));
153         assertEquals("Jg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)38 })));
154         assertEquals("Jw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)39 })));
155         assertEquals("KA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)40 })));
156         assertEquals("KQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)41 })));
157         assertEquals("Kg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)42 })));
158         assertEquals("Kw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)43 })));
159         assertEquals("LA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)44 })));
160         assertEquals("LQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)45 })));
161         assertEquals("Lg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)46 })));
162         assertEquals("Lw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)47 })));
163         assertEquals("MA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)48 })));
164         assertEquals("MQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)49 })));
165         assertEquals("Mg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)50 })));
166         assertEquals("Mw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)51 })));
167         assertEquals("NA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)52 })));
168         assertEquals("NQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)53 })));
169         assertEquals("Ng==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)54 })));
170         assertEquals("Nw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)55 })));
171         assertEquals("OA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)56 })));
172         assertEquals("OQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)57 })));
173         assertEquals("Og==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)58 })));
174         assertEquals("Ow==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)59 })));
175         assertEquals("PA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)60 })));
176         assertEquals("PQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)61 })));
177         assertEquals("Pg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)62 })));
178         assertEquals("Pw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)63 })));
179         assertEquals("QA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)64 })));
180         assertEquals("QQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)65 })));
181         assertEquals("Qg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)66 })));
182         assertEquals("Qw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)67 })));
183         assertEquals("RA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)68 })));
184         assertEquals("RQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)69 })));
185         assertEquals("Rg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)70 })));
186         assertEquals("Rw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)71 })));
187         assertEquals("SA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)72 })));
188         assertEquals("SQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)73 })));
189         assertEquals("Sg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)74 })));
190         assertEquals("Sw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)75 })));
191         assertEquals("TA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)76 })));
192         assertEquals("TQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)77 })));
193         assertEquals("Tg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)78 })));
194         assertEquals("Tw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)79 })));
195         assertEquals("UA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)80 })));
196         assertEquals("UQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)81 })));
197         assertEquals("Ug==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)82 })));
198         assertEquals("Uw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)83 })));
199         assertEquals("VA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)84 })));
200         assertEquals("VQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)85 })));
201         assertEquals("Vg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)86 })));
202         assertEquals("Vw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)87 })));
203         assertEquals("WA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)88 })));
204         assertEquals("WQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)89 })));
205         assertEquals("Wg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)90 })));
206         assertEquals("Ww==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)91 })));
207         assertEquals("XA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)92 })));
208         assertEquals("XQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)93 })));
209         assertEquals("Xg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)94 })));
210         assertEquals("Xw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)95 })));
211         assertEquals("YA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)96 })));
212         assertEquals("YQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)97 })));
213         assertEquals("Yg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)98 })));
214         assertEquals("Yw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)99 })));
215         assertEquals("ZA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)100 })));
216         assertEquals("ZQ==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)101 })));
217         assertEquals("Zg==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)102 })));
218         assertEquals("Zw==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)103 })));
219         assertEquals("aA==",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)104 })));
220     }
221
222     public void testSingletonsChunked() {
223         assertEquals("AA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0 })));
224         assertEquals("AQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)1 })));
225         assertEquals("Ag==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)2 })));
226         assertEquals("Aw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)3 })));
227         assertEquals("BA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)4 })));
228         assertEquals("BQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)5 })));
229         assertEquals("Bg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)6 })));
230         assertEquals("Bw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)7 })));
231         assertEquals("CA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)8 })));
232         assertEquals("CQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)9 })));
233         assertEquals("Cg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)10 })));
234         assertEquals("Cw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)11 })));
235         assertEquals("DA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)12 })));
236         assertEquals("DQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)13 })));
237         assertEquals("Dg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)14 })));
238         assertEquals("Dw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)15 })));
239         assertEquals("EA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)16 })));
240         assertEquals("EQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)17 })));
241         assertEquals("Eg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)18 })));
242         assertEquals("Ew==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)19 })));
243         assertEquals("FA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)20 })));
244         assertEquals("FQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)21 })));
245         assertEquals("Fg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)22 })));
246         assertEquals("Fw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)23 })));
247         assertEquals("GA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)24 })));
248         assertEquals("GQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)25 })));
249         assertEquals("Gg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)26 })));
250         assertEquals("Gw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)27 })));
251         assertEquals("HA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)28 })));
252         assertEquals("HQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)29 })));
253         assertEquals("Hg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)30 })));
254         assertEquals("Hw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)31 })));
255         assertEquals("IA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)32 })));
256         assertEquals("IQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)33 })));
257         assertEquals("Ig==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)34 })));
258         assertEquals("Iw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)35 })));
259         assertEquals("JA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)36 })));
260         assertEquals("JQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)37 })));
261         assertEquals("Jg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)38 })));
262         assertEquals("Jw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)39 })));
263         assertEquals("KA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)40 })));
264         assertEquals("KQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)41 })));
265         assertEquals("Kg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)42 })));
266         assertEquals("Kw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)43 })));
267         assertEquals("LA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)44 })));
268         assertEquals("LQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)45 })));
269         assertEquals("Lg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)46 })));
270         assertEquals("Lw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)47 })));
271         assertEquals("MA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)48 })));
272         assertEquals("MQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)49 })));
273         assertEquals("Mg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)50 })));
274         assertEquals("Mw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)51 })));
275         assertEquals("NA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)52 })));
276         assertEquals("NQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)53 })));
277         assertEquals("Ng==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)54 })));
278         assertEquals("Nw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)55 })));
279         assertEquals("OA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)56 })));
280         assertEquals("OQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)57 })));
281         assertEquals("Og==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)58 })));
282         assertEquals("Ow==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)59 })));
283         assertEquals("PA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)60 })));
284         assertEquals("PQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)61 })));
285         assertEquals("Pg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)62 })));
286         assertEquals("Pw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)63 })));
287         assertEquals("QA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)64 })));
288         assertEquals("QQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)65 })));
289         assertEquals("Qg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)66 })));
290         assertEquals("Qw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)67 })));
291         assertEquals("RA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)68 })));
292         assertEquals("RQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)69 })));
293         assertEquals("Rg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)70 })));
294         assertEquals("Rw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)71 })));
295         assertEquals("SA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)72 })));
296         assertEquals("SQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)73 })));
297         assertEquals("Sg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)74 })));
298         assertEquals("Sw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)75 })));
299         assertEquals("TA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)76 })));
300         assertEquals("TQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)77 })));
301         assertEquals("Tg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)78 })));
302         assertEquals("Tw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)79 })));
303         assertEquals("UA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)80 })));
304         assertEquals("UQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)81 })));
305         assertEquals("Ug==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)82 })));
306         assertEquals("Uw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)83 })));
307         assertEquals("VA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)84 })));
308         assertEquals("VQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)85 })));
309         assertEquals("Vg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)86 })));
310         assertEquals("Vw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)87 })));
311         assertEquals("WA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)88 })));
312         assertEquals("WQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)89 })));
313         assertEquals("Wg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)90 })));
314         assertEquals("Ww==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)91 })));
315         assertEquals("XA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)92 })));
316         assertEquals("XQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)93 })));
317         assertEquals("Xg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)94 })));
318         assertEquals("Xw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)95 })));
319         assertEquals("YA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)96 })));
320         assertEquals("YQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)97 })));
321         assertEquals("Yg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)98 })));
322         assertEquals("Yw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)99 })));
323         assertEquals("ZA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)100 })));
324         assertEquals("ZQ==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)101 })));
325         assertEquals("Zg==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)102 })));
326         assertEquals("Zw==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)103 })));
327         assertEquals("aA==\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)104 })));
328     }
329
330     public void testTriplets() {
331         assertEquals("AAAA",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)0 })));
332         assertEquals("AAAB",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)1 })));
333         assertEquals("AAAC",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)2 })));
334         assertEquals("AAAD",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)3 })));
335         assertEquals("AAAE",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)4 })));
336         assertEquals("AAAF",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)5 })));
337         assertEquals("AAAG",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)6 })));
338         assertEquals("AAAH",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)7 })));
339         assertEquals("AAAI",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)8 })));
340         assertEquals("AAAJ",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)9 })));
341         assertEquals("AAAK",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)10 })));
342         assertEquals("AAAL",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)11 })));
343         assertEquals("AAAM",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)12 })));
344         assertEquals("AAAN",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)13 })));
345         assertEquals("AAAO",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)14 })));
346         assertEquals("AAAP",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)15 })));
347         assertEquals("AAAQ",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)16 })));
348         assertEquals("AAAR",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)17 })));
349         assertEquals("AAAS",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)18 })));
350         assertEquals("AAAT",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)19 })));
351         assertEquals("AAAU",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)20 })));
352         assertEquals("AAAV",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)21 })));
353         assertEquals("AAAW",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)22 })));
354         assertEquals("AAAX",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)23 })));
355         assertEquals("AAAY",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)24 })));
356         assertEquals("AAAZ",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)25 })));
357         assertEquals("AAAa",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)26 })));
358         assertEquals("AAAb",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)27 })));
359         assertEquals("AAAc",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)28 })));
360         assertEquals("AAAd",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)29 })));
361         assertEquals("AAAe",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)30 })));
362         assertEquals("AAAf",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)31 })));
363         assertEquals("AAAg",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)32 })));
364         assertEquals("AAAh",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)33 })));
365         assertEquals("AAAi",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)34 })));
366         assertEquals("AAAj",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)35 })));
367         assertEquals("AAAk",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)36 })));
368         assertEquals("AAAl",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)37 })));
369         assertEquals("AAAm",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)38 })));
370         assertEquals("AAAn",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)39 })));
371         assertEquals("AAAo",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)40 })));
372         assertEquals("AAAp",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)41 })));
373         assertEquals("AAAq",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)42 })));
374         assertEquals("AAAr",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)43 })));
375         assertEquals("AAAs",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)44 })));
376         assertEquals("AAAt",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)45 })));
377         assertEquals("AAAu",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)46 })));
378         assertEquals("AAAv",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)47 })));
379         assertEquals("AAAw",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)48 })));
380         assertEquals("AAAx",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)49 })));
381         assertEquals("AAAy",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)50 })));
382         assertEquals("AAAz",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)51 })));
383         assertEquals("AAA0",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)52 })));
384         assertEquals("AAA1",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)53 })));
385         assertEquals("AAA2",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)54 })));
386         assertEquals("AAA3",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)55 })));
387         assertEquals("AAA4",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)56 })));
388         assertEquals("AAA5",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)57 })));
389         assertEquals("AAA6",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)58 })));
390         assertEquals("AAA7",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)59 })));
391         assertEquals("AAA8",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)60 })));
392         assertEquals("AAA9",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)61 })));
393         assertEquals("AAA+",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)62 })));
394         assertEquals("AAA/",new String JavaDoc(Base64.encodeBase64(new byte[] { (byte)0, (byte)0, (byte)63 })));
395     }
396
397     public void testTripletsChunked() {
398         assertEquals("AAAA\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)0 })));
399         assertEquals("AAAB\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)1 })));
400         assertEquals("AAAC\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)2 })));
401         assertEquals("AAAD\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)3 })));
402         assertEquals("AAAE\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)4 })));
403         assertEquals("AAAF\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)5 })));
404         assertEquals("AAAG\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)6 })));
405         assertEquals("AAAH\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)7 })));
406         assertEquals("AAAI\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)8 })));
407         assertEquals("AAAJ\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)9 })));
408         assertEquals("AAAK\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)10 })));
409         assertEquals("AAAL\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)11 })));
410         assertEquals("AAAM\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)12 })));
411         assertEquals("AAAN\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)13 })));
412         assertEquals("AAAO\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)14 })));
413         assertEquals("AAAP\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)15 })));
414         assertEquals("AAAQ\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)16 })));
415         assertEquals("AAAR\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)17 })));
416         assertEquals("AAAS\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)18 })));
417         assertEquals("AAAT\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)19 })));
418         assertEquals("AAAU\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)20 })));
419         assertEquals("AAAV\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)21 })));
420         assertEquals("AAAW\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)22 })));
421         assertEquals("AAAX\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)23 })));
422         assertEquals("AAAY\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)24 })));
423         assertEquals("AAAZ\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)25 })));
424         assertEquals("AAAa\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)26 })));
425         assertEquals("AAAb\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)27 })));
426         assertEquals("AAAc\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)28 })));
427         assertEquals("AAAd\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)29 })));
428         assertEquals("AAAe\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)30 })));
429         assertEquals("AAAf\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)31 })));
430         assertEquals("AAAg\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)32 })));
431         assertEquals("AAAh\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)33 })));
432         assertEquals("AAAi\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)34 })));
433         assertEquals("AAAj\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)35 })));
434         assertEquals("AAAk\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)36 })));
435         assertEquals("AAAl\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)37 })));
436         assertEquals("AAAm\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)38 })));
437         assertEquals("AAAn\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)39 })));
438         assertEquals("AAAo\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)40 })));
439         assertEquals("AAAp\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)41 })));
440         assertEquals("AAAq\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)42 })));
441         assertEquals("AAAr\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)43 })));
442         assertEquals("AAAs\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)44 })));
443         assertEquals("AAAt\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)45 })));
444         assertEquals("AAAu\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)46 })));
445         assertEquals("AAAv\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)47 })));
446         assertEquals("AAAw\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)48 })));
447         assertEquals("AAAx\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)49 })));
448         assertEquals("AAAy\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)50 })));
449         assertEquals("AAAz\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)51 })));
450         assertEquals("AAA0\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)52 })));
451         assertEquals("AAA1\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)53 })));
452         assertEquals("AAA2\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)54 })));
453         assertEquals("AAA3\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)55 })));
454         assertEquals("AAA4\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)56 })));
455         assertEquals("AAA5\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)57 })));
456         assertEquals("AAA6\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)58 })));
457         assertEquals("AAA7\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)59 })));
458         assertEquals("AAA8\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)60 })));
459         assertEquals("AAA9\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)61 })));
460         assertEquals("AAA+\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)62 })));
461         assertEquals("AAA/\r\n",new String JavaDoc(Base64.encodeBase64Chunked(new byte[] { (byte)0, (byte)0, (byte)63 })));
462     }
463
464     public void testKnownEncodings() {
465         assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==",new String JavaDoc(Base64.encodeBase64("The quick brown fox jumped over the lazy dogs.".getBytes())));
466     assertEquals("YmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJs\r\nYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo\r\nIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi\r\nbGFoIGJsYWg=\r\n",new String JavaDoc(Base64.encodeBase64Chunked("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah".getBytes())));
467         assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==",new String JavaDoc(Base64.encodeBase64("It was the best of times, it was the worst of times.".getBytes())));
468         assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==",new String JavaDoc(Base64.encodeBase64("http://jakarta.apache.org/commmons".getBytes())));
469         assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==",new String JavaDoc(Base64.encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes())));
470         assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=",new String JavaDoc(Base64.encodeBase64("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }".getBytes())));
471         assertEquals("eHl6enkh",new String JavaDoc(Base64.encodeBase64("xyzzy!".getBytes())));
472     }
473
474     public void testKnownDecodings() {
475         assertEquals("The quick brown fox jumped over the lazy dogs.",new String JavaDoc(Base64.decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes())));
476         assertEquals("It was the best of times, it was the worst of times.",new String JavaDoc(Base64.decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes())));
477         assertEquals("http://jakarta.apache.org/commmons",new String JavaDoc(Base64.decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes())));
478         assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",new String JavaDoc(Base64.decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes())));
479         assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",new String JavaDoc(Base64.decodeBase64("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=".getBytes())));
480         assertEquals("xyzzy!",new String JavaDoc(Base64.decodeBase64("eHl6enkh".getBytes())));
481         }
482
483     public void testNonBase64Test() throws Exception JavaDoc {
484
485         byte[] bArray = { '%' };
486
487         assertFalse( "Invalid Base64 array was incorrectly validated as " +
488                      "an array of Base64 encoded data",
489                      Base64.isArrayByteBase64( bArray ) );
490         
491         boolean exceptionThrown = false;
492
493         try {
494             Base64 b64 = new Base64();
495             byte[] result = b64.decode( bArray );
496             
497             assertTrue( "The result should be empty as the test encoded content did " +
498                 "not contain any valid base 64 characters", result.length == 0);
499         }
500         catch( Exception JavaDoc e ) {
501             exceptionThrown = true;
502         }
503
504         assertFalse( "Exception was thrown when trying to decode " +
505                     "invalid base64 encoded data - RFC 2045 requires that all " +
506                     "non base64 character be discarded, an exception should not" +
507                     " have been thrown", exceptionThrown );
508           
509
510     }
511     
512     public void testIgnoringNonBase64InDecode() throws Exception JavaDoc {
513         assertEquals("The quick brown fox jumped over the lazy dogs.",new String JavaDoc(Base64.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes())));
514     }
515
516     public void testObjectDecodeWithInvalidParameter() throws Exception JavaDoc {
517         boolean exceptionThrown = false;
518
519         Base64 b64 = new Base64();
520
521         try {
522             Object JavaDoc o = new String JavaDoc( "Yadayadayada" );
523             b64.decode( o );
524         } catch( Exception JavaDoc e ) {
525             exceptionThrown = true;
526         }
527
528         assertTrue( "decode(Object) didn't throw an exception when passed a " +
529                     "String object", exceptionThrown );
530     }
531
532     public void testObjectDecodeWithValidParameter() throws Exception JavaDoc {
533
534         String JavaDoc original = "Hello World!";
535         byte[] bArray =
536             Base64.encodeBase64( (new String JavaDoc(original)).getBytes() );
537         Object JavaDoc o = bArray;
538         
539         Base64 b64 = new Base64();
540         Object JavaDoc oDecoded = b64.decode( o );
541         byte[] baDecoded = (byte[]) oDecoded;
542         String JavaDoc dest = new String JavaDoc( baDecoded );
543
544         assertTrue( "dest string down not equal original",
545                     dest.equals( original ) );
546     }
547
548     public void testObjectEncodeWithInvalidParameter() throws Exception JavaDoc {
549         boolean exceptionThrown = false;
550
551         Base64 b64 = new Base64();
552
553         try {
554             Object JavaDoc o = new String JavaDoc( "Yadayadayada" );
555             b64.encode( o );
556         } catch( Exception JavaDoc e ) {
557             exceptionThrown = true;
558         }
559
560         assertTrue( "encode(Object) didn't throw an exception when passed a " +
561                     "String object", exceptionThrown );
562     }
563
564     public void testObjectEncodeWithValidParameter() throws Exception JavaDoc {
565
566         String JavaDoc original = "Hello World!";
567         byte[] origBytes = original.getBytes();
568         Object JavaDoc origObj = origBytes;
569
570         Base64 b64 = new Base64();
571         Object JavaDoc oEncoded = b64.encode( origObj );
572         byte[] bArray =
573             Base64.decodeBase64( (byte[]) oEncoded );
574         String JavaDoc dest = new String JavaDoc( bArray );
575
576         assertTrue( "dest string down not equal original",
577                     dest.equals( original ) );
578     }
579
580     public void testDecodeWithWhitespace() throws Exception JavaDoc {
581
582         String JavaDoc orig = "I am a late night coder.";
583
584         byte[] encodedArray = Base64.encodeBase64( orig.getBytes() );
585         StringBuffer JavaDoc intermediate =
586             new StringBuffer JavaDoc( new String JavaDoc(encodedArray) );
587
588         intermediate.insert( 2, ' ' );
589         intermediate.insert( 5, '\t' );
590         intermediate.insert( 10, '\r' );
591         intermediate.insert( 15, '\n' );
592
593         byte[] encodedWithWS = intermediate.toString().getBytes();
594         byte[] decodedWithWS = Base64.decodeBase64( encodedWithWS );
595
596         String JavaDoc dest = new String JavaDoc( decodedWithWS );
597
598         assertTrue( "Dest string doesn't equals the original",
599                     dest.equals( orig ) );
600     }
601
602     public void testDiscardWhitespace() throws Exception JavaDoc {
603
604         String JavaDoc orig = "I am a late night coder.";
605
606         byte[] encodedArray = Base64.encodeBase64( orig.getBytes() );
607         StringBuffer JavaDoc intermediate =
608             new StringBuffer JavaDoc( new String JavaDoc(encodedArray) );
609
610         intermediate.insert( 2, ' ' );
611         intermediate.insert( 5, '\t' );
612         intermediate.insert( 10, '\r' );
613         intermediate.insert( 15, '\n' );
614
615         byte[] encodedWithWS = intermediate.toString().getBytes();
616         byte[] encodedNoWS = Base64.discardWhitespace( encodedWithWS );
617         byte[] decodedWithWS = Base64.decodeBase64( encodedWithWS );
618         byte[] decodedNoWS = Base64.decodeBase64( encodedNoWS );
619
620         String JavaDoc destFromWS = new String JavaDoc( decodedWithWS );
621         String JavaDoc destFromNoWS = new String JavaDoc( decodedNoWS );
622
623         assertTrue( "Dest string doesn't eausl original",
624                 destFromWS.equals( orig ) );
625         assertTrue( "Dest string doesn't eausl original",
626                 destFromNoWS.equals( orig ) );
627     }
628
629     // -------------------------------------------------------- Private Methods
630

631     private String JavaDoc toString(byte[] data) {
632         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
633         for(int i=0;i<data.length;i++) {
634             buf.append(data[i]);
635             if(i != data.length-1) {
636                 buf.append(",");
637             }
638         }
639         return buf.toString();
640     }
641
642     // ------------------------------------------------------------------------
643

644     private Random JavaDoc _random = new Random JavaDoc();
645
646     /**
647      * @return Returns the _random.
648      */

649     public Random JavaDoc getRandom() {
650         return this._random;
651     }
652
653 }
654
Popular Tags