1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBase64.java,v 1.8 2003/02/07 14:38:02 jsdever Exp $ 3 * $Revision: 1.8 $ 4 * $Date: 2003/02/07 14:38:02 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import junit.framework.*; 66 import java.util.Random; 67 import org.apache.commons.httpclient.util.Base64; 68 69 /*** 70 * Simple tests of {@link Base64}. 71 * 72 * @deprecated The commons-codec Base64 class will be used in HttpClient 2.1 73 * @author Rodney Waldhoff 74 * @version $Id: TestBase64.java,v 1.8 2003/02/07 14:38:02 jsdever Exp $ 75 */ 76 public class TestBase64 extends TestCase { 77 78 // ------------------------------------------------------------ Constructor 79 public TestBase64(String testName) { 80 super(testName); 81 } 82 83 // ------------------------------------------------------------------- Main 84 public static void main(String args[]) { 85 String[] testCaseName = { TestBase64.class.getName() }; 86 junit.textui.TestRunner.main(testCaseName); 87 } 88 89 // ------------------------------------------------------- TestCase Methods 90 public static Test suite() { 91 return new TestSuite(TestBase64.class); 92 } 93 94 public void setUp() throws Exception { 95 } 96 97 private Random _rand = new Random(); 98 99 // encode/decode random arrays from size 0 to size 11 100 public void testEncodeDecodeSmall() { 101 for(int i=0;i<12;i++) { 102 byte[] data = new byte[i]; 103 _rand.nextBytes(data); 104 byte[] enc = Base64.encode(data); 105 assertTrue("\"" + (HttpConstants.getAsciiString(enc)) + "\" is Base64 data.",Base64.isBase64((HttpConstants.getAsciiString(enc)))); 106 byte[] data2 = Base64.decode(enc); 107 assertTrue(toString(data) + " equals " + toString(data2),isEqual(data,data2)); 108 } 109 } 110 111 // encode/decode a large random array 112 public void testEncodeDecodeRandom() { 113 for(int i=1;i<5;i++) { 114 byte[] data = new byte[_rand.nextInt(10000)+1]; 115 _rand.nextBytes(data); 116 byte[] enc = Base64.encode(data); 117 assertTrue(Base64.isBase64(HttpConstants.getAsciiString(enc))); 118 byte[] data2 = Base64.decode(enc); 119 assertTrue(isEqual(data,data2)); 120 } 121 } 122 123 public void testSingletons() { 124 assertEquals("AA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0 }))); 125 assertEquals("AQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)1 }))); 126 assertEquals("Ag==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)2 }))); 127 assertEquals("Aw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)3 }))); 128 assertEquals("BA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)4 }))); 129 assertEquals("BQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)5 }))); 130 assertEquals("Bg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)6 }))); 131 assertEquals("Bw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)7 }))); 132 assertEquals("CA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)8 }))); 133 assertEquals("CQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)9 }))); 134 assertEquals("Cg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)10 }))); 135 assertEquals("Cw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)11 }))); 136 assertEquals("DA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)12 }))); 137 assertEquals("DQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)13 }))); 138 assertEquals("Dg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)14 }))); 139 assertEquals("Dw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)15 }))); 140 assertEquals("EA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)16 }))); 141 assertEquals("EQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)17 }))); 142 assertEquals("Eg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)18 }))); 143 assertEquals("Ew==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)19 }))); 144 assertEquals("FA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)20 }))); 145 assertEquals("FQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)21 }))); 146 assertEquals("Fg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)22 }))); 147 assertEquals("Fw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)23 }))); 148 assertEquals("GA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)24 }))); 149 assertEquals("GQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)25 }))); 150 assertEquals("Gg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)26 }))); 151 assertEquals("Gw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)27 }))); 152 assertEquals("HA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)28 }))); 153 assertEquals("HQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)29 }))); 154 assertEquals("Hg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)30 }))); 155 assertEquals("Hw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)31 }))); 156 assertEquals("IA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)32 }))); 157 assertEquals("IQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)33 }))); 158 assertEquals("Ig==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)34 }))); 159 assertEquals("Iw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)35 }))); 160 assertEquals("JA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)36 }))); 161 assertEquals("JQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)37 }))); 162 assertEquals("Jg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)38 }))); 163 assertEquals("Jw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)39 }))); 164 assertEquals("KA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)40 }))); 165 assertEquals("KQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)41 }))); 166 assertEquals("Kg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)42 }))); 167 assertEquals("Kw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)43 }))); 168 assertEquals("LA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)44 }))); 169 assertEquals("LQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)45 }))); 170 assertEquals("Lg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)46 }))); 171 assertEquals("Lw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)47 }))); 172 assertEquals("MA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)48 }))); 173 assertEquals("MQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)49 }))); 174 assertEquals("Mg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)50 }))); 175 assertEquals("Mw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)51 }))); 176 assertEquals("NA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)52 }))); 177 assertEquals("NQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)53 }))); 178 assertEquals("Ng==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)54 }))); 179 assertEquals("Nw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)55 }))); 180 assertEquals("OA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)56 }))); 181 assertEquals("OQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)57 }))); 182 assertEquals("Og==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)58 }))); 183 assertEquals("Ow==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)59 }))); 184 assertEquals("PA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)60 }))); 185 assertEquals("PQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)61 }))); 186 assertEquals("Pg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)62 }))); 187 assertEquals("Pw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)63 }))); 188 assertEquals("QA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)64 }))); 189 assertEquals("QQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)65 }))); 190 assertEquals("Qg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)66 }))); 191 assertEquals("Qw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)67 }))); 192 assertEquals("RA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)68 }))); 193 assertEquals("RQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)69 }))); 194 assertEquals("Rg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)70 }))); 195 assertEquals("Rw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)71 }))); 196 assertEquals("SA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)72 }))); 197 assertEquals("SQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)73 }))); 198 assertEquals("Sg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)74 }))); 199 assertEquals("Sw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)75 }))); 200 assertEquals("TA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)76 }))); 201 assertEquals("TQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)77 }))); 202 assertEquals("Tg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)78 }))); 203 assertEquals("Tw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)79 }))); 204 assertEquals("UA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)80 }))); 205 assertEquals("UQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)81 }))); 206 assertEquals("Ug==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)82 }))); 207 assertEquals("Uw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)83 }))); 208 assertEquals("VA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)84 }))); 209 assertEquals("VQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)85 }))); 210 assertEquals("Vg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)86 }))); 211 assertEquals("Vw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)87 }))); 212 assertEquals("WA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)88 }))); 213 assertEquals("WQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)89 }))); 214 assertEquals("Wg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)90 }))); 215 assertEquals("Ww==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)91 }))); 216 assertEquals("XA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)92 }))); 217 assertEquals("XQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)93 }))); 218 assertEquals("Xg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)94 }))); 219 assertEquals("Xw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)95 }))); 220 assertEquals("YA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)96 }))); 221 assertEquals("YQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)97 }))); 222 assertEquals("Yg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)98 }))); 223 assertEquals("Yw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)99 }))); 224 assertEquals("ZA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)100 }))); 225 assertEquals("ZQ==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)101 }))); 226 assertEquals("Zg==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)102 }))); 227 assertEquals("Zw==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)103 }))); 228 assertEquals("aA==",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)104 }))); 229 } 230 231 public void testTriplets() { 232 assertEquals("AAAA",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)0 }))); 233 assertEquals("AAAB",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)1 }))); 234 assertEquals("AAAC",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)2 }))); 235 assertEquals("AAAD",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)3 }))); 236 assertEquals("AAAE",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)4 }))); 237 assertEquals("AAAF",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)5 }))); 238 assertEquals("AAAG",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)6 }))); 239 assertEquals("AAAH",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)7 }))); 240 assertEquals("AAAI",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)8 }))); 241 assertEquals("AAAJ",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)9 }))); 242 assertEquals("AAAK",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)10 }))); 243 assertEquals("AAAL",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)11 }))); 244 assertEquals("AAAM",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)12 }))); 245 assertEquals("AAAN",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)13 }))); 246 assertEquals("AAAO",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)14 }))); 247 assertEquals("AAAP",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)15 }))); 248 assertEquals("AAAQ",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)16 }))); 249 assertEquals("AAAR",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)17 }))); 250 assertEquals("AAAS",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)18 }))); 251 assertEquals("AAAT",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)19 }))); 252 assertEquals("AAAU",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)20 }))); 253 assertEquals("AAAV",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)21 }))); 254 assertEquals("AAAW",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)22 }))); 255 assertEquals("AAAX",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)23 }))); 256 assertEquals("AAAY",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)24 }))); 257 assertEquals("AAAZ",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)25 }))); 258 assertEquals("AAAa",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)26 }))); 259 assertEquals("AAAb",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)27 }))); 260 assertEquals("AAAc",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)28 }))); 261 assertEquals("AAAd",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)29 }))); 262 assertEquals("AAAe",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)30 }))); 263 assertEquals("AAAf",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)31 }))); 264 assertEquals("AAAg",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)32 }))); 265 assertEquals("AAAh",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)33 }))); 266 assertEquals("AAAi",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)34 }))); 267 assertEquals("AAAj",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)35 }))); 268 assertEquals("AAAk",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)36 }))); 269 assertEquals("AAAl",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)37 }))); 270 assertEquals("AAAm",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)38 }))); 271 assertEquals("AAAn",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)39 }))); 272 assertEquals("AAAo",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)40 }))); 273 assertEquals("AAAp",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)41 }))); 274 assertEquals("AAAq",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)42 }))); 275 assertEquals("AAAr",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)43 }))); 276 assertEquals("AAAs",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)44 }))); 277 assertEquals("AAAt",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)45 }))); 278 assertEquals("AAAu",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)46 }))); 279 assertEquals("AAAv",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)47 }))); 280 assertEquals("AAAw",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)48 }))); 281 assertEquals("AAAx",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)49 }))); 282 assertEquals("AAAy",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)50 }))); 283 assertEquals("AAAz",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)51 }))); 284 assertEquals("AAA0",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)52 }))); 285 assertEquals("AAA1",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)53 }))); 286 assertEquals("AAA2",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)54 }))); 287 assertEquals("AAA3",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)55 }))); 288 assertEquals("AAA4",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)56 }))); 289 assertEquals("AAA5",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)57 }))); 290 assertEquals("AAA6",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)58 }))); 291 assertEquals("AAA7",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)59 }))); 292 assertEquals("AAA8",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)60 }))); 293 assertEquals("AAA9",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)61 }))); 294 assertEquals("AAA+",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)62 }))); 295 assertEquals("AAA/",HttpConstants.getAsciiString(Base64.encode(new byte[] { (byte)0, (byte)0, (byte)63 }))); 296 } 297 298 public void testKnownEncodings() { 299 assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("The quick brown fox jumped over the lazy dogs.")))); 300 assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("It was the best of times, it was the worst of times.")))); 301 assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("http://jakarta.apache.org/commmons")))); 302 assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz")))); 303 assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }")))); 304 assertEquals("eHl6enkh",HttpConstants.getAsciiString(Base64.encode(HttpConstants.getAsciiBytes("xyzzy!")))); 305 } 306 307 public void testKnownDecodings() { 308 assertEquals("The quick brown fox jumped over the lazy dogs.",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==")))); 309 assertEquals("It was the best of times, it was the worst of times.",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==")))); 310 assertEquals("http://jakarta.apache.org/commmons",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==")))); 311 assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==")))); 312 assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=")))); 313 assertEquals("xyzzy!",HttpConstants.getAsciiString(Base64.decode(HttpConstants.getAsciiBytes("eHl6enkh")))); 314 } 315 316 // -------------------------------------------------------- Private Methods 317 private boolean isEqual(byte[] a, byte[] b) { 318 if(a.length != b.length) { return false; } 319 for(int i=0;i<a.length;i++) { 320 if(a[i] != b[i]) { return false; } 321 } 322 return true; 323 } 324 325 private String toString(byte[] data) { 326 StringBuffer buf = new StringBuffer(); 327 for(int i=0;i<data.length;i++) { 328 buf.append(data[i]); 329 if(i != data.length-1) { 330 buf.append(","); 331 } 332 } 333 return buf.toString(); 334 } 335 }

This page was automatically generated by Maven