8076359: Test Task: Develop new tests for Leverage CPU Instructions for GHASH and RSA
authorasmotrak
Wed, 11 Nov 2015 10:42:20 +0300
changeset 33673 ab8e6d8e3ab9
parent 33672 971eeb07166a
child 33674 566777f73c32
8076359: Test Task: Develop new tests for Leverage CPU Instructions for GHASH and RSA Reviewed-by: mullan, ascarpino Contributed-by: tiantian.du@oracle.com
jdk/test/sun/security/jca/PreferredProviderNegativeTest.java
jdk/test/sun/security/jca/PreferredProviderTest.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/jca/PreferredProviderNegativeTest.java	Wed Nov 11 10:42:20 2015 +0300
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2015, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * 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;
+
+public class PreferredProviderNegativeTest {
+
+    /*
+     * 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);
+
+        if (!Security.getProperty("jdk.security.provider.preferred")
+                .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");
+            }
+        } else {
+            if (!cipher.getProvider().getName().equals(arrays[1])) {
+                throw new RuntimeException(
+                        "Test Faild:The provider could be set "
+                                + "by valid provider ");
+            }
+        }
+        System.out.println("Test Pass");
+    }
+
+    /*
+     * Test that the setting of the security property after Cipher.getInstance()
+     * does not influence previously loaded instances
+     */
+    public static void afterJCESet(String value)
+            throws NoSuchAlgorithmException, NoSuchPaddingException {
+        String[] arrays = value.split(":");
+        Cipher cipher = Cipher.getInstance(arrays[0]);
+
+        Security.setProperty("jdk.security.provider.preferred", value);
+        if (!cipher.getProvider().getName().equals("SunJCE")) {
+            throw new RuntimeException(
+                    "Test Failed:The security property can't be updated after JCE load.");
+        }
+        System.out.println("Test Pass");
+    }
+
+    /* Test the security property with negative algorithm */
+    public static void invalidAlg(String value) throws NoSuchPaddingException {
+        String[] arrays = value.split(":");
+
+        try {
+            Security.setProperty("jdk.security.provider.preferred", value);
+            Cipher.getInstance(arrays[0]);
+        } catch (NoSuchAlgorithmException e) {
+            System.out.println("Test Pass:Got NoSuchAlgorithmException as expired");
+            return;
+        }
+        throw new RuntimeException(
+                "Test Failed:Expected NoSuchAlgorithmException was not thrown");
+    }
+
+    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);
+                    break;
+                case "afterJCESet":
+                    PreferredProviderNegativeTest.afterJCESet(args[1]);
+                    break;
+                case "invalidAlg":
+                    PreferredProviderNegativeTest.invalidAlg(args[1]);
+                    break;
+            }
+        } else {
+            throw new RuntimeException(
+                    "Test Failed:Please pass the correct args");
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/jca/PreferredProviderTest.java	Wed Nov 11 10:42:20 2015 +0300
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2015, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8076359 8133151
+ * @summary Test the value for new jdk.security.provider.preferred security property
+ * @requires os.name == "SunOS"
+ */
+
+import java.security.KeyFactory;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.Security;
+import java.util.Arrays;
+import java.util.List;
+import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
+
+public class PreferredProviderTest {
+
+    private static final List<DataTuple> SPARC_DATA = Arrays.asList(
+            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)
+            throws NoSuchAlgorithmException, NoSuchPaddingException {
+        String preferredProvider = Security
+                .getProperty("jdk.security.provider.preferred");
+        String actualProvider = null;
+        if (type.equals("sparcv9")) {
+            if (!preferredProvider.equals(
+                    "AES:SunJCE, SHA-256:SUN, SHA-384:SUN, SHA-512:SUN")) {
+                throw new RuntimeException(
+                        "Test Failed: wrong jdk.security.provider.preferred "
+                                + "value on solaris-sparcv9");
+            }
+            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");
+        }
+
+        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 Solaris-%s platform, "
+                            + "Expected Provider: SunJCE, Returned Provider: %s",
+                    type, actualProvider));
+        }
+
+        MessageDigest md = MessageDigest.getInstance("MD5");
+        actualProvider = md.getProvider().getName();
+        if (!actualProvider.equals("OracleUcrypto")) {
+            throw new RuntimeException(String.format(
+                    "Test Failed:Got wrong provider from Solaris-%s platform,"
+                            + "Expected Provider: OracleUcrypto, Returned Provider: %s",
+                    type, actualProvider));
+        }
+    }
+
+    private static class DataTuple {
+        private final String provider;
+        private final String algorithm;
+
+        private DataTuple(String algorithm, String provider) {
+            this.algorithm = algorithm;
+            this.provider = provider;
+        }
+    }
+
+    public static void main(String[] args)
+            throws NoSuchAlgorithmException, NoSuchPaddingException {
+
+        String arch = System.getProperty("os.arch");
+        PreferredProviderTest pp = new PreferredProviderTest();
+        pp.RunTest(arch);
+    }
+}
+