8150512: Update test for jdk.security.provider.preferred security property.
authorssahoo
Mon, 21 Mar 2016 11:54:23 -0700
changeset 36647 26bac33028c6
parent 36646 de0663d2d82e
child 36648 6b77fc70dabf
8150512: Update test for jdk.security.provider.preferred security property. Reviewed-by: ascarpino
jdk/test/sun/security/jca/PreferredProviderNegativeTest.java
jdk/test/sun/security/jca/PreferredProviderTest.java
--- a/jdk/test/sun/security/jca/PreferredProviderNegativeTest.java	Mon Mar 21 14:24:11 2016 -0400
+++ b/jdk/test/sun/security/jca/PreferredProviderNegativeTest.java	Mon Mar 21 11:54:23 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,55 +21,52 @@
  * questions.
  */
 
-/**
- * @test
- * @bug 8076359 8133151
- * @summary Test for jdk.security.provider.preferred security property
- * @requires os.name == "SunOS"
- * @run main/othervm  PreferredProviderNegativeTest preJCESet AES:OracleUcrypto false
- * @run main/othervm  PreferredProviderNegativeTest preJCESet AES:SunNegative true
- * @run main/othervm  PreferredProviderNegativeTest afterJCESet AES:SunJGSS
- * @run main/othervm  PreferredProviderNegativeTest afterJCESet AES:SunECNegative
- * @run main/othervm  PreferredProviderNegativeTest invalidAlg AESNegative:SunJCE
- */
-
 import java.security.Security;
 import java.security.NoSuchAlgorithmException;
 import javax.crypto.Cipher;
 import javax.crypto.NoSuchPaddingException;
 
