Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/MessageDigest/ByteBuffers.java
41152 views
1
/*
2
* Copyright (c) 2003, 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 4844847
27
* @summary Test the MessageDigest.update(ByteBuffer) method
28
* @author Andreas Sterbenz
29
* @key randomness
30
*/
31
32
import java.util.*;
33
import java.nio.*;
34
35
import java.security.*;
36
37
public class ByteBuffers {
38
39
public static void main(String[] args) throws Exception {
40
Provider p = Security.getProvider("SUN");
41
Random random = new Random();
42
int n = 10 * 1024;
43
byte[] t = new byte[n];
44
random.nextBytes(t);
45
46
MessageDigest md = MessageDigest.getInstance("MD5", p);
47
byte[] d1 = md.digest(t);
48
49
// test 1: ByteBuffer with an accessible backing array
50
ByteBuffer b1 = ByteBuffer.allocate(n + 256);
51
b1.position(random.nextInt(256));
52
b1.limit(b1.position() + n);
53
ByteBuffer b2 = b1.slice();
54
b2.put(t);
55
b2.clear();
56
byte[] d2 = digest(md, b2, random);
57
if (Arrays.equals(d1, d2) == false) {
58
throw new Exception("Test 1 failed");
59
}
60
61
// test 2: direct ByteBuffer
62
ByteBuffer b3 = ByteBuffer.allocateDirect(t.length);
63
b3.put(t);
64
b3.clear();
65
byte[] d3 = digest(md, b3, random);
66
if (Arrays.equals(d1, d3) == false) {
67
throw new Exception("Test 2 failed");
68
}
69
70
// test 3: ByteBuffer without an accessible backing array
71
b2.clear();
72
ByteBuffer b4 = b2.asReadOnlyBuffer();
73
byte[] d4 = digest(md, b4, random);
74
if (Arrays.equals(d1, d4) == false) {
75
throw new Exception("Test 3 failed");
76
}
77
System.out.println("All tests passed");
78
}
79
80
private static byte[] digest(MessageDigest md, ByteBuffer b, Random random) throws Exception {
81
int lim = b.limit();
82
b.limit(random.nextInt(lim));
83
md.update(b);
84
if (b.hasRemaining()) {
85
throw new Exception("Buffer not consumed");
86
}
87
b.limit(lim);
88
md.update(b);
89
if (b.hasRemaining()) {
90
throw new Exception("Buffer not consumed");
91
}
92
return md.digest();
93
}
94
}
95
96