0001: /*
0002: * Copyright 2001-2004 The Apache Software Foundation.
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016:
0017: package org.apache.commons.codec.binary;
0018:
0019: import java.util.Arrays;
0020: import java.util.Random;
0021:
0022: import junit.framework.TestCase;
0023:
0024: /**
0025: * @version $Id: Base64Test.java,v 1.15 2004/04/19 01:14:29 ggregory Exp $
0026: * @author Apache Software Foundation
0027: */
0028: public class Base64Test extends TestCase {
0029:
0030: /**
0031: * Construct a new instance of this test case.
0032: *
0033: * @param name Name of the test case
0034: */
0035: public Base64Test(String name) {
0036: super (name);
0037: }
0038:
0039: /**
0040: * Test the Base64 implementation
0041: */
0042: public void testBase64() {
0043: String content = "Hello World";
0044: String encodedContent;
0045: encodedContent = new String(Base64.encodeBase64(content
0046: .getBytes()));
0047: assertTrue("encoding hello world", encodedContent
0048: .equals("SGVsbG8gV29ybGQ="));
0049: }
0050:
0051: /**
0052: * Tests conditional true branch for "marker0" test.
0053: */
0054: public void testDecodePadMarkerIndex2() {
0055: assertEquals("A", new String(Base64.decodeBase64("QQ=="
0056: .getBytes())));
0057: }
0058:
0059: /**
0060: * Tests conditional branches for "marker1" test.
0061: */
0062: public void testDecodePadMarkerIndex3() {
0063: assertEquals("AA", new String(Base64.decodeBase64("QUE="
0064: .getBytes())));
0065: assertEquals("AAA", new String(Base64.decodeBase64("QUFB"
0066: .getBytes())));
0067: }
0068:
0069: public void testDecodePadOnly() {
0070: assertTrue(Base64.decodeBase64("====".getBytes()).length == 0);
0071: assertEquals("", new String(Base64.decodeBase64("===="
0072: .getBytes())));
0073: }
0074:
0075: public void testDecodePadOnlyChunked() {
0076: assertTrue(Base64.decodeBase64("====\n".getBytes()).length == 0);
0077: assertEquals("", new String(Base64.decodeBase64("====\n"
0078: .getBytes())));
0079: }
0080:
0081: // encode/decode random arrays from size 0 to size 11
0082: public void testEncodeDecodeSmall() {
0083: for (int i = 0; i < 12; i++) {
0084: byte[] data = new byte[i];
0085: this .getRandom().nextBytes(data);
0086: byte[] enc = Base64.encodeBase64(data);
0087: assertTrue("\"" + (new String(enc)) + "\" is Base64 data.",
0088: Base64.isArrayByteBase64(enc));
0089: byte[] data2 = Base64.decodeBase64(enc);
0090: assertTrue(toString(data) + " equals " + toString(data2),
0091: Arrays.equals(data, data2));
0092: }
0093: }
0094:
0095: // encode/decode a large random array
0096: public void testEncodeDecodeRandom() {
0097: for (int i = 1; i < 5; i++) {
0098: byte[] data = new byte[this .getRandom().nextInt(10000) + 1];
0099: this .getRandom().nextBytes(data);
0100: byte[] enc = Base64.encodeBase64(data);
0101: assertTrue(Base64.isArrayByteBase64(enc));
0102: byte[] data2 = Base64.decodeBase64(enc);
0103: assertTrue(Arrays.equals(data, data2));
0104: }
0105: }
0106:
0107: /**
0108: * Tests RFC 2045 section 2.1 CRLF definition.
0109: */
0110: public void testRfc2045Section2Dot1CrLfDefinition() {
0111: assertTrue(Arrays.equals(new byte[] { 13, 10 },
0112: Base64.CHUNK_SEPARATOR));
0113: }
0114:
0115: /**
0116: * Tests RFC 2045 section 6.8 chuck size definition.
0117: */
0118: public void testRfc2045Section6Dot8ChunkSizeDefinition() {
0119: assertEquals(76, Base64.CHUNK_SIZE);
0120: }
0121:
0122: public void testSingletons() {
0123: assertEquals("AA==", new String(Base64
0124: .encodeBase64(new byte[] { (byte) 0 })));
0125: assertEquals("AQ==", new String(Base64
0126: .encodeBase64(new byte[] { (byte) 1 })));
0127: assertEquals("Ag==", new String(Base64
0128: .encodeBase64(new byte[] { (byte) 2 })));
0129: assertEquals("Aw==", new String(Base64
0130: .encodeBase64(new byte[] { (byte) 3 })));
0131: assertEquals("BA==", new String(Base64
0132: .encodeBase64(new byte[] { (byte) 4 })));
0133: assertEquals("BQ==", new String(Base64
0134: .encodeBase64(new byte[] { (byte) 5 })));
0135: assertEquals("Bg==", new String(Base64
0136: .encodeBase64(new byte[] { (byte) 6 })));
0137: assertEquals("Bw==", new String(Base64
0138: .encodeBase64(new byte[] { (byte) 7 })));
0139: assertEquals("CA==", new String(Base64
0140: .encodeBase64(new byte[] { (byte) 8 })));
0141: assertEquals("CQ==", new String(Base64
0142: .encodeBase64(new byte[] { (byte) 9 })));
0143: assertEquals("Cg==", new String(Base64
0144: .encodeBase64(new byte[] { (byte) 10 })));
0145: assertEquals("Cw==", new String(Base64
0146: .encodeBase64(new byte[] { (byte) 11 })));
0147: assertEquals("DA==", new String(Base64
0148: .encodeBase64(new byte[] { (byte) 12 })));
0149: assertEquals("DQ==", new String(Base64
0150: .encodeBase64(new byte[] { (byte) 13 })));
0151: assertEquals("Dg==", new String(Base64
0152: .encodeBase64(new byte[] { (byte) 14 })));
0153: assertEquals("Dw==", new String(Base64
0154: .encodeBase64(new byte[] { (byte) 15 })));
0155: assertEquals("EA==", new String(Base64
0156: .encodeBase64(new byte[] { (byte) 16 })));
0157: assertEquals("EQ==", new String(Base64
0158: .encodeBase64(new byte[] { (byte) 17 })));
0159: assertEquals("Eg==", new String(Base64
0160: .encodeBase64(new byte[] { (byte) 18 })));
0161: assertEquals("Ew==", new String(Base64
0162: .encodeBase64(new byte[] { (byte) 19 })));
0163: assertEquals("FA==", new String(Base64
0164: .encodeBase64(new byte[] { (byte) 20 })));
0165: assertEquals("FQ==", new String(Base64
0166: .encodeBase64(new byte[] { (byte) 21 })));
0167: assertEquals("Fg==", new String(Base64
0168: .encodeBase64(new byte[] { (byte) 22 })));
0169: assertEquals("Fw==", new String(Base64
0170: .encodeBase64(new byte[] { (byte) 23 })));
0171: assertEquals("GA==", new String(Base64
0172: .encodeBase64(new byte[] { (byte) 24 })));
0173: assertEquals("GQ==", new String(Base64
0174: .encodeBase64(new byte[] { (byte) 25 })));
0175: assertEquals("Gg==", new String(Base64
0176: .encodeBase64(new byte[] { (byte) 26 })));
0177: assertEquals("Gw==", new String(Base64
0178: .encodeBase64(new byte[] { (byte) 27 })));
0179: assertEquals("HA==", new String(Base64
0180: .encodeBase64(new byte[] { (byte) 28 })));
0181: assertEquals("HQ==", new String(Base64
0182: .encodeBase64(new byte[] { (byte) 29 })));
0183: assertEquals("Hg==", new String(Base64
0184: .encodeBase64(new byte[] { (byte) 30 })));
0185: assertEquals("Hw==", new String(Base64
0186: .encodeBase64(new byte[] { (byte) 31 })));
0187: assertEquals("IA==", new String(Base64
0188: .encodeBase64(new byte[] { (byte) 32 })));
0189: assertEquals("IQ==", new String(Base64
0190: .encodeBase64(new byte[] { (byte) 33 })));
0191: assertEquals("Ig==", new String(Base64
0192: .encodeBase64(new byte[] { (byte) 34 })));
0193: assertEquals("Iw==", new String(Base64
0194: .encodeBase64(new byte[] { (byte) 35 })));
0195: assertEquals("JA==", new String(Base64
0196: .encodeBase64(new byte[] { (byte) 36 })));
0197: assertEquals("JQ==", new String(Base64
0198: .encodeBase64(new byte[] { (byte) 37 })));
0199: assertEquals("Jg==", new String(Base64
0200: .encodeBase64(new byte[] { (byte) 38 })));
0201: assertEquals("Jw==", new String(Base64
0202: .encodeBase64(new byte[] { (byte) 39 })));
0203: assertEquals("KA==", new String(Base64
0204: .encodeBase64(new byte[] { (byte) 40 })));
0205: assertEquals("KQ==", new String(Base64
0206: .encodeBase64(new byte[] { (byte) 41 })));
0207: assertEquals("Kg==", new String(Base64
0208: .encodeBase64(new byte[] { (byte) 42 })));
0209: assertEquals("Kw==", new String(Base64
0210: .encodeBase64(new byte[] { (byte) 43 })));
0211: assertEquals("LA==", new String(Base64
0212: .encodeBase64(new byte[] { (byte) 44 })));
0213: assertEquals("LQ==", new String(Base64
0214: .encodeBase64(new byte[] { (byte) 45 })));
0215: assertEquals("Lg==", new String(Base64
0216: .encodeBase64(new byte[] { (byte) 46 })));
0217: assertEquals("Lw==", new String(Base64
0218: .encodeBase64(new byte[] { (byte) 47 })));
0219: assertEquals("MA==", new String(Base64
0220: .encodeBase64(new byte[] { (byte) 48 })));
0221: assertEquals("MQ==", new String(Base64
0222: .encodeBase64(new byte[] { (byte) 49 })));
0223: assertEquals("Mg==", new String(Base64
0224: .encodeBase64(new byte[] { (byte) 50 })));
0225: assertEquals("Mw==", new String(Base64
0226: .encodeBase64(new byte[] { (byte) 51 })));
0227: assertEquals("NA==", new String(Base64
0228: .encodeBase64(new byte[] { (byte) 52 })));
0229: assertEquals("NQ==", new String(Base64
0230: .encodeBase64(new byte[] { (byte) 53 })));
0231: assertEquals("Ng==", new String(Base64
0232: .encodeBase64(new byte[] { (byte) 54 })));
0233: assertEquals("Nw==", new String(Base64
0234: .encodeBase64(new byte[] { (byte) 55 })));
0235: assertEquals("OA==", new String(Base64
0236: .encodeBase64(new byte[] { (byte) 56 })));
0237: assertEquals("OQ==", new String(Base64
0238: .encodeBase64(new byte[] { (byte) 57 })));
0239: assertEquals("Og==", new String(Base64
0240: .encodeBase64(new byte[] { (byte) 58 })));
0241: assertEquals("Ow==", new String(Base64
0242: .encodeBase64(new byte[] { (byte) 59 })));
0243: assertEquals("PA==", new String(Base64
0244: .encodeBase64(new byte[] { (byte) 60 })));
0245: assertEquals("PQ==", new String(Base64
0246: .encodeBase64(new byte[] { (byte) 61 })));
0247: assertEquals("Pg==", new String(Base64
0248: .encodeBase64(new byte[] { (byte) 62 })));
0249: assertEquals("Pw==", new String(Base64
0250: .encodeBase64(new byte[] { (byte) 63 })));
0251: assertEquals("QA==", new String(Base64
0252: .encodeBase64(new byte[] { (byte) 64 })));
0253: assertEquals("QQ==", new String(Base64
0254: .encodeBase64(new byte[] { (byte) 65 })));
0255: assertEquals("Qg==", new String(Base64
0256: .encodeBase64(new byte[] { (byte) 66 })));
0257: assertEquals("Qw==", new String(Base64
0258: .encodeBase64(new byte[] { (byte) 67 })));
0259: assertEquals("RA==", new String(Base64
0260: .encodeBase64(new byte[] { (byte) 68 })));
0261: assertEquals("RQ==", new String(Base64
0262: .encodeBase64(new byte[] { (byte) 69 })));
0263: assertEquals("Rg==", new String(Base64
0264: .encodeBase64(new byte[] { (byte) 70 })));
0265: assertEquals("Rw==", new String(Base64
0266: .encodeBase64(new byte[] { (byte) 71 })));
0267: assertEquals("SA==", new String(Base64
0268: .encodeBase64(new byte[] { (byte) 72 })));
0269: assertEquals("SQ==", new String(Base64
0270: .encodeBase64(new byte[] { (byte) 73 })));
0271: assertEquals("Sg==", new String(Base64
0272: .encodeBase64(new byte[] { (byte) 74 })));
0273: assertEquals("Sw==", new String(Base64
0274: .encodeBase64(new byte[] { (byte) 75 })));
0275: assertEquals("TA==", new String(Base64
0276: .encodeBase64(new byte[] { (byte) 76 })));
0277: assertEquals("TQ==", new String(Base64
0278: .encodeBase64(new byte[] { (byte) 77 })));
0279: assertEquals("Tg==", new String(Base64
0280: .encodeBase64(new byte[] { (byte) 78 })));
0281: assertEquals("Tw==", new String(Base64
0282: .encodeBase64(new byte[] { (byte) 79 })));
0283: assertEquals("UA==", new String(Base64
0284: .encodeBase64(new byte[] { (byte) 80 })));
0285: assertEquals("UQ==", new String(Base64
0286: .encodeBase64(new byte[] { (byte) 81 })));
0287: assertEquals("Ug==", new String(Base64
0288: .encodeBase64(new byte[] { (byte) 82 })));
0289: assertEquals("Uw==", new String(Base64
0290: .encodeBase64(new byte[] { (byte) 83 })));
0291: assertEquals("VA==", new String(Base64
0292: .encodeBase64(new byte[] { (byte) 84 })));
0293: assertEquals("VQ==", new String(Base64
0294: .encodeBase64(new byte[] { (byte) 85 })));
0295: assertEquals("Vg==", new String(Base64
0296: .encodeBase64(new byte[] { (byte) 86 })));
0297: assertEquals("Vw==", new String(Base64
0298: .encodeBase64(new byte[] { (byte) 87 })));
0299: assertEquals("WA==", new String(Base64
0300: .encodeBase64(new byte[] { (byte) 88 })));
0301: assertEquals("WQ==", new String(Base64
0302: .encodeBase64(new byte[] { (byte) 89 })));
0303: assertEquals("Wg==", new String(Base64
0304: .encodeBase64(new byte[] { (byte) 90 })));
0305: assertEquals("Ww==", new String(Base64
0306: .encodeBase64(new byte[] { (byte) 91 })));
0307: assertEquals("XA==", new String(Base64
0308: .encodeBase64(new byte[] { (byte) 92 })));
0309: assertEquals("XQ==", new String(Base64
0310: .encodeBase64(new byte[] { (byte) 93 })));
0311: assertEquals("Xg==", new String(Base64
0312: .encodeBase64(new byte[] { (byte) 94 })));
0313: assertEquals("Xw==", new String(Base64
0314: .encodeBase64(new byte[] { (byte) 95 })));
0315: assertEquals("YA==", new String(Base64
0316: .encodeBase64(new byte[] { (byte) 96 })));
0317: assertEquals("YQ==", new String(Base64
0318: .encodeBase64(new byte[] { (byte) 97 })));
0319: assertEquals("Yg==", new String(Base64
0320: .encodeBase64(new byte[] { (byte) 98 })));
0321: assertEquals("Yw==", new String(Base64
0322: .encodeBase64(new byte[] { (byte) 99 })));
0323: assertEquals("ZA==", new String(Base64
0324: .encodeBase64(new byte[] { (byte) 100 })));
0325: assertEquals("ZQ==", new String(Base64
0326: .encodeBase64(new byte[] { (byte) 101 })));
0327: assertEquals("Zg==", new String(Base64
0328: .encodeBase64(new byte[] { (byte) 102 })));
0329: assertEquals("Zw==", new String(Base64
0330: .encodeBase64(new byte[] { (byte) 103 })));
0331: assertEquals("aA==", new String(Base64
0332: .encodeBase64(new byte[] { (byte) 104 })));
0333: }
0334:
0335: public void testSingletonsChunked() {
0336: assertEquals("AA==\r\n", new String(Base64
0337: .encodeBase64Chunked(new byte[] { (byte) 0 })));
0338: assertEquals("AQ==\r\n", new String(Base64
0339: .encodeBase64Chunked(new byte[] { (byte) 1 })));
0340: assertEquals("Ag==\r\n", new String(Base64
0341: .encodeBase64Chunked(new byte[] { (byte) 2 })));
0342: assertEquals("Aw==\r\n", new String(Base64
0343: .encodeBase64Chunked(new byte[] { (byte) 3 })));
0344: assertEquals("BA==\r\n", new String(Base64
0345: .encodeBase64Chunked(new byte[] { (byte) 4 })));
0346: assertEquals("BQ==\r\n", new String(Base64
0347: .encodeBase64Chunked(new byte[] { (byte) 5 })));
0348: assertEquals("Bg==\r\n", new String(Base64
0349: .encodeBase64Chunked(new byte[] { (byte) 6 })));
0350: assertEquals("Bw==\r\n", new String(Base64
0351: .encodeBase64Chunked(new byte[] { (byte) 7 })));
0352: assertEquals("CA==\r\n", new String(Base64
0353: .encodeBase64Chunked(new byte[] { (byte) 8 })));
0354: assertEquals("CQ==\r\n", new String(Base64
0355: .encodeBase64Chunked(new byte[] { (byte) 9 })));
0356: assertEquals("Cg==\r\n", new String(Base64
0357: .encodeBase64Chunked(new byte[] { (byte) 10 })));
0358: assertEquals("Cw==\r\n", new String(Base64
0359: .encodeBase64Chunked(new byte[] { (byte) 11 })));
0360: assertEquals("DA==\r\n", new String(Base64
0361: .encodeBase64Chunked(new byte[] { (byte) 12 })));
0362: assertEquals("DQ==\r\n", new String(Base64
0363: .encodeBase64Chunked(new byte[] { (byte) 13 })));
0364: assertEquals("Dg==\r\n", new String(Base64
0365: .encodeBase64Chunked(new byte[] { (byte) 14 })));
0366: assertEquals("Dw==\r\n", new String(Base64
0367: .encodeBase64Chunked(new byte[] { (byte) 15 })));
0368: assertEquals("EA==\r\n", new String(Base64
0369: .encodeBase64Chunked(new byte[] { (byte) 16 })));
0370: assertEquals("EQ==\r\n", new String(Base64
0371: .encodeBase64Chunked(new byte[] { (byte) 17 })));
0372: assertEquals("Eg==\r\n", new String(Base64
0373: .encodeBase64Chunked(new byte[] { (byte) 18 })));
0374: assertEquals("Ew==\r\n", new String(Base64
0375: .encodeBase64Chunked(new byte[] { (byte) 19 })));
0376: assertEquals("FA==\r\n", new String(Base64
0377: .encodeBase64Chunked(new byte[] { (byte) 20 })));
0378: assertEquals("FQ==\r\n", new String(Base64
0379: .encodeBase64Chunked(new byte[] { (byte) 21 })));
0380: assertEquals("Fg==\r\n", new String(Base64
0381: .encodeBase64Chunked(new byte[] { (byte) 22 })));
0382: assertEquals("Fw==\r\n", new String(Base64
0383: .encodeBase64Chunked(new byte[] { (byte) 23 })));
0384: assertEquals("GA==\r\n", new String(Base64
0385: .encodeBase64Chunked(new byte[] { (byte) 24 })));
0386: assertEquals("GQ==\r\n", new String(Base64
0387: .encodeBase64Chunked(new byte[] { (byte) 25 })));
0388: assertEquals("Gg==\r\n", new String(Base64
0389: .encodeBase64Chunked(new byte[] { (byte) 26 })));
0390: assertEquals("Gw==\r\n", new String(Base64
0391: .encodeBase64Chunked(new byte[] { (byte) 27 })));
0392: assertEquals("HA==\r\n", new String(Base64
0393: .encodeBase64Chunked(new byte[] { (byte) 28 })));
0394: assertEquals("HQ==\r\n", new String(Base64
0395: .encodeBase64Chunked(new byte[] { (byte) 29 })));
0396: assertEquals("Hg==\r\n", new String(Base64
0397: .encodeBase64Chunked(new byte[] { (byte) 30 })));
0398: assertEquals("Hw==\r\n", new String(Base64
0399: .encodeBase64Chunked(new byte[] { (byte) 31 })));
0400: assertEquals("IA==\r\n", new String(Base64
0401: .encodeBase64Chunked(new byte[] { (byte) 32 })));
0402: assertEquals("IQ==\r\n", new String(Base64
0403: .encodeBase64Chunked(new byte[] { (byte) 33 })));
0404: assertEquals("Ig==\r\n", new String(Base64
0405: .encodeBase64Chunked(new byte[] { (byte) 34 })));
0406: assertEquals("Iw==\r\n", new String(Base64
0407: .encodeBase64Chunked(new byte[] { (byte) 35 })));
0408: assertEquals("JA==\r\n", new String(Base64
0409: .encodeBase64Chunked(new byte[] { (byte) 36 })));
0410: assertEquals("JQ==\r\n", new String(Base64
0411: .encodeBase64Chunked(new byte[] { (byte) 37 })));
0412: assertEquals("Jg==\r\n", new String(Base64
0413: .encodeBase64Chunked(new byte[] { (byte) 38 })));
0414: assertEquals("Jw==\r\n", new String(Base64
0415: .encodeBase64Chunked(new byte[] { (byte) 39 })));
0416: assertEquals("KA==\r\n", new String(Base64
0417: .encodeBase64Chunked(new byte[] { (byte) 40 })));
0418: assertEquals("KQ==\r\n", new String(Base64
0419: .encodeBase64Chunked(new byte[] { (byte) 41 })));
0420: assertEquals("Kg==\r\n", new String(Base64
0421: .encodeBase64Chunked(new byte[] { (byte) 42 })));
0422: assertEquals("Kw==\r\n", new String(Base64
0423: .encodeBase64Chunked(new byte[] { (byte) 43 })));
0424: assertEquals("LA==\r\n", new String(Base64
0425: .encodeBase64Chunked(new byte[] { (byte) 44 })));
0426: assertEquals("LQ==\r\n", new String(Base64
0427: .encodeBase64Chunked(new byte[] { (byte) 45 })));
0428: assertEquals("Lg==\r\n", new String(Base64
0429: .encodeBase64Chunked(new byte[] { (byte) 46 })));
0430: assertEquals("Lw==\r\n", new String(Base64
0431: .encodeBase64Chunked(new byte[] { (byte) 47 })));
0432: assertEquals("MA==\r\n", new String(Base64
0433: .encodeBase64Chunked(new byte[] { (byte) 48 })));
0434: assertEquals("MQ==\r\n", new String(Base64
0435: .encodeBase64Chunked(new byte[] { (byte) 49 })));
0436: assertEquals("Mg==\r\n", new String(Base64
0437: .encodeBase64Chunked(new byte[] { (byte) 50 })));
0438: assertEquals("Mw==\r\n", new String(Base64
0439: .encodeBase64Chunked(new byte[] { (byte) 51 })));
0440: assertEquals("NA==\r\n", new String(Base64
0441: .encodeBase64Chunked(new byte[] { (byte) 52 })));
0442: assertEquals("NQ==\r\n", new String(Base64
0443: .encodeBase64Chunked(new byte[] { (byte) 53 })));
0444: assertEquals("Ng==\r\n", new String(Base64
0445: .encodeBase64Chunked(new byte[] { (byte) 54 })));
0446: assertEquals("Nw==\r\n", new String(Base64
0447: .encodeBase64Chunked(new byte[] { (byte) 55 })));
0448: assertEquals("OA==\r\n", new String(Base64
0449: .encodeBase64Chunked(new byte[] { (byte) 56 })));
0450: assertEquals("OQ==\r\n", new String(Base64
0451: .encodeBase64Chunked(new byte[] { (byte) 57 })));
0452: assertEquals("Og==\r\n", new String(Base64
0453: .encodeBase64Chunked(new byte[] { (byte) 58 })));
0454: assertEquals("Ow==\r\n", new String(Base64
0455: .encodeBase64Chunked(new byte[] { (byte) 59 })));
0456: assertEquals("PA==\r\n", new String(Base64
0457: .encodeBase64Chunked(new byte[] { (byte) 60 })));
0458: assertEquals("PQ==\r\n", new String(Base64
0459: .encodeBase64Chunked(new byte[] { (byte) 61 })));
0460: assertEquals("Pg==\r\n", new String(Base64
0461: .encodeBase64Chunked(new byte[] { (byte) 62 })));
0462: assertEquals("Pw==\r\n", new String(Base64
0463: .encodeBase64Chunked(new byte[] { (byte) 63 })));
0464: assertEquals("QA==\r\n", new String(Base64
0465: .encodeBase64Chunked(new byte[] { (byte) 64 })));
0466: assertEquals("QQ==\r\n", new String(Base64
0467: .encodeBase64Chunked(new byte[] { (byte) 65 })));
0468: assertEquals("Qg==\r\n", new String(Base64
0469: .encodeBase64Chunked(new byte[] { (byte) 66 })));
0470: assertEquals("Qw==\r\n", new String(Base64
0471: .encodeBase64Chunked(new byte[] { (byte) 67 })));
0472: assertEquals("RA==\r\n", new String(Base64
0473: .encodeBase64Chunked(new byte[] { (byte) 68 })));
0474: assertEquals("RQ==\r\n", new String(Base64
0475: .encodeBase64Chunked(new byte[] { (byte) 69 })));
0476: assertEquals("Rg==\r\n", new String(Base64
0477: .encodeBase64Chunked(new byte[] { (byte) 70 })));
0478: assertEquals("Rw==\r\n", new String(Base64
0479: .encodeBase64Chunked(new byte[] { (byte) 71 })));
0480: assertEquals("SA==\r\n", new String(Base64
0481: .encodeBase64Chunked(new byte[] { (byte) 72 })));
0482: assertEquals("SQ==\r\n", new String(Base64
0483: .encodeBase64Chunked(new byte[] { (byte) 73 })));
0484: assertEquals("Sg==\r\n", new String(Base64
0485: .encodeBase64Chunked(new byte[] { (byte) 74 })));
0486: assertEquals("Sw==\r\n", new String(Base64
0487: .encodeBase64Chunked(new byte[] { (byte) 75 })));
0488: assertEquals("TA==\r\n", new String(Base64
0489: .encodeBase64Chunked(new byte[] { (byte) 76 })));
0490: assertEquals("TQ==\r\n", new String(Base64
0491: .encodeBase64Chunked(new byte[] { (byte) 77 })));
0492: assertEquals("Tg==\r\n", new String(Base64
0493: .encodeBase64Chunked(new byte[] { (byte) 78 })));
0494: assertEquals("Tw==\r\n", new String(Base64
0495: .encodeBase64Chunked(new byte[] { (byte) 79 })));
0496: assertEquals("UA==\r\n", new String(Base64
0497: .encodeBase64Chunked(new byte[] { (byte) 80 })));
0498: assertEquals("UQ==\r\n", new String(Base64
0499: .encodeBase64Chunked(new byte[] { (byte) 81 })));
0500: assertEquals("Ug==\r\n", new String(Base64
0501: .encodeBase64Chunked(new byte[] { (byte) 82 })));
0502: assertEquals("Uw==\r\n", new String(Base64
0503: .encodeBase64Chunked(new byte[] { (byte) 83 })));
0504: assertEquals("VA==\r\n", new String(Base64
0505: .encodeBase64Chunked(new byte[] { (byte) 84 })));
0506: assertEquals("VQ==\r\n", new String(Base64
0507: .encodeBase64Chunked(new byte[] { (byte) 85 })));
0508: assertEquals("Vg==\r\n", new String(Base64
0509: .encodeBase64Chunked(new byte[] { (byte) 86 })));
0510: assertEquals("Vw==\r\n", new String(Base64
0511: .encodeBase64Chunked(new byte[] { (byte) 87 })));
0512: assertEquals("WA==\r\n", new String(Base64
0513: .encodeBase64Chunked(new byte[] { (byte) 88 })));
0514: assertEquals("WQ==\r\n", new String(Base64
0515: .encodeBase64Chunked(new byte[] { (byte) 89 })));
0516: assertEquals("Wg==\r\n", new String(Base64
0517: .encodeBase64Chunked(new byte[] { (byte) 90 })));
0518: assertEquals("Ww==\r\n", new String(Base64
0519: .encodeBase64Chunked(new byte[] { (byte) 91 })));
0520: assertEquals("XA==\r\n", new String(Base64
0521: .encodeBase64Chunked(new byte[] { (byte) 92 })));
0522: assertEquals("XQ==\r\n", new String(Base64
0523: .encodeBase64Chunked(new byte[] { (byte) 93 })));
0524: assertEquals("Xg==\r\n", new String(Base64
0525: .encodeBase64Chunked(new byte[] { (byte) 94 })));
0526: assertEquals("Xw==\r\n", new String(Base64
0527: .encodeBase64Chunked(new byte[] { (byte) 95 })));
0528: assertEquals("YA==\r\n", new String(Base64
0529: .encodeBase64Chunked(new byte[] { (byte) 96 })));
0530: assertEquals("YQ==\r\n", new String(Base64
0531: .encodeBase64Chunked(new byte[] { (byte) 97 })));
0532: assertEquals("Yg==\r\n", new String(Base64
0533: .encodeBase64Chunked(new byte[] { (byte) 98 })));
0534: assertEquals("Yw==\r\n", new String(Base64
0535: .encodeBase64Chunked(new byte[] { (byte) 99 })));
0536: assertEquals("ZA==\r\n", new String(Base64
0537: .encodeBase64Chunked(new byte[] { (byte) 100 })));
0538: assertEquals("ZQ==\r\n", new String(Base64
0539: .encodeBase64Chunked(new byte[] { (byte) 101 })));
0540: assertEquals("Zg==\r\n", new String(Base64
0541: .encodeBase64Chunked(new byte[] { (byte) 102 })));
0542: assertEquals("Zw==\r\n", new String(Base64
0543: .encodeBase64Chunked(new byte[] { (byte) 103 })));
0544: assertEquals("aA==\r\n", new String(Base64
0545: .encodeBase64Chunked(new byte[] { (byte) 104 })));
0546: }
0547:
0548: public void testTriplets() {
0549: assertEquals("AAAA", new String(Base64.encodeBase64(new byte[] {
0550: (byte) 0, (byte) 0, (byte) 0 })));
0551: assertEquals("AAAB", new String(Base64.encodeBase64(new byte[] {
0552: (byte) 0, (byte) 0, (byte) 1 })));
0553: assertEquals("AAAC", new String(Base64.encodeBase64(new byte[] {
0554: (byte) 0, (byte) 0, (byte) 2 })));
0555: assertEquals("AAAD", new String(Base64.encodeBase64(new byte[] {
0556: (byte) 0, (byte) 0, (byte) 3 })));
0557: assertEquals("AAAE", new String(Base64.encodeBase64(new byte[] {
0558: (byte) 0, (byte) 0, (byte) 4 })));
0559: assertEquals("AAAF", new String(Base64.encodeBase64(new byte[] {
0560: (byte) 0, (byte) 0, (byte) 5 })));
0561: assertEquals("AAAG", new String(Base64.encodeBase64(new byte[] {
0562: (byte) 0, (byte) 0, (byte) 6 })));
0563: assertEquals("AAAH", new String(Base64.encodeBase64(new byte[] {
0564: (byte) 0, (byte) 0, (byte) 7 })));
0565: assertEquals("AAAI", new String(Base64.encodeBase64(new byte[] {
0566: (byte) 0, (byte) 0, (byte) 8 })));
0567: assertEquals("AAAJ", new String(Base64.encodeBase64(new byte[] {
0568: (byte) 0, (byte) 0, (byte) 9 })));
0569: assertEquals("AAAK", new String(Base64.encodeBase64(new byte[] {
0570: (byte) 0, (byte) 0, (byte) 10 })));
0571: assertEquals("AAAL", new String(Base64.encodeBase64(new byte[] {
0572: (byte) 0, (byte) 0, (byte) 11 })));
0573: assertEquals("AAAM", new String(Base64.encodeBase64(new byte[] {
0574: (byte) 0, (byte) 0, (byte) 12 })));
0575: assertEquals("AAAN", new String(Base64.encodeBase64(new byte[] {
0576: (byte) 0, (byte) 0, (byte) 13 })));
0577: assertEquals("AAAO", new String(Base64.encodeBase64(new byte[] {
0578: (byte) 0, (byte) 0, (byte) 14 })));
0579: assertEquals("AAAP", new String(Base64.encodeBase64(new byte[] {
0580: (byte) 0, (byte) 0, (byte) 15 })));
0581: assertEquals("AAAQ", new String(Base64.encodeBase64(new byte[] {
0582: (byte) 0, (byte) 0, (byte) 16 })));
0583: assertEquals("AAAR", new String(Base64.encodeBase64(new byte[] {
0584: (byte) 0, (byte) 0, (byte) 17 })));
0585: assertEquals("AAAS", new String(Base64.encodeBase64(new byte[] {
0586: (byte) 0, (byte) 0, (byte) 18 })));
0587: assertEquals("AAAT", new String(Base64.encodeBase64(new byte[] {
0588: (byte) 0, (byte) 0, (byte) 19 })));
0589: assertEquals("AAAU", new String(Base64.encodeBase64(new byte[] {
0590: (byte) 0, (byte) 0, (byte) 20 })));
0591: assertEquals("AAAV", new String(Base64.encodeBase64(new byte[] {
0592: (byte) 0, (byte) 0, (byte) 21 })));
0593: assertEquals("AAAW", new String(Base64.encodeBase64(new byte[] {
0594: (byte) 0, (byte) 0, (byte) 22 })));
0595: assertEquals("AAAX", new String(Base64.encodeBase64(new byte[] {
0596: (byte) 0, (byte) 0, (byte) 23 })));
0597: assertEquals("AAAY", new String(Base64.encodeBase64(new byte[] {
0598: (byte) 0, (byte) 0, (byte) 24 })));
0599: assertEquals("AAAZ", new String(Base64.encodeBase64(new byte[] {
0600: (byte) 0, (byte) 0, (byte) 25 })));
0601: assertEquals("AAAa", new String(Base64.encodeBase64(new byte[] {
0602: (byte) 0, (byte) 0, (byte) 26 })));
0603: assertEquals("AAAb", new String(Base64.encodeBase64(new byte[] {
0604: (byte) 0, (byte) 0, (byte) 27 })));
0605: assertEquals("AAAc", new String(Base64.encodeBase64(new byte[] {
0606: (byte) 0, (byte) 0, (byte) 28 })));
0607: assertEquals("AAAd", new String(Base64.encodeBase64(new byte[] {
0608: (byte) 0, (byte) 0, (byte) 29 })));
0609: assertEquals("AAAe", new String(Base64.encodeBase64(new byte[] {
0610: (byte) 0, (byte) 0, (byte) 30 })));
0611: assertEquals("AAAf", new String(Base64.encodeBase64(new byte[] {
0612: (byte) 0, (byte) 0, (byte) 31 })));
0613: assertEquals("AAAg", new String(Base64.encodeBase64(new byte[] {
0614: (byte) 0, (byte) 0, (byte) 32 })));
0615: assertEquals("AAAh", new String(Base64.encodeBase64(new byte[] {
0616: (byte) 0, (byte) 0, (byte) 33 })));
0617: assertEquals("AAAi", new String(Base64.encodeBase64(new byte[] {
0618: (byte) 0, (byte) 0, (byte) 34 })));
0619: assertEquals("AAAj", new String(Base64.encodeBase64(new byte[] {
0620: (byte) 0, (byte) 0, (byte) 35 })));
0621: assertEquals("AAAk", new String(Base64.encodeBase64(new byte[] {
0622: (byte) 0, (byte) 0, (byte) 36 })));
0623: assertEquals("AAAl", new String(Base64.encodeBase64(new byte[] {
0624: (byte) 0, (byte) 0, (byte) 37 })));
0625: assertEquals("AAAm", new String(Base64.encodeBase64(new byte[] {
0626: (byte) 0, (byte) 0, (byte) 38 })));
0627: assertEquals("AAAn", new String(Base64.encodeBase64(new byte[] {
0628: (byte) 0, (byte) 0, (byte) 39 })));
0629: assertEquals("AAAo", new String(Base64.encodeBase64(new byte[] {
0630: (byte) 0, (byte) 0, (byte) 40 })));
0631: assertEquals("AAAp", new String(Base64.encodeBase64(new byte[] {
0632: (byte) 0, (byte) 0, (byte) 41 })));
0633: assertEquals("AAAq", new String(Base64.encodeBase64(new byte[] {
0634: (byte) 0, (byte) 0, (byte) 42 })));
0635: assertEquals("AAAr", new String(Base64.encodeBase64(new byte[] {
0636: (byte) 0, (byte) 0, (byte) 43 })));
0637: assertEquals("AAAs", new String(Base64.encodeBase64(new byte[] {
0638: (byte) 0, (byte) 0, (byte) 44 })));
0639: assertEquals("AAAt", new String(Base64.encodeBase64(new byte[] {
0640: (byte) 0, (byte) 0, (byte) 45 })));
0641: assertEquals("AAAu", new String(Base64.encodeBase64(new byte[] {
0642: (byte) 0, (byte) 0, (byte) 46 })));
0643: assertEquals("AAAv", new String(Base64.encodeBase64(new byte[] {
0644: (byte) 0, (byte) 0, (byte) 47 })));
0645: assertEquals("AAAw", new String(Base64.encodeBase64(new byte[] {
0646: (byte) 0, (byte) 0, (byte) 48 })));
0647: assertEquals("AAAx", new String(Base64.encodeBase64(new byte[] {
0648: (byte) 0, (byte) 0, (byte) 49 })));
0649: assertEquals("AAAy", new String(Base64.encodeBase64(new byte[] {
0650: (byte) 0, (byte) 0, (byte) 50 })));
0651: assertEquals("AAAz", new String(Base64.encodeBase64(new byte[] {
0652: (byte) 0, (byte) 0, (byte) 51 })));
0653: assertEquals("AAA0", new String(Base64.encodeBase64(new byte[] {
0654: (byte) 0, (byte) 0, (byte) 52 })));
0655: assertEquals("AAA1", new String(Base64.encodeBase64(new byte[] {
0656: (byte) 0, (byte) 0, (byte) 53 })));
0657: assertEquals("AAA2", new String(Base64.encodeBase64(new byte[] {
0658: (byte) 0, (byte) 0, (byte) 54 })));
0659: assertEquals("AAA3", new String(Base64.encodeBase64(new byte[] {
0660: (byte) 0, (byte) 0, (byte) 55 })));
0661: assertEquals("AAA4", new String(Base64.encodeBase64(new byte[] {
0662: (byte) 0, (byte) 0, (byte) 56 })));
0663: assertEquals("AAA5", new String(Base64.encodeBase64(new byte[] {
0664: (byte) 0, (byte) 0, (byte) 57 })));
0665: assertEquals("AAA6", new String(Base64.encodeBase64(new byte[] {
0666: (byte) 0, (byte) 0, (byte) 58 })));
0667: assertEquals("AAA7", new String(Base64.encodeBase64(new byte[] {
0668: (byte) 0, (byte) 0, (byte) 59 })));
0669: assertEquals("AAA8", new String(Base64.encodeBase64(new byte[] {
0670: (byte) 0, (byte) 0, (byte) 60 })));
0671: assertEquals("AAA9", new String(Base64.encodeBase64(new byte[] {
0672: (byte) 0, (byte) 0, (byte) 61 })));
0673: assertEquals("AAA+", new String(Base64.encodeBase64(new byte[] {
0674: (byte) 0, (byte) 0, (byte) 62 })));
0675: assertEquals("AAA/", new String(Base64.encodeBase64(new byte[] {
0676: (byte) 0, (byte) 0, (byte) 63 })));
0677: }
0678:
0679: public void testTripletsChunked() {
0680: assertEquals("AAAA\r\n", new String(Base64
0681: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0682: (byte) 0 })));
0683: assertEquals("AAAB\r\n", new String(Base64
0684: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0685: (byte) 1 })));
0686: assertEquals("AAAC\r\n", new String(Base64
0687: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0688: (byte) 2 })));
0689: assertEquals("AAAD\r\n", new String(Base64
0690: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0691: (byte) 3 })));
0692: assertEquals("AAAE\r\n", new String(Base64
0693: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0694: (byte) 4 })));
0695: assertEquals("AAAF\r\n", new String(Base64
0696: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0697: (byte) 5 })));
0698: assertEquals("AAAG\r\n", new String(Base64
0699: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0700: (byte) 6 })));
0701: assertEquals("AAAH\r\n", new String(Base64
0702: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0703: (byte) 7 })));
0704: assertEquals("AAAI\r\n", new String(Base64
0705: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0706: (byte) 8 })));
0707: assertEquals("AAAJ\r\n", new String(Base64
0708: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0709: (byte) 9 })));
0710: assertEquals("AAAK\r\n", new String(Base64
0711: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0712: (byte) 10 })));
0713: assertEquals("AAAL\r\n", new String(Base64
0714: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0715: (byte) 11 })));
0716: assertEquals("AAAM\r\n", new String(Base64
0717: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0718: (byte) 12 })));
0719: assertEquals("AAAN\r\n", new String(Base64
0720: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0721: (byte) 13 })));
0722: assertEquals("AAAO\r\n", new String(Base64
0723: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0724: (byte) 14 })));
0725: assertEquals("AAAP\r\n", new String(Base64
0726: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0727: (byte) 15 })));
0728: assertEquals("AAAQ\r\n", new String(Base64
0729: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0730: (byte) 16 })));
0731: assertEquals("AAAR\r\n", new String(Base64
0732: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0733: (byte) 17 })));
0734: assertEquals("AAAS\r\n", new String(Base64
0735: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0736: (byte) 18 })));
0737: assertEquals("AAAT\r\n", new String(Base64
0738: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0739: (byte) 19 })));
0740: assertEquals("AAAU\r\n", new String(Base64
0741: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0742: (byte) 20 })));
0743: assertEquals("AAAV\r\n", new String(Base64
0744: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0745: (byte) 21 })));
0746: assertEquals("AAAW\r\n", new String(Base64
0747: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0748: (byte) 22 })));
0749: assertEquals("AAAX\r\n", new String(Base64
0750: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0751: (byte) 23 })));
0752: assertEquals("AAAY\r\n", new String(Base64
0753: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0754: (byte) 24 })));
0755: assertEquals("AAAZ\r\n", new String(Base64
0756: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0757: (byte) 25 })));
0758: assertEquals("AAAa\r\n", new String(Base64
0759: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0760: (byte) 26 })));
0761: assertEquals("AAAb\r\n", new String(Base64
0762: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0763: (byte) 27 })));
0764: assertEquals("AAAc\r\n", new String(Base64
0765: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0766: (byte) 28 })));
0767: assertEquals("AAAd\r\n", new String(Base64
0768: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0769: (byte) 29 })));
0770: assertEquals("AAAe\r\n", new String(Base64
0771: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0772: (byte) 30 })));
0773: assertEquals("AAAf\r\n", new String(Base64
0774: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0775: (byte) 31 })));
0776: assertEquals("AAAg\r\n", new String(Base64
0777: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0778: (byte) 32 })));
0779: assertEquals("AAAh\r\n", new String(Base64
0780: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0781: (byte) 33 })));
0782: assertEquals("AAAi\r\n", new String(Base64
0783: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0784: (byte) 34 })));
0785: assertEquals("AAAj\r\n", new String(Base64
0786: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0787: (byte) 35 })));
0788: assertEquals("AAAk\r\n", new String(Base64
0789: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0790: (byte) 36 })));
0791: assertEquals("AAAl\r\n", new String(Base64
0792: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0793: (byte) 37 })));
0794: assertEquals("AAAm\r\n", new String(Base64
0795: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0796: (byte) 38 })));
0797: assertEquals("AAAn\r\n", new String(Base64
0798: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0799: (byte) 39 })));
0800: assertEquals("AAAo\r\n", new String(Base64
0801: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0802: (byte) 40 })));
0803: assertEquals("AAAp\r\n", new String(Base64
0804: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0805: (byte) 41 })));
0806: assertEquals("AAAq\r\n", new String(Base64
0807: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0808: (byte) 42 })));
0809: assertEquals("AAAr\r\n", new String(Base64
0810: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0811: (byte) 43 })));
0812: assertEquals("AAAs\r\n", new String(Base64
0813: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0814: (byte) 44 })));
0815: assertEquals("AAAt\r\n", new String(Base64
0816: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0817: (byte) 45 })));
0818: assertEquals("AAAu\r\n", new String(Base64
0819: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0820: (byte) 46 })));
0821: assertEquals("AAAv\r\n", new String(Base64
0822: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0823: (byte) 47 })));
0824: assertEquals("AAAw\r\n", new String(Base64
0825: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0826: (byte) 48 })));
0827: assertEquals("AAAx\r\n", new String(Base64
0828: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0829: (byte) 49 })));
0830: assertEquals("AAAy\r\n", new String(Base64
0831: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0832: (byte) 50 })));
0833: assertEquals("AAAz\r\n", new String(Base64
0834: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0835: (byte) 51 })));
0836: assertEquals("AAA0\r\n", new String(Base64
0837: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0838: (byte) 52 })));
0839: assertEquals("AAA1\r\n", new String(Base64
0840: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0841: (byte) 53 })));
0842: assertEquals("AAA2\r\n", new String(Base64
0843: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0844: (byte) 54 })));
0845: assertEquals("AAA3\r\n", new String(Base64
0846: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0847: (byte) 55 })));
0848: assertEquals("AAA4\r\n", new String(Base64
0849: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0850: (byte) 56 })));
0851: assertEquals("AAA5\r\n", new String(Base64
0852: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0853: (byte) 57 })));
0854: assertEquals("AAA6\r\n", new String(Base64
0855: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0856: (byte) 58 })));
0857: assertEquals("AAA7\r\n", new String(Base64
0858: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0859: (byte) 59 })));
0860: assertEquals("AAA8\r\n", new String(Base64
0861: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0862: (byte) 60 })));
0863: assertEquals("AAA9\r\n", new String(Base64
0864: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0865: (byte) 61 })));
0866: assertEquals("AAA+\r\n", new String(Base64
0867: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0868: (byte) 62 })));
0869: assertEquals("AAA/\r\n", new String(Base64
0870: .encodeBase64Chunked(new byte[] { (byte) 0, (byte) 0,
0871: (byte) 63 })));
0872: }
0873:
0874: public void testKnownEncodings() {
0875: assertEquals(
0876: "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==",
0877: new String(
0878: Base64
0879: .encodeBase64("The quick brown fox jumped over the lazy dogs."
0880: .getBytes())));
0881: assertEquals(
0882: "YmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJs\r\nYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo\r\nIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi\r\nbGFoIGJsYWg=\r\n",
0883: new String(
0884: Base64
0885: .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"
0886: .getBytes())));
0887: assertEquals(
0888: "SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==",
0889: new String(
0890: Base64
0891: .encodeBase64("It was the best of times, it was the worst of times."
0892: .getBytes())));
0893: assertEquals(
0894: "aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==",
0895: new String(
0896: Base64
0897: .encodeBase64("http://jakarta.apache.org/commmons"
0898: .getBytes())));
0899: assertEquals(
0900: "QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==",
0901: new String(
0902: Base64
0903: .encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
0904: .getBytes())));
0905: assertEquals(
0906: "eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=",
0907: new String(
0908: Base64
0909: .encodeBase64("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }"
0910: .getBytes())));
0911: assertEquals("eHl6enkh", new String(Base64
0912: .encodeBase64("xyzzy!".getBytes())));
0913: }
0914:
0915: public void testKnownDecodings() {
0916: assertEquals(
0917: "The quick brown fox jumped over the lazy dogs.",
0918: new String(
0919: Base64
0920: .decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg=="
0921: .getBytes())));
0922: assertEquals(
0923: "It was the best of times, it was the worst of times.",
0924: new String(
0925: Base64
0926: .decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg=="
0927: .getBytes())));
0928: assertEquals(
0929: "http://jakarta.apache.org/commmons",
0930: new String(
0931: Base64
0932: .decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw=="
0933: .getBytes())));
0934: assertEquals(
0935: "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",
0936: new String(
0937: Base64
0938: .decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg=="
0939: .getBytes())));
0940: assertEquals(
0941: "{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",
0942: new String(
0943: Base64
0944: .decodeBase64("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0="
0945: .getBytes())));
0946: assertEquals("xyzzy!", new String(Base64
0947: .decodeBase64("eHl6enkh".getBytes())));
0948: }
0949:
0950: public void testNonBase64Test() throws Exception {
0951:
0952: byte[] bArray = { '%' };
0953:
0954: assertFalse(
0955: "Invalid Base64 array was incorrectly validated as "
0956: + "an array of Base64 encoded data", Base64
0957: .isArrayByteBase64(bArray));
0958:
0959: boolean exceptionThrown = false;
0960:
0961: try {
0962: Base64 b64 = new Base64();
0963: byte[] result = b64.decode(bArray);
0964:
0965: assertTrue(
0966: "The result should be empty as the test encoded content did "
0967: + "not contain any valid base 64 characters",
0968: result.length == 0);
0969: } catch (Exception e) {
0970: exceptionThrown = true;
0971: }
0972:
0973: assertFalse(
0974: "Exception was thrown when trying to decode "
0975: + "invalid base64 encoded data - RFC 2045 requires that all "
0976: + "non base64 character be discarded, an exception should not"
0977: + " have been thrown", exceptionThrown);
0978:
0979: }
0980:
0981: public void testIgnoringNonBase64InDecode() throws Exception {
0982: assertEquals(
0983: "The quick brown fox jumped over the lazy dogs.",
0984: new String(
0985: Base64
0986: .decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg=="
0987: .getBytes())));
0988: }
0989:
0990: public void testObjectDecodeWithInvalidParameter() throws Exception {
0991: boolean exceptionThrown = false;
0992:
0993: Base64 b64 = new Base64();
0994:
0995: try {
0996: Object o = new String("Yadayadayada");
0997: b64.decode(o);
0998: } catch (Exception e) {
0999: exceptionThrown = true;
1000: }
1001:
1002: assertTrue(
1003: "decode(Object) didn't throw an exception when passed a "
1004: + "String object", exceptionThrown);
1005: }
1006:
1007: public void testObjectDecodeWithValidParameter() throws Exception {
1008:
1009: String original = "Hello World!";
1010: byte[] bArray = Base64.encodeBase64((new String(original))
1011: .getBytes());
1012: Object o = bArray;
1013:
1014: Base64 b64 = new Base64();
1015: Object oDecoded = b64.decode(o);
1016: byte[] baDecoded = (byte[]) oDecoded;
1017: String dest = new String(baDecoded);
1018:
1019: assertTrue("dest string down not equal original", dest
1020: .equals(original));
1021: }
1022:
1023: public void testObjectEncodeWithInvalidParameter() throws Exception {
1024: boolean exceptionThrown = false;
1025:
1026: Base64 b64 = new Base64();
1027:
1028: try {
1029: Object o = new String("Yadayadayada");
1030: b64.encode(o);
1031: } catch (Exception e) {
1032: exceptionThrown = true;
1033: }
1034:
1035: assertTrue(
1036: "encode(Object) didn't throw an exception when passed a "
1037: + "String object", exceptionThrown);
1038: }
1039:
1040: public void testObjectEncodeWithValidParameter() throws Exception {
1041:
1042: String original = "Hello World!";
1043: byte[] origBytes = original.getBytes();
1044: Object origObj = origBytes;
1045:
1046: Base64 b64 = new Base64();
1047: Object oEncoded = b64.encode(origObj);
1048: byte[] bArray = Base64.decodeBase64((byte[]) oEncoded);
1049: String dest = new String(bArray);
1050:
1051: assertTrue("dest string down not equal original", dest
1052: .equals(original));
1053: }
1054:
1055: public void testDecodeWithWhitespace() throws Exception {
1056:
1057: String orig = "I am a late night coder.";
1058:
1059: byte[] encodedArray = Base64.encodeBase64(orig.getBytes());
1060: StringBuffer intermediate = new StringBuffer(new String(
1061: encodedArray));
1062:
1063: intermediate.insert(2, ' ');
1064: intermediate.insert(5, '\t');
1065: intermediate.insert(10, '\r');
1066: intermediate.insert(15, '\n');
1067:
1068: byte[] encodedWithWS = intermediate.toString().getBytes();
1069: byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
1070:
1071: String dest = new String(decodedWithWS);
1072:
1073: assertTrue("Dest string doesn't equals the original", dest
1074: .equals(orig));
1075: }
1076:
1077: public void testDiscardWhitespace() throws Exception {
1078:
1079: String orig = "I am a late night coder.";
1080:
1081: byte[] encodedArray = Base64.encodeBase64(orig.getBytes());
1082: StringBuffer intermediate = new StringBuffer(new String(
1083: encodedArray));
1084:
1085: intermediate.insert(2, ' ');
1086: intermediate.insert(5, '\t');
1087: intermediate.insert(10, '\r');
1088: intermediate.insert(15, '\n');
1089:
1090: byte[] encodedWithWS = intermediate.toString().getBytes();
1091: byte[] encodedNoWS = Base64.discardWhitespace(encodedWithWS);
1092: byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
1093: byte[] decodedNoWS = Base64.decodeBase64(encodedNoWS);
1094:
1095: String destFromWS = new String(decodedWithWS);
1096: String destFromNoWS = new String(decodedNoWS);
1097:
1098: assertTrue("Dest string doesn't eausl original", destFromWS
1099: .equals(orig));
1100: assertTrue("Dest string doesn't eausl original", destFromNoWS
1101: .equals(orig));
1102: }
1103:
1104: // -------------------------------------------------------- Private Methods
1105:
1106: private String toString(byte[] data) {
1107: StringBuffer buf = new StringBuffer();
1108: for (int i = 0; i < data.length; i++) {
1109: buf.append(data[i]);
1110: if (i != data.length - 1) {
1111: buf.append(",");
1112: }
1113: }
1114: return buf.toString();
1115: }
1116:
1117: // ------------------------------------------------------------------------
1118:
1119: private Random _random = new Random();
1120:
1121: /**
1122: * @return Returns the _random.
1123: */
1124: public Random getRandom() {
1125: return this._random;
1126: }
1127:
1128: }
|