+/**
+ * @test
+ * @bug 8076359 8133151 8150512
+ * @summary Test for jdk.security.provider.preferred security property
+ * @run main/othervm  PreferredProviderNegativeTest preSet AES false
+ * @run main/othervm  PreferredProviderNegativeTest preSet AES:SunNegative true
+ * @run main/othervm  PreferredProviderNegativeTest afterSet AES:SunJGSS
+ * @run main/othervm  PreferredProviderNegativeTest afterSet AES:SunECNegative
+ * @run main/othervm  PreferredProviderNegativeTest invalidAlg AESInvalid:SunJCE
+ */
 public class PreferredProviderNegativeTest {
 
+    static final String SEC_PREF_PROP = "jdk.security.provider.preferred";
+
     /*
      * Test security property could be set by valid and invalid provider
      * before JCE was loaded
      */
     public static void preJCESet(String value, boolean negativeProvider)
             throws NoSuchAlgorithmException, NoSuchPaddingException {
-        Security.setProperty("jdk.security.provider.preferred", value);
+
+        Security.setProperty(SEC_PREF_PROP, value);
 
-        if (!Security.getProperty("jdk.security.provider.preferred")
-                .equals(value)) {
-            throw new RuntimeException(
-                    "Test Failed:The property wasn't set");
+        if (!Security.getProperty(SEC_PREF_PROP).equals(value)) {
+            throw new RuntimeException("Test Failed:The property wasn't set");
         }
 
         String[] arrays = value.split(":");
         Cipher cipher = Cipher.getInstance(arrays[0]);
-
         if (negativeProvider) {
             if (cipher.getProvider().getName().equals(arrays[1])) {
                 throw new RuntimeException(
-                        "Test Failed:The provider shouldn't be set");
+                        "Test Failed:The provider shouldn't be set.");
             }
         } else {
             if (!cipher.getProvider().getName().equals(arrays[1])) {
-                throw new RuntimeException(
-                        "Test Faild:The provider could be set "
-                                + "by valid provider ");
+                throw new RuntimeException("Test Faild:The provider could be "
+                        + "set by valid provider.");
             }
         }
-        System.out.println("Test Pass");
+        System.out.println("Test Pass.");
     }
 
     /*
@@ -81,10 +78,10 @@
         String[] arrays = value.split(":");
         Cipher cipher = Cipher.getInstance(arrays[0]);
 
-        Security.setProperty("jdk.security.provider.preferred", value);
+        Security.setProperty(SEC_PREF_PROP, value);
         if (!cipher.getProvider().getName().equals("SunJCE")) {
-            throw new RuntimeException(
-                    "Test Failed:The security property can't be updated after JCE load.");
+            throw new RuntimeException("Test Failed:The security property can't"
+                    + " be updated after JCE load.");
         }
         System.out.println("Test Pass");
     }
@@ -94,10 +91,11 @@
         String[] arrays = value.split(":");
 
         try {
-            Security.setProperty("jdk.security.provider.preferred", value);
+            Security.setProperty(SEC_PREF_PROP, value);
             Cipher.getInstance(arrays[0]);
         } catch (NoSuchAlgorithmException e) {
-            System.out.println("Test Pass:Got NoSuchAlgorithmException as expired");
+            System.out.println(
+                    "Test Pass:Got NoSuchAlgorithmException as expired");
             return;
         }
         throw new RuntimeException(
@@ -106,15 +104,25 @@
 
     public static void main(String[] args)
             throws NoSuchAlgorithmException, NoSuchPaddingException {
-        boolean negativeProvider;
 
         if (args.length >= 2) {
             switch (args[0]) {
-                case "preJCESet":
-                    negativeProvider = Boolean.valueOf(args[2]);
-                    PreferredProviderNegativeTest.preJCESet(args[1], negativeProvider);
+                case "preSet":
+                    boolean negativeProvider = Boolean.valueOf(args[2]);
+                    boolean solaris = System.getProperty("os.name")
+                            .toLowerCase().contains("sun");
+                    String value = args[1];
+                    if (args[1].split(":").length < 2) {
+                        if (solaris) {
+                            value += ":OracleUcrypto";
+                        } else {
+                            value += ":SunJCE";
+                        }
+                    }
+                    PreferredProviderNegativeTest.preJCESet(
+                            value, negativeProvider);
                     break;
-                case "afterJCESet":
+                case "afterSet":
                     PreferredProviderNegativeTest.afterJCESet(args[1]);
                     break;
                 case "invalidAlg":
@@ -127,4 +135,3 @@
         }
     }
 }
-
--- a/jdk/test/sun/security/jca/PreferredProviderTest.java	Mon Mar 21 14:24:11 2016 -0400
+++ b/jdk/test/sun/security/jca/PreferredProviderTest.java	Mon Mar 21 11:54:23 2016 -0700
@@ -21,97 +21,131 @@
  * questions.
  */
 
-/**
- * @test
- * @bug 8076359 8133151 8145344
- * @summary Test the value for new jdk.security.provider.preferred security property
- * @requires os.name == "SunOS"
- */
-
+import java.security.MessageDigest;
 import java.security.KeyFactory;
-import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.Security;
+import java.security.Provider;
 import java.util.Arrays;
 import java.util.List;
 import javax.crypto.Cipher;
 import javax.crypto.NoSuchPaddingException;
 
+/**
+ * @test
+ * @bug 8076359 8133151 8145344 8150512
+ * @summary Test the value for new jdk.security.provider.preferred
+ *          security property
+ */
 public class PreferredProviderTest {
 
-    private static final List<DataTuple> SPARC_DATA = Arrays.asList(
-            new DataTuple("SHA1", "SUN"), new DataTuple("SHA-1", "SUN"),
-            new DataTuple("SHA-224", "SUN"), new DataTuple("SHA-256", "SUN"),
-            new DataTuple("SHA-384", "SUN"), new DataTuple("SHA-512", "SUN"));
-    private static final List<DataTuple> X86_DATA = Arrays
-            .asList(new DataTuple("RSA", "SunRsaSign"));
-
-    public void RunTest(String type)
+    public void RunTest(String type, String os)
             throws NoSuchAlgorithmException, NoSuchPaddingException {
-        String preferredProvider = Security
-                .getProperty("jdk.security.provider.preferred");
+
         String actualProvider = null;
-        if (type.equals("sparcv9")) {
-            if (!preferredProvider.equals(
-                    "AES:SunJCE, SHA1:SUN, SHA-224:SUN, SHA-256:SUN, SHA-384:SUN, SHA-512:SUN")) {
-                throw new RuntimeException(
-                        "Test Failed: wrong jdk.security.provider.preferred "
-                                + "value on solaris-sparcv9");
+        boolean solaris = os.contains("sun");
+        String preferredProp
+                = "AES/GCM/NoPadding:SunJCE, MessageDigest.SHA-256:SUN";
+        System.out.printf("%nExecuting test for the platform '%s'%n", os);
+        if (!solaris) {
+            //For other platform it will try to set the preferred algorithm and
+            //Provider and verify the usage of it.
+            Security.setProperty(
+                    "jdk.security.provider.preferred", preferredProp);
+            verifyPreferredProviderProperty(os, type, preferredProp);
+
+            verifyDigestProvider(os, type, Arrays.asList(
+                    new DataTuple("SHA-256", "SUN")));
+        } else {
+            //For solaris the preferred algorithm/provider is already set in
+            //java.security file which will be verified.
+            switch (type) {
+                case "sparcv9":
+                    preferredProp = "AES:SunJCE, SHA1:SUN, SHA-224:SUN,"
+                            + " SHA-256:SUN, SHA-384:SUN, SHA-512:SUN";
+                    verifyPreferredProviderProperty(os, type, preferredProp);
+
+                    verifyDigestProvider(os, type, Arrays.asList(
+                            new DataTuple("SHA1", "SUN"),
+                            new DataTuple("SHA-1", "SUN"),
+                            new DataTuple("SHA-224", "SUN"),
+                            new DataTuple("SHA-256", "SUN"),
+                            new DataTuple("SHA-384", "SUN"),
+                            new DataTuple("SHA-512", "SUN")));
+                    break;
+                case "amd64":
+                    preferredProp = "AES:SunJCE, RSA:SunRsaSign";
+                    verifyPreferredProviderProperty(os, type, preferredProp);
+
+                    verifyKeyFactoryProvider(os, type, Arrays.asList(
+                            new DataTuple("RSA", "SunRsaSign")));
+                    break;
             }
-            for (DataTuple dataTuple : SPARC_DATA) {
-                MessageDigest md = MessageDigest
-                        .getInstance(dataTuple.algorithm);
-                actualProvider = md.getProvider().getName();
-                if (!actualProvider.equals(dataTuple.provider)) {
-                    throw new RuntimeException(String.format(
-                            "Test Failed:Got wrong "
-                                    + "provider from Solaris-sparcv9 platform,"
-                                    + "Expected Provider: %s, Returned Provider: %s",
-                            dataTuple.provider, actualProvider));
-                }
-            }
-        } else if (type.equals("amd64")) {
-            if (!preferredProvider.equals("AES:SunJCE, RSA:SunRsaSign")) {
-                throw new RuntimeException(
-                        "Test Failed: wrong jdk.security.provider.preferred "
-                                + "value on solaris-x86");
-            }
-            for (DataTuple dataTuple : X86_DATA) {
-                KeyFactory keyFactory = KeyFactory
-                        .getInstance(dataTuple.algorithm);
-                actualProvider = keyFactory.getProvider().getName();
-                if (!actualProvider.equals(dataTuple.provider)) {
-                    throw new RuntimeException(String.format(
-                            "Test Failed:Got wrong "
-                                    + "provider from Solaris-x86 platform,"
-                                    + "Expected Provider: %s, Returned Provider: %s",
-                            dataTuple.provider, actualProvider));
-                }
-            }
-        } else {
-            throw new RuntimeException("Test Failed: wrong platform value");
+            verifyDigestProvider(os, type, Arrays.asList(
+                    new DataTuple("MD5", "OracleUcrypto")));
         }
 
         Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
         actualProvider = cipher.getProvider().getName();
         if (!actualProvider.equals("SunJCE")) {
+            throw new RuntimeException(String.format("Test Failed:Got wrong "
+                    + "provider from %s-%s platform, Expected Provider: SunJCE,"
+                    + " Returned Provider: %s", os, type, actualProvider));
+        }
+    }
+
+    private static void verifyPreferredProviderProperty(String os, String arch,
+            String preferred) {
+        String preferredProvider
+                = Security.getProperty("jdk.security.provider.preferred");
+        if (!preferredProvider.equals(preferred)) {
             throw new RuntimeException(String.format(
-                    "Test Failed:Got wrong provider from Solaris-%s platform, "
-                            + "Expected Provider: SunJCE, Returned Provider: %s",
-                    type, actualProvider));
+                    "Test Failed: wrong jdk.security.provider.preferred value "
+                    + "on %s-%s", os, arch));
         }
+        System.out.println(
+                "Preferred provider security property verification complete.");
+    }
 
-        MessageDigest md = MessageDigest.getInstance("MD5");
-        actualProvider = md.getProvider().getName();
-        if (!actualProvider.equals("OracleUcrypto")) {
+    private static void verifyDigestProvider(String os, String arch,
+            List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
+        for (DataTuple dataTuple : algoProviders) {
+            System.out.printf(
+                    "Verifying MessageDigest for '%s'%n", dataTuple.algorithm);
+            MessageDigest md = MessageDigest.getInstance(dataTuple.algorithm);
+            matchProvider(md.getProvider(), dataTuple.provider,
+                    dataTuple.algorithm, os, arch);
+        }
+        System.out.println(
+                "Preferred MessageDigest algorithm verification successful.");
+    }
+
+    private static void verifyKeyFactoryProvider(String os, String arch,
+            List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
+        for (DataTuple dataTuple : algoProviders) {
+            System.out.printf(
+                    "Verifying KeyFactory for '%s'%n", dataTuple.algorithm);
+            KeyFactory kf = KeyFactory.getInstance(dataTuple.algorithm);
+            matchProvider(kf.getProvider(), dataTuple.provider,
+                    dataTuple.algorithm, os, arch);
+        }
+        System.out.println(
+                "Preferred KeyFactory algorithm verification successful.");
+    }
+
+    private static void matchProvider(Provider provider, String expected,
+            String algo, String os, String arch) {
+        if (!provider.getName().equals(expected)) {
             throw new RuntimeException(String.format(
-                    "Test Failed:Got wrong provider from Solaris-%s platform,"
-                            + "Expected Provider: OracleUcrypto, Returned Provider: %s",
-                    type, actualProvider));
+                    "Test Failed:Got wrong provider from %s-%s platform, "
+                    + "for algorithm %s. Expected Provider: %s,"
+                    + " Returned Provider: %s", os, arch, algo,
+                    expected, provider.getName()));
         }
     }
 
     private static class DataTuple {
+
         private final String provider;
         private final String algorithm;
 
@@ -123,10 +157,9 @@
 
     public static void main(String[] args)
             throws NoSuchAlgorithmException, NoSuchPaddingException {
-
-        String arch = System.getProperty("os.arch");
+        String os = System.getProperty("os.name").toLowerCase();
+        String arch = System.getProperty("os.arch").toLowerCase();
         PreferredProviderTest pp = new PreferredProviderTest();
-        pp.RunTest(arch);
+        pp.RunTest(arch, os);
     }
 }
-