Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/TestCloning.java
41153 views
1
/*
2
* Copyright (c) 2012, 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
/*
25
* @test
26
* @bug 6414899 8242332
27
* @summary Ensure the cloning functionality works.
28
* @author Valerie Peng
29
* @library /test/lib ..
30
* @key randomness
31
* @modules jdk.crypto.cryptoki
32
* @run main/othervm TestCloning
33
* @run main/othervm -Djava.security.manager=allow TestCloning sm
34
*/
35
36
import java.security.MessageDigest;
37
import java.security.Provider;
38
import java.util.Arrays;
39
import java.util.Random;
40
import java.util.List;
41
42
public class TestCloning extends PKCS11Test {
43
44
public static void main(String[] args) throws Exception {
45
main(new TestCloning(), args);
46
}
47
48
private static final byte[] data1 = new byte[10];
49
private static final byte[] data2 = new byte[10*1024];
50
51
@Override
52
public void main(Provider p) throws Exception {
53
List<String> ALGS = getSupportedAlgorithms("MessageDigest", "SHA", p);
54
Random r = new Random();
55
byte[] data1 = new byte[10];
56
byte[] data2 = new byte[2*1024];
57
r.nextBytes(data1);
58
r.nextBytes(data2);
59
System.out.println("Testing against provider " + p.getName());
60
for (String alg : ALGS) {
61
System.out.println("Testing " + alg);
62
MessageDigest md = MessageDigest.getInstance(alg, p);
63
md = testCloning(md, p);
64
// repeat the test again after generating digest once
65
for (int j = 0; j < 10; j++) {
66
md = testCloning(md, p);
67
}
68
}
69
}
70
71
private static MessageDigest testCloning(MessageDigest mdObj, Provider p)
72
throws Exception {
73
// copy#0: clone at state BLANK w/o any data
74
MessageDigest mdCopy0 = (MessageDigest) mdObj.clone();
75
76
// copy#1: clone again at state BUFFERED w/ very short data
77
mdObj.update(data1);
78
mdCopy0.update(data1);
79
MessageDigest mdCopy1 = (MessageDigest) mdObj.clone();
80
81
// copy#2: clone again after updating it w/ long data to trigger
82
// the state into INIT
83
mdObj.update(data2);
84
mdCopy0.update(data2);
85
mdCopy1.update(data2);
86
MessageDigest mdCopy2 = (MessageDigest) mdObj.clone();
87
88
// copy#3: clone again after updating it w/ very short data
89
mdObj.update(data1);
90
mdCopy0.update(data1);
91
mdCopy1.update(data1);
92
mdCopy2.update(data1);
93
MessageDigest mdCopy3 = (MessageDigest) mdObj.clone();
94
95
// copy#4: clone again after updating it w/ long data
96
mdObj.update(data2);
97
mdCopy0.update(data2);
98
mdCopy1.update(data2);
99
mdCopy2.update(data2);
100
mdCopy3.update(data2);
101
MessageDigest mdCopy4 = (MessageDigest) mdObj.clone();
102
103
// check digest equalities
104
byte[] answer = mdObj.digest();
105
byte[] result0 = mdCopy0.digest();
106
byte[] result1 = mdCopy1.digest();
107
byte[] result2 = mdCopy2.digest();
108
byte[] result3 = mdCopy3.digest();
109
byte[] result4 = mdCopy4.digest();
110
111
112
check(answer, result0, "copy0");
113
check(answer, result1, "copy1");
114
check(answer, result2, "copy2");
115
check(answer, result3, "copy3");
116
check(answer, result4, "copy4");
117
118
return mdCopy3;
119
}
120
121
private static void check(byte[] d1, byte[] d2, String copyName)
122
throws Exception {
123
if (Arrays.equals(d1, d2) == false) {
124
throw new RuntimeException(copyName + " digest mismatch!");
125
}
126
}
127
}
128
129
130