Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/SecureRandom/EnoughSeedTest.java
41149 views
1
/*
2
* Copyright (c) 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 8141039
27
* @library /lib/testlibrary
28
* @summary Check SecureRandom generate expected seed counts what the caller
29
* asked for.
30
* @run main/othervm EnoughSeedTest
31
*/
32
import java.security.SecureRandom;
33
import java.security.Security;
34
import static java.lang.Math.*;
35
36
public class EnoughSeedTest {
37
38
private static final String DRBG_CONFIG = "securerandom.drbg.config";
39
private static final String DRBG_CONFIG_VALUE
40
= Security.getProperty(DRBG_CONFIG);
41
42
public static void main(String[] args) {
43
System.setProperty("java.security.egd", "file:/dev/urandom");
44
45
boolean success = true;
46
for (String mech : new String[]{
47
"SHA1PRNG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
48
System.out.printf("%nTest for SecureRandom algorithm: '%s'", mech);
49
try {
50
SecureRandom sr = null;
51
if (!mech.contains("_DRBG")) {
52
sr = SecureRandom.getInstance(mech);
53
} else {
54
Security.setProperty(DRBG_CONFIG, mech);
55
sr = SecureRandom.getInstance("DRBG");
56
}
57
58
success &= forEachSeedBytes(sr);
59
System.out.printf("%nCompleted test for SecureRandom "
60
+ "mechanism: '%s'", mech);
61
} catch (Exception e) {
62
success &= false;
63
e.printStackTrace(System.out);
64
} finally {
65
Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);
66
}
67
}
68
if (!success) {
69
throw new RuntimeException("At least one test failed.");
70
}
71
}
72
73
/**
74
* Generates fixed number of seed bytes through a SecureRandom instance
75
* to verify it's seed generation status.
76
* @param sr SecureRandom instance
77
* @return The test success indicator
78
*/
79
private static boolean forEachSeedBytes(SecureRandom sr) {
80
boolean success = true;
81
sr.setSeed(1l);
82
for (int seedByte : new int[]{Integer.MIN_VALUE, -1, 0, 1, 256, 1024,
83
Short.MAX_VALUE, (int) pow(2, 20)}) {
84
try {
85
byte[] seed = sr.generateSeed(seedByte);
86
if (seed.length != seedByte) {
87
throw new RuntimeException("Not able to produce expected "
88
+ "seed size.");
89
}
90
} catch (IllegalArgumentException e) {
91
if (seedByte >= 0) {
92
throw new RuntimeException("Unknown Exception occured.", e);
93
}
94
System.out.printf("%nPASS - Exception expected when required "
95
+ "seed size requested is negative: %s", seedByte);
96
}
97
}
98
return success;
99
}
100
101
}
102
103