Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/MessageDigest/ThreadSafetyTest.java
41149 views
1
/*
2
* Copyright (c) 2020, Azul Systems, Inc. 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
* @bug 8241960
27
* @summary Confirm that java.security.MessageDigest is thread-safe after clone.
28
* @run main/othervm ThreadSafetyTest 5 4
29
*/
30
31
import java.security.MessageDigest;
32
import java.security.NoSuchAlgorithmException;
33
import java.security.Provider;
34
import java.security.Security;
35
import java.util.Arrays;
36
import java.util.Random;
37
38
// Usage: java ThreadSafetyTest [threadsFactor [duration]]
39
public class ThreadSafetyTest {
40
41
static volatile boolean runrun = true;
42
static volatile boolean error = false;
43
44
private static final String[] ALGORITHM_ARRAY = { "MD2", "MD5",
45
"SHA1", "SHA-224", "SHA-256", "SHA-384",
46
"SHA-512", "SHA-512/224", "SHA-512/256",
47
"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512" };
48
49
public static void main(String[] args) throws Exception {
50
int threadsFactor = 5;
51
if (args.length > 0) {
52
threadsFactor = Integer.parseInt(args[0]);
53
}
54
int duration = 4;
55
if (args.length > 1) {
56
duration = Integer.parseInt(args[1]);
57
}
58
int nProcessors = Runtime.getRuntime().availableProcessors();
59
int nTasks = nProcessors * threadsFactor;
60
61
System.out.println("Testing with " + nTasks + " threads on " +
62
nProcessors + " processors for " + duration +
63
" seconds.");
64
65
// Initialize input data
66
byte input[] = new byte[1024];
67
(new Random()).nextBytes(input);
68
69
for (Provider p : Security.getProviders()) {
70
for (String alg : ALGORITHM_ARRAY) {
71
try {
72
MessageDigest md = MessageDigest.getInstance(alg, p);
73
testThreadSafety(md, input, nTasks, duration, false);
74
75
if (isClonable(md)) {
76
md.reset();
77
testThreadSafety(md, input, nTasks, duration, true);
78
}
79
} catch (NoSuchAlgorithmException nae) {
80
// algorithm not supported, just skip
81
}
82
}
83
}
84
}
85
86
static private void testThreadSafety(final MessageDigest originalMD,
87
final byte[] input, final int nTasks,
88
final int duration, final boolean useClone) {
89
Thread[] tasks = new Thread[nTasks];
90
91
byte[] expectedOut = getHash(originalMD, input, useClone);
92
originalMD.reset();
93
94
runrun = true;
95
96
for (int i = 0; i < nTasks; i++) {
97
tasks[i] = new Thread(new Runnable() {
98
public void run() {
99
MessageDigest md = getMessageDigest(originalMD, useClone);
100
while (runrun) {
101
byte newOut[] = getHash(md, input, useClone);
102
if (!Arrays.equals(expectedOut, newOut)) {
103
runrun = false;
104
error = true;
105
}
106
}
107
}
108
});
109
}
110
for (int i = 0; i < nTasks; i++) {
111
tasks[i].start();
112
}
113
114
try {
115
for (int i = 0; runrun && i < duration; i++) {
116
Thread.sleep(1000); // 1 second
117
}
118
runrun = false;
119
for (int i = 0; i < nTasks; i++) {
120
tasks[i].join();
121
}
122
} catch (InterruptedException e) {
123
}
124
if (error) {
125
throw new RuntimeException("MessageDigest " + originalMD.getAlgorithm() +
126
" in the provider " + originalMD.getProvider().getName() +
127
" is not thread-safe" + (useClone?" after clone.":"." ));
128
}
129
}
130
131
static private byte[] getHash(final MessageDigest messageDigest,
132
final byte[] input, final boolean useClone) {
133
for (int i = 0; i < 100; i++)
134
messageDigest.update(input);
135
return messageDigest.digest();
136
}
137
138
static private MessageDigest getMessageDigest(final MessageDigest prototype,
139
final boolean useClone) {
140
try {
141
if (useClone) {
142
return (MessageDigest)prototype.clone();
143
}
144
return MessageDigest.getInstance(
145
prototype.getAlgorithm(), prototype.getProvider());
146
} catch (final CloneNotSupportedException | NoSuchAlgorithmException e) {
147
throw new RuntimeException(e);
148
}
149
}
150
151
// The impls from libucrypto does not support clone but ones
152
// from libmd do.
153
static private boolean isClonable(final MessageDigest messageDigest) {
154
try {
155
messageDigest.clone();
156
return true;
157
} catch (final CloneNotSupportedException e) {
158
return false;
159
}
160
}
161
162
}
163
164