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