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 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925 26 * 8014217 8025003 27 * @summary tests java.util.Base64 28 */ 29 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.InputStream; 33 import java.io.IOException; 34 import java.io.OutputStream; 35 import java.nio.ByteBuffer; 36 import java.util.Arrays; 37 import java.util.Base64; 38 import java.util.Random; 39 40 public class TestBase64 { 41 42 public static void main(String args[]) throws Throwable { 43 int numRuns = 10; 44 int numBytes = 200; 45 if (args.length > 1) { 46 numRuns = Integer.parseInt(args[0]); 47 numBytes = Integer.parseInt(args[1]); 48 } 49 50 test(Base64.getEncoder(), Base64.getDecoder(), 51 numRuns, numBytes); 52 test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), 53 numRuns, numBytes); 54 test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), 55 numRuns, numBytes); 56 57 Random rnd = new java.util.Random(); 58 byte[] nl_1 = new byte[] {'\n'}; 59 byte[] nl_2 = new byte[] {'\n', '\r'}; 60 byte[] nl_3 = new byte[] {'\n', '\r', '\n'}; 61 for (int i = 0; i < 10; i++) { 62 int len = rnd.nextInt(200) + 4; 63 test(Base64.getMimeEncoder(len, nl_1), 64 Base64.getMimeDecoder(), 65 numRuns, numBytes); 66 test(Base64.getMimeEncoder(len, nl_2), 67 Base64.getMimeDecoder(), 68 numRuns, numBytes); 69 test(Base64.getMimeEncoder(len, nl_3), 70 Base64.getMimeDecoder(), 71 numRuns, numBytes); 72 } 73 74 testNull(Base64.getEncoder()); 75 testNull(Base64.getUrlEncoder()); 76 testNull(Base64.getMimeEncoder()); 77 testNull(Base64.getMimeEncoder(10, new byte[]{'\n'})); 78 testNull(Base64.getDecoder()); 79 testNull(Base64.getUrlDecoder()); 80 testNull(Base64.getMimeDecoder()); 81 checkNull(new Runnable() { public void run() { Base64.getMimeEncoder(10, null); }}); 82 83 testIOE(Base64.getEncoder()); 84 testIOE(Base64.getUrlEncoder()); 85 testIOE(Base64.getMimeEncoder()); 86 testIOE(Base64.getMimeEncoder(10, new byte[]{'\n'})); 87 88 byte[] src = new byte[1024]; 89 new Random().nextBytes(src); 90 final byte[] decoded = Base64.getEncoder().encode(src); 91 testIOE(Base64.getDecoder(), decoded); 92 testIOE(Base64.getMimeDecoder(), decoded); 93 testIOE(Base64.getUrlDecoder(), Base64.getUrlEncoder().encode(src)); 94 95 // illegal line separator 96 checkIAE(new Runnable() { public void run() { Base64.getMimeEncoder(10, new byte[]{'\r', 'N'}); }}); 97 98 // illegal base64 character 99 decoded[2] = (byte)0xe0; 100 checkIAE(new Runnable() { 101 public void run() { Base64.getDecoder().decode(decoded); }}); 102 checkIAE(new Runnable() { 103 public void run() { Base64.getDecoder().decode(decoded, new byte[1024]); }}); 104 checkIAE(new Runnable() { public void run() { 105 Base64.getDecoder().decode(ByteBuffer.wrap(decoded)); }}); 106 checkIAE(new Runnable() { public void run() { 107 Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocate(1024)); }}); 108 checkIAE(new Runnable() { public void run() { 109 Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocateDirect(1024)); }}); 110 111 // illegal ending unit 112 checkIOE(new Testable() { public void test() throws IOException { 113 byte[] bytes = "AA=".getBytes("ASCII"); 114 try (InputStream stream = 115 Base64.getDecoder().wrap(new ByteArrayInputStream(bytes))) { 116 while (stream.read() != -1); 117 } 118 }}); 119 120 // test return value from decode(ByteBuffer, ByteBuffer) 121 testDecBufRet(); 122 123 // test single-non-base64 character for mime decoding 124 testSingleNonBase64MimeDec(); 125 126 // test decoding of unpadded data 127 testDecodeUnpadded(); 128 // test mime decoding with ignored character after padding 129 testDecodeIgnoredAfterPadding(); 130 131 // lenient mode for ending unit 132 testLenientPadding(); 133 134 } 135 136 private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder(); 137 138 private static void test(Base64.Encoder enc, Base64.Decoder dec, 139 int numRuns, int numBytes) throws Throwable { 140 Random rnd = new java.util.Random(); 141 142 enc.encode(new byte[0]); 143 dec.decode(new byte[0]); 144 145 for (int i=0; i<numRuns; i++) { 146 for (int j=1; j<numBytes; j++) { 147 byte[] orig = new byte[j]; 148 rnd.nextBytes(orig); 149 150 // --------testing encode/decode(byte[])-------- 151 byte[] encoded = enc.encode(orig); 152 byte[] decoded = dec.decode(encoded); 153 154 checkEqual(orig, decoded, 155 "Base64 array encoding/decoding failed!"); 156 157 // compare to sun.misc.BASE64Encoder 158 byte[] encoded2 = sunmisc.encode(orig).getBytes("ASCII"); 159 checkEqual(normalize(encoded), 160 normalize(encoded2), 161 "Base64 enc.encode() does not match sun.misc.base64!"); 162 163 // remove padding '=' to test non-padding decoding case 164 if (encoded[encoded.length -2] == '=') 165 encoded2 = Arrays.copyOf(encoded, encoded.length -2); 166 else if (encoded[encoded.length -1] == '=') 167 encoded2 = Arrays.copyOf(encoded, encoded.length -1); 168 else 169 encoded2 = null; 170 171 // --------testing encodetoString(byte[])/decode(String)-------- 172 String str = enc.encodeToString(orig); 173 if (!Arrays.equals(str.getBytes("ASCII"), encoded)) { 174 throw new RuntimeException( 175 "Base64 encodingToString() failed!"); 176 } 177 byte[] buf = dec.decode(new String(encoded, "ASCII")); 178 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 179 180 if (encoded2 != null) { 181 buf = dec.decode(new String(encoded2, "ASCII")); 182 checkEqual(buf, orig, "Base64 decoding(String) failed!"); 183 } 184 185 //-------- testing encode/decode(Buffer)-------- 186 testEncode(enc, ByteBuffer.wrap(orig), encoded); 187 ByteBuffer bin = ByteBuffer.allocateDirect(orig.length); 188 bin.put(orig).flip(); 189 testEncode(enc, bin, encoded); 190 191 testDecode(dec, ByteBuffer.wrap(encoded), orig); 192 bin = ByteBuffer.allocateDirect(encoded.length); 193 bin.put(encoded).flip(); 194 testDecode(dec, bin, orig); 195 196 if (encoded2 != null) 197 testDecode(dec, ByteBuffer.wrap(encoded2), orig); 198 199 // -------- testing encode(Buffer, Buffer)-------- 200 testEncode(enc, encoded, 201 ByteBuffer.wrap(orig), 202 ByteBuffer.allocate(encoded.length + 10)); 203 204 testEncode(enc, encoded, 205 ByteBuffer.wrap(orig), 206 ByteBuffer.allocateDirect(encoded.length + 10)); 207 208 // --------testing decode(Buffer, Buffer);-------- 209 testDecode(dec, orig, 210 ByteBuffer.wrap(encoded), 211 ByteBuffer.allocate(orig.length + 10)); 212 213 testDecode(dec, orig, 214 ByteBuffer.wrap(encoded), 215 ByteBuffer.allocateDirect(orig.length + 10)); 216 217 // --------testing decode.wrap(input stream)-------- 218 // 1) random buf length 219 ByteArrayInputStream bais = new ByteArrayInputStream(encoded); 220 InputStream is = dec.wrap(bais); 221 buf = new byte[orig.length + 10]; 222 int len = orig.length; 223 int off = 0; 224 while (true) { 225 int n = rnd.nextInt(len); 226 if (n == 0) 227 n = 1; 228 n = is.read(buf, off, n); 229 if (n == -1) { 230 checkEqual(off, orig.length, 231 "Base64 stream decoding failed"); 232 break; 233 } 234 off += n; 235 len -= n; 236 if (len == 0) 237 break; 238 } 239 buf = Arrays.copyOf(buf, off); 240 checkEqual(buf, orig, "Base64 stream decoding failed!"); 241 242 // 2) read one byte each 243 bais.reset(); 244 is = dec.wrap(bais); 245 buf = new byte[orig.length + 10]; 246 off = 0; 247 int b; 248 while ((b = is.read()) != -1) { 249 buf[off++] = (byte)b; 250 } 251 buf = Arrays.copyOf(buf, off); 252 checkEqual(buf, orig, "Base64 stream decoding failed!"); 253 254 // --------testing encode.wrap(output stream)-------- 255 ByteArrayOutputStream baos = new ByteArrayOutputStream((orig.length + 2) / 3 * 4 + 10); 256 OutputStream os = enc.wrap(baos); 257 off = 0; 258 len = orig.length; 259 for (int k = 0; k < 5; k++) { 260 if (len == 0) 261 break; 262 int n = rnd.nextInt(len); 263 if (n == 0) 264 n = 1; 265 os.write(orig, off, n); 266 off += n; 267 len -= n; 268 } 269 if (len != 0) 270 os.write(orig, off, len); 271 os.close(); 272 buf = baos.toByteArray(); 273 checkEqual(buf, encoded, "Base64 stream encoding failed!"); 274 275 // 2) write one byte each 276 baos.reset(); 277 os = enc.wrap(baos); 278 off = 0; 279 while (off < orig.length) { 280 os.write(orig[off++]); 281 } 282 os.close(); 283 buf = baos.toByteArray(); 284 checkEqual(buf, encoded, "Base64 stream encoding failed!"); 285 286 // --------testing encode(in, out); -> bigger buf-------- 287 buf = new byte[encoded.length + rnd.nextInt(100)]; 288 int ret = enc.encode(orig, buf); 289 checkEqual(ret, encoded.length, 290 "Base64 enc.encode(src, null) returns wrong size!"); 291 buf = Arrays.copyOf(buf, ret); 292 checkEqual(buf, encoded, 293 "Base64 enc.encode(src, dst) failed!"); 294 295 // --------testing decode(in, out); -> bigger buf-------- 296 buf = new byte[orig.length + rnd.nextInt(100)]; 297 ret = dec.decode(encoded, buf); 298 checkEqual(ret, orig.length, 299 "Base64 enc.encode(src, null) returns wrong size!"); 300 buf = Arrays.copyOf(buf, ret); 301 checkEqual(buf, orig, 302 "Base64 dec.decode(src, dst) failed!"); 303 304 } 305 } 306 } 307 308 private static final byte[] ba_null = null; 309 private static final String str_null = null; 310 private static final ByteBuffer bb_null = null; 311 312 private static void testNull(final Base64.Encoder enc) { 313 checkNull(new Runnable() { public void run() { enc.encode(ba_null); }}); 314 checkNull(new Runnable() { public void run() { enc.encodeToString(ba_null); }}); 315 checkNull(new Runnable() { public void run() { enc.encode(ba_null, new byte[10]); }}); 316 checkNull(new Runnable() { public void run() { enc.encode(new byte[10], ba_null); }}); 317 checkNull(new Runnable() { public void run() { enc.encode(bb_null); }}); 318 checkNull(new Runnable() { public void run() { enc.encode(bb_null, ByteBuffer.allocate(10), 0); }}); 319 checkNull(new Runnable() { public void run() { enc.encode(ByteBuffer.allocate(10), bb_null, 0); }}); 320 checkNull(new Runnable() { public void run() { enc.wrap(null); }}); 321 } 322 323 private static void testNull(final Base64.Decoder dec) { 324 checkNull(new Runnable() { public void run() { dec.decode(ba_null); }}); 325 checkNull(new Runnable() { public void run() { dec.decode(str_null); }}); 326 checkNull(new Runnable() { public void run() { dec.decode(ba_null, new byte[10]); }}); 327 checkNull(new Runnable() { public void run() { dec.decode(new byte[10], ba_null); }}); 328 checkNull(new Runnable() { public void run() { dec.decode(bb_null); }}); 329 checkNull(new Runnable() { public void run() { dec.decode(bb_null, ByteBuffer.allocate(10)); }}); 330 checkNull(new Runnable() { public void run() { dec.decode(ByteBuffer.allocate(10), bb_null); }}); 331 checkNull(new Runnable() { public void run() { dec.wrap(null); }}); 332 } 333 334 private static interface Testable { 335 public void test() throws Throwable; 336 } 337 338 private static void testIOE(final Base64.Encoder enc) throws Throwable { 339 ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); 340 final OutputStream os = enc.wrap(baos); 341 os.write(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9}); 342 os.close(); 343 checkIOE(new Testable() { public void test() throws Throwable { os.write(10); }}); 344 checkIOE(new Testable() { public void test() throws Throwable { os.write(new byte[] {10}); }}); 345 checkIOE(new Testable() { public void test() throws Throwable { os.write(new byte[] {10}, 1, 4); }}); 346 } 347 348 private static void testIOE(final Base64.Decoder dec, byte[] decoded) throws Throwable { 349 ByteArrayInputStream bais = new ByteArrayInputStream(decoded); 350 final InputStream is = dec.wrap(bais); 351 is.read(new byte[10]); 352 is.close(); 353 checkIOE(new Testable() { public void test() throws Throwable { is.read(); }}); 354 checkIOE(new Testable() { public void test() throws Throwable { is.read(new byte[] {10}); }}); 355 checkIOE(new Testable() { public void test() throws Throwable { is.read(new byte[] {10}, 1, 4); }}); 356 checkIOE(new Testable() { public void test() throws Throwable { is.available(); }}); 357 checkIOE(new Testable() { public void test() throws Throwable { is.skip(20); }}); 358 } 359 360 private static final void checkNull(Runnable r) { 361 try { 362 r.run(); 363 throw new RuntimeException("NPE is not thrown as expected"); 364 } catch (NullPointerException npe) {} 365 } 366 367 private static final void checkIOE(Testable t) throws Throwable { 368 try { 369 t.test(); 370 throw new RuntimeException("IOE is not thrown as expected"); 371 } catch (IOException ioe) {} 372 } 373 374 private static final void checkIAE(Runnable r) throws Throwable { 375 try { 376 r.run(); 377 throw new RuntimeException("IAE is not thrown as expected"); 378 } catch (IllegalArgumentException iae) {} 379 } 380 381 private static void testDecodeIgnoredAfterPadding() throws Throwable { 382 for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) { 383 byte[][] src = new byte[][] { 384 "A".getBytes("ascii"), 385 "AB".getBytes("ascii"), 386 "ABC".getBytes("ascii"), 387 "ABCD".getBytes("ascii"), 388 "ABCDE".getBytes("ascii") 389 }; 390 Base64.Encoder encM = Base64.getMimeEncoder(); 391 Base64.Decoder decM = Base64.getMimeDecoder(); 392 Base64.Encoder enc = Base64.getEncoder(); 393 Base64.Decoder dec = Base64.getDecoder(); 394 for (int i = 0; i < src.length; i++) { 395 // decode(byte[]) 396 byte[] encoded = encM.encode(src[i]); 397 encoded = Arrays.copyOf(encoded, encoded.length + 1); 398 encoded[encoded.length - 1] = nonBase64; 399 checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored"); 400 byte[] decoded = new byte[src[i].length]; 401 decM.decode(encoded, decoded); 402 checkEqual(decoded, src[i], "Non-base64 char is not ignored"); 403 404 try { 405 dec.decode(encoded); 406 throw new RuntimeException("No IAE for non-base64 char"); 407 } catch (IllegalArgumentException iae) {} 408 409 // decode(ByteBuffer[], ByteBuffer[]) 410 ByteBuffer encodedBB = ByteBuffer.wrap(encoded); 411 ByteBuffer decodedBB = ByteBuffer.allocate(100); 412 int ret = decM.decode(encodedBB, decodedBB); 413 byte[] buf = new byte[ret]; 414 decodedBB.flip(); 415 decodedBB.get(buf); 416 checkEqual(buf, src[i], "Non-base64 char is not ignored"); 417 try { 418 encodedBB.rewind(); 419 decodedBB.clear(); 420 dec.decode(encodedBB, decodedBB); 421 throw new RuntimeException("No IAE for non-base64 char"); 422 } catch (IllegalArgumentException iae) {} 423 // direct 424 encodedBB.rewind(); 425 decodedBB = ByteBuffer.allocateDirect(100); 426 ret = decM.decode(encodedBB, decodedBB); 427 buf = new byte[ret]; 428 decodedBB.flip(); 429 decodedBB.get(buf); 430 checkEqual(buf, src[i], "Non-base64 char is not ignored"); 431 try { 432 encodedBB.rewind(); 433 decodedBB.clear(); 434 dec.decode(encodedBB, decodedBB); 435 throw new RuntimeException("No IAE for non-base64 char"); 436 } catch (IllegalArgumentException iae) {} 437 } 438 } 439 } 440 441 private static void testLenientPadding() throws Throwable { 442 String[] data = new String[] { 443 "=", "", // unnecessary padding 444 "QUJD=", "ABC", //"ABC".encode() -> "QUJD" 445 446 "QQ=", "A", // incomplete padding 447 "QQ=N", "A", // incorrect padding 448 "QQ=?", "A", 449 "QUJDQQ=", "ABCA", 450 "QUJDQQ=N", "ABCA", 451 "QUJDQQ=?", "ABCA", 452 453 "QUI=X", "AB", // incorrect padding 454 "QUI=?", "AB", // incorrect padding 455 }; 456 Base64.Decoder dec = Base64.getMimeDecoder(); 457 458 for (int i = 0; i < data.length; i += 2) { 459 byte[] src = data[i].getBytes("ASCII"); 460 byte[] expected = data[i + 1].getBytes("ASCII"); 461 // decode(byte[]) 462 byte[] ret = dec.decode(src); 463 checkEqual(ret, expected, "lenient padding decoding failed!"); 464 465 // decode(String) 466 ret = dec.decode(data[i]); 467 checkEqual(ret, expected, "lenient padding decoding failed!"); 468 469 // decode(ByteBuffer) 470 ByteBuffer srcBB = ByteBuffer.wrap(src); 471 ByteBuffer retBB = dec.decode(srcBB); 472 checkEqual(srcBB.remaining(), 0, "lenient padding decoding failed!"); 473 checkEqual(Arrays.copyOf(retBB.array(), retBB.remaining()), 474 expected, "lenient padding decoding failed!"); 475 476 // wrap.decode(byte[]) 477 ret = new byte[10]; 478 int n = dec.wrap(new ByteArrayInputStream(src)).read(ret); 479 checkEqual(Arrays.copyOf(ret, n), expected, "lenient padding decoding failed!"); 480 } 481 } 482 483 private static void testDecodeUnpadded() throws Throwable { 484 byte[] srcA = new byte[] { 'Q', 'Q' }; 485 byte[] srcAA = new byte[] { 'Q', 'Q', 'E'}; 486 Base64.Decoder dec = Base64.getDecoder(); 487 byte[] ret = dec.decode(srcA); 488 if (ret[0] != 'A') 489 throw new RuntimeException("Decoding unpadding input A failed"); 490 ret = dec.decode(srcAA); 491 if (ret[0] != 'A' && ret[1] != 'A') 492 throw new RuntimeException("Decoding unpadding input AA failed"); 493 ret = new byte[10]; 494 if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 && 495 ret[0] != 'A') 496 throw new RuntimeException("Decoding unpadding input A from stream failed"); 497 if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 && 498 ret[0] != 'A' && ret[1] != 'A') 499 throw new RuntimeException("Decoding unpadding input AA from stream failed"); 500 } 501 502 // single-non-base64-char should be ignored for mime decoding, but 503 // iae for basic decoding 504 private static void testSingleNonBase64MimeDec() throws Throwable { 505 for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) { 506 if (Base64.getMimeDecoder().decode(nonBase64).length != 0) { 507 throw new RuntimeException("non-base64 char is not ignored"); 508 } 509 try { 510 Base64.getDecoder().decode(nonBase64); 511 throw new RuntimeException("No IAE for single non-base64 char"); 512 } catch (IllegalArgumentException iae) {} 513 } 514 } 515 516 private static void testDecBufRet() throws Throwable { 517 Random rnd = new java.util.Random(); 518 Base64.Encoder encoder = Base64.getEncoder(); 519 Base64.Decoder decoder = Base64.getDecoder(); 520 // src pos, len expected 521 int[][] tests = { { 6, 3, 3, 3}, // xxx xxx -> yyyy yyyy 522 { 6, 3, 4, 3}, 523 { 6, 3, 5, 3}, 524 { 6, 3, 6, 6}, 525 { 6, 11, 4, 3}, 526 { 6, 11, 4, 3}, 527 { 6, 11, 5, 3}, 528 { 6, 11, 6, 6}, 529 { 7, 3, 6, 6}, // xxx xxx x -> yyyy yyyy yy== 530 { 7, 3, 7, 7}, 531 { 7, 11, 6, 6}, 532 { 7, 11, 7, 7}, 533 { 8, 3, 6, 6}, // xxx xxx xx -> yyyy yyyy yyy= 534 { 8, 3, 7, 6}, 535 { 8, 3, 8, 8}, 536 { 8, 13, 6, 6}, 537 { 8, 13, 7, 6}, 538 { 8, 13, 8, 8}, 539 540 }; 541 ByteBuffer dstBuf = ByteBuffer.allocate(100); 542 for (boolean direct : new boolean[] { false, true}) { 543 for (int[] test : tests) { 544 byte[] src = new byte[test[0]]; 545 rnd.nextBytes(src); 546 ByteBuffer srcBuf = direct ? ByteBuffer.allocate(100) 547 : ByteBuffer.allocateDirect(100); 548 srcBuf.put(encoder.encode(src)).flip(); 549 dstBuf.clear().position(test[1]).limit(test[1]+ test[2]); 550 int ret = decoder.decode(srcBuf, dstBuf); 551 if (ret != test[3]) { 552 System.out.printf(" [%6s] src=%d, pos=%d, len=%d, expected=%d, ret=%d%n", 553 direct?"direct":"", 554 test[0], test[1], test[2], test[3], ret); 555 throw new RuntimeException("ret != expected"); 556 } 557 } 558 } 559 } 560 561 private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected) 562 throws Throwable { 563 564 ByteBuffer bout = enc.encode(bin); 565 byte[] buf = new byte[bout.remaining()]; 566 bout.get(buf); 567 if (bin.hasRemaining()) { 568 throw new RuntimeException( 569 "Base64 enc.encode(ByteBuffer) failed!"); 570 } 571 checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); 572 } 573 574 private static final void testDecode(Base64.Decoder dec, ByteBuffer bin, byte[] expected) 575 throws Throwable { 576 577 ByteBuffer bout = dec.decode(bin); 578 byte[] buf = new byte[bout.remaining()]; 579 bout.get(buf); 580 checkEqual(buf, expected, "Base64 dec.decode(bf) failed!"); 581 } 582 583 private static final void testEncode(Base64.Encoder enc, byte[] expected, 584 ByteBuffer ibb, ByteBuffer obb) 585 throws Throwable { 586 Random rnd = new Random(); 587 int bytesOut = enc.encode(ibb, obb, 0); 588 if (ibb.hasRemaining()) { 589 throw new RuntimeException( 590 "Base64 enc.encode(bf, bf) failed with wrong return!"); 591 } 592 obb.flip(); 593 byte[] buf = new byte[obb.remaining()]; 594 obb.get(buf); 595 checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); 596 ibb.rewind(); 597 obb.position(0); 598 obb.limit(0); 599 bytesOut = 0; 600 601 do { // increase the "limit" incrementally & randomly 602 int n = rnd.nextInt(expected.length - obb.position()); 603 if (n == 0) 604 n = 1; 605 obb.limit(obb.limit() + n); 606 //obb.limit(Math.min(obb.limit() + n, expected.length)); 607 bytesOut = enc.encode(ibb, obb, bytesOut); 608 } while (ibb.hasRemaining()); 609 obb.flip(); 610 buf = new byte[obb.remaining()]; 611 obb.get(buf); 612 checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); 613 } 614 615 private static final void testDecode(Base64.Decoder dec, byte[] expected, 616 ByteBuffer ibb, ByteBuffer obb) 617 throws Throwable { 618 Random rnd = new Random(); 619 620 dec.decode(ibb, obb); 621 if (ibb.hasRemaining()) { 622 throw new RuntimeException( 623 "Base64 dec.decode(bf, bf) failed with un-decoded ibb!"); 624 } 625 obb.flip(); 626 byte[] buf = new byte[obb.remaining()]; 627 obb.get(buf); 628 checkEqual(buf, expected, "Base64 dec.decode(bf, bf) failed!"); 629 630 ibb.rewind(); 631 obb.position(0); 632 obb.limit(0); 633 do { // increase the "limit" incrementally & randomly 634 int n = rnd.nextInt(expected.length - obb.position()); 635 if (n == 0) 636 n = 1; 637 obb.limit(obb.limit() + n); 638 dec.decode(ibb, obb); 639 } while (ibb.hasRemaining()); 640 641 642 obb.flip(); 643 buf = new byte[obb.remaining()]; 644 obb.get(buf); 645 checkEqual(buf, expected, "Base64 dec.decode(bf, bf) failed!"); 646 } 647 648 private static final void checkEqual(int v1, int v2, String msg) 649 throws Throwable { 650 if (v1 != v2) { 651 System.out.printf(" v1=%d%n", v1); 652 System.out.printf(" v2=%d%n", v2); 653 throw new RuntimeException(msg); 654 } 655 } 656 657 private static final void checkEqual(byte[] r1, byte[] r2, String msg) 658 throws Throwable { 659 if (!Arrays.equals(r1, r2)) { 660 System.out.printf(" r1[%d]=[%s]%n", r1.length, new String(r1)); 661 System.out.printf(" r2[%d]=[%s]%n", r2.length, new String(r2)); 662 throw new RuntimeException(msg); 663 } 664 } 665 666 // remove line feeds, 667 private static final byte[] normalize(byte[] src) { 668 int n = 0; 669 boolean hasUrl = false; 670 for (int i = 0; i < src.length; i++) { 671 if (src[i] == '\r' || src[i] == '\n') 672 n++; 673 if (src[i] == '-' || src[i] == '_') 674 hasUrl = true; 675 } 676 if (n == 0 && hasUrl == false) 677 return src; 678 byte[] ret = new byte[src.length - n]; 679 int j = 0; 680 for (int i = 0; i < src.length; i++) { 681 if (src[i] == '-') 682 ret[j++] = '+'; 683 else if (src[i] == '_') 684 ret[j++] = '/'; 685 else if (src[i] != '\r' && src[i] != '\n') 686 ret[j++] = src[i]; 687 } 688 return ret; 689 } 690 }