11/*
2- * Copyright (c) 2021, 2024 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2021, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24+ /*
25+ * @test
26+ * @bug 8240256 8269034
27+ * @summary
28+ * @library /test/lib/ /sun/security/pkcs11/
29+ * @modules jdk.crypto.cryptoki/sun.security.pkcs11
30+ * @run main/othervm
31+ * -DCUSTOM_P11_CONFIG=${test.src}/MultipleLogins-nss.txt
32+ * -DCUSTOM_DB_DIR=./nss/db
33+ * MultipleLogins
34+ */
2435
2536import sun .security .pkcs11 .SunPKCS11 ;
2637
3041import javax .security .auth .callback .PasswordCallback ;
3142import javax .security .auth .callback .UnsupportedCallbackException ;
3243import javax .security .auth .login .LoginException ;
44+
3345import java .io .IOException ;
3446import java .lang .ref .WeakReference ;
3547import java .security .*;
3648
3749import jdk .test .lib .util .ForceGC ;
3850import jtreg .SkippedException ;
3951
40- public class MultipleLogins {
41- private static final String KS_TYPE = "PKCS11" ;
52+ public class MultipleLogins extends PKCS11Test {
4253 private static final int NUM_PROVIDERS = 20 ;
4354 private static final SunPKCS11 [] providers = new SunPKCS11 [NUM_PROVIDERS ];
4455
4556 public static void main (String [] args ) throws Exception {
46- String nssConfig = null ;
47- try {
48- nssConfig = PKCS11Test .getNssConfig ();
49- } catch (SkippedException exc ) {
50- System .out .println ("Skipping test: " + exc .getMessage ());
51- }
57+ // This bypasses the PKCS11Test settings and run the mandatory
58+ // main method directly. This is needed to keep the custom logic of the test
59+ new MultipleLogins ().main ((Provider )null );
60+ }
61+
62+ @ Override
63+ public void main (Provider p ) throws Exception {
64+ copyNssCertKeyToClassesDir ();
65+
66+ String nssConfig = getNssConfig ();
5267
5368 if (nssConfig == null ) {
5469 // No test framework support yet. Ignore
55- System .out .println ("No NSS config found. Skipping." );
56- return ;
70+ throw new SkippedException ("No NSS config found. Skipping." );
5771 }
5872
59- for (int i =0 ; i < NUM_PROVIDERS ; i ++) {
73+ for (int i = 0 ; i < NUM_PROVIDERS ; i ++) {
6074 // loop to set up test without security manger
61- providers [i ] = (SunPKCS11 )PKCS11Test . newPKCS11Provider ();
75+ providers [i ] = (SunPKCS11 )newPKCS11Provider ();
6276 }
6377
6478 for (int i =0 ; i < NUM_PROVIDERS ; i ++) {
@@ -68,7 +82,7 @@ public static void main(String[] args) throws Exception {
6882 }
6983
7084 WeakReference <SunPKCS11 >[] weakRef = new WeakReference [NUM_PROVIDERS ];
71- for (int i =0 ; i < NUM_PROVIDERS ; i ++) {
85+ for (int i = 0 ; i < NUM_PROVIDERS ; i ++) {
7286 weakRef [i ] = new WeakReference <>(providers [i ]);
7387 providers [i ].logout ();
7488
@@ -95,7 +109,7 @@ public static void main(String[] args) throws Exception {
95109 }
96110
97111 private static void test (SunPKCS11 p ) throws Exception {
98- KeyStore ks = KeyStore .getInstance (KS_TYPE , p );
112+ KeyStore ks = KeyStore .getInstance (PKCS11 , p );
99113 p .setCallbackHandler (new PasswordCallbackHandler ());
100114 try {
101115 ks .load (null , (char []) null );
@@ -111,23 +125,23 @@ private static void test(SunPKCS11 p) throws Exception {
111125 try {
112126 ks .load (null , (char []) null );
113127 } catch (IOException e ) {
114- if (e .getCause () instanceof LoginException &&
115- e .getCause ().getMessage ().contains ("No token present" )) {
116- // expected
117- } else {
128+ if (!(e .getCause () instanceof LoginException ) ||
129+ !(e .getCause ().getMessage ().contains ("No token present" ))) {
130+
118131 throw new RuntimeException ("Token was present" , e );
119- }
132+ } // else expected
120133 }
121134 }
122135
123136 public static class PasswordCallbackHandler implements CallbackHandler {
124137 public void handle (Callback [] callbacks )
125138 throws IOException , UnsupportedCallbackException {
126- if (!(callbacks [0 ] instanceof PasswordCallback )) {
139+ if (callbacks [0 ] instanceof PasswordCallback pc ) {
140+ pc .setPassword (null );
141+ } else {
127142 throw new UnsupportedCallbackException (callbacks [0 ]);
128143 }
129- PasswordCallback pc = (PasswordCallback )callbacks [0 ];
130- pc .setPassword (null );
144+
131145 }
132146 }
133147}
0 commit comments