Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Base64/TestBase64Golden.java
38812 views
1
/*
2
* Copyright (c) 2012, 2013, 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 4235519
26
* @author Eric Wang <[email protected]>
27
* @summary tests java.util.Base64
28
*/
29
30
import java.io.BufferedReader;
31
import java.io.FileReader;
32
import java.nio.ByteBuffer;
33
import java.nio.charset.Charset;
34
import java.nio.charset.StandardCharsets;
35
import java.nio.file.Files;
36
import java.nio.file.Paths;
37
import java.util.Base64;
38
import java.util.Base64.Decoder;
39
import java.util.Base64.Encoder;
40
import java.util.Objects;
41
import java.util.Random;
42
43
import sun.misc.BASE64Decoder;
44
import sun.misc.BASE64Encoder;
45
46
public class TestBase64Golden {
47
48
public static void main(String[] args) throws Exception {
49
test0(Base64Type.BASIC, Base64.getEncoder(), Base64.getDecoder(),
50
"plain.txt", "baseEncode.txt");
51
52
test0(Base64Type.URLSAFE, Base64.getUrlEncoder(), Base64.getUrlDecoder(),
53
"plain.txt", "urlEncode.txt");
54
55
test0(Base64Type.MIME, Base64.getMimeEncoder(), Base64.getMimeDecoder(),
56
"plain.txt", "mimeEncode.txt");
57
test1();
58
}
59
60
public static void test0(Base64Type type, Encoder encoder, Decoder decoder,
61
String srcFile, String encodedFile) 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),
66
DEF_CHARSET)
67
.toArray(new String[0]);
68
int lns = 0;
69
for (String srcStr : srcLns) {
70
String encodedStr = null;
71
if (type != Base64Type.MIME) {
72
encodedStr = encodedLns[lns++];
73
} else {
74
while (lns < encodedLns.length) {
75
String s = encodedLns[lns++];
76
if (s.length() == 0)
77
break;
78
if (encodedStr != null) {
79
encodedStr += DEFAULT_CRLF + s;
80
} else {
81
encodedStr = s;
82
}
83
}
84
if (encodedStr == null && srcStr.length() == 0) {
85
encodedStr = "";
86
}
87
}
88
System.out.printf("%n src[%d]: %s%n", srcStr.length(), srcStr);
89
System.out.printf("encoded[%d]: %s%n", encodedStr.length(), encodedStr);
90
91
byte[] srcArr = srcStr.getBytes(DEF_CHARSET);
92
byte[] encodedArr = encodedStr.getBytes(DEF_CHARSET);
93
94
ByteBuffer srcBuf = ByteBuffer.wrap(srcArr);
95
ByteBuffer encodedBuf = ByteBuffer.wrap(encodedArr);
96
byte[] resArr = new byte[encodedArr.length];
97
98
// test int encode(byte[], byte[])
99
int len = encoder.encode(srcArr, resArr);
100
assertEqual(len, encodedArr.length);
101
assertEqual(resArr, encodedArr);
102
103
// test byte[] encode(byte[])
104
resArr = encoder.encode(srcArr);
105
assertEqual(resArr, encodedArr);
106
107
// test ByteBuffer encode(ByteBuffer)
108
int limit = srcBuf.limit();
109
ByteBuffer resBuf = encoder.encode(srcBuf);
110
assertEqual(srcBuf.position(), limit);
111
assertEqual(srcBuf.limit(), limit);
112
assertEqual(resBuf, encodedBuf);
113
srcBuf.rewind(); // reset for next test
114
115
// test String encodeToString(byte[])
116
String resEncodeStr = encoder.encodeToString(srcArr);
117
assertEqual(resEncodeStr, encodedStr);
118
119
// test int decode(byte[], byte[])
120
resArr = new byte[srcArr.length];
121
len = decoder.decode(encodedArr, resArr);
122
assertEqual(len, srcArr.length);
123
assertEqual(resArr, srcArr);
124
125
// test byte[] decode(byte[])
126
resArr = decoder.decode(encodedArr);
127
assertEqual(resArr, srcArr);
128
129
// test ByteBuffer decode(ByteBuffer)
130
limit = encodedBuf.limit();
131
resBuf = decoder.decode(encodedBuf);
132
assertEqual(encodedBuf.position(), limit);
133
assertEqual(encodedBuf.limit(), limit);
134
assertEqual(resBuf, srcBuf);
135
encodedBuf.rewind(); // reset for next test
136
137
// test byte[] decode(String)
138
resArr = decoder.decode(encodedStr);
139
assertEqual(resArr, srcArr);
140
141
// test compatible with sun.misc.Base64Encoder
142
if (type == Base64Type.MIME) {
143
sun.misc.BASE64Encoder miscEncoder = new BASE64Encoder();
144
sun.misc.BASE64Decoder miscDecoder = new BASE64Decoder();
145
resArr = decoder.decode(miscEncoder.encode(srcArr));
146
assertEqual(resArr, srcArr);
147
148
resArr = encoder.encode(miscDecoder.decodeBuffer(encodedStr));
149
assertEqual(new String(resArr, DEF_CHARSET), encodedStr);
150
}
151
}
152
}
153
154
private static void test1() throws Exception {
155
byte[] src = new byte[] {
156
46, -97, -35, -44, 127, -60, -39, -4, -112, 34, -57, 47, -14, 67,
157
40, 18, 90, -59, 68, 112, 23, 121, -91, 94, 35, 49, 104, 17, 30,
158
-80, -104, -3, -53, 27, 38, -72, -47, 113, -52, 18, 5, -126 };
159
Encoder encoder = Base64.getMimeEncoder(49, new byte[] { 0x7e });
160
byte[] encoded = encoder.encode(src);
161
Decoder decoder = Base64.getMimeDecoder();
162
byte[] decoded = decoder.decode(encoded);
163
if (!Objects.deepEquals(src, decoded)) {
164
throw new RuntimeException();
165
}
166
}
167
168
// helper
169
enum Base64Type {
170
BASIC, URLSAFE, MIME
171
}
172
173
private static final String SRCDIR = System.getProperty("test.src", ".");
174
private static final Charset DEF_CHARSET = StandardCharsets.US_ASCII;
175
private static final String DEF_EXCEPTION_MSG =
176
"Assertion failed! The result is not same as expected";
177
private static final String DEFAULT_CRLF = "\r\n";
178
179
private static void assertEqual(Object result, Object expect) {
180
if (!Objects.deepEquals(result, expect)) {
181
String resultStr = result.toString();
182
String expectStr = expect.toString();
183
if (result instanceof byte[]) {
184
resultStr = new String((byte[]) result, DEF_CHARSET);
185
}
186
if (expect instanceof byte[]) {
187
expectStr = new String((byte[]) expect, DEF_CHARSET);
188
}
189
throw new RuntimeException(DEF_EXCEPTION_MSG +
190
" result: " + resultStr + " expected: " + expectStr);
191
}
192
}
193
}
194
195