Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/base64/TestBase64.java
41153 views
1
/*
2
* Copyright (c) 2018, 2021, 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 <[email protected]>
27
* @summary tests java.util.Base64
28
* @library /test/lib /
29
* @build sun.hotspot.WhiteBox
30
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
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
import jdk.test.lib.Utils;
56
57
public class TestBase64 {
58
static boolean checkOutput = Boolean.getBoolean("checkOutput");
59
60
public static void main(String[] args) throws Exception {
61
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)) {
62
throw new SkippedException("Base64 intrinsic is not available");
63
}
64
int iters = (args.length > 0 ? Integer.valueOf(args[0]) : 5_000);
65
System.out.println(iters + " iterations");
66
67
initNonBase64Arrays();
68
69
warmup();
70
71
test0(FileType.ASCII, Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),"plain.txt", "baseEncode.txt", iters);
72
test0(FileType.ASCII, Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),"plain.txt", "urlEncode.txt", iters);
73
test0(FileType.ASCII, Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),"plain.txt", "mimeEncode.txt", iters);
74
75
test0(FileType.HEXASCII, Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),"longLineHEX.txt", "longLineBaseEncode.txt", iters);
76
test0(FileType.HEXASCII, Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),"longLineHEX.txt", "longLineUrlEncode.txt", iters);
77
test0(FileType.HEXASCII, Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),"longLineHEX.txt", "longLineMimeEncode.txt", iters);
78
}
79
80
private static void warmup() {
81
final int warmupCount = 20_000;
82
final int bufSize = 60;
83
byte[] srcBuf = new byte[bufSize];
84
byte[] encBuf = new byte[(bufSize / 3) * 4];
85
byte[] decBuf = new byte[bufSize];
86
87
ran.nextBytes(srcBuf);
88
89
// This should be enough to get both encode and decode compiled on
90
// the highest tier.
91
for (int i = 0; i < warmupCount; i++) {
92
Base64.getEncoder().encode(srcBuf, encBuf);
93
Base64.getDecoder().decode(encBuf, decBuf);
94
}
95
}
96
97
public static void test0(FileType inputFileType, Base64Type type, Encoder encoder, Decoder decoder, String srcFile, String encodedFile, int numIterations) throws Exception {
98
99
String[] srcLns = Files.readAllLines(Paths.get(SRCDIR, srcFile), DEF_CHARSET)
100
.toArray(new String[0]);
101
String[] encodedLns = Files.readAllLines(Paths.get(SRCDIR, encodedFile), DEF_CHARSET)
102
.toArray(new String[0]);
103
104
for (int i = 0; i < numIterations; i++) {
105
int lns = 0;
106
for (String srcStr : srcLns) {
107
String encodedStr = null;
108
if (type != Base64Type.MIME) {
109
encodedStr = encodedLns[lns++];
110
} else {
111
while (lns < encodedLns.length) {
112
String s = encodedLns[lns++];
113
if (s.length() == 0)
114
break;
115
if (encodedStr != null) {
116
encodedStr += DEFAULT_CRLF + s;
117
} else {
118
encodedStr = s;
119
}
120
}
121
if (encodedStr == null && srcStr.length() == 0) {
122
encodedStr = "";
123
}
124
}
125
126
byte[] srcArr;
127
switch (inputFileType) {
128
case ASCII:
129
srcArr = srcStr.getBytes(DEF_CHARSET);
130
break;
131
case HEXASCII:
132
srcArr = Utils.toByteArray(srcStr);
133
break;
134
default:
135
throw new IllegalStateException();
136
}
137
138
byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET);
139
140
ByteBuffer srcBuf = ByteBuffer.wrap(srcArr);
141
ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);
142
byte[] resArr = new byte[encodedArr.length];
143
144
// test int encode(byte[], byte[])
145
int len = encoder.encode(srcArr, resArr);
146
assertEqual(len, encodedArr.length);
147
assertEqual(resArr, encodedArr);
148
149
// test byte[] encode(byte[])
150
resArr = encoder.encode(srcArr);
151
assertEqual(resArr, encodedArr);
152
153
// test ByteBuffer encode(ByteBuffer)
154
int limit = srcBuf.limit();
155
ByteBuffer resBuf = encoder.encode(srcBuf);
156
assertEqual(srcBuf.position(), limit);
157
assertEqual(srcBuf.limit(), limit);
158
assertEqual(resBuf, encodedBuf);
159
srcBuf.rewind(); // reset for next test
160
161
// test String encodeToString(byte[])
162
String resEncodeStr = encoder.encodeToString(srcArr);
163
assertEqual(resEncodeStr, encodedStr);
164
165
// test int decode(byte[], byte[])
166
resArr = new byte[srcArr.length];
167
len = decoder.decode(encodedArr, resArr);
168
assertEqual(len, srcArr.length);
169
assertEqual(resArr, srcArr);
170
171
// test byte[] decode(byte[])
172
resArr = decoder.decode(encodedArr);
173
assertEqual(resArr, srcArr);
174
175
// test that an illegal Base64 character is detected
176
if ((type != Base64Type.MIME) && (encodedArr.length > 0)) {
177
int bytePosToCorrupt = ran.nextInt(encodedArr.length);
178
byte orig = encodedArr[bytePosToCorrupt];
179
encodedArr[bytePosToCorrupt] = getBadBase64Char(type);
180
boolean caught = false;
181
try {
182
// resArr is already allocated
183
len = decoder.decode(encodedArr, resArr);
184
} catch (IllegalArgumentException e) {
185
caught = true;
186
}
187
if (!caught) {
188
throw new RuntimeException(String.format("Decoder did not catch an illegal base64 character: 0x%02x at position: %d in encoded buffer of length %d",
189
encodedArr[bytePosToCorrupt], bytePosToCorrupt, encodedArr.length));
190
}
191
encodedArr[bytePosToCorrupt] = orig;
192
}
193
194
// test ByteBuffer decode(ByteBuffer)
195
limit = encodedBuf.limit();
196
resBuf = decoder.decode(encodedBuf);
197
assertEqual(encodedBuf.position(), limit);
198
assertEqual(encodedBuf.limit(), limit);
199
assertEqual(resBuf, srcBuf);
200
encodedBuf.rewind(); // reset for next test
201
202
// test byte[] decode(String)
203
resArr = decoder.decode(encodedStr);
204
assertEqual(resArr, srcArr);
205
206
}
207
}
208
}
209
210
// Data type in the input file
211
enum FileType {
212
ASCII, HEXASCII
213
}
214
215
// helper
216
enum Base64Type {
217
BASIC, URLSAFE, MIME
218
}
219
220
private static final String SRCDIR = System.getProperty("test.src", "compiler/intrinsics/base64/");
221
private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;
222
private static final String DEF_EXCEPTION_MSG =
223
"Assertion failed! The result is not same as expected\n";
224
private static final String DEFAULT_CRLF = "\r\n";
225
private static final Random ran = new Random(1000); // Constant seed for repeatability
226
227
private static void assertEqual(Object result, Object expect) {
228
if (checkOutput) {
229
if (!Objects.deepEquals(result, expect)) {
230
String resultStr = result.toString();
231
String expectStr = expect.toString();
232
if (result instanceof byte[]) {
233
resultStr = new String((byte[]) result, DEF_CHARSET);
234
}
235
if (expect instanceof byte[]) {
236
expectStr = new String((byte[]) expect, DEF_CHARSET);
237
}
238
throw new RuntimeException(DEF_EXCEPTION_MSG +
239
" result: " + resultStr + " expected: " + expectStr);
240
}
241
}
242
}
243
244
// This array will contain all possible 8-bit values *except* those
245
// that are legal Base64 characters: A-Z a-z 0-9 + / =
246
private static final byte[] nonBase64 = new byte[256 - 65];
247
248
// This array will contain all possible 8-bit values *except* those
249
// that are legal URL-safe Base64 characters: A-Z a-z 0-9 - _ =
250
private static final byte[] nonBase64URL = new byte[256 - 65];
251
252
private static final byte[] legalBase64 = new byte[] {
253
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
254
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
255
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
256
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
257
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
258
'+', '/', '=' };
259
260
private static final byte[] legalBase64URL = new byte[] {
261
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
262
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
263
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
264
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
265
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
266
'-', '_', '=' };
267
268
private static final boolean contains(byte[] ary, byte b) {
269
for (int i = 0; i < ary.length; i++) {
270
if (ary[i] == b) {
271
return true;
272
}
273
}
274
return false;
275
}
276
277
private static final void initNonBase64Arrays() {
278
int i0 = 0, i1 = 0;
279
for (int val = 0; val < 256; val++) {
280
if (! contains(legalBase64, (byte)val)) {
281
nonBase64[i0++] = (byte)val;
282
}
283
if (! contains(legalBase64URL, (byte)val)) {
284
nonBase64URL[i1++] = (byte)val;
285
}
286
}
287
}
288
289
private static final byte getBadBase64Char(Base64Type b64Type) {
290
int ch = ran.nextInt(256 - 65); // 64 base64 characters, and one for the '=' padding character
291
switch (b64Type) {
292
case MIME:
293
case BASIC:
294
return nonBase64[ch];
295
case URLSAFE:
296
return nonBase64URL[ch];
297
default:
298
throw new InternalError("Internal test error: getBadBase64Char called with unknown Base64Type value");
299
}
300
}
301
}
302
303