/* * 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. * * 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 4235519 * @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); } testNull(Base64.getEncoder()); testNull(Base64.getUrlEncoder()); testNull(Base64.getMimeEncoder()); testNull(Base64.getEncoder(10, new byte[]{'\n'})); testNull(Base64.getDecoder()); testNull(Base64.getUrlDecoder()); testNull(Base64.getMimeDecoder()); checkNull(new Runnable() { public void run() { Base64.getEncoder(10, null); }}); testIOE(Base64.getEncoder()); testIOE(Base64.getUrlEncoder()); testIOE(Base64.getMimeEncoder()); testIOE(Base64.getEncoder(10, new byte[]{'\n'})); byte[] src = new byte[1024]; new Random().nextBytes(src); final byte[] decoded = Base64.getEncoder().encode(src); testIOE(Base64.getDecoder(), decoded); testIOE(Base64.getMimeDecoder(), decoded); testIOE(Base64.getUrlDecoder(), Base64.getUrlEncoder().encode(src)); // illegal line separator checkIAE(new Runnable() { public void run() { Base64.getEncoder(10, new byte[]{'\r', 'N'}); }}); // illegal base64 character decoded[2] = (byte)0xe0; checkIAE(new Runnable() { public void run() { Base64.getDecoder().decode(decoded); }}); checkIAE(new Runnable() { public void run() { Base64.getDecoder().decode(decoded, new byte[1024]); }}); checkIAE(new Runnable() { public void run() { Base64.getDecoder().decode(ByteBuffer.wrap(decoded)); }}); checkIAE(new Runnable() { public void run() { Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocate(1024)); }}); checkIAE(new Runnable() { public void run() { Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocateDirect(1024)); }}); } 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(); enc.encode(new byte[0]); dec.decode(new byte[0]); for (int i=0; i