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 This test do API coverage for SecureRandom. It covers most of 29 * supported operations along with possible positive and negative 30 * parameters for DRBG mechanism. 31 * @run main ApiTest Hash_DRBG 32 * @run main ApiTest HMAC_DRBG 33 * @run main ApiTest CTR_DRBG 34 * @run main ApiTest SHA1PRNG 35 * @run main ApiTest NATIVE 36 */ 37 import java.security.NoSuchAlgorithmException; 38 import java.security.SecureRandom; 39 import java.security.Security; 40 import java.security.SecureRandomParameters; 41 import java.security.DrbgParameters; 42 import java.security.DrbgParameters.Instantiation; 43 import java.security.DrbgParameters.Capability; 44 import javax.crypto.Cipher; 45 46 public class ApiTest { 47 48 private static final boolean SHOULD_PASS = true; 49 private static final long SEED = 1l; 50 private static final String INVALID_ALGO = "INVALID"; 51 private static final String DRBG_CONFIG = "securerandom.drbg.config"; 52 private static final String DRBG_CONFIG_VALUE 53 = Security.getProperty(DRBG_CONFIG); 54 55 public static void main(String[] args) throws Exception { 56 57 if (args == null || args.length < 1) { 58 throw new RuntimeException("No mechanism available to run test."); 59 } 60 String mech 61 = "NATIVE".equals(args[0]) ? supportedNativeAlgo() : args[0]; 62 String[] algs = null; 63 boolean success = true; 64 65 try { 66 if (!isDRBG(mech)) { 67 SecureRandom random = SecureRandom.getInstance(mech); 68 verifyAPI(random, mech); 69 return; 70 } else if (mech.equals("CTR_DRBG")) { 71 algs = new String[]{"AES-128", "AES-192", "AES-256", 72 INVALID_ALGO}; 73 } else if (mech.equals("Hash_DRBG") || mech.equals("HMAC_DRBG")) { 74 algs = new String[]{"SHA-224", "SHA-256", "SHA-512/224", 75 "SHA-512/256", "SHA-384", "SHA-512", INVALID_ALGO}; | 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 This test do API coverage for SecureRandom. It covers most of 29 * supported operations along with possible positive and negative 30 * parameters for DRBG mechanism. 31 * @run main/othervm ApiTest Hash_DRBG 32 * @run main/othervm ApiTest HMAC_DRBG 33 * @run main/othervm ApiTest CTR_DRBG 34 * @run main/othervm ApiTest SHA1PRNG 35 * @run main/othervm ApiTest NATIVE 36 */ 37 import java.security.NoSuchAlgorithmException; 38 import java.security.SecureRandom; 39 import java.security.Security; 40 import java.security.SecureRandomParameters; 41 import java.security.DrbgParameters; 42 import java.security.DrbgParameters.Instantiation; 43 import java.security.DrbgParameters.Capability; 44 import javax.crypto.Cipher; 45 46 public class ApiTest { 47 48 private static final boolean SHOULD_PASS = true; 49 private static final long SEED = 1l; 50 private static final String INVALID_ALGO = "INVALID"; 51 private static final String DRBG_CONFIG = "securerandom.drbg.config"; 52 private static final String DRBG_CONFIG_VALUE 53 = Security.getProperty(DRBG_CONFIG); 54 55 public static void main(String[] args) throws Exception { 56 System.setProperty("java.security.egd", "file:/dev/urandom"); 57 58 if (args == null || args.length < 1) { 59 throw new RuntimeException("No mechanism available to run test."); 60 } 61 String mech 62 = "NATIVE".equals(args[0]) ? supportedNativeAlgo() : args[0]; 63 String[] algs = null; 64 boolean success = true; 65 66 try { 67 if (!isDRBG(mech)) { 68 SecureRandom random = SecureRandom.getInstance(mech); 69 verifyAPI(random, mech); 70 return; 71 } else if (mech.equals("CTR_DRBG")) { 72 algs = new String[]{"AES-128", "AES-192", "AES-256", 73 INVALID_ALGO}; 74 } else if (mech.equals("Hash_DRBG") || mech.equals("HMAC_DRBG")) { 75 algs = new String[]{"SHA-224", "SHA-256", "SHA-512/224", 76 "SHA-512/256", "SHA-384", "SHA-512", INVALID_ALGO}; |