/* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test xxxxxxx * @summary tests java.util.Base64 */ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Base64; import java.util.Random; public class TestBase64 { public static void main(String args[]) throws Throwable { int numRuns = 10; int numBytes = 200; if (args.length > 1) { numRuns = Integer.parseInt(args[0]); numBytes = Integer.parseInt(args[1]); } test(Base64.getEncoder(), Base64.getDecoder(), numRuns, numBytes); test(Base64.getUrlEncoder(), Base64.getUrlDecoder(), numRuns, numBytes); test(Base64.getMimeEncoder(), Base64.getMimeDecoder(), numRuns, numBytes); Random rnd = new java.util.Random(); byte[] nl_1 = new byte[] {'\n'}; byte[] nl_2 = new byte[] {'\n', '\r'}; byte[] nl_3 = new byte[] {'\n', '\r', '\n'}; for (int i = 0; i < 10; i++) { int len = rnd.nextInt(200) + 4; test(Base64.getEncoder(len, nl_1), Base64.getMimeDecoder(), numRuns, numBytes); test(Base64.getEncoder(len, nl_2), Base64.getMimeDecoder(), numRuns, numBytes); test(Base64.getEncoder(len, nl_3), Base64.getMimeDecoder(), numRuns, numBytes); } } private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder(); private static void test(Base64.Encoder enc, Base64.Decoder dec, int numRuns, int numBytes) throws Throwable { Random rnd = new java.util.Random(); for (int i=0; i bigger buf-------- buf = new byte[dstLen + rnd.nextInt(100)]; ret = enc.encode(orig, buf); checkEqual(ret, dstLen, "Base64 enc.encode(src, null) returns wrong size!"); buf = Arrays.copyOf(buf, dstLen); checkEqual(buf, encoded, "Base64 enc.encode(src, dst) failed!"); // --------testing decode(in, out);-------- dstLen = dec.decode(encoded, null); buf = new byte[dstLen]; ret = dec.decode(encoded, buf); checkEqual(ret, buf.length, "Base64 dec.decode(src, null) returns wrong size!"); checkEqual(buf, orig, "Base64 dec.decode(src, dst) failed!"); // --------testing decode(in, out); -> bigger buf-------- buf = new byte[dstLen + rnd.nextInt(100)]; ret = dec.decode(encoded, buf); checkEqual(ret, dstLen, "Base64 enc.encode(src, null) returns wrong size!"); buf = Arrays.copyOf(buf, dstLen); checkEqual(buf, orig, "Base64 dec.decode(src, dst) failed!"); } } } private static final void testEncode(Base64.Encoder enc, byte[] expected, ByteBuffer ibb, ByteBuffer obb) throws Throwable { Random rnd = new Random(); int bytesOut = enc.encode(ibb, obb, 0); if (ibb.hasRemaining()) { throw new RuntimeException( "Base64 enc.encode(bf, bf) failed with wrong return!"); } obb.flip(); byte[] buf = new byte[obb.remaining()]; obb.get(buf); checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); ibb.rewind(); obb.position(0); obb.limit(0); bytesOut = 0; do { // increase the "limit" incrementally & randomly int n = rnd.nextInt(expected.length - obb.position()); if (n == 0) n = 1; obb.limit(obb.limit() + n); bytesOut = enc.encode(ibb, obb, bytesOut); } while (ibb.hasRemaining()); obb.flip(); buf = new byte[obb.remaining()]; obb.get(buf); checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); } private static final void testDecode(Base64.Decoder dec, byte[] expected, ByteBuffer ibb, ByteBuffer obb) throws Throwable { Random rnd = new Random(); if (!dec.decode(ibb, obb)) { throw new RuntimeException( "Base64 dec.decode(bf, bf) failed with wrong return!"); } obb.flip(); byte[] buf = new byte[obb.remaining()]; obb.get(buf); checkEqual(buf, expected, "Base64 dec.decode(bf, bf) failed!"); ibb.rewind(); obb.position(0); obb.limit(0); do { // increase the "limit" incrementally & randomly int n = rnd.nextInt(expected.length - obb.position()); if (n == 0) n = 1; obb.limit(obb.limit() + n); } while (!dec.decode(ibb, obb)); obb.flip(); buf = new byte[obb.remaining()]; obb.get(buf); checkEqual(buf, expected, "Base64 dec.decode(bf, bf) failed!"); } private static final void checkEqual(int v1, int v2, String msg) throws Throwable { if (v1 != v2) { System.out.printf(" v1=%d%n", v1); System.out.printf(" v2=%d%n", v2); throw new RuntimeException(msg); } } private static final void checkEqual(byte[] r1, byte[] r2, String msg) throws Throwable { if (!Arrays.equals(r1, r2)) { System.out.printf(" r1[%d]=[%s]%n", r1.length, new String(r1)); System.out.printf(" r2[%d]=[%s]%n", r2.length, new String(r2)); throw new RuntimeException(msg); } } // remove line feeds, private static final byte[] normalize(byte[] src) { int n = 0; boolean hasUrl = false; for (int i = 0; i < src.length; i++) { if (src[i] == '\r' || src[i] == '\n') n++; if (src[i] == '-' || src[i] == '_') hasUrl = true; } if (n == 0 && hasUrl == false) return src; byte[] ret = new byte[src.length - n]; int j = 0; for (int i = 0; i < src.length; i++) { if (src[i] == '-') ret[j++] = '+'; else if (src[i] == '_') ret[j++] = '/'; else if (src[i] != '\r' && src[i] != '\n') ret[j++] = src[i]; } return ret; } }