1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 4235519 26 * @summary tests java.util.Base64 27 */ 28 29 import java.io.ByteArrayInputStream; 30 import java.io.ByteArrayOutputStream; 31 import java.io.InputStream; 32 import java.io.IOException; 33 import java.io.OutputStream; 34 import java.nio.ByteBuffer; 35 import java.util.Arrays; 36 import java.util.Base64; 37 import java.util.Random; 38 39 public class TestBase64 { 40 41 public static void main(String args[]) throws Throwable { 42 int numRuns = 10; 43 int numBytes = 200; 44 if (args.length > 1) { 45 numRuns = Integer.parseInt(args[0]); 46 numBytes = Integer.parseInt(args[1]); 47 } 48 49 test(Base64.getEncoder(), Base64.getDecoder(), 50 numRuns, numBytes); 51 test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), 52 numRuns, numBytes); 53 test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), 54 numRuns, numBytes); 55 56 Random rnd = new java.util.Random(); 57 byte[] nl_1 = new byte[] {'\n'}; 58 byte[] nl_2 = new byte[] {'\n', '\r'}; 59 byte[] nl_3 = new byte[] {'\n', '\r', '\n'}; 60 for (int i = 0; i < 10; i++) { 61 int len = rnd.nextInt(200) + 4; 62 test(Base64.getEncoder(len, nl_1), 63 Base64.getMimeDecoder(), 64 numRuns, numBytes); 65 test(Base64.getEncoder(len, nl_2), 66 Base64.getMimeDecoder(), 67 numRuns, numBytes); 68 test(Base64.getEncoder(len, nl_3), 69 Base64.getMimeDecoder(), 70 numRuns, numBytes); 71 } 72 73 testNull(Base64.getEncoder()); 74 testNull(Base64.getUrlEncoder()); 75 testNull(Base64.getMimeEncoder()); 76 testNull(Base64.getEncoder(10, new byte[]{'\n'})); 77 testNull(Base64.getDecoder()); 78 testNull(Base64.getUrlDecoder()); 79 testNull(Base64.getMimeDecoder()); 80 checkNull(new Runnable() { public void run() { Base64.getEncoder(10, null); }}); 81 82 testIOE(Base64.getEncoder()); 83 testIOE(Base64.getUrlEncoder()); 84 testIOE(Base64.getMimeEncoder()); 85 testIOE(Base64.getEncoder(10, new byte[]{'\n'})); 86 87 byte[] src = new byte[1024]; 88 new Random().nextBytes(src); 89 final byte[] decoded = Base64.getEncoder().encode(src); 90 testIOE(Base64.getDecoder(), decoded); 91 testIOE(Base64.getMimeDecoder(), decoded); 92 testIOE(Base64.getUrlDecoder(), Base64.getUrlEncoder().encode(src)); 93 94 // illegal line separator 95 checkIAE(new Runnable() { public void run() { Base64.getEncoder(10, new byte[]{'\r', 'N'}); }}); 96 97 // illegal base64 character 98 decoded[2] = (byte)0xe0; 99 checkIAE(new Runnable() { 100 public void run() { Base64.getDecoder().decode(decoded); }}); 101 checkIAE(new Runnable() { 102 public void run() { Base64.getDecoder().decode(decoded, new byte[1024]); }}); 103 checkIAE(new Runnable() { public void run() { 104 Base64.getDecoder().decode(ByteBuffer.wrap(decoded)); }}); 105 checkIAE(new Runnable() { public void run() { 106 Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocate(1024)); }}); 107 checkIAE(new Runnable() { public void run() { 108 Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocateDirect(1024)); }}); 109 } 110 111 private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder(); 112 113 private static void test(Base64.Encoder enc, Base64.Decoder dec, 114 int numRuns, int numBytes) throws Throwable { 115 Random rnd = new java.util.Random(); 116 117 enc.encode(new byte[0]); 118 dec.decode(new byte[0]); 119 120 for (int i=0; i<numRuns; i++) { 121 for (int j=1; j<numBytes; j++) { 122 byte[] orig = new byte[j]; 123 rnd.nextBytes(orig); 124 125 // --------testing encode/decode(byte[])-------- 126 byte[] encoded = enc.encode(orig); 127 byte[] decoded = dec.decode(encoded); 128 129 checkEqual(orig, decoded, 130 "Base64 array encoding/decoding failed!"); 131 132 // compare to sun.misc.BASE64Encoder 133 byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII"); 134 checkEqual(normalize(encoded), 135 normalize(encoded2), 136 "Base64 enc.encode() does not match sun.misc.base64!"); 137 138 // remove padding '=' to test non-padding decoding case 139 if (encoded[encoded.length -2] == '=') 140 encoded2 = Arrays.copyOf(encoded, encoded.length -2); 141 else if (encoded[encoded.length -1] == '=') 142 encoded2 = Arrays.copyOf(encoded, encoded.length -1); 143 else 144 encoded2 = null; 145 146 // --------testing encodetoString(byte[])/decode(String)-------- 147 String str = enc.encodeToString(orig); 148 if (!Arrays.equals(str.getBytes("ASCII"), encoded)) { 149 throw new RuntimeException( 150 "Base64 encodingToString() failed!"); 151 } 152 byte[] buf = dec.decode(new String(encoded, "ASCII")); 153 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 154 155 if (encoded2 != null) { 156 buf = dec.decode(new String(encoded2, "ASCII")); 157 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 158 } 159 160 //-------- testing encode/decode(Buffer)-------- 161 testEncode(enc, ByteBuffer.wrap(orig), encoded); 162 ByteBuffer bin = ByteBuffer.allocateDirect(orig.length); 163 bin.put(orig).flip(); 164 testEncode(enc, bin, encoded); 165 166 testDecode(dec, ByteBuffer.wrap(encoded), orig); 167 bin = ByteBuffer.allocateDirect(encoded.length); 168 bin.put(encoded).flip(); 169 testDecode(dec, bin, orig); 170 171 if (encoded2 != null) 172 testDecode(dec, ByteBuffer.wrap(encoded2), orig); 173 174 // -------- testing encode(Buffer, Buffer)-------- 175 testEncode(enc, encoded, 176 ByteBuffer.wrap(orig), 177 ByteBuffer.allocate(encoded.length + 10)); 178 179 testEncode(enc, encoded, 180 ByteBuffer.wrap(orig), 181 ByteBuffer.allocateDirect(encoded.length + 10)); 182 183 // --------testing decode(Buffer, Buffer);-------- 184 testDecode(dec, orig, 185 ByteBuffer.wrap(encoded), 186 ByteBuffer.allocate(orig.length + 10)); 187 188 testDecode(dec, orig, 189 ByteBuffer.wrap(encoded), 190 ByteBuffer.allocateDirect(orig.length + 10)); 191 192 // --------testing decode.wrap(input stream)-------- 193 // 1) random buf length 194 ByteArrayInputStream bais = new ByteArrayInputStream(encoded); 195 InputStream is = dec.wrap(bais); 196 buf = new byte[orig.length + 10]; 197 int len = orig.length; 198 int off = 0; 199 while (true) { 200 int n = rnd.nextInt(len); 201 if (n == 0) 202 n = 1; 203 n = is.read(buf, off, n); 204 if (n == -1) { 205 checkEqual(off, orig.length, 206 "Base64 stream decoding failed"); 207 break; 208 } 209 off += n; 210 len -= n; 211 if (len == 0) 212 break; 213 } 214 buf = Arrays.copyOf(buf, off); 215 checkEqual(buf, orig, "Base64 stream decoding failed!"); 216 217 // 2) read one byte each 218 bais.reset(); 219 is = dec.wrap(bais); 220 buf = new byte[orig.length + 10]; 221 off = 0; 222 int b; 223 while ((b = is.read()) != -1) { 224 buf[off++] = (byte)b; 225 } 226 buf = Arrays.copyOf(buf, off); 227 checkEqual(buf, orig, "Base64 stream decoding failed!"); 228 229 // --------testing encode.wrap(output stream)-------- 230 ByteArrayOutputStream baos = new ByteArrayOutputStream((orig.length + 2) / 3 * 4 + 10); 231 OutputStream os = enc.wrap(baos); 232 off = 0; 233 len = orig.length; 234 for (int k = 0; k < 5; k++) { 235 if (len == 0) 236 break; 237 int n = rnd.nextInt(len); 238 if (n == 0) 239 n = 1; 240 os.write(orig, off, n); 241 off += n; 242 len -= n; 243 } 244 if (len != 0) 245 os.write(orig, off, len); 246 os.close(); 247 buf = baos.toByteArray(); 248 checkEqual(buf, encoded, "Base64 stream encoding failed!"); 249 250 // 2) write one byte each 251 baos.reset(); 252 os = enc.wrap(baos); 253 off = 0; 254 while (off < orig.length) { 255 os.write(orig[off++]); 256 } 257 os.close(); 258 buf = baos.toByteArray(); 259 checkEqual(buf, encoded, "Base64 stream encoding failed!"); 260 261 // --------testing encode(in, out); -> bigger buf-------- 262 buf = new byte[encoded.length + rnd.nextInt(100)]; 263 int ret = enc.encode(orig, buf); 264 checkEqual(ret, encoded.length, 265 "Base64 enc.encode(src, null) returns wrong size!"); 266 buf = Arrays.copyOf(buf, ret); 267 checkEqual(buf, encoded, 268 "Base64 enc.encode(src, dst) failed!"); 269 270 // --------testing decode(in, out); -> bigger buf-------- 271 buf = new byte[orig.length + rnd.nextInt(100)]; 272 ret = dec.decode(encoded, buf); 273 checkEqual(ret, orig.length, 274 "Base64 enc.encode(src, null) returns wrong size!"); 275 buf = Arrays.copyOf(buf, ret); 276 checkEqual(buf, orig, 277 "Base64 dec.decode(src, dst) failed!"); 278 279 } 280 } 281 } 282 283 private static final byte[] ba_null = null; 284 private static final String str_null = null; 285 private static final ByteBuffer bb_null = null; 286 287 private static void testNull(final Base64.Encoder enc) { 288 checkNull(new Runnable() { public void run() { enc.encode(ba_null); }}); 289 checkNull(new Runnable() { public void run() { enc.encodeToString(ba_null); }}); 290 checkNull(new Runnable() { public void run() { enc.encode(ba_null, new byte[10]); }}); 291 checkNull(new Runnable() { public void run() { enc.encode(new byte[10], ba_null); }}); 292 checkNull(new Runnable() { public void run() { enc.encode(bb_null); }}); 293 checkNull(new Runnable() { public void run() { enc.encode(bb_null, ByteBuffer.allocate(10), 0); }}); 294 checkNull(new Runnable() { public void run() { enc.encode(ByteBuffer.allocate(10), bb_null, 0); }}); 295 } 296 297 private static void testNull(final Base64.Decoder dec) { 298 checkNull(new Runnable() { public void run() { dec.decode(ba_null); }}); 299 checkNull(new Runnable() { public void run() { dec.decode(str_null); }}); 300 checkNull(new Runnable() { public void run() { dec.decode(ba_null, new byte[10]); }}); 301 checkNull(new Runnable() { public void run() { dec.decode(new byte[10], ba_null); }}); 302 checkNull(new Runnable() { public void run() { dec.decode(bb_null); }}); 303 checkNull(new Runnable() { public void run() { dec.decode(bb_null, ByteBuffer.allocate(10)); }}); 304 checkNull(new Runnable() { public void run() { dec.decode(ByteBuffer.allocate(10), bb_null); }}); 305 } 306 307 private static interface Testable { 308 public void test() throws Throwable; 309 } 310 311 private static void testIOE(final Base64.Encoder enc) throws Throwable { 312 ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); 313 final OutputStream os = enc.wrap(baos); 314 os.write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9}); 315 os.close(); 316 checkIOE(new Testable() { public void test() throws Throwable { os.write(10); }}); 317 checkIOE(new Testable() { public void test() throws Throwable { os.write(new byte[] {10}); }}); 318 checkIOE(new Testable() { public void test() throws Throwable { os.write(new byte[] {10}, 1, 4); }}); 319 } 320 321 private static void testIOE(final Base64.Decoder dec, byte[] decoded) throws Throwable { 322 ByteArrayInputStream bais = new ByteArrayInputStream(decoded); 323 final InputStream is = dec.wrap(bais); 324 is.read(new byte[10]); 325 is.close(); 326 checkIOE(new Testable() { public void test() throws Throwable { is.read(); }}); 327 checkIOE(new Testable() { public void test() throws Throwable { is.read(new byte[] {10}); }}); 328 checkIOE(new Testable() { public void test() throws Throwable { is.read(new byte[] {10}, 1, 4); }}); 329 checkIOE(new Testable() { public void test() throws Throwable { is.available(); }}); 330 checkIOE(new Testable() { public void test() throws Throwable { is.skip(20); }}); 331 } 332 333 private static final void checkNull(Runnable r) { 334 try { 335 r.run(); 336 throw new RuntimeException("NPE is not thrown as expected"); 337 } catch (NullPointerException npe) {} 338 } 339 340 private static final void checkIOE(Testable t) throws Throwable { 341 try { 342 t.test(); 343 throw new RuntimeException("IOE is not thrown as expected"); 344 } catch (IOException ioe) {} 345 } 346 347 private static final void checkIAE(Runnable r) throws Throwable { 348 try { 349 r.run(); 350 throw new RuntimeException("IAE is not thrown as expected"); 351 } catch (IllegalArgumentException iae) {} 352 } 353 354 private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected) 355 throws Throwable { 356 357 ByteBuffer bout = enc.encode(bin); 358 byte[] buf = new byte[bout.remaining()]; 359 bout.get(buf); 360 if (bin.hasRemaining()) { 361 throw new RuntimeException( 362 "Base64 enc.encode(ByteBuffer) failed!"); 363 } 364 checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); 365 } 366 367 private static final void testDecode(Base64.Decoder dec, ByteBuffer bin, byte[] expected) 368 throws Throwable { 369 370 ByteBuffer bout = dec.decode(bin); 371 byte[] buf = new byte[bout.remaining()]; 372 bout.get(buf); 373 checkEqual(buf, expected, "Base64 dec.decode(bf) failed!"); 374 } 375 376 private static final void testEncode(Base64.Encoder enc, byte[] expected, 377 ByteBuffer ibb, ByteBuffer obb) 378 throws Throwable { 379 Random rnd = new Random(); 380 int bytesOut = enc.encode(ibb, obb, 0); 381 if (ibb.hasRemaining()) { 382 throw new RuntimeException( 383 "Base64 enc.encode(bf, bf) failed with wrong return!"); 384 } 385 obb.flip(); 386 byte[] buf = new byte[obb.remaining()]; 387 obb.get(buf); 388 checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); 389 ibb.rewind(); 390 obb.position(0); 391 obb.limit(0); 392 bytesOut = 0; 393 394 do { // increase the "limit" incrementally & randomly 395 int n = rnd.nextInt(expected.length - obb.position()); 396 if (n == 0) 397 n = 1; 398 obb.limit(obb.limit() + n); 399 //obb.limit(Math.min(obb.limit() + n, expected.length)); 400 bytesOut = enc.encode(ibb, obb, bytesOut); 401 } while (ibb.hasRemaining()); 402 obb.flip(); 403 buf = new byte[obb.remaining()]; 404 obb.get(buf); 405 checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); 406 } 407 408 private static final void testDecode(Base64.Decoder dec, byte[] expected, 409 ByteBuffer ibb, ByteBuffer obb) 410 throws Throwable { 411 Random rnd = new Random(); 412 413 dec.decode(ibb, obb); 414 if (ibb.hasRemaining()) { 415 throw new RuntimeException( 416 "Base64 dec.decode(bf, bf) failed with un-decoded ibb!"); 417 } 418 obb.flip(); 419 byte[] buf = new byte[obb.remaining()]; 420 obb.get(buf); 421 checkEqual(buf, expected, "Base64 dec.decode(bf, bf) failed!"); 422 423 ibb.rewind(); 424 obb.position(0); 425 obb.limit(0); 426 do { // increase the "limit" incrementally & randomly 427 int n = rnd.nextInt(expected.length - obb.position()); 428 if (n == 0) 429 n = 1; 430 obb.limit(obb.limit() + n); 431 dec.decode(ibb, obb); 432 } while (ibb.hasRemaining()); 433 434 435 obb.flip(); 436 buf = new byte[obb.remaining()]; 437 obb.get(buf); 438 checkEqual(buf, expected, "Base64 dec.decode(bf, bf) failed!"); 439 } 440 441 private static final void checkEqual(int v1, int v2, String msg) 442 throws Throwable { 443 if (v1 != v2) { 444 System.out.printf(" v1=%d%n", v1); 445 System.out.printf(" v2=%d%n", v2); 446 throw new RuntimeException(msg); 447 } 448 } 449 450 private static final void checkEqual(byte[] r1, byte[] r2, String msg) 451 throws Throwable { 452 if (!Arrays.equals(r1, r2)) { 453 System.out.printf(" r1[%d]=[%s]%n", r1.length, new String(r1)); 454 System.out.printf(" r2[%d]=[%s]%n", r2.length, new String(r2)); 455 throw new RuntimeException(msg); 456 } 457 } 458 459 // remove line feeds, 460 private static final byte[] normalize(byte[] src) { 461 int n = 0; 462 boolean hasUrl = false; 463 for (int i = 0; i < src.length; i++) { 464 if (src[i] == '\r' || src[i] == '\n') 465 n++; 466 if (src[i] == '-' || src[i] == '_') 467 hasUrl = true; 468 } 469 if (n == 0 && hasUrl == false) 470 return src; 471 byte[] ret = new byte[src.length - n]; 472 int j = 0; 473 for (int i = 0; i < src.length; i++) { 474 if (src[i] == '-') 475 ret[j++] = '+'; 476 else if (src[i] == '_') 477 ret[j++] = '/'; 478 else if (src[i] != '\r' && src[i] != '\n') 479 ret[j++] = src[i]; 480 } 481 return ret; 482 } 483 }