Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/ByteBuffers.java
41153 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
/*
25
* @test
26
* @bug 4856966 8080462 8242332
27
* @summary Test the MessageDigest.update(ByteBuffer) method
28
* @author Andreas Sterbenz
29
* @library /test/lib ..
30
* @key randomness
31
* @modules jdk.crypto.cryptoki
32
* @run main/othervm ByteBuffers
33
*/
34
35
import java.nio.ByteBuffer;
36
import java.security.*;
37
import java.util.Arrays;
38
import java.util.Random;
39
import java.util.List;
40
41
public class ByteBuffers extends PKCS11Test {
42
43
private static Random random = new Random();
44
45
public static void main(String[] args) throws Exception {
46
main(new ByteBuffers(), args);
47
}
48
49
@Override
50
public void main(Provider p) throws Exception {
51
List<String> ALGS = getSupportedAlgorithms("MessageDigest",
52
"SHA", p);
53
54
int n = 10 * 1024;
55
byte[] t = new byte[n];
56
random.nextBytes(t);
57
58
for (String alg : ALGS) {
59
runTest(p, alg, t);
60
}
61
}
62
63
private void runTest(Provider p, String alg, byte[] data) throws Exception {
64
System.out.println("Test against " + p.getName() + " and " + alg);
65
MessageDigest md = MessageDigest.getInstance(alg, p);
66
67
byte[] d1 = md.digest(data);
68
69
int n = data.length;
70
71
// test 1: ByteBuffer with an accessible backing array
72
ByteBuffer b1 = ByteBuffer.allocate(n + 256);
73
b1.position(random.nextInt(256));
74
b1.limit(b1.position() + n);
75
ByteBuffer b2 = b1.slice();
76
b2.put(data);
77
b2.clear();
78
byte[] d2 = digest(md, b2);
79
if (Arrays.equals(d1, d2) == false) {
80
throw new Exception("Test 1 failed");
81
}
82
83
// test 2: direct ByteBuffer
84
ByteBuffer b3 = ByteBuffer.allocateDirect(n);
85
b3.put(data);
86
b3.clear();
87
byte[] d3 = digest(md, b3);
88
if (Arrays.equals(d1, d2) == false) {
89
throw new Exception("Test 2 failed");
90
}
91
92
// test 3: ByteBuffer without an accessible backing array
93
b2.clear();
94
ByteBuffer b4 = b2.asReadOnlyBuffer();
95
byte[] d4 = digest(md, b4);
96
if (Arrays.equals(d1, d2) == false) {
97
throw new Exception("Test 3 failed");
98
}
99
System.out.println("All tests passed");
100
}
101
102
private static byte[] digest(MessageDigest md, ByteBuffer b)
103
throws Exception {
104
int lim = b.limit();
105
b.limit(random.nextInt(lim));
106
md.update(b);
107
if (b.hasRemaining()) {
108
throw new Exception("Buffer not consumed");
109
}
110
b.limit(lim);
111
md.update(b);
112
if (b.hasRemaining()) {
113
throw new Exception("Buffer not consumed");
114
}
115
return md.digest();
116
}
117
}
118
119