# HG changeset patch
# User Tobias Wagner <tobias.wagner@n-design.de>
# Date 1520026086 -3600
#      Fri Mar 02 22:28:06 2018 +0100
# Node ID 50b1abbd6d5c52d19cf12e1c68fb38aec831bab3
# Parent  c6041452e86b24359881f6b9085c0bb22d2c448c
Additional PKCS#11 test with SoftHSM2

diff --git a/test/jdk/sun/security/pkcs11/PKCS11Test.java b/test/jdk/sun/security/pkcs11/PKCS11Test.java
--- a/test/jdk/sun/security/pkcs11/PKCS11Test.java
+++ b/test/jdk/sun/security/pkcs11/PKCS11Test.java
@@ -33,6 +33,7 @@
 import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.security.AlgorithmParameters;
+import java.security.AuthProvider;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.KeyPairGenerator;
 import java.security.NoSuchProviderException;
@@ -53,6 +54,12 @@
 import java.util.ServiceLoader;
 import java.util.Set;
 
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.PasswordCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+
 public abstract class PKCS11Test {
 
     private boolean enableSM = false;
@@ -203,6 +210,7 @@
             testDefault(test);
             testNSS(test);
             testDeimos(test);
+            testSoftHSM2(test);
         } finally {
             // NOTE: Do not place a 'return' in any finally block
             // as it will suppress exceptions and hide test failures.
@@ -241,6 +249,33 @@
         test.premain(p);
     }
 
+    public static void testSoftHSM2(PKCS11Test test) throws Exception {
+        if (new File("/usr/local/lib/softhsm/libsofthsm2.so").isFile() == false ||
+            "true".equals(System.getProperty("NO_SOFTHSM2"))) {
+            return;
+        }
+        String base = getBase();
+        String p11config = base + SEP + "nss" + SEP + "p11-softhsm2.txt";
+        AuthProvider p = (AuthProvider) getSunPKCS11(p11config);
+        p.login(null, new CallbackHandler(){
+        
+            @Override
+            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+                for(Callback cb : callbacks) {
+                    if(cb instanceof PasswordCallback) {
+                        PasswordCallback pcb = (PasswordCallback) cb;
+                        pcb.setPassword("123456".toCharArray());
+                    } else {
+                        String msg = cb.getClass().getCanonicalName() + " callbacks are not supported.";
+                        System.out.println(msg);
+                        throw new UnsupportedCallbackException(cb, msg);
+                    }
+                }
+            }
+        });
+        test.premain(p);
+    }
+
     public static void testDefault(PKCS11Test test) throws Exception {
         // run test for default configured PKCS11 providers (if any)
 
diff --git a/test/jdk/sun/security/pkcs11/nss/p11-softhsm2.txt b/test/jdk/sun/security/pkcs11/nss/p11-softhsm2.txt
new file mode 100644
--- /dev/null
+++ b/test/jdk/sun/security/pkcs11/nss/p11-softhsm2.txt
@@ -0,0 +1,15 @@
+#
+#
+#
+# Configuration to run unit tests with the SoftHSM2 software
+#
+
+name = SoftHSM2
+
+#showInfo = true
+
+library = /usr/local/lib/softhsm/libsofthsm2.so
+
+attributes = compatibility
+
+slotListIndex = 0
\ No newline at end of file
