/* * 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 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925 * 8014217 8025003 8026330 * @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.getMimeEncoder(len, nl_1), Base64.getMimeDecoder(), numRuns, numBytes); test(Base64.getMimeEncoder(len, nl_2), Base64.getMimeDecoder(), numRuns, numBytes); test(Base64.getMimeEncoder(len, nl_3), Base64.getMimeDecoder(), numRuns, numBytes); } testNull(Base64.getEncoder()); testNull(Base64.getUrlEncoder()); testNull(Base64.getMimeEncoder()); testNull(Base64.getMimeEncoder(10, new byte[]{'\n'})); testNull(Base64.getDecoder()); testNull(Base64.getUrlDecoder()); testNull(Base64.getMimeDecoder()); checkNull(new Runnable() { public void run() { Base64.getMimeEncoder(10, null); }}); testIOE(Base64.getEncoder()); testIOE(Base64.getUrlEncoder()); testIOE(Base64.getMimeEncoder()); testIOE(Base64.getMimeEncoder(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.getMimeEncoder(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)); }}); // illegal ending unit checkIOE(new Testable() { public void test() throws IOException { byte[] bytes = "AA=".getBytes("ASCII"); try (InputStream stream = Base64.getDecoder().wrap(new ByteArrayInputStream(bytes))) { while (stream.read() != -1); } }}); // test return value from decode(ByteBuffer, ByteBuffer) testDecBufRet(); // test single-non-base64 character for mime decoding testSingleNonBase64MimeDec(); // test decoding of unpadded data testDecodeUnpadded(); // test mime decoding with ignored character after padding testDecodeIgnoredAfterPadding(); // lenient mode for ending unit testLenientPadding(); } 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 (boolean withoutPadding : new boolean[] { false, true}) { if (withoutPadding) { enc = enc.withoutPadding(); } for (int i=0; i "QUJD" "QQ=", "A", // incomplete padding "QQ=N", "A", // incorrect padding "QQ=?", "A", "QUJDQQ=", "ABCA", "QUJDQQ=N", "ABCA", "QUJDQQ=?", "ABCA", "QUI=X", "AB", // incorrect padding "QUI=?", "AB", // incorrect padding }; Base64.Decoder dec = Base64.getMimeDecoder(); for (int i = 0; i < data.length; i += 2) { byte[] src = data[i].getBytes("ASCII"); byte[] expected = data[i + 1].getBytes("ASCII"); // decode(byte[]) byte[] ret = dec.decode(src); checkEqual(ret, expected, "lenient padding decoding failed!"); // decode(String) ret = dec.decode(data[i]); checkEqual(ret, expected, "lenient padding decoding failed!"); // decode(ByteBuffer) ByteBuffer srcBB = ByteBuffer.wrap(src); ByteBuffer retBB = dec.decode(srcBB); checkEqual(srcBB.remaining(), 0, "lenient padding decoding failed!"); checkEqual(Arrays.copyOf(retBB.array(), retBB.remaining()), expected, "lenient padding decoding failed!"); // wrap.decode(byte[]) ret = new byte[10]; int n = dec.wrap(new ByteArrayInputStream(src)).read(ret); checkEqual(Arrays.copyOf(ret, n), expected, "lenient padding decoding failed!"); } } private static void testDecodeUnpadded() throws Throwable { byte[] srcA = new byte[] { 'Q', 'Q' }; byte[] srcAA = new byte[] { 'Q', 'Q', 'E'}; Base64.Decoder dec = Base64.getDecoder(); byte[] ret = dec.decode(srcA); if (ret[0] != 'A') throw new RuntimeException("Decoding unpadding input A failed"); ret = dec.decode(srcAA); if (ret[0] != 'A' && ret[1] != 'A') throw new RuntimeException("Decoding unpadding input AA failed"); ret = new byte[10]; if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 1 && ret[0] != 'A') throw new RuntimeException("Decoding unpadding input A from stream failed"); if (dec.wrap(new ByteArrayInputStream(srcA)).read(ret) != 2 && ret[0] != 'A' && ret[1] != 'A') throw new RuntimeException("Decoding unpadding input AA from stream failed"); } // single-non-base64-char should be ignored for mime decoding, but // iae for basic decoding private static void testSingleNonBase64MimeDec() throws Throwable { for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) { if (Base64.getMimeDecoder().decode(nonBase64).length != 0) { throw new RuntimeException("non-base64 char is not ignored"); } try { Base64.getDecoder().decode(nonBase64); throw new RuntimeException("No IAE for single non-base64 char"); } catch (IllegalArgumentException iae) {} } } private static void testDecBufRet() throws Throwable { Random rnd = new java.util.Random(); Base64.Encoder encoder = Base64.getEncoder(); Base64.Decoder decoder = Base64.getDecoder(); // src pos, len expected int[][] tests = { { 6, 3, 3, 3}, // xxx xxx -> yyyy yyyy { 6, 3, 4, 3}, { 6, 3, 5, 3}, { 6, 3, 6, 6}, { 6, 11, 4, 3}, { 6, 11, 4, 3}, { 6, 11, 5, 3}, { 6, 11, 6, 6}, { 7, 3, 6, 6}, // xxx xxx x -> yyyy yyyy yy== { 7, 3, 7, 7}, { 7, 11, 6, 6}, { 7, 11, 7, 7}, { 8, 3, 6, 6}, // xxx xxx xx -> yyyy yyyy yyy= { 8, 3, 7, 6}, { 8, 3, 8, 8}, { 8, 13, 6, 6}, { 8, 13, 7, 6}, { 8, 13, 8, 8}, }; ByteBuffer dstBuf = ByteBuffer.allocate(100); for (boolean direct : new boolean[] { false, true}) { for (int[] test : tests) { byte[] src = new byte[test[0]]; rnd.nextBytes(src); ByteBuffer srcBuf = direct ? ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100); srcBuf.put(encoder.encode(src)).flip(); dstBuf.clear().position(test[1]).limit(test[1]+ test[2]); int ret = decoder.decode(srcBuf, dstBuf); if (ret != test[3]) { System.out.printf(" [%6s] src=%d, pos=%d, len=%d, expected=%d, ret=%d%n", direct?"direct":"", test[0], test[1], test[2], test[3], ret); throw new RuntimeException("ret != expected"); } } } } private static final void testEncode(Base64.Encoder enc, ByteBuffer bin, byte[] expected) throws Throwable { ByteBuffer bout = enc.encode(bin); byte[] buf = new byte[bout.remaining()]; bout.get(buf); if (bin.hasRemaining()) { throw new RuntimeException( "Base64 enc.encode(ByteBuffer) failed!"); } checkEqual(buf, expected, "Base64 enc.encode(bf, bf) failed!"); } private static final void testDecode(Base64.Decoder dec, ByteBuffer bin, byte[] expected) throws Throwable { ByteBuffer bout = dec.decode(bin); byte[] buf = new byte[bout.remaining()]; bout.get(buf); checkEqual(buf, expected, "Base64 dec.decode(bf) 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); //obb.limit(Math.min(obb.limit() + n, expected.length)); 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(); dec.decode(ibb, obb); if (ibb.hasRemaining()) { throw new RuntimeException( "Base64 dec.decode(bf, bf) failed with un-decoded ibb!"); } 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); dec.decode(ibb, obb); } while (ibb.hasRemaining()); 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; } }