Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/MessageDigest/TestDigestIOStream.java
41149 views
1
/*
2
* Copyright (c) 2003, 2020, 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
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.PrintStream;
27
import java.security.DigestInputStream;
28
import java.security.DigestOutputStream;
29
import java.security.MessageDigest;
30
import java.security.NoSuchAlgorithmException;
31
import java.security.Security;
32
import java.util.Arrays;
33
import java.util.Random;
34
import jdk.test.lib.RandomFactory;
35
import static java.lang.System.out;
36
37
/**
38
* @test
39
* @bug 8050370 8156059
40
* @summary MessageDigest tests with DigestIOStream
41
* @author Kevin Liu
42
* @key randomness
43
* @library /test/lib
44
* @build jdk.test.lib.RandomFactory
45
* @run main/timeout=180 TestDigestIOStream
46
*/
47
48
enum ReadModel {
49
READ, BUFFER_READ, MIX_READ
50
}
51
52
public class TestDigestIOStream {
53
54
private static final int[] DATA_LEN_ARRAY = { 1, 50, 2500, 125000,
55
6250000 };
56
private static final String[] ALGORITHM_ARRAY = { "MD2", "MD5", "SHA1",
57
"SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA3-224", "SHA3-256",
58
"SHA3-384", "SHA3-512" };
59
60
private static byte[] data;
61
62
private static MessageDigest md = null;
63
64
public static void main(String argv[]) throws Exception {
65
TestDigestIOStream test = new TestDigestIOStream();
66
test.run();
67
}
68
69
public void run() throws Exception {
70
for (String algorithm : ALGORITHM_ARRAY) {
71
md = MessageDigest.getInstance(algorithm);
72
for (int length : DATA_LEN_ARRAY) {
73
74
Random rdm = RandomFactory.getRandom();
75
data = new byte[length];
76
rdm.nextBytes(data);
77
78
if (!testMDChange(algorithm, length)) {
79
throw new RuntimeException("testMDChange failed at:"
80
+ algorithm + "/" + length);
81
}
82
if (!testMDShare(algorithm, length)) {
83
throw new RuntimeException("testMDShare failed at:"
84
+ algorithm + "/" + length);
85
}
86
for (ReadModel readModel : ReadModel.values()) {
87
// test Digest function when digest switch on
88
if (!testDigestOnOff(algorithm, readModel, true,
89
length)) {
90
throw new RuntimeException(
91
"testDigestOn failed at:" + algorithm + "/"
92
+ length + "/" + readModel);
93
}
94
// test Digest function when digest switch off
95
if (!testDigestOnOff(algorithm, readModel, false,
96
length)) {
97
throw new RuntimeException(
98
"testDigestOff failed at:" + algorithm + "/"
99
+ length + "/" + readModel);
100
}
101
}
102
}
103
}
104
int testNumber = ALGORITHM_ARRAY.length * ReadModel.values().length
105
* DATA_LEN_ARRAY.length * 2
106
+ ALGORITHM_ARRAY.length * DATA_LEN_ARRAY.length * 2;
107
out.println("All " + testNumber + " Tests Passed");
108
}
109
110
/**
111
* Test DigestInputStream and DigestOutputStream digest function when digest
112
* set on and off
113
*
114
* @param algo
115
* Message Digest algorithm
116
* @param readModel
117
* which read method used(READ, BUFFER_READ, MIX_READ)
118
* @param on
119
* digest switch(on and off)
120
* @param dataLength
121
* plain test data length.
122
* @exception Exception
123
* throw unexpected exception
124
*/
125
public boolean testDigestOnOff(String algo, ReadModel readModel, boolean on,
126
int dataLength) throws Exception {
127
128
// Generate the DigestInputStream/DigestOutputStream object
129
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
130
DigestInputStream dis = new DigestInputStream(bais,
131
MessageDigest.getInstance(algo));
132
ByteArrayOutputStream baos = new ByteArrayOutputStream();
133
DigestOutputStream dos = new DigestOutputStream(baos,
134
MessageDigest.getInstance(algo));
135
ByteArrayOutputStream baOut = new ByteArrayOutputStream();) {
136
137
// Perform the update using all available/possible update methods
138
int k = 0;
139
byte[] buffer = new byte[5];
140
boolean enDigest = true;
141
// Make sure the digest function is on (default)
142
dis.on(enDigest);
143
dos.on(enDigest);
144
145
switch (readModel) {
146
case READ: // use only read()
147
while ((k = dis.read()) != -1) {
148
if (on) {
149
dos.write(k);
150
} else {
151
dos.write(k);
152
if (enDigest) {
153
baOut.write(k);
154
}
155
enDigest = !enDigest;
156
dos.on(enDigest);
157
dis.on(enDigest);
158
}
159
}
160
break;
161
case BUFFER_READ: // use only read(byte[], int, int)
162
while ((k = dis.read(buffer, 0, buffer.length)) != -1) {
163
if (on) {
164
dos.write(buffer, 0, k);
165
} else {
166
dos.write(buffer, 0, k);
167
if (enDigest) {
168
baOut.write(buffer, 0, k);
169
}
170
enDigest = !enDigest;
171
dis.on(enDigest);
172
dos.on(enDigest);
173
}
174
}
175
break;
176
case MIX_READ: // use both read() and read(byte[], int, int)
177
while ((k = dis.read()) != -1) {
178
if (on) {
179
dos.write(k);
180
if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
181
dos.write(buffer, 0, k);
182
}
183
} else {
184
dos.write(k);
185
if (enDigest) {
186
baOut.write(k);
187
}
188
enDigest = !enDigest;
189
dis.on(enDigest);
190
dos.on(enDigest);
191
if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
192
dos.write(buffer, 0, k);
193
if (enDigest) {
194
baOut.write(buffer, 0, k);
195
}
196
enDigest = !enDigest;
197
dis.on(enDigest);
198
dos.on(enDigest);
199
}
200
}
201
}
202
break;
203
default:
204
out.println("ERROR: Invalid read/write combination choice!");
205
return false;
206
}
207
208
// Get the output and the "correct" digest values
209
byte[] output1 = dis.getMessageDigest().digest();
210
byte[] output2 = dos.getMessageDigest().digest();
211
byte[] standard;
212
if (on) {
213
standard = md.digest(data);
214
} else {
215
byte[] dataDigested = baOut.toByteArray();
216
standard = md.digest(dataDigested);
217
}
218
219
// Compare the output byte array value to the input data
220
if (!MessageDigest.isEqual(data, baos.toByteArray())) {
221
out.println("ERROR of " + readModel
222
+ ": output and input data unexpectedly changed");
223
return false;
224
}
225
// Compare generated digest values
226
if (!MessageDigest.isEqual(output1, standard)
227
|| !MessageDigest.isEqual(output2, standard)) {
228
out.println("ERROR" + readModel
229
+ ": generated digest data unexpectedly changed");
230
return false;
231
}
232
233
return true;
234
} catch (Exception ex) {
235
out.println("testDigestOnOff failed at:" + algo + "/" + readModel
236
+ "/" + dataLength + " with unexpected exception");
237
throw ex;
238
}
239
}
240
241
/**
242
* Test DigestInputStream and DigestOutputStream digest function when Swap
243
* the message digest engines between DigestIn/OutputStream
244
*
245
* @param algo
246
* Message Digest algorithm
247
* @param dataLength
248
* plain test data length.
249
* @exception Exception
250
* throw unexpected exception
251
*/
252
public boolean testMDChange(String algo, int dataLength) throws Exception {
253
// Generate the DigestInputStream/DigestOutputStream object
254
MessageDigest mdIn = MessageDigest.getInstance(algo);
255
MessageDigest mdOut = MessageDigest.getInstance(algo);
256
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
257
DigestInputStream dis = new DigestInputStream(bais, mdIn);
258
ByteArrayOutputStream baos = new ByteArrayOutputStream();
259
DigestOutputStream dos = new DigestOutputStream(baos, mdOut);) {
260
261
// Perform the update using all available/possible update methods
262
int k = 0;
263
byte[] buffer = new byte[10];
264
265
// use both read() and read(byte[], int, int)
266
while ((k = dis.read()) != -1) {
267
dos.write(k);
268
if ((k = dis.read(buffer, 0, buffer.length)) != -1) {
269
dos.write(buffer, 0, k);
270
}
271
272
// Swap the message digest engines between
273
// DigestIn/OutputStream objects
274
dis.setMessageDigest(mdOut);
275
dos.setMessageDigest(mdIn);
276
mdIn = dis.getMessageDigest();
277
mdOut = dos.getMessageDigest();
278
}
279
280
// Get the output and the "correct" digest values
281
byte[] output1 = mdIn.digest();
282
byte[] output2 = mdOut.digest();
283
byte[] standard = md.digest(data);
284
285
// Compare generated digest values
286
return MessageDigest.isEqual(output1, standard)
287
&& MessageDigest.isEqual(output2, standard);
288
} catch (Exception ex) {
289
out.println("testMDChange failed at:" + algo + "/" + dataLength
290
+ " with unexpected exception");
291
throw ex;
292
}
293
}
294
295
/**
296
* Test DigestInputStream and DigestOutputStream digest function when use
297
* same message digest object.
298
*
299
* @param algo
300
* Message Digest algorithm
301
* @param dataLength
302
* plain test data length.
303
* @exception Exception
304
* throw unexpected exception
305
*/
306
public boolean testMDShare(String algo, int dataLength) throws Exception {
307
MessageDigest mdCommon = MessageDigest.getInstance(algo);
308
// Generate the DigestInputStream/DigestOutputStream object
309
try (ByteArrayInputStream bais = new ByteArrayInputStream(data);
310
DigestInputStream dis = new DigestInputStream(bais, mdCommon);
311
ByteArrayOutputStream baos = new ByteArrayOutputStream();
312
DigestOutputStream dos = new DigestOutputStream(baos,
313
mdCommon);) {
314
315
// Perform the update using all available/possible update methods
316
int k = 0;
317
byte[] buffer = new byte[10];
318
319
// use both read() and read(byte[], int, int)
320
while (k < data.length) {
321
int len = dis.read(buffer, 0, buffer.length);
322
if (len != -1) {
323
k += len;
324
if (k < data.length) {
325
dos.write(data[k]);
326
k++;
327
dis.skip(1);
328
}
329
}
330
}
331
332
// Get the output and the "correct" digest values
333
byte[] output = mdCommon.digest();
334
byte[] standard = md.digest(data);
335
336
// Compare generated digest values
337
return MessageDigest.isEqual(output, standard);
338
} catch (Exception ex) {
339
out.println("TestMDShare failed at:" + algo + "/" + dataLength
340
+ " with unexpected exception");
341
throw ex;
342
}
343
}
344
}
345
346