1 /* 2 * Copyright (c) 2018 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 26 * @author Eric Wang <yiming.wang@oracle.com> 27 * @summary tests java.util.Base64 28 * @library /test/lib / 29 * @build sun.hotspot.WhiteBox 30 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 31 * 32 * @run main/othervm/timeout=600 -Xbatch -DcheckOutput=true 33 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. 34 * compiler.intrinsics.base64.TestBase64 35 */ 36 37 package compiler.intrinsics.base64; 38 39 import java.io.BufferedReader; 40 import java.io.FileReader; 41 import java.nio.ByteBuffer; 42 import java.nio.charset.Charset; 43 import java.nio.charset.StandardCharsets; 44 import java.nio.file.Files; 45 import java.nio.file.Paths; 46 import java.util.Base64; 47 import java.util.Base64.Decoder; 48 import java.util.Base64.Encoder; 49 import java.util.Objects; 50 import java.util.Random; 51 52 import compiler.whitebox.CompilerWhiteBoxTest; 53 import sun.hotspot.code.Compiler; 54 import jtreg.SkippedException; 55 56 public class TestBase64 { 57 static boolean checkOutput = Boolean.getBoolean("checkOutput"); 58 main(String[] args)59 public static void main(String[] args) throws Exception { 60 if (!Compiler.isIntrinsicAvailable(CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION, "java.util.Base64$Encoder", "encodeBlock", byte[].class, int.class, int.class, byte[].class, int.class, boolean.class)) { 61 throw new SkippedException("Base64 intrinsic is not available"); 62 } 63 int iters = (args.length > 0 ? Integer.valueOf(args[0]) : 100000); 64 System.out.println(iters + " iterations"); 65 66 test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),"plain.txt", "baseEncode.txt", iters); 67 test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),"plain.txt", "urlEncode.txt", iters); 68 test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),"plain.txt", "mimeEncode.txt", iters); 69 } 70 test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile, int numIterations)71 public static void test0(Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile, int numIterations) throws Exception { 72 73 String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET) 74 .toArray(new String[0]); 75 String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET) 76 .toArray(new String[0]); 77 78 for (int i = 0; i < numIterations; i++) { 79 int lns = 0; 80 for (String srcStr : srcLns) { 81 String encodedStr = null; 82 if (type != Base64Type.MIME) { 83 encodedStr = encodedLns[lns++]; 84 } else { 85 while (lns < encodedLns.length) { 86 String s = encodedLns[lns++]; 87 if (s.length() == 0) 88 break; 89 if (encodedStr != null) { 90 encodedStr += DEFAULT_CRLF + s; 91 } else { 92 encodedStr = s; 93 } 94 } 95 if (encodedStr == null && srcStr.length() == 0) { 96 encodedStr = ""; 97 } 98 } 99 100 byte[] srcArr = srcStr.getBytes(DEF_CHARSET); 101 byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET); 102 103 ByteBuffer srcBuf = ByteBuffer.wrap(srcArr); 104 ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr); 105 byte[] resArr = new byte[encodedArr.length]; 106 107 // test int encode(byte[], byte[]) 108 int len = encoder.encode(srcArr, resArr); 109 assertEqual(len, encodedArr.length); 110 assertEqual(resArr, encodedArr); 111 112 // test byte[] encode(byte[]) 113 resArr = encoder.encode(srcArr); 114 assertEqual(resArr, encodedArr); 115 116 // test ByteBuffer encode(ByteBuffer) 117 int limit = srcBuf.limit(); 118 ByteBuffer resBuf = encoder.encode(srcBuf); 119 assertEqual(srcBuf.position(), limit); 120 assertEqual(srcBuf.limit(), limit); 121 assertEqual(resBuf, encodedBuf); 122 srcBuf.rewind(); // reset for next test 123 124 // test String encodeToString(byte[]) 125 String resEncodeStr = encoder.encodeToString(srcArr); 126 assertEqual(resEncodeStr, encodedStr); 127 128 // test int decode(byte[], byte[]) 129 resArr = new byte[srcArr.length]; 130 len = decoder.decode(encodedArr, resArr); 131 assertEqual(len, srcArr.length); 132 assertEqual(resArr, srcArr); 133 134 // test byte[] decode(byte[]) 135 resArr = decoder.decode(encodedArr); 136 assertEqual(resArr, srcArr); 137 138 // test ByteBuffer decode(ByteBuffer) 139 limit = encodedBuf.limit(); 140 resBuf = decoder.decode(encodedBuf); 141 assertEqual(encodedBuf.position(), limit); 142 assertEqual(encodedBuf.limit(), limit); 143 assertEqual(resBuf, srcBuf); 144 encodedBuf.rewind(); // reset for next test 145 146 // test byte[] decode(String) 147 resArr = decoder.decode(encodedStr); 148 assertEqual(resArr, srcArr); 149 150 } 151 } 152 } 153 154 // helper 155 enum Base64Type { 156 BASIC, URLSAFE, MIME 157 } 158 159 private static final String SRCDIR = System.getProperty("test.src", "compiler/intrinsics/base64/"); 160 private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII; 161 private static final String DEF_EXCEPTION_MSG = 162 "Assertion failed! The result is not same as expected\n"; 163 private static final String DEFAULT_CRLF = "\r\n"; 164 assertEqual(Object result, Object expect)165 private static void assertEqual(Object result, Object expect) { 166 if (checkOutput) { 167 if (!Objects.deepEquals(result, expect)) { 168 String resultStr = result.toString(); 169 String expectStr = expect.toString(); 170 if (result instanceof byte[]) { 171 resultStr = new String((byte[]) result, DEF_CHARSET); 172 } 173 if (expect instanceof byte[]) { 174 expectStr = new String((byte[]) expect, DEF_CHARSET); 175 } 176 throw new RuntimeException(DEF_EXCEPTION_MSG + 177 " result: " + resultStr + " expected: " + expectStr); 178 } 179 } 180 } 181 } 182