author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
child 51840 | dc15e45122b2 |
permissions | -rw-r--r-- |
38423
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
1 |
/* |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
4 |
* |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
8 |
* |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
13 |
* accompanied this code). |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
14 |
* |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
18 |
* |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
21 |
* questions. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
22 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
23 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
24 |
/* |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
25 |
* @test |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
26 |
* @bug 8141039 |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
27 |
* @library /lib/testlibrary |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
28 |
* @summary SecureRandom supports multiple getInstance method including |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
29 |
* getInstanceStrong() method. This test verifies a set of possible |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
30 |
* cases for getInstance with different SecureRandom mechanism |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
31 |
* supported in Java. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
32 |
* @run main GetInstanceTest |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
33 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
34 |
import java.security.NoSuchAlgorithmException; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
35 |
import java.security.NoSuchProviderException; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
36 |
import java.security.SecureRandom; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
37 |
import java.security.SecureRandomParameters; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
38 |
import java.security.DrbgParameters; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
39 |
import static java.security.DrbgParameters.Capability.*; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
40 |
import java.security.Security; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
41 |
import java.util.Arrays; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
42 |
import jdk.testlibrary.Asserts; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
43 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
44 |
public class GetInstanceTest { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
45 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
46 |
private static final boolean PASS = true; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
47 |
private static final String INVALID_ALGO = "INVALID"; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
48 |
private static final String SUN_PROVIDER = "SUN"; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
49 |
private static final String INVALID_PROVIDER = "INVALID"; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
50 |
private static final String STRONG_ALG_SEC_PROP |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
51 |
= "securerandom.strongAlgorithms"; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
52 |
private static final String DRBG_CONFIG = "securerandom.drbg.config"; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
53 |
private static final String DRBG_CONFIG_VALUE |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
54 |
= Security.getProperty(DRBG_CONFIG); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
55 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
56 |
public static void main(String[] args) throws Exception { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
57 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
58 |
boolean success = true; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
59 |
// Only accepted failure is NoSuchAlgorithmException. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
60 |
// For any other failure the test case will fail here. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
61 |
SecureRandom sr = matchExc(() -> SecureRandom.getInstanceStrong(), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
62 |
PASS, NoSuchAlgorithmException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
63 |
"PASS - Undefined security Property " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
64 |
+ "'securerandom.strongAlgorithms'"); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
65 |
System.out.format("Current platform supports mechanism: '%s' through " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
66 |
+ "provider: '%s' for the method getInstanceStrong().", |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
67 |
sr.getAlgorithm(), sr.getProvider().getName()); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
68 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
69 |
// DRBG name should appear with "securerandom.strongAlgorithms" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
70 |
// security property. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
71 |
String origDRBGConfig = Security.getProperty(STRONG_ALG_SEC_PROP); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
72 |
if (!origDRBGConfig.contains("DRBG")) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
73 |
throw new RuntimeException("DRBG is not associated with default " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
74 |
+ "strong algorithm through security Property: " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
75 |
+ "'securerandom.strongAlgorithms'."); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
76 |
} |
38447
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
77 |
try { |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
78 |
Security.setProperty(STRONG_ALG_SEC_PROP, "DRBG:SUN"); |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
79 |
sr = matchExc(() -> SecureRandom.getInstanceStrong(), |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
80 |
PASS, NoSuchAlgorithmException.class, |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
81 |
"PASS - Undefined security Property " |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
82 |
+ "'securerandom.strongAlgorithms'"); |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
83 |
checkAttributes(sr, "DRBG"); |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
84 |
} finally { |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
85 |
Security.setProperty(STRONG_ALG_SEC_PROP, origDRBGConfig); |
435b76ccc9e3
8157417: Some of SecureRandom test might get timed out in linux
ssahoo
parents:
38423
diff
changeset
|
86 |
} |
38423
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
87 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
88 |
for (String mech : new String[]{ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
89 |
"SHA1PRNG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG", INVALID_ALGO,}) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
90 |
System.out.printf("%nTest SecureRandom mechanism: '%s'", mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
91 |
try { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
92 |
if (isDRBG(mech)) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
93 |
Security.setProperty(DRBG_CONFIG, mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
94 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
95 |
verifyInstance(mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
96 |
} catch (Exception e) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
97 |
e.printStackTrace(System.out); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
98 |
success = false; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
99 |
} finally { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
100 |
Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
101 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
102 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
103 |
if (!success) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
104 |
throw new RuntimeException("At least one test failed."); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
105 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
106 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
107 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
108 |
private static void verifyInstance(String mech) throws Exception { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
109 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
110 |
String srAlgo = isDRBG(mech) ? "DRBG" : mech; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
111 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
112 |
// Test for getInstance(algorithm) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
113 |
// It should pass for all case other than invalid algorithm name. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
114 |
// If it fails then the expected exception type should be |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
115 |
// NoSuchAlgorithmException. Any other Exception type occured will be |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
116 |
// treated as failure. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
117 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
118 |
matchExc(() -> SecureRandom.getInstance(srAlgo), !(nsa(mech)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
119 |
NoSuchAlgorithmException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
120 |
String.format("PASS - It is expected to fail for" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
121 |
+ " getInstance(algorithm) when algorithm: '%s'" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
122 |
+ " is null or invalid.", mech)), mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
123 |
// Test for getInstance(algorithm, provider) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
124 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
125 |
matchExc(() -> SecureRandom.getInstance(srAlgo, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
126 |
Security.getProvider(SUN_PROVIDER)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
127 |
!(nsa(mech)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
128 |
NoSuchAlgorithmException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
129 |
String.format("PASS - It is expected to fail for" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
130 |
+ " getInstance(algorithm, provider) when" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
131 |
+ " algorithm:'%s' is null or invalid.", mech)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
132 |
mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
133 |
// Test for getInstance(algorithm, providerName) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
134 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
135 |
matchExc(() -> SecureRandom.getInstance(srAlgo, SUN_PROVIDER), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
136 |
!(nsa(mech)), NoSuchAlgorithmException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
137 |
String.format("PASS - It is expected to fail for " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
138 |
+ "getInstance(algorithm, providerName) when " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
139 |
+ "algorithm: '%s' is null or invalid.", mech)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
140 |
mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
141 |
// Test for getInstance(algorithm, providerName) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
142 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
143 |
matchExc(() -> SecureRandom.getInstance( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
144 |
srAlgo, INVALID_PROVIDER), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
145 |
!PASS, NoSuchProviderException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
146 |
String.format("PASS - It is expected to fail for " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
147 |
+ "getInstance(algorithm, providerName) when " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
148 |
+ "provider name: '%s' is invalid and " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
149 |
+ "algorithm: '%s'", INVALID_PROVIDER, mech)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
150 |
mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
151 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
152 |
// Run the test for a set of SecureRandomParameters |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
153 |
for (SecureRandomParameters param : Arrays.asList(null, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
154 |
DrbgParameters.instantiation(-1, NONE, null))) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
155 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
156 |
System.out.printf("%nRunning DRBG param getInstance() methods " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
157 |
+ "for algorithm: %s and DRBG param type: %s", mech, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
158 |
(param != null) ? param.getClass().getName() : param); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
159 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
160 |
// Following Test are applicable for new DRBG methods only. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
161 |
// Test for getInstance(algorithm, params) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
162 |
// Tests are expected to pass for DRBG type with valid parameter |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
163 |
// If it fails the expected exception type is derived from |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
164 |
// getExcType(mech, param) method. If exception type is not |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
165 |
// expected then the test will be considered as failure. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
166 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
167 |
matchExc(() -> SecureRandom.getInstance(srAlgo, param), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
168 |
(isDRBG(mech)) && (isValidDRBGParam(param)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
169 |
getExcType(mech, param), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
170 |
String.format("PASS - It is expected to fail " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
171 |
+ "for getInstance(algorithm, params) " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
172 |
+ "for algorithm: %s and parameter: %s", |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
173 |
mech, param)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
174 |
mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
175 |
// Test for getInstance(algorithm, params, provider) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
176 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
177 |
matchExc(() -> SecureRandom.getInstance(srAlgo, param, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
178 |
Security.getProvider(SUN_PROVIDER)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
179 |
(isDRBG(mech)) && (isValidDRBGParam(param)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
180 |
getExcType(mech, param), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
181 |
String.format("PASS - It is expected to fail " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
182 |
+ "for getInstance(algorithm, params, " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
183 |
+ "provider) for algorithm: %s and " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
184 |
+ "parameter: %s", mech, param)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
185 |
mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
186 |
// Test for getInstance(algorithm, params, providerName) method. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
187 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
188 |
matchExc(() -> SecureRandom.getInstance(srAlgo, param, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
189 |
SUN_PROVIDER), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
190 |
(isDRBG(mech)) && (isValidDRBGParam(param)), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
191 |
getExcType(mech, param), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
192 |
String.format("PASS - It is expected to fail " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
193 |
+ "for getInstance(algorithm, params, " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
194 |
+ "providerName) for algorithm: %s and " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
195 |
+ "parameter: %s", mech, param)), mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
196 |
// getInstance(algorithm, params, providerName) when |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
197 |
// providerName is invalid |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
198 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
199 |
matchExc(() -> SecureRandom.getInstance(srAlgo, param, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
200 |
INVALID_PROVIDER), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
201 |
!PASS, ((param == null) |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
202 |
? IllegalArgumentException.class |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
203 |
: NoSuchProviderException.class), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
204 |
String.format("PASS - It is expected to fail " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
205 |
+ "for getInstance(algorithm, params, " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
206 |
+ "providerName) when param is null or" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
207 |
+ " provider: %s is invalid for " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
208 |
+ "algorithm: '%s'", INVALID_PROVIDER, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
209 |
mech)), mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
210 |
// getInstance(algorithm, params, provider) when provider=null |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
211 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
212 |
matchExc(() -> SecureRandom.getInstance(srAlgo, param, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
213 |
(String) null), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
214 |
!PASS, IllegalArgumentException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
215 |
String.format("PASS - It is expected to fail " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
216 |
+ "for getInstance(algorithm, params, " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
217 |
+ "providerName) when provider name " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
218 |
+ "is null")), mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
219 |
// getInstance(algorithm, params, providerName) when |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
220 |
// providerName is empty. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
221 |
checkAttributes( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
222 |
matchExc(() -> SecureRandom.getInstance( |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
223 |
srAlgo, param, ""), |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
224 |
!PASS, IllegalArgumentException.class, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
225 |
String.format("PASS - It is expected to fail " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
226 |
+ "for getInstance(algorithm, params, " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
227 |
+ "providerName) when provider name " |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
228 |
+ "is empty")), mech); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
229 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
230 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
231 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
232 |
private static boolean isValidDRBGParam(SecureRandomParameters param) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
233 |
return (param instanceof DrbgParameters.Instantiation); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
234 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
235 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
236 |
/** |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
237 |
* If the mechanism should occur NoSuchAlgorithmException. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
238 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
239 |
private static boolean nsa(String mech) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
240 |
return mech.equals(INVALID_ALGO); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
241 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
242 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
243 |
/** |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
244 |
* Verify if the mechanism is DRBG type. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
245 |
* @param mech Mechanism name |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
246 |
* @return True if the mechanism name is DRBG type else False. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
247 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
248 |
private static boolean isDRBG(String mech) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
249 |
return mech.contains("_DRBG"); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
250 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
251 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
252 |
/** |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
253 |
* Type of exception expected for a SecureRandom instance when exception |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
254 |
* occurred while calling getInstance method with a fixed set of parameter. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
255 |
* @param mech Mechanism used to create a SecureRandom instance |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
256 |
* @param param Parameter to getInstance() method |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
257 |
* @return Exception type expected |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
258 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
259 |
private static Class getExcType(String mech, SecureRandomParameters param) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
260 |
return ((isDRBG(mech) && !isValidDRBGParam(param)) || param == null) |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
261 |
? IllegalArgumentException.class |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
262 |
: NoSuchAlgorithmException.class; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
263 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
264 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
265 |
private interface RunnableCode { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
266 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
267 |
SecureRandom run() throws Exception; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
268 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
269 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
270 |
/** |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
271 |
* Execute a given code block and verify, if the exception type is expected. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
272 |
* @param r Code block to run |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
273 |
* @param ex Expected exception type |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
274 |
* @param shouldPass If the code execution expected to pass without failure |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
275 |
* @param msg Message to log in case of expected failure |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
276 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
277 |
private static SecureRandom matchExc(RunnableCode r, boolean shouldPass, |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
278 |
Class ex, String msg) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
279 |
SecureRandom sr = null; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
280 |
try { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
281 |
sr = r.run(); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
282 |
if (!shouldPass) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
283 |
throw new RuntimeException("Excecution should fail here."); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
284 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
285 |
} catch (Exception e) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
286 |
System.out.printf("%nOccured exception: %s - Expected exception: %s" |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
287 |
+ " : ", e.getClass(), ex.getCanonicalName()); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
288 |
if (ex.isAssignableFrom(e.getClass())) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
289 |
System.out.printf("%n%s : Expected Exception: %s : ", |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
290 |
e.getClass(), msg); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
291 |
} else if (shouldPass) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
292 |
throw new RuntimeException(e); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
293 |
} else { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
294 |
System.out.printf("%nIgnore the following exception: %s%n", |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
295 |
e.getMessage()); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
296 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
297 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
298 |
return sr; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
299 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
300 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
301 |
/** |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
302 |
* Check specific attributes of a SecureRandom instance. |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
303 |
*/ |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
304 |
private static void checkAttributes(SecureRandom sr, String mech) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
305 |
if (sr == null) { |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
306 |
return; |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
307 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
308 |
Asserts.assertEquals(sr.getAlgorithm(), (isDRBG(mech) ? "DRBG" : mech)); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
309 |
Asserts.assertEquals(sr.getProvider().getName(), SUN_PROVIDER); |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
310 |
} |
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
311 |
|
5d6ff7d76bf4
8141039: Test Task: Develop new tests for JEP 273: DRBG-Based SecureRandom Implementations
ssahoo
parents:
diff
changeset
|
312 |
} |