001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.codec.binary;
019    
020    import static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertFalse;
022    import static org.junit.Assert.assertNotNull;
023    import static org.junit.Assert.assertTrue;
024    import static org.junit.Assert.fail;
025    
026    import java.math.BigInteger;
027    import java.util.Arrays;
028    import java.util.Random;
029    
030    import org.apache.commons.codec.Charsets;
031    import org.apache.commons.codec.DecoderException;
032    import org.apache.commons.codec.EncoderException;
033    import org.junit.Ignore;
034    import org.junit.Test;
035    
036    /**
037     * Test cases for Base64 class.
038     *
039     * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>
040     * @version $Id: Base64Test.html 889935 2013-12-11 05:05:13Z ggregory $
041     */
042    public class Base64Test {
043    
044        private final Random _random = new Random();
045    
046        /**
047         * @return Returns the _random.
048         */
049        public Random getRandom() {
050            return this._random;
051        }
052    
053        /**
054         * Test the isStringBase64 method.
055         */
056        @Test
057        public void testIsStringBase64() {
058            String nullString = null;
059            String emptyString = "";
060            String validString = "abc===defg\n\r123456\r789\r\rABC\n\nDEF==GHI\r\nJKL==============";
061            String invalidString = validString + (char)0; // append null character
062    
063            try {
064                Base64.isBase64(nullString);
065                fail("Base64.isStringBase64() should not be null-safe.");
066            } catch (NullPointerException npe) {
067                assertNotNull("Base64.isStringBase64() should not be null-safe.", npe);
068            }
069    
070            assertTrue("Base64.isStringBase64(empty-string) is true", Base64.isBase64(emptyString));
071            assertTrue("Base64.isStringBase64(valid-string) is true", Base64.isBase64(validString));
072            assertFalse("Base64.isStringBase64(invalid-string) is false", Base64.isBase64(invalidString));
073        }
074    
075        /**
076         * Test the Base64 implementation
077         */
078        @Test
079        public void testBase64() {
080            String content = "Hello World";
081            String encodedContent;
082            byte[] encodedBytes = Base64.encodeBase64(StringUtils.getBytesUtf8(content));
083            encodedContent = StringUtils.newStringUtf8(encodedBytes);
084            assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);
085    
086            Base64 b64 = new Base64(BaseNCodec.MIME_CHUNK_SIZE, null);  // null lineSeparator same as saying no-chunking
087            encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
088            encodedContent = StringUtils.newStringUtf8(encodedBytes);
089            assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);
090    
091            b64 = new Base64(0, null);  // null lineSeparator same as saying no-chunking
092            encodedBytes = b64.encode(StringUtils.getBytesUtf8(content));
093            encodedContent = StringUtils.newStringUtf8(encodedBytes);
094            assertEquals("encoding hello world", "SGVsbG8gV29ybGQ=", encodedContent);
095    
096            // bogus characters to decode (to skip actually) {e-acute*6}
097            byte[] decode = b64.decode("SGVsbG{\u00e9\u00e9\u00e9\u00e9\u00e9\u00e9}8gV29ybGQ=");
098            String decodeString = StringUtils.newStringUtf8(decode);
099            assertEquals("decode hello world", "Hello World", decodeString);
100        }
101    
102        /**
103         * Tests Base64.encodeBase64().
104         */
105        @Test
106        public void testChunkedEncodeMultipleOf76() {
107            byte[] expectedEncode = Base64.encodeBase64(Base64TestData.DECODED, true);
108            // convert to "\r\n" so we're equal to the old openssl encoding test stored
109            // in Base64TestData.ENCODED_76_CHARS_PER_LINE:
110            String actualResult = Base64TestData.ENCODED_76_CHARS_PER_LINE.replaceAll("\n", "\r\n");
111            byte[] actualEncode = StringUtils.getBytesUtf8(actualResult);
112            assertTrue("chunkedEncodeMultipleOf76", Arrays.equals(expectedEncode, actualEncode));
113        }
114    
115        /**
116         * CODEC-68: isBase64 throws ArrayIndexOutOfBoundsException on some non-BASE64 bytes
117         */
118        @Test
119        public void testCodec68() {
120            byte[] x = new byte[]{'n', 'A', '=', '=', (byte) 0x9c};
121            Base64.decodeBase64(x);
122        }
123    
124        @Test
125        public void testCodeInteger1() {
126            String encodedInt1 = "li7dzDacuo67Jg7mtqEm2TRuOMU=";
127            BigInteger bigInt1 = new BigInteger("85739377120809420210425962799" + "0318636601332086981");
128    
129            assertEquals(encodedInt1, new String(Base64.encodeInteger(bigInt1)));
130            assertEquals(bigInt1, Base64.decodeInteger(encodedInt1.getBytes(Charsets.UTF_8)));
131        }
132    
133        @Test
134        public void testCodeInteger2() {
135            String encodedInt2 = "9B5ypLY9pMOmtxCeTDHgwdNFeGs=";
136            BigInteger bigInt2 = new BigInteger("13936727572861167254666467268" + "91466679477132949611");
137    
138            assertEquals(encodedInt2, new String(Base64.encodeInteger(bigInt2)));
139            assertEquals(bigInt2, Base64.decodeInteger(encodedInt2.getBytes(Charsets.UTF_8)));
140        }
141    
142        @Test
143        public void testCodeInteger3() {
144            String encodedInt3 = "FKIhdgaG5LGKiEtF1vHy4f3y700zaD6QwDS3IrNVGzNp2" + "rY+1LFWTK6D44AyiC1n8uWz1itkYMZF0/aKDK0Yjg==";
145            BigInteger bigInt3 = new BigInteger("10806548154093873461951748545"
146                + "1196989136416448805819079363524309897749044958112417136240557"
147                + "4495062430572478766856090958495998158114332651671116876320938126");
148    
149            assertEquals(encodedInt3, new String(Base64.encodeInteger(bigInt3)));
150            assertEquals(bigInt3, Base64.decodeInteger(encodedInt3.getBytes(Charsets.UTF_8)));
151        }
152    
153        @Test
154        public void testCodeInteger4() {
155            String encodedInt4 = "ctA8YGxrtngg/zKVvqEOefnwmViFztcnPBYPlJsvh6yKI"
156                + "4iDm68fnp4Mi3RrJ6bZAygFrUIQLxLjV+OJtgJAEto0xAs+Mehuq1DkSFEpP3o"
157                + "DzCTOsrOiS1DwQe4oIb7zVk/9l7aPtJMHW0LVlMdwZNFNNJoqMcT2ZfCPrfvYv"
158                + "Q0=";
159            BigInteger bigInt4 = new BigInteger("80624726256040348115552042320"
160                + "6968135001872753709424419772586693950232350200555646471175944"
161                + "519297087885987040810778908507262272892702303774422853675597"
162                + "748008534040890923814202286633163248086055216976551456088015"
163                + "338880713818192088877057717530169381044092839402438015097654"
164                + "53542091716518238707344493641683483917");
165    
166            assertEquals(encodedInt4, new String(Base64.encodeInteger(bigInt4)));
167            assertEquals(bigInt4, Base64.decodeInteger(encodedInt4.getBytes(Charsets.UTF_8)));
168        }
169    
170        @Test
171        public void testCodeIntegerEdgeCases() {
172            // TODO
173        }
174    
175        @Test
176        public void testCodeIntegerNull() {
177            try {
178                Base64.encodeInteger(null);
179                fail("Exception not thrown when passing in null to encodeInteger(BigInteger)");
180            } catch (NullPointerException npe) {
181                // expected
182            } catch (Exception e) {
183                fail("Incorrect Exception caught when passing in null to encodeInteger(BigInteger)");
184            }
185        }
186    
187        @Test
188        public void testConstructors() {
189            Base64 base64;
190            base64 = new Base64();
191            base64 = new Base64(-1);
192            base64 = new Base64(-1, new byte[]{});
193            base64 = new Base64(64, new byte[]{});
194            try {
195                base64 = new Base64(-1, new byte[]{'A'}); // TODO do we need to check sep if len = -1?
196                fail("Should have rejected attempt to use 'A' as a line separator");
197            } catch (IllegalArgumentException ignored) {
198                // Expected
199            }
200            try {
201                base64 = new Base64(64, new byte[]{'A'});
202                fail("Should have rejected attempt to use 'A' as a line separator");
203            } catch (IllegalArgumentException ignored) {
204                // Expected
205            }
206            try {
207                base64 = new Base64(64, new byte[]{'='});
208                fail("Should have rejected attempt to use '=' as a line separator");
209            } catch (IllegalArgumentException ignored) {
210                // Expected
211            }
212            base64 = new Base64(64, new byte[]{'$'}); // OK
213            try {
214                base64 = new Base64(64, new byte[]{'A', '$'});
215                fail("Should have rejected attempt to use 'A$' as a line separator");
216            } catch (IllegalArgumentException ignored) {
217                // Expected
218            }
219            base64 = new Base64(64, new byte[]{' ', '$', '\n', '\r', '\t'}); // OK
220            assertNotNull(base64);
221        }
222    
223        @Test
224        public void testConstructor_Int_ByteArray_Boolean() {
225            Base64 base64 = new Base64(65, new byte[]{'\t'}, false);
226            byte[] encoded = base64.encode(Base64TestData.DECODED);
227            String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE;
228            expectedResult = expectedResult.replace('\n', '\t');
229            String result = StringUtils.newStringUtf8(encoded);
230            assertEquals("new Base64(65, \\t, false)", expectedResult, result);
231        }
232    
233        @Test
234        public void testConstructor_Int_ByteArray_Boolean_UrlSafe() {
235            // url-safe variation
236            Base64 base64 = new Base64(64, new byte[]{'\t'}, true);
237            byte[] encoded = base64.encode(Base64TestData.DECODED);
238            String expectedResult = Base64TestData.ENCODED_64_CHARS_PER_LINE;
239            expectedResult = expectedResult.replaceAll("=", ""); // url-safe has no == padding.
240            expectedResult = expectedResult.replace('\n', '\t');
241            expectedResult = expectedResult.replace('+', '-');
242            expectedResult = expectedResult.replace('/', '_');
243            String result = StringUtils.newStringUtf8(encoded);
244            assertEquals("new Base64(64, \\t, true)", result, expectedResult);
245        }
246    
247        /**
248         * Tests conditional true branch for "marker0" test.
249         */
250        @Test
251        public void testDecodePadMarkerIndex2() {
252            assertEquals("A", new String(Base64.decodeBase64("QQ==".getBytes(Charsets.UTF_8))));
253        }
254    
255        /**
256         * Tests conditional branches for "marker1" test.
257         */
258        @Test
259        public void testDecodePadMarkerIndex3() {
260            assertEquals("AA", new String(Base64.decodeBase64("QUE=".getBytes(Charsets.UTF_8))));
261            assertEquals("AAA", new String(Base64.decodeBase64("QUFB".getBytes(Charsets.UTF_8))));
262        }
263    
264        @Test
265        public void testDecodePadOnly() {
266            assertEquals(0, Base64.decodeBase64("====".getBytes(Charsets.UTF_8)).length);
267            assertEquals("", new String(Base64.decodeBase64("====".getBytes(Charsets.UTF_8))));
268            // Test truncated padding
269            assertEquals(0, Base64.decodeBase64("===".getBytes(Charsets.UTF_8)).length);
270            assertEquals(0, Base64.decodeBase64("==".getBytes(Charsets.UTF_8)).length);
271            assertEquals(0, Base64.decodeBase64("=".getBytes(Charsets.UTF_8)).length);
272            assertEquals(0, Base64.decodeBase64("".getBytes(Charsets.UTF_8)).length);
273        }
274    
275        @Test
276        public void testDecodePadOnlyChunked() {
277            assertEquals(0, Base64.decodeBase64("====\n".getBytes(Charsets.UTF_8)).length);
278            assertEquals("", new String(Base64.decodeBase64("====\n".getBytes(Charsets.UTF_8))));
279            // Test truncated padding
280            assertEquals(0, Base64.decodeBase64("===\n".getBytes(Charsets.UTF_8)).length);
281            assertEquals(0, Base64.decodeBase64("==\n".getBytes(Charsets.UTF_8)).length);
282            assertEquals(0, Base64.decodeBase64("=\n".getBytes(Charsets.UTF_8)).length);
283            assertEquals(0, Base64.decodeBase64("\n".getBytes(Charsets.UTF_8)).length);
284        }
285    
286        @Test
287        public void testDecodeWithWhitespace() throws Exception {
288    
289            String orig = "I am a late night coder.";
290    
291            byte[] encodedArray = Base64.encodeBase64(orig.getBytes(Charsets.UTF_8));
292            StringBuilder intermediate = new StringBuilder(new String(encodedArray));
293    
294            intermediate.insert(2, ' ');
295            intermediate.insert(5, '\t');
296            intermediate.insert(10, '\r');
297            intermediate.insert(15, '\n');
298    
299            byte[] encodedWithWS = intermediate.toString().getBytes(Charsets.UTF_8);
300            byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
301    
302            String dest = new String(decodedWithWS);
303    
304            assertEquals("Dest string doesn't equal the original", orig, dest);
305        }
306    
307        /**
308         * Test encode and decode of empty byte array.
309         */
310        @Test
311        public void testEmptyBase64() {
312            byte[] empty = new byte[0];
313            byte[] result = Base64.encodeBase64(empty);
314            assertEquals("empty base64 encode", 0, result.length);
315            assertEquals("empty base64 encode", null, Base64.encodeBase64(null));
316    
317            empty = new byte[0];
318            result = Base64.decodeBase64(empty);
319            assertEquals("empty base64 decode", 0, result.length);
320            assertEquals("empty base64 encode", null, Base64.decodeBase64((byte[]) null));
321        }
322    
323        // encode/decode a large random array
324        @Test
325        public void testEncodeDecodeRandom() {
326            for (int i = 1; i < 5; i++) {
327                byte[] data = new byte[this.getRandom().nextInt(10000) + 1];
328                this.getRandom().nextBytes(data);
329                byte[] enc = Base64.encodeBase64(data);
330                assertTrue(Base64.isBase64(enc));
331                byte[] data2 = Base64.decodeBase64(enc);
332                assertTrue(Arrays.equals(data, data2));
333            }
334        }
335    
336        // encode/decode random arrays from size 0 to size 11
337        @Test
338        public void testEncodeDecodeSmall() {
339            for (int i = 0; i < 12; i++) {
340                byte[] data = new byte[i];
341                this.getRandom().nextBytes(data);
342                byte[] enc = Base64.encodeBase64(data);
343                assertTrue("\"" + new String(enc) + "\" is Base64 data.", Base64.isBase64(enc));
344                byte[] data2 = Base64.decodeBase64(enc);
345                assertTrue(toString(data) + " equals " + toString(data2), Arrays.equals(data, data2));
346            }
347        }
348    
349        @Test
350        public void testEncodeOverMaxSize() throws Exception {
351            testEncodeOverMaxSize(-1);
352            testEncodeOverMaxSize(0);
353            testEncodeOverMaxSize(1);
354            testEncodeOverMaxSize(2);
355        }
356    
357        @Test
358        public void testCodec112() { // size calculation assumes always chunked
359            byte[] in = new byte[] {0};
360            byte[] out=Base64.encodeBase64(in);
361            Base64.encodeBase64(in, false, false, out.length);
362        }
363    
364        private void testEncodeOverMaxSize(int maxSize) throws Exception {
365            try {
366                Base64.encodeBase64(Base64TestData.DECODED, true, false, maxSize);
367                fail("Expected " + IllegalArgumentException.class.getName());
368            } catch (IllegalArgumentException e) {
369                // Expected
370            }
371        }
372    
373        @Test
374        public void testIgnoringNonBase64InDecode() throws Exception {
375            assertEquals("The quick brown fox jumped over the lazy dogs.", new String(Base64
376                    .decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes(Charsets.UTF_8))));
377        }
378    
379        @Test
380        public void testIsArrayByteBase64() {
381            assertFalse(Base64.isBase64(new byte[]{Byte.MIN_VALUE}));
382            assertFalse(Base64.isBase64(new byte[]{-125}));
383            assertFalse(Base64.isBase64(new byte[]{-10}));
384            assertFalse(Base64.isBase64(new byte[]{0}));
385            assertFalse(Base64.isBase64(new byte[]{64, Byte.MAX_VALUE}));
386            assertFalse(Base64.isBase64(new byte[]{Byte.MAX_VALUE}));
387            assertTrue(Base64.isBase64(new byte[]{'A'}));
388            assertFalse(Base64.isBase64(new byte[]{'A', Byte.MIN_VALUE}));
389            assertTrue(Base64.isBase64(new byte[]{'A', 'Z', 'a'}));
390            assertTrue(Base64.isBase64(new byte[]{'/', '=', '+'}));
391            assertFalse(Base64.isBase64(new byte[]{'$'}));
392        }
393    
394        /**
395         * Tests isUrlSafe.
396         */
397        @Test
398        public void testIsUrlSafe() {
399            Base64 base64Standard = new Base64(false);
400            Base64 base64URLSafe = new Base64(true);
401    
402            assertFalse("Base64.isUrlSafe=false", base64Standard.isUrlSafe());
403            assertTrue("Base64.isUrlSafe=true", base64URLSafe.isUrlSafe());
404    
405            byte[] whiteSpace = {' ', '\n', '\r', '\t'};
406            assertTrue("Base64.isBase64(whiteSpace)=true", Base64.isBase64(whiteSpace));
407        }
408    
409        @Test
410        public void testKnownDecodings() {
411            assertEquals("The quick brown fox jumped over the lazy dogs.", new String(Base64
412                    .decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes(Charsets.UTF_8))));
413            assertEquals("It was the best of times, it was the worst of times.", new String(Base64
414                    .decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes(Charsets.UTF_8))));
415            assertEquals("http://jakarta.apache.org/commmons", new String(Base64
416                    .decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes(Charsets.UTF_8))));
417            assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", new String(Base64
418                    .decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes(Charsets.UTF_8))));
419            assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }", new String(Base64.decodeBase64("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0="
420                    .getBytes(Charsets.UTF_8))));
421            assertEquals("xyzzy!", new String(Base64.decodeBase64("eHl6enkh".getBytes(Charsets.UTF_8))));
422        }
423    
424        @Test
425        public void testKnownEncodings() {
426            assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==", new String(Base64
427                    .encodeBase64("The quick brown fox jumped over the lazy dogs.".getBytes(Charsets.UTF_8))));
428            assertEquals(
429                    "YmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJs\r\nYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo\r\nIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi\r\nbGFoIGJsYWg=\r\n",
430                    new String(
431                            Base64
432                                    .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"
433                                            .getBytes(Charsets.UTF_8))));
434            assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==", new String(Base64
435                    .encodeBase64("It was the best of times, it was the worst of times.".getBytes(Charsets.UTF_8))));
436            assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==", new String(Base64
437                    .encodeBase64("http://jakarta.apache.org/commmons".getBytes(Charsets.UTF_8))));
438            assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==", new String(Base64
439                    .encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes(Charsets.UTF_8))));
440            assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=", new String(Base64.encodeBase64("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }"
441                    .getBytes(Charsets.UTF_8))));
442            assertEquals("eHl6enkh", new String(Base64.encodeBase64("xyzzy!".getBytes(Charsets.UTF_8))));
443        }
444    
445        @Test
446        public void testNonBase64Test() throws Exception {
447    
448            byte[] bArray = {'%'};
449    
450            assertFalse("Invalid Base64 array was incorrectly validated as " + "an array of Base64 encoded data", Base64
451                    .isBase64(bArray));
452    
453            try {
454                Base64 b64 = new Base64();
455                byte[] result = b64.decode(bArray);
456    
457                assertEquals("The result should be empty as the test encoded content did " + "not contain any valid base 64 characters",
458                        0, result.length);
459            } catch (Exception e) {
460                fail("Exception was thrown when trying to decode "
461                    + "invalid base64 encoded data - RFC 2045 requires that all "
462                    + "non base64 character be discarded, an exception should not"
463                    + " have been thrown");
464            }
465        }
466    
467        @Test
468        public void testObjectDecodeWithInvalidParameter() throws Exception {
469            Base64 b64 = new Base64();
470    
471            try {
472                b64.decode(Integer.valueOf(5));
473                fail("decode(Object) didn't throw an exception when passed an Integer object");
474            } catch (DecoderException e) {
475                // ignored
476            }
477    
478        }
479    
480        @Test
481        public void testObjectDecodeWithValidParameter() throws Exception {
482    
483            String original = "Hello World!";
484            Object o = Base64.encodeBase64(original.getBytes(Charsets.UTF_8));
485    
486            Base64 b64 = new Base64();
487            Object oDecoded = b64.decode(o);
488            byte[] baDecoded = (byte[]) oDecoded;
489            String dest = new String(baDecoded);
490    
491            assertEquals("dest string does not equal original", original, dest);
492        }
493    
494        @Test
495        public void testObjectEncodeWithInvalidParameter() throws Exception {
496            Base64 b64 = new Base64();
497            try {
498                b64.encode("Yadayadayada");
499                fail("encode(Object) didn't throw an exception when passed a String object");
500            } catch (EncoderException e) {
501                // Expected
502            }
503        }
504    
505        @Test
506        public void testObjectEncodeWithValidParameter() throws Exception {
507    
508            String original = "Hello World!";
509            Object origObj = original.getBytes(Charsets.UTF_8);
510    
511            Base64 b64 = new Base64();
512            Object oEncoded = b64.encode(origObj);
513            byte[] bArray = Base64.decodeBase64((byte[]) oEncoded);
514            String dest = new String(bArray);
515    
516            assertEquals("dest string does not equal original", original, dest);
517        }
518    
519        @Test
520        public void testObjectEncode() throws Exception {
521            Base64 b64 = new Base64();
522            assertEquals("SGVsbG8gV29ybGQ=", new String(b64.encode("Hello World".getBytes(Charsets.UTF_8))));
523        }
524    
525        @Test
526        public void testPairs() {
527            assertEquals("AAA=", new String(Base64.encodeBase64(new byte[]{0, 0})));
528            for (int i = -128; i <= 127; i++) {
529                byte test[] = {(byte) i, (byte) i};
530                assertTrue(Arrays.equals(test, Base64.decodeBase64(Base64.encodeBase64(test))));
531            }
532        }
533    
534        /**
535         * Tests RFC 2045 section 2.1 CRLF definition.
536         */
537        @Test
538        public void testRfc2045Section2Dot1CrLfDefinition() {
539            assertTrue(Arrays.equals(new byte[]{13, 10}, Base64.CHUNK_SEPARATOR));
540        }
541    
542        /**
543         * Tests RFC 2045 section 6.8 chuck size definition.
544         */
545        @Test
546        public void testRfc2045Section6Dot8ChunkSizeDefinition() {
547            assertEquals(76, BaseNCodec.MIME_CHUNK_SIZE);
548        }
549    
550        /**
551         * Tests RFC 1421 section 4.3.2.4 chuck size definition.
552         */
553        @Test
554        public void testRfc1421Section6Dot8ChunkSizeDefinition() {
555            assertEquals(64, BaseNCodec.PEM_CHUNK_SIZE);
556        }
557    
558        /**
559         * Tests RFC 4648 section 10 test vectors.
560         * <ul>
561         * <li>BASE64("") = ""</li>
562         * <li>BASE64("f") = "Zg=="</li>
563         * <li>BASE64("fo") = "Zm8="</li>
564         * <li>BASE64("foo") = "Zm9v"</li>
565         * <li>BASE64("foob") = "Zm9vYg=="</li>
566         * <li>BASE64("fooba") = "Zm9vYmE="</li>
567         * <li>BASE64("foobar") = "Zm9vYmFy"</li>
568         * </ul>
569         *
570         * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
571         */
572        @Test
573        public void testRfc4648Section10Decode() {
574            assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("")));
575            assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==")));
576            assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=")));
577            assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v")));
578            assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==")));
579            assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=")));
580            assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy")));
581        }
582    
583        /**
584         * Tests RFC 4648 section 10 test vectors.
585         * <ul>
586         * <li>BASE64("") = ""</li>
587         * <li>BASE64("f") = "Zg=="</li>
588         * <li>BASE64("fo") = "Zm8="</li>
589         * <li>BASE64("foo") = "Zm9v"</li>
590         * <li>BASE64("foob") = "Zm9vYg=="</li>
591         * <li>BASE64("fooba") = "Zm9vYmE="</li>
592         * <li>BASE64("foobar") = "Zm9vYmFy"</li>
593         * </ul>
594         *
595         * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
596         */
597        @Test
598        public void testRfc4648Section10DecodeWithCrLf() {
599            String CRLF = StringUtils.newStringUsAscii(Base64.CHUNK_SEPARATOR);
600            assertEquals("", StringUtils.newStringUsAscii(Base64.decodeBase64("" + CRLF)));
601            assertEquals("f", StringUtils.newStringUsAscii(Base64.decodeBase64("Zg==" + CRLF)));
602            assertEquals("fo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm8=" + CRLF)));
603            assertEquals("foo", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9v" + CRLF)));
604            assertEquals("foob", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYg==" + CRLF)));
605            assertEquals("fooba", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmE=" + CRLF)));
606            assertEquals("foobar", StringUtils.newStringUsAscii(Base64.decodeBase64("Zm9vYmFy" + CRLF)));
607        }
608    
609        /**
610         * Tests RFC 4648 section 10 test vectors.
611         * <ul>
612         * <li>BASE64("") = ""</li>
613         * <li>BASE64("f") = "Zg=="</li>
614         * <li>BASE64("fo") = "Zm8="</li>
615         * <li>BASE64("foo") = "Zm9v"</li>
616         * <li>BASE64("foob") = "Zm9vYg=="</li>
617         * <li>BASE64("fooba") = "Zm9vYmE="</li>
618         * <li>BASE64("foobar") = "Zm9vYmFy"</li>
619         * </ul>
620         *
621         * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
622         */
623        @Test
624        public void testRfc4648Section10Encode() {
625            assertEquals("", Base64.encodeBase64String(StringUtils.getBytesUtf8("")));
626            assertEquals("Zg==", Base64.encodeBase64String(StringUtils.getBytesUtf8("f")));
627            assertEquals("Zm8=", Base64.encodeBase64String(StringUtils.getBytesUtf8("fo")));
628            assertEquals("Zm9v", Base64.encodeBase64String(StringUtils.getBytesUtf8("foo")));
629            assertEquals("Zm9vYg==", Base64.encodeBase64String(StringUtils.getBytesUtf8("foob")));
630            assertEquals("Zm9vYmE=", Base64.encodeBase64String(StringUtils.getBytesUtf8("fooba")));
631            assertEquals("Zm9vYmFy", Base64.encodeBase64String(StringUtils.getBytesUtf8("foobar")));
632        }
633    
634        /**
635         * Tests RFC 4648 section 10 test vectors.
636         * <ul>
637         * <li>BASE64("") = ""</li>
638         * <li>BASE64("f") = "Zg=="</li>
639         * <li>BASE64("fo") = "Zm8="</li>
640         * <li>BASE64("foo") = "Zm9v"</li>
641         * <li>BASE64("foob") = "Zm9vYg=="</li>
642         * <li>BASE64("fooba") = "Zm9vYmE="</li>
643         * <li>BASE64("foobar") = "Zm9vYmFy"</li>
644         * </ul>
645         *
646         * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
647         */
648        @Test
649        public void testRfc4648Section10DecodeEncode() {
650            testDecodeEncode("");
651            testDecodeEncode("Zg==");
652            testDecodeEncode("Zm8=");
653            testDecodeEncode("Zm9v");
654            testDecodeEncode("Zm9vYg==");
655            testDecodeEncode("Zm9vYmE=");
656            testDecodeEncode("Zm9vYmFy");
657        }
658    
659        private void testDecodeEncode(String encodedText) {
660            String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
661            String encodedText2 = Base64.encodeBase64String(StringUtils.getBytesUtf8(decodedText));
662            assertEquals(encodedText, encodedText2);
663        }
664    
665        /**
666         * Tests RFC 4648 section 10 test vectors.
667         * <ul>
668         * <li>BASE64("") = ""</li>
669         * <li>BASE64("f") = "Zg=="</li>
670         * <li>BASE64("fo") = "Zm8="</li>
671         * <li>BASE64("foo") = "Zm9v"</li>
672         * <li>BASE64("foob") = "Zm9vYg=="</li>
673         * <li>BASE64("fooba") = "Zm9vYmE="</li>
674         * <li>BASE64("foobar") = "Zm9vYmFy"</li>
675         * </ul>
676         *
677         * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
678         */
679        @Test
680        public void testRfc4648Section10EncodeDecode() {
681            testEncodeDecode("");
682            testEncodeDecode("f");
683            testEncodeDecode("fo");
684            testEncodeDecode("foo");
685            testEncodeDecode("foob");
686            testEncodeDecode("fooba");
687            testEncodeDecode("foobar");
688        }
689    
690        private void testEncodeDecode(String plainText) {
691            String encodedText = Base64.encodeBase64String(StringUtils.getBytesUtf8(plainText));
692            String decodedText = StringUtils.newStringUsAscii(Base64.decodeBase64(encodedText));
693            assertEquals(plainText, decodedText);
694        }
695    
696        @Test
697        public void testSingletons() {
698            assertEquals("AA==", new String(Base64.encodeBase64(new byte[]{(byte) 0})));
699            assertEquals("AQ==", new String(Base64.encodeBase64(new byte[]{(byte) 1})));
700            assertEquals("Ag==", new String(Base64.encodeBase64(new byte[]{(byte) 2})));
701            assertEquals("Aw==", new String(Base64.encodeBase64(new byte[]{(byte) 3})));
702            assertEquals("BA==", new String(Base64.encodeBase64(new byte[]{(byte) 4})));
703            assertEquals("BQ==", new String(Base64.encodeBase64(new byte[]{(byte) 5})));
704            assertEquals("Bg==", new String(Base64.encodeBase64(new byte[]{(byte) 6})));
705            assertEquals("Bw==", new String(Base64.encodeBase64(new byte[]{(byte) 7})));
706            assertEquals("CA==", new String(Base64.encodeBase64(new byte[]{(byte) 8})));
707            assertEquals("CQ==", new String(Base64.encodeBase64(new byte[]{(byte) 9})));
708            assertEquals("Cg==", new String(Base64.encodeBase64(new byte[]{(byte) 10})));
709            assertEquals("Cw==", new String(Base64.encodeBase64(new byte[]{(byte) 11})));
710            assertEquals("DA==", new String(Base64.encodeBase64(new byte[]{(byte) 12})));
711            assertEquals("DQ==", new String(Base64.encodeBase64(new byte[]{(byte) 13})));
712            assertEquals("Dg==", new String(Base64.encodeBase64(new byte[]{(byte) 14})));
713            assertEquals("Dw==", new String(Base64.encodeBase64(new byte[]{(byte) 15})));
714            assertEquals("EA==", new String(Base64.encodeBase64(new byte[]{(byte) 16})));
715            assertEquals("EQ==", new String(Base64.encodeBase64(new byte[]{(byte) 17})));
716            assertEquals("Eg==", new String(Base64.encodeBase64(new byte[]{(byte) 18})));
717            assertEquals("Ew==", new String(Base64.encodeBase64(new byte[]{(byte) 19})));
718            assertEquals("FA==", new String(Base64.encodeBase64(new byte[]{(byte) 20})));
719            assertEquals("FQ==", new String(Base64.encodeBase64(new byte[]{(byte) 21})));
720            assertEquals("Fg==", new String(Base64.encodeBase64(new byte[]{(byte) 22})));
721            assertEquals("Fw==", new String(Base64.encodeBase64(new byte[]{(byte) 23})));
722            assertEquals("GA==", new String(Base64.encodeBase64(new byte[]{(byte) 24})));
723            assertEquals("GQ==", new String(Base64.encodeBase64(new byte[]{(byte) 25})));
724            assertEquals("Gg==", new String(Base64.encodeBase64(new byte[]{(byte) 26})));
725            assertEquals("Gw==", new String(Base64.encodeBase64(new byte[]{(byte) 27})));
726            assertEquals("HA==", new String(Base64.encodeBase64(new byte[]{(byte) 28})));
727            assertEquals("HQ==", new String(Base64.encodeBase64(new byte[]{(byte) 29})));
728            assertEquals("Hg==", new String(Base64.encodeBase64(new byte[]{(byte) 30})));
729            assertEquals("Hw==", new String(Base64.encodeBase64(new byte[]{(byte) 31})));
730            assertEquals("IA==", new String(Base64.encodeBase64(new byte[]{(byte) 32})));
731            assertEquals("IQ==", new String(Base64.encodeBase64(new byte[]{(byte) 33})));
732            assertEquals("Ig==", new String(Base64.encodeBase64(new byte[]{(byte) 34})));
733            assertEquals("Iw==", new String(Base64.encodeBase64(new byte[]{(byte) 35})));
734            assertEquals("JA==", new String(Base64.encodeBase64(new byte[]{(byte) 36})));
735            assertEquals("JQ==", new String(Base64.encodeBase64(new byte[]{(byte) 37})));
736            assertEquals("Jg==", new String(Base64.encodeBase64(new byte[]{(byte) 38})));
737            assertEquals("Jw==", new String(Base64.encodeBase64(new byte[]{(byte) 39})));
738            assertEquals("KA==", new String(Base64.encodeBase64(new byte[]{(byte) 40})));
739            assertEquals("KQ==", new String(Base64.encodeBase64(new byte[]{(byte) 41})));
740            assertEquals("Kg==", new String(Base64.encodeBase64(new byte[]{(byte) 42})));
741            assertEquals("Kw==", new String(Base64.encodeBase64(new byte[]{(byte) 43})));
742            assertEquals("LA==", new String(Base64.encodeBase64(new byte[]{(byte) 44})));
743            assertEquals("LQ==", new String(Base64.encodeBase64(new byte[]{(byte) 45})));
744            assertEquals("Lg==", new String(Base64.encodeBase64(new byte[]{(byte) 46})));
745            assertEquals("Lw==", new String(Base64.encodeBase64(new byte[]{(byte) 47})));
746            assertEquals("MA==", new String(Base64.encodeBase64(new byte[]{(byte) 48})));
747            assertEquals("MQ==", new String(Base64.encodeBase64(new byte[]{(byte) 49})));
748            assertEquals("Mg==", new String(Base64.encodeBase64(new byte[]{(byte) 50})));
749            assertEquals("Mw==", new String(Base64.encodeBase64(new byte[]{(byte) 51})));
750            assertEquals("NA==", new String(Base64.encodeBase64(new byte[]{(byte) 52})));
751            assertEquals("NQ==", new String(Base64.encodeBase64(new byte[]{(byte) 53})));
752            assertEquals("Ng==", new String(Base64.encodeBase64(new byte[]{(byte) 54})));
753            assertEquals("Nw==", new String(Base64.encodeBase64(new byte[]{(byte) 55})));
754            assertEquals("OA==", new String(Base64.encodeBase64(new byte[]{(byte) 56})));
755            assertEquals("OQ==", new String(Base64.encodeBase64(new byte[]{(byte) 57})));
756            assertEquals("Og==", new String(Base64.encodeBase64(new byte[]{(byte) 58})));
757            assertEquals("Ow==", new String(Base64.encodeBase64(new byte[]{(byte) 59})));
758            assertEquals("PA==", new String(Base64.encodeBase64(new byte[]{(byte) 60})));
759            assertEquals("PQ==", new String(Base64.encodeBase64(new byte[]{(byte) 61})));
760            assertEquals("Pg==", new String(Base64.encodeBase64(new byte[]{(byte) 62})));
761            assertEquals("Pw==", new String(Base64.encodeBase64(new byte[]{(byte) 63})));
762            assertEquals("QA==", new String(Base64.encodeBase64(new byte[]{(byte) 64})));
763            assertEquals("QQ==", new String(Base64.encodeBase64(new byte[]{(byte) 65})));
764            assertEquals("Qg==", new String(Base64.encodeBase64(new byte[]{(byte) 66})));
765            assertEquals("Qw==", new String(Base64.encodeBase64(new byte[]{(byte) 67})));
766            assertEquals("RA==", new String(Base64.encodeBase64(new byte[]{(byte) 68})));
767            assertEquals("RQ==", new String(Base64.encodeBase64(new byte[]{(byte) 69})));
768            assertEquals("Rg==", new String(Base64.encodeBase64(new byte[]{(byte) 70})));
769            assertEquals("Rw==", new String(Base64.encodeBase64(new byte[]{(byte) 71})));
770            assertEquals("SA==", new String(Base64.encodeBase64(new byte[]{(byte) 72})));
771            assertEquals("SQ==", new String(Base64.encodeBase64(new byte[]{(byte) 73})));
772            assertEquals("Sg==", new String(Base64.encodeBase64(new byte[]{(byte) 74})));
773            assertEquals("Sw==", new String(Base64.encodeBase64(new byte[]{(byte) 75})));
774            assertEquals("TA==", new String(Base64.encodeBase64(new byte[]{(byte) 76})));
775            assertEquals("TQ==", new String(Base64.encodeBase64(new byte[]{(byte) 77})));
776            assertEquals("Tg==", new String(Base64.encodeBase64(new byte[]{(byte) 78})));
777            assertEquals("Tw==", new String(Base64.encodeBase64(new byte[]{(byte) 79})));
778            assertEquals("UA==", new String(Base64.encodeBase64(new byte[]{(byte) 80})));
779            assertEquals("UQ==", new String(Base64.encodeBase64(new byte[]{(byte) 81})));
780            assertEquals("Ug==", new String(Base64.encodeBase64(new byte[]{(byte) 82})));
781            assertEquals("Uw==", new String(Base64.encodeBase64(new byte[]{(byte) 83})));
782            assertEquals("VA==", new String(Base64.encodeBase64(new byte[]{(byte) 84})));
783            assertEquals("VQ==", new String(Base64.encodeBase64(new byte[]{(byte) 85})));
784            assertEquals("Vg==", new String(Base64.encodeBase64(new byte[]{(byte) 86})));
785            assertEquals("Vw==", new String(Base64.encodeBase64(new byte[]{(byte) 87})));
786            assertEquals("WA==", new String(Base64.encodeBase64(new byte[]{(byte) 88})));
787            assertEquals("WQ==", new String(Base64.encodeBase64(new byte[]{(byte) 89})));
788            assertEquals("Wg==", new String(Base64.encodeBase64(new byte[]{(byte) 90})));
789            assertEquals("Ww==", new String(Base64.encodeBase64(new byte[]{(byte) 91})));
790            assertEquals("XA==", new String(Base64.encodeBase64(new byte[]{(byte) 92})));
791            assertEquals("XQ==", new String(Base64.encodeBase64(new byte[]{(byte) 93})));
792            assertEquals("Xg==", new String(Base64.encodeBase64(new byte[]{(byte) 94})));
793            assertEquals("Xw==", new String(Base64.encodeBase64(new byte[]{(byte) 95})));
794            assertEquals("YA==", new String(Base64.encodeBase64(new byte[]{(byte) 96})));
795            assertEquals("YQ==", new String(Base64.encodeBase64(new byte[]{(byte) 97})));
796            assertEquals("Yg==", new String(Base64.encodeBase64(new byte[]{(byte) 98})));
797            assertEquals("Yw==", new String(Base64.encodeBase64(new byte[]{(byte) 99})));
798            assertEquals("ZA==", new String(Base64.encodeBase64(new byte[]{(byte) 100})));
799            assertEquals("ZQ==", new String(Base64.encodeBase64(new byte[]{(byte) 101})));
800            assertEquals("Zg==", new String(Base64.encodeBase64(new byte[]{(byte) 102})));
801            assertEquals("Zw==", new String(Base64.encodeBase64(new byte[]{(byte) 103})));
802            assertEquals("aA==", new String(Base64.encodeBase64(new byte[]{(byte) 104})));
803            for (int i = -128; i <= 127; i++) {
804                byte test[] = {(byte) i};
805                assertTrue(Arrays.equals(test, Base64.decodeBase64(Base64.encodeBase64(test))));
806            }
807        }
808    
809        @Test
810        public void testSingletonsChunked() {
811            assertEquals("AA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0})));
812            assertEquals("AQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 1})));
813            assertEquals("Ag==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 2})));
814            assertEquals("Aw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 3})));
815            assertEquals("BA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 4})));
816            assertEquals("BQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 5})));
817            assertEquals("Bg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 6})));
818            assertEquals("Bw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 7})));
819            assertEquals("CA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 8})));
820            assertEquals("CQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 9})));
821            assertEquals("Cg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 10})));
822            assertEquals("Cw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 11})));
823            assertEquals("DA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 12})));
824            assertEquals("DQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 13})));
825            assertEquals("Dg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 14})));
826            assertEquals("Dw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 15})));
827            assertEquals("EA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 16})));
828            assertEquals("EQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 17})));
829            assertEquals("Eg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 18})));
830            assertEquals("Ew==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 19})));
831            assertEquals("FA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 20})));
832            assertEquals("FQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 21})));
833            assertEquals("Fg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 22})));
834            assertEquals("Fw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 23})));
835            assertEquals("GA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 24})));
836            assertEquals("GQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 25})));
837            assertEquals("Gg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 26})));
838            assertEquals("Gw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 27})));
839            assertEquals("HA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 28})));
840            assertEquals("HQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 29})));
841            assertEquals("Hg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 30})));
842            assertEquals("Hw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 31})));
843            assertEquals("IA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 32})));
844            assertEquals("IQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 33})));
845            assertEquals("Ig==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 34})));
846            assertEquals("Iw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 35})));
847            assertEquals("JA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 36})));
848            assertEquals("JQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 37})));
849            assertEquals("Jg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 38})));
850            assertEquals("Jw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 39})));
851            assertEquals("KA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 40})));
852            assertEquals("KQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 41})));
853            assertEquals("Kg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 42})));
854            assertEquals("Kw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 43})));
855            assertEquals("LA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 44})));
856            assertEquals("LQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 45})));
857            assertEquals("Lg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 46})));
858            assertEquals("Lw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 47})));
859            assertEquals("MA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 48})));
860            assertEquals("MQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 49})));
861            assertEquals("Mg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 50})));
862            assertEquals("Mw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 51})));
863            assertEquals("NA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 52})));
864            assertEquals("NQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 53})));
865            assertEquals("Ng==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 54})));
866            assertEquals("Nw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 55})));
867            assertEquals("OA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 56})));
868            assertEquals("OQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 57})));
869            assertEquals("Og==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 58})));
870            assertEquals("Ow==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 59})));
871            assertEquals("PA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 60})));
872            assertEquals("PQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 61})));
873            assertEquals("Pg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 62})));
874            assertEquals("Pw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 63})));
875            assertEquals("QA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 64})));
876            assertEquals("QQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 65})));
877            assertEquals("Qg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 66})));
878            assertEquals("Qw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 67})));
879            assertEquals("RA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 68})));
880            assertEquals("RQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 69})));
881            assertEquals("Rg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 70})));
882            assertEquals("Rw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 71})));
883            assertEquals("SA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 72})));
884            assertEquals("SQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 73})));
885            assertEquals("Sg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 74})));
886            assertEquals("Sw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 75})));
887            assertEquals("TA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 76})));
888            assertEquals("TQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 77})));
889            assertEquals("Tg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 78})));
890            assertEquals("Tw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 79})));
891            assertEquals("UA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 80})));
892            assertEquals("UQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 81})));
893            assertEquals("Ug==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 82})));
894            assertEquals("Uw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 83})));
895            assertEquals("VA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 84})));
896            assertEquals("VQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 85})));
897            assertEquals("Vg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 86})));
898            assertEquals("Vw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 87})));
899            assertEquals("WA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 88})));
900            assertEquals("WQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 89})));
901            assertEquals("Wg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 90})));
902            assertEquals("Ww==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 91})));
903            assertEquals("XA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 92})));
904            assertEquals("XQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 93})));
905            assertEquals("Xg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 94})));
906            assertEquals("Xw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 95})));
907            assertEquals("YA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 96})));
908            assertEquals("YQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 97})));
909            assertEquals("Yg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 98})));
910            assertEquals("Yw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 99})));
911            assertEquals("ZA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 100})));
912            assertEquals("ZQ==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 101})));
913            assertEquals("Zg==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 102})));
914            assertEquals("Zw==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 103})));
915            assertEquals("aA==\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 104})));
916        }
917    
918        @Test
919        public void testTriplets() {
920            assertEquals("AAAA", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 0})));
921            assertEquals("AAAB", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 1})));
922            assertEquals("AAAC", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 2})));
923            assertEquals("AAAD", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 3})));
924            assertEquals("AAAE", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 4})));
925            assertEquals("AAAF", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 5})));
926            assertEquals("AAAG", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 6})));
927            assertEquals("AAAH", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 7})));
928            assertEquals("AAAI", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 8})));
929            assertEquals("AAAJ", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 9})));
930            assertEquals("AAAK", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 10})));
931            assertEquals("AAAL", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 11})));
932            assertEquals("AAAM", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 12})));
933            assertEquals("AAAN", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 13})));
934            assertEquals("AAAO", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 14})));
935            assertEquals("AAAP", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 15})));
936            assertEquals("AAAQ", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 16})));
937            assertEquals("AAAR", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 17})));
938            assertEquals("AAAS", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 18})));
939            assertEquals("AAAT", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 19})));
940            assertEquals("AAAU", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 20})));
941            assertEquals("AAAV", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 21})));
942            assertEquals("AAAW", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 22})));
943            assertEquals("AAAX", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 23})));
944            assertEquals("AAAY", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 24})));
945            assertEquals("AAAZ", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 25})));
946            assertEquals("AAAa", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 26})));
947            assertEquals("AAAb", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 27})));
948            assertEquals("AAAc", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 28})));
949            assertEquals("AAAd", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 29})));
950            assertEquals("AAAe", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 30})));
951            assertEquals("AAAf", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 31})));
952            assertEquals("AAAg", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 32})));
953            assertEquals("AAAh", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 33})));
954            assertEquals("AAAi", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 34})));
955            assertEquals("AAAj", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 35})));
956            assertEquals("AAAk", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 36})));
957            assertEquals("AAAl", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 37})));
958            assertEquals("AAAm", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 38})));
959            assertEquals("AAAn", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 39})));
960            assertEquals("AAAo", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 40})));
961            assertEquals("AAAp", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 41})));
962            assertEquals("AAAq", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 42})));
963            assertEquals("AAAr", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 43})));
964            assertEquals("AAAs", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 44})));
965            assertEquals("AAAt", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 45})));
966            assertEquals("AAAu", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 46})));
967            assertEquals("AAAv", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 47})));
968            assertEquals("AAAw", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 48})));
969            assertEquals("AAAx", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 49})));
970            assertEquals("AAAy", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 50})));
971            assertEquals("AAAz", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 51})));
972            assertEquals("AAA0", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 52})));
973            assertEquals("AAA1", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 53})));
974            assertEquals("AAA2", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 54})));
975            assertEquals("AAA3", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 55})));
976            assertEquals("AAA4", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 56})));
977            assertEquals("AAA5", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 57})));
978            assertEquals("AAA6", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 58})));
979            assertEquals("AAA7", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 59})));
980            assertEquals("AAA8", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 60})));
981            assertEquals("AAA9", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 61})));
982            assertEquals("AAA+", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 62})));
983            assertEquals("AAA/", new String(Base64.encodeBase64(new byte[]{(byte) 0, (byte) 0, (byte) 63})));
984        }
985    
986        @Test
987        public void testTripletsChunked() {
988            assertEquals("AAAA\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 0})));
989            assertEquals("AAAB\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 1})));
990            assertEquals("AAAC\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 2})));
991            assertEquals("AAAD\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 3})));
992            assertEquals("AAAE\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 4})));
993            assertEquals("AAAF\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 5})));
994            assertEquals("AAAG\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 6})));
995            assertEquals("AAAH\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 7})));
996            assertEquals("AAAI\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 8})));
997            assertEquals("AAAJ\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 9})));
998            assertEquals("AAAK\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 10})));
999            assertEquals("AAAL\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 11})));
1000            assertEquals("AAAM\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 12})));
1001            assertEquals("AAAN\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 13})));
1002            assertEquals("AAAO\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 14})));
1003            assertEquals("AAAP\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 15})));
1004            assertEquals("AAAQ\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 16})));
1005            assertEquals("AAAR\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 17})));
1006            assertEquals("AAAS\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 18})));
1007            assertEquals("AAAT\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 19})));
1008            assertEquals("AAAU\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 20})));
1009            assertEquals("AAAV\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 21})));
1010            assertEquals("AAAW\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 22})));
1011            assertEquals("AAAX\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 23})));
1012            assertEquals("AAAY\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 24})));
1013            assertEquals("AAAZ\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 25})));
1014            assertEquals("AAAa\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 26})));
1015            assertEquals("AAAb\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 27})));
1016            assertEquals("AAAc\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 28})));
1017            assertEquals("AAAd\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 29})));
1018            assertEquals("AAAe\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 30})));
1019            assertEquals("AAAf\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 31})));
1020            assertEquals("AAAg\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 32})));
1021            assertEquals("AAAh\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 33})));
1022            assertEquals("AAAi\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 34})));
1023            assertEquals("AAAj\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 35})));
1024            assertEquals("AAAk\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 36})));
1025            assertEquals("AAAl\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 37})));
1026            assertEquals("AAAm\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 38})));
1027            assertEquals("AAAn\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 39})));
1028            assertEquals("AAAo\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 40})));
1029            assertEquals("AAAp\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 41})));
1030            assertEquals("AAAq\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 42})));
1031            assertEquals("AAAr\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 43})));
1032            assertEquals("AAAs\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 44})));
1033            assertEquals("AAAt\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 45})));
1034            assertEquals("AAAu\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 46})));
1035            assertEquals("AAAv\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 47})));
1036            assertEquals("AAAw\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 48})));
1037            assertEquals("AAAx\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 49})));
1038            assertEquals("AAAy\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 50})));
1039            assertEquals("AAAz\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 51})));
1040            assertEquals("AAA0\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 52})));
1041            assertEquals("AAA1\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 53})));
1042            assertEquals("AAA2\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 54})));
1043            assertEquals("AAA3\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 55})));
1044            assertEquals("AAA4\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 56})));
1045            assertEquals("AAA5\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 57})));
1046            assertEquals("AAA6\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 58})));
1047            assertEquals("AAA7\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 59})));
1048            assertEquals("AAA8\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 60})));
1049            assertEquals("AAA9\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 61})));
1050            assertEquals("AAA+\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 62})));
1051            assertEquals("AAA/\r\n", new String(Base64.encodeBase64Chunked(new byte[]{(byte) 0, (byte) 0, (byte) 63})));
1052        }
1053    
1054        /**
1055         * Tests url-safe Base64 against random data, sizes 0 to 150.
1056         */
1057        @Test
1058        public void testUrlSafe() {
1059            // test random data of sizes 0 thru 150
1060            for (int i = 0; i <= 150; i++) {
1061                byte[][] randomData = Base64TestData.randomData(i, true);
1062                byte[] encoded = randomData[1];
1063                byte[] decoded = randomData[0];
1064                byte[] result = Base64.decodeBase64(encoded);
1065                assertTrue("url-safe i=" + i, Arrays.equals(decoded, result));
1066                assertFalse("url-safe i=" + i + " no '='", Base64TestData.bytesContain(encoded, (byte) '='));
1067                assertFalse("url-safe i=" + i + " no '\\'", Base64TestData.bytesContain(encoded, (byte) '\\'));
1068                assertFalse("url-safe i=" + i + " no '+'", Base64TestData.bytesContain(encoded, (byte) '+'));
1069            }
1070    
1071        }
1072    
1073        /**
1074         * Base64 encoding of UUID's is a common use-case, especially in URL-SAFE mode. This test case ends up being the
1075         * "URL-SAFE" JUnit's.
1076         *
1077         * @throws DecoderException
1078         *             if Hex.decode() fails - a serious problem since Hex comes from our own commons-codec!
1079         */
1080        @Test
1081        public void testUUID() throws DecoderException {
1082            // The 4 UUID's below contains mixtures of + and / to help us test the
1083            // URL-SAFE encoding mode.
1084            byte[][] ids = new byte[4][];
1085    
1086            // ids[0] was chosen so that it encodes with at least one +.
1087            ids[0] = Hex.decodeHex("94ed8d0319e4493399560fb67404d370".toCharArray());
1088    
1089            // ids[1] was chosen so that it encodes with both / and +.
1090            ids[1] = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090".toCharArray());
1091    
1092            // ids[2] was chosen so that it encodes with at least one /.
1093            ids[2] = Hex.decodeHex("64be154b6ffa40258d1a01288e7c31ca".toCharArray());
1094    
1095            // ids[3] was chosen so that it encodes with both / and +, with /
1096            // right at the beginning.
1097            ids[3] = Hex.decodeHex("ff7f8fc01cdb471a8c8b5a9306183fe8".toCharArray());
1098    
1099            byte[][] standard = new byte[4][];
1100            standard[0] = StringUtils.getBytesUtf8("lO2NAxnkSTOZVg+2dATTcA==");
1101            standard[1] = StringUtils.getBytesUtf8("K/fMJwH+Q5e0nr7tWsxwkA==");
1102            standard[2] = StringUtils.getBytesUtf8("ZL4VS2/6QCWNGgEojnwxyg==");
1103            standard[3] = StringUtils.getBytesUtf8("/3+PwBzbRxqMi1qTBhg/6A==");
1104    
1105            byte[][] urlSafe1 = new byte[4][];
1106            // regular padding (two '==' signs).
1107            urlSafe1[0] = StringUtils.getBytesUtf8("lO2NAxnkSTOZVg-2dATTcA==");
1108            urlSafe1[1] = StringUtils.getBytesUtf8("K_fMJwH-Q5e0nr7tWsxwkA==");
1109            urlSafe1[2] = StringUtils.getBytesUtf8("ZL4VS2_6QCWNGgEojnwxyg==");
1110            urlSafe1[3] = StringUtils.getBytesUtf8("_3-PwBzbRxqMi1qTBhg_6A==");
1111    
1112            byte[][] urlSafe2 = new byte[4][];
1113            // single padding (only one '=' sign).
1114            urlSafe2[0] = StringUtils.getBytesUtf8("lO2NAxnkSTOZVg-2dATTcA=");
1115            urlSafe2[1] = StringUtils.getBytesUtf8("K_fMJwH-Q5e0nr7tWsxwkA=");
1116            urlSafe2[2] = StringUtils.getBytesUtf8("ZL4VS2_6QCWNGgEojnwxyg=");
1117            urlSafe2[3] = StringUtils.getBytesUtf8("_3-PwBzbRxqMi1qTBhg_6A=");
1118    
1119            byte[][] urlSafe3 = new byte[4][];
1120            // no padding (no '=' signs).
1121            urlSafe3[0] = StringUtils.getBytesUtf8("lO2NAxnkSTOZVg-2dATTcA");
1122            urlSafe3[1] = StringUtils.getBytesUtf8("K_fMJwH-Q5e0nr7tWsxwkA");
1123            urlSafe3[2] = StringUtils.getBytesUtf8("ZL4VS2_6QCWNGgEojnwxyg");
1124            urlSafe3[3] = StringUtils.getBytesUtf8("_3-PwBzbRxqMi1qTBhg_6A");
1125    
1126            for (int i = 0; i < 4; i++) {
1127                byte[] encodedStandard = Base64.encodeBase64(ids[i]);
1128                byte[] encodedUrlSafe = Base64.encodeBase64URLSafe(ids[i]);
1129                byte[] decodedStandard = Base64.decodeBase64(standard[i]);
1130                byte[] decodedUrlSafe1 = Base64.decodeBase64(urlSafe1[i]);
1131                byte[] decodedUrlSafe2 = Base64.decodeBase64(urlSafe2[i]);
1132                byte[] decodedUrlSafe3 = Base64.decodeBase64(urlSafe3[i]);
1133    
1134                // Very important debugging output should anyone
1135                // ever need to delve closely into this stuff.
1136                if (false) {
1137                    System.out.println("reference: [" + Hex.encodeHexString(ids[i]) + "]");
1138                    System.out.println("standard:  [" +
1139                            Hex.encodeHexString(decodedStandard) +
1140                        "] From: [" +
1141                        StringUtils.newStringUtf8(standard[i]) +
1142                        "]");
1143                    System.out.println("safe1:     [" +
1144                            Hex.encodeHexString(decodedUrlSafe1) +
1145                        "] From: [" +
1146                        StringUtils.newStringUtf8(urlSafe1[i]) +
1147                        "]");
1148                    System.out.println("safe2:     [" +
1149                            Hex.encodeHexString(decodedUrlSafe2) +
1150                        "] From: [" +
1151                        StringUtils.newStringUtf8(urlSafe2[i]) +
1152                        "]");
1153                    System.out.println("safe3:     [" +
1154                            Hex.encodeHexString(decodedUrlSafe3) +
1155                        "] From: [" +
1156                        StringUtils.newStringUtf8(urlSafe3[i]) +
1157                        "]");
1158                }
1159    
1160                assertTrue("standard encode uuid", Arrays.equals(encodedStandard, standard[i]));
1161                assertTrue("url-safe encode uuid", Arrays.equals(encodedUrlSafe, urlSafe3[i]));
1162                assertTrue("standard decode uuid", Arrays.equals(decodedStandard, ids[i]));
1163                assertTrue("url-safe1 decode uuid", Arrays.equals(decodedUrlSafe1, ids[i]));
1164                assertTrue("url-safe2 decode uuid", Arrays.equals(decodedUrlSafe2, ids[i]));
1165                assertTrue("url-safe3 decode uuid", Arrays.equals(decodedUrlSafe3, ids[i]));
1166            }
1167        }
1168    
1169        @Test
1170        public void testByteToStringVariations() throws DecoderException {
1171            Base64 base64 = new Base64(0);
1172            byte[] b1 = StringUtils.getBytesUtf8("Hello World");
1173            byte[] b2 = new byte[0];
1174            byte[] b3 = null;
1175            byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090".toCharArray());  // for url-safe tests
1176    
1177            assertEquals("byteToString Hello World", "SGVsbG8gV29ybGQ=", base64.encodeToString(b1));
1178            assertEquals("byteToString static Hello World", "SGVsbG8gV29ybGQ=", Base64.encodeBase64String(b1));
1179            assertEquals("byteToString \"\"", "", base64.encodeToString(b2));
1180            assertEquals("byteToString static \"\"", "", Base64.encodeBase64String(b2));
1181            assertEquals("byteToString null", null, base64.encodeToString(b3));
1182            assertEquals("byteToString static null", null, Base64.encodeBase64String(b3));
1183            assertEquals("byteToString UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", base64.encodeToString(b4));
1184            assertEquals("byteToString static UUID", "K/fMJwH+Q5e0nr7tWsxwkA==", Base64.encodeBase64String(b4));
1185            assertEquals("byteToString static-url-safe UUID", "K_fMJwH-Q5e0nr7tWsxwkA", Base64.encodeBase64URLSafeString(b4));
1186        }
1187    
1188        @Test
1189        public void testStringToByteVariations() throws DecoderException {
1190            Base64 base64 = new Base64();
1191            String s1 = "SGVsbG8gV29ybGQ=\r\n";
1192            String s2 = "";
1193            String s3 = null;
1194            String s4a = "K/fMJwH+Q5e0nr7tWsxwkA==\r\n";
1195            String s4b = "K_fMJwH-Q5e0nr7tWsxwkA";
1196            byte[] b4 = Hex.decodeHex("2bf7cc2701fe4397b49ebeed5acc7090".toCharArray());  // for url-safe tests
1197    
1198            assertEquals("StringToByte Hello World", "Hello World", StringUtils.newStringUtf8(base64.decode(s1)));
1199            assertEquals("StringToByte Hello World", "Hello World", StringUtils.newStringUtf8((byte[])base64.decode((Object)s1)));
1200            assertEquals("StringToByte static Hello World", "Hello World", StringUtils.newStringUtf8(Base64.decodeBase64(s1)));
1201            assertEquals("StringToByte \"\"", "", StringUtils.newStringUtf8(base64.decode(s2)));
1202            assertEquals("StringToByte static \"\"", "", StringUtils.newStringUtf8(Base64.decodeBase64(s2)));
1203            assertEquals("StringToByte null", null, StringUtils.newStringUtf8(base64.decode(s3)));
1204            assertEquals("StringToByte static null", null, StringUtils.newStringUtf8(Base64.decodeBase64(s3)));
1205            assertTrue("StringToByte UUID", Arrays.equals(b4, base64.decode(s4b)));
1206            assertTrue("StringToByte static UUID", Arrays.equals(b4, Base64.decodeBase64(s4a)));
1207            assertTrue("StringToByte static-url-safe UUID", Arrays.equals(b4, Base64.decodeBase64(s4b)));
1208        }
1209    
1210        private String toString(byte[] data) {
1211            StringBuilder buf = new StringBuilder();
1212            for (int i = 0; i < data.length; i++) {
1213                buf.append(data[i]);
1214                if (i != data.length - 1) {
1215                    buf.append(",");
1216                }
1217            }
1218            return buf.toString();
1219        }
1220    
1221        /**
1222         * Tests a lineSeparator much bigger than DEFAULT_BUFFER_SIZE.
1223         *
1224         * @see "<a href='http://mail-archives.apache.org/mod_mbox/commons-dev/201202.mbox/%3C4F3C85D7.5060706@snafu.de%3E'>dev@commons.apache.org</a>"
1225         */
1226        @Test
1227        @Ignore
1228        public void testHugeLineSeparator() {
1229            final int BaseNCodec_DEFAULT_BUFFER_SIZE = 8192;
1230            final int Base64_BYTES_PER_ENCODED_BLOCK = 4;
1231            byte[] baLineSeparator = new byte[BaseNCodec_DEFAULT_BUFFER_SIZE * 4 - 3];
1232            Base64 b64 = new Base64(Base64_BYTES_PER_ENCODED_BLOCK, baLineSeparator);
1233            String strOriginal = "Hello World";
1234            String strDecoded = new String(b64.decode(b64.encode(StringUtils.getBytesUtf8(strOriginal))));
1235            assertEquals("testDEFAULT_BUFFER_SIZE", strOriginal, strDecoded);
1236        }
1237    
1238    }