Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/MessageDigest/TestCloning.java
38855 views
1
/*
2
* Copyright (c) 2012, 2016, 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
* @bug 6414899
27
* @summary Ensure the cloning functionality works.
28
* @author Valerie Peng
29
* @library ..
30
* @key randomness
31
* @run main/othervm TestCloning
32
* @run main/othervm TestCloning sm
33
*/
34
35
import java.security.MessageDigest;
36
import java.security.Provider;
37
import java.util.Arrays;
38
import java.util.Random;
39
40
public class TestCloning extends PKCS11Test {
41
42
private static final String[] ALGOS = {
43
"MD2", "MD5", "SHA1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"
44
};
45
46
public static void main(String[] args) throws Exception {
47
main(new TestCloning(), args);
48
}
49
50
private static final byte[] data1 = new byte[10];
51
private static final byte[] data2 = new byte[10*1024];
52
53
54
@Override
55
public void main(Provider p) throws Exception {
56
Random r = new Random();
57
byte[] data1 = new byte[10];
58
byte[] data2 = new byte[2*1024];
59
r.nextBytes(data1);
60
r.nextBytes(data2);
61
System.out.println("Testing against provider " + p.getName());
62
for (int i = 0; i < ALGOS.length; i++) {
63
if (p.getService("MessageDigest", ALGOS[i]) == null) {
64
System.out.println(ALGOS[i] + " is not supported, skipping");
65
continue;
66
} else {
67
System.out.println("Testing " + ALGOS[i] + " of " + p.getName());
68
MessageDigest md = MessageDigest.getInstance(ALGOS[i], p);
69
try {
70
md = testCloning(md, p);
71
// repeat the test again after generating digest once
72
for (int j = 0; j < 10; j++) {
73
md = testCloning(md, p);
74
}
75
} catch (Exception ex) {
76
if (ALGOS[i] == "MD2" &&
77
p.getName().equalsIgnoreCase("SunPKCS11-NSS")) {
78
// known bug in NSS; ignore for now
79
System.out.println("Ignore Known bug in MD2 of NSS");
80
continue;
81
}
82
throw ex;
83
}
84
}
85
}
86
}
87
88
private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
89
throws Exception {
90
91
// copy#0: clone at state BLANK w/o any data
92
MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();
93
94
// copy#1: clone again at state BUFFERED w/ very short data
95
mdObj.update(data1);
96
mdCopy0.update(data1);
97
MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();
98
99
// copy#2: clone again after updating it w/ long data to trigger
100
// the state into INIT
101
mdObj.update(data2);
102
mdCopy0.update(data2);
103
mdCopy1.update(data2);
104
MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();
105
106
// copy#3: clone again after updating it w/ very short data
107
mdObj.update(data1);
108
mdCopy0.update(data1);
109
mdCopy1.update(data1);
110
mdCopy2.update(data1);
111
MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();
112
113
// copy#4: clone again after updating it w/ long data
114
mdObj.update(data2);
115
mdCopy0.update(data2);
116
mdCopy1.update(data2);
117
mdCopy2.update(data2);
118
mdCopy3.update(data2);
119
MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();
120
121
// check digest equalities
122
byte[] answer = mdObj.digest();
123
byte[] result0 = mdCopy0.digest();
124
byte[] result1 = mdCopy1.digest();
125
byte[] result2 = mdCopy2.digest();
126
byte[] result3 = mdCopy3.digest();
127
byte[] result4 = mdCopy4.digest();
128
129
130
check(answer, result0, "copy0");
131
check(answer, result1, "copy1");
132
check(answer, result2, "copy2");
133
check(answer, result3, "copy3");
134
check(answer, result4, "copy4");
135
136
return mdCopy3;
137
}
138
139
private static void check(byte[] d1, byte[] d2, String copyName)
140
throws Exception {
141
if (Arrays.equals(d1, d2) == false) {
142
throw new RuntimeException(copyName + " digest mismatch!");
143
}
144
}
145
}
146
147
148