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