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
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]);
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 // test return value from decode(ByteBuffer, ByteBuffer)
111 testDecBufRet();
112
113 // test single-non-base64 character for mime decoding
114 testSingleNonBase64MimeDec();
115 }
116
117 private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
118
119 private static void test(Base64.Encoder enc, Base64.Decoder dec,
120 int numRuns, int numBytes) throws Throwable {
121 Random rnd = new java.util.Random();
122
123 enc.encode(new byte[0]);
124 dec.decode(new byte[0]);
125
126 for (int i=0; i<numRuns; i++) {
127 for (int j=1; j<numBytes; j++) {
128 byte[] orig = new byte[j];
129 rnd.nextBytes(orig);
130
131 // --------testing encode/decode(byte[])--------
132 byte[] encoded = enc.encode(orig);
133 byte[] decoded = dec.decode(encoded);
134
342 try {
343 r.run();
344 throw new RuntimeException("NPE is not thrown as expected");
345 } catch (NullPointerException npe) {}
346 }
347
348 private static final void checkIOE(Testable t) throws Throwable {
349 try {
350 t.test();
351 throw new RuntimeException("IOE is not thrown as expected");
352 } catch (IOException ioe) {}
353 }
354
355 private static final void checkIAE(Runnable r) throws Throwable {
356 try {
357 r.run();
358 throw new RuntimeException("IAE is not thrown as expected");
359 } catch (IllegalArgumentException iae) {}
360 }
361
362 // single-non-base64-char should be ignored for mime decoding, but
363 // iae for basic decoding
364 private static void testSingleNonBase64MimeDec() throws Throwable {
365 for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
366 if (Base64.getMimeDecoder().decode(nonBase64).length != 0) {
367 throw new RuntimeException("non-base64 char is not ignored");
368 }
369 try {
370 Base64.getDecoder().decode(nonBase64);
371 throw new RuntimeException("No IAE for single non-base64 char");
372 } catch (IllegalArgumentException iae) {}
373 }
374 }
375
376 private static void testDecBufRet() throws Throwable {
377 Random rnd = new java.util.Random();
378 Base64.Encoder encoder = Base64.getEncoder();
379 Base64.Decoder decoder = Base64.getDecoder();
380 // src pos, len expected
381 int[][] tests = { { 6, 3, 3, 3}, // xxx xxx -> yyyy yyyy
|
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 8006315 8006530
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]);
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 // test return value from decode(ByteBuffer, ByteBuffer)
111 testDecBufRet();
112
113 // test single-non-base64 character for mime decoding
114 testSingleNonBase64MimeDec();
115
116 // test decoding of unpadded data
117 testDecodeUnpadded();
118
119 // test mime decoding with ignored character after padding
120 testDecodeIgnoredAfterPadding();
121 }
122
123 private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
124
125 private static void test(Base64.Encoder enc, Base64.Decoder dec,
126 int numRuns, int numBytes) throws Throwable {
127 Random rnd = new java.util.Random();
128
129 enc.encode(new byte[0]);
130 dec.decode(new byte[0]);
131
132 for (int i=0; i<numRuns; i++) {
133 for (int j=1; j<numBytes; j++) {
134 byte[] orig = new byte[j];
135 rnd.nextBytes(orig);
136
137 // --------testing encode/decode(byte[])--------
138 byte[] encoded = enc.encode(orig);
139 byte[] decoded = dec.decode(encoded);
140
348 try {
349 r.run();
350 throw new RuntimeException("NPE is not thrown as expected");
351 } catch (NullPointerException npe) {}
352 }
353
354 private static final void checkIOE(Testable t) throws Throwable {
355 try {
356 t.test();
357 throw new RuntimeException("IOE is not thrown as expected");
358 } catch (IOException ioe) {}
359 }
360
361 private static final void checkIAE(Runnable r) throws Throwable {
362 try {
363 r.run();
364 throw new RuntimeException("IAE is not thrown as expected");
365 } catch (IllegalArgumentException iae) {}
366 }
367
368 private static void testDecodeIgnoredAfterPadding() throws Throwable {
369 for (byte nonBase64 : new byte[] {'#', '(', '!', '\\', '-', '_', '\n', '\r'}) {
370 byte[][] src = new byte[][] {
371 "A".getBytes("ascii"),
372 "AB".getBytes("ascii"),
373 "ABC".getBytes("ascii"),
374 "ABCD".getBytes("ascii"),
375 "ABCDE".getBytes("ascii")
376 };
377 Base64.Encoder encM = Base64.getMimeEncoder();
378 Base64.Decoder decM = Base64.getMimeDecoder();
379 Base64.Encoder enc = Base64.getEncoder();
380 Base64.Decoder dec = Base64.getDecoder();
381 for (int i = 0; i < src.length; i++) {
382 // decode(byte[])
383 byte[] encoded = encM.encode(src[i]);
384 encoded = Arrays.copyOf(encoded, encoded.length + 1);
385 encoded[encoded.length - 1] = nonBase64;
386 checkEqual(decM.decode(encoded), src[i], "Non-base64 char is not ignored");
387 try {
388 dec.decode(encoded);
389 throw new RuntimeException("No IAE for non-base64 char");
390 } catch (IllegalArgumentException iae) {}
391
392 // decode(ByteBuffer[], ByteBuffer[])
393 ByteBuffer encodedBB = ByteBuffer.wrap(encoded);
394 ByteBuffer decodedBB = ByteBuffer.allocate(100);
395 int ret = decM.decode(encodedBB, decodedBB);
396 byte[] buf = new byte[ret];
397 decodedBB.flip();
398 decodedBB.get(buf);
399 checkEqual(buf, src[i], "Non-base64 char is not ignored");
400 try {
401 encodedBB.rewind();
402 decodedBB.clear();
403 dec.decode(encodedBB, decodedBB);
404 throw new RuntimeException("No IAE for non-base64 char");
405 } catch (IllegalArgumentException iae) {}
406 // direct
407 encodedBB.rewind();
408 decodedBB = ByteBuffer.allocateDirect(100);
409 ret = decM.decode(encodedBB, decodedBB);
410 buf = new byte[ret];
411 decodedBB.flip();
412 decodedBB.get(buf);
413 checkEqual(buf, src[i], "Non-base64 char is not ignored");
414 try {
415 encodedBB.rewind();
416 decodedBB.clear();
417 dec.decode(encodedBB, decodedBB);
418 throw new RuntimeException("No IAE for non-base64 char");
419 } catch (IllegalArgumentException iae) {}
420 }
421 }
422 }
423
424 private static void testDecodeUnpadded() throws Throwable {
425 byte[] srcA = new byte[] { 'Q', 'Q' };
426 byte[] srcAA = new byte[] { 'Q', 'Q', 'E'};
427 Base64.Decoder dec = Base64.getDecoder();
428 byte[] ret = dec.decode(srcA);
429 if (ret[0] != 'A')
430 throw new RuntimeException("Decoding unpadding input A failed");
431 ret = dec.decode(srcAA);
432 if (ret[0] != 'A' && ret[1] != 'A')
433 throw new RuntimeException("Decoding unpadding input AA failed");
434 ret = new byte[10];
435 if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 &&
436 ret[0] != 'A')
437 throw new RuntimeException("Decoding unpadding input A from stream failed");
438 if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 &&
439 ret[0] != 'A' && ret[1] != 'A')
440 throw new RuntimeException("Decoding unpadding input AA from stream failed");
441 }
442
443 // single-non-base64-char should be ignored for mime decoding, but
444 // iae for basic decoding
445 private static void testSingleNonBase64MimeDec() throws Throwable {
446 for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
447 if (Base64.getMimeDecoder().decode(nonBase64).length != 0) {
448 throw new RuntimeException("non-base64 char is not ignored");
449 }
450 try {
451 Base64.getDecoder().decode(nonBase64);
452 throw new RuntimeException("No IAE for single non-base64 char");
453 } catch (IllegalArgumentException iae) {}
454 }
455 }
456
457 private static void testDecBufRet() throws Throwable {
458 Random rnd = new java.util.Random();
459 Base64.Encoder encoder = Base64.getEncoder();
460 Base64.Decoder decoder = Base64.getDecoder();
461 // src pos, len expected
462 int[][] tests = { { 6, 3, 3, 3}, // xxx xxx -> yyyy yyyy
|