Skip to content

Commit 3dbd486

Browse files
committed
Multiple logins test now extends PKCS11Test
* Multiple logins test now extends PKCS11Test * cleanup * skip exceptions are now throws
1 parent 0969eae commit 3dbd486

File tree

1 file changed

+22
-51
lines changed

1 file changed

+22
-51
lines changed

test/jdk/sun/security/pkcs11/Provider/MultipleLogins.java

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,38 @@
4141
import javax.security.auth.callback.PasswordCallback;
4242
import javax.security.auth.callback.UnsupportedCallbackException;
4343
import javax.security.auth.login.LoginException;
44-
import jdk.test.lib.Utils;
4544

4645
import java.io.IOException;
4746
import java.lang.ref.WeakReference;
48-
import java.nio.file.Files;
49-
import java.nio.file.Path;
50-
import java.nio.file.Paths;
51-
import java.nio.file.StandardCopyOption;
5247
import java.security.*;
53-
import java.util.List;
5448

5549
import jdk.test.lib.util.ForceGC;
5650
import jtreg.SkippedException;
5751

58-
public class MultipleLogins {
59-
private static final String KS_TYPE = "PKCS11";
52+
public class MultipleLogins extends PKCS11Test {
6053
private static final int NUM_PROVIDERS = 20;
6154
private static final SunPKCS11[] providers = new SunPKCS11[NUM_PROVIDERS];
6255

63-
private static void copyDbFiles() throws IOException {
64-
final var testFolder = System.getProperty("test.src", ".");
65-
final var srcDbFolder = Paths.get(testFolder).getParent().resolve("nss", "db");
66-
67-
// Getting path & creating the temporary scratch directory ./nss/db
68-
final var nssFolder = Path.of(".").resolve("nss");
69-
Files.createDirectory(nssFolder);
70-
final var destination = nssFolder.resolve("db");
71-
72-
final var sourceFiles = List.of(
73-
srcDbFolder.resolve("cert9.db"),
74-
srcDbFolder.resolve("key4.db"),
75-
srcDbFolder.resolve("cert8.db"),
76-
srcDbFolder.resolve("key3.db")
77-
);
78-
79-
final var copiedFiles = Utils.copyFiles(sourceFiles, destination, StandardCopyOption.REPLACE_EXISTING);
80-
copiedFiles.forEach(path -> path.toFile().setWritable(true));
81-
82-
System.out.println("NSS db files copied to: ");
83-
copiedFiles.forEach(System.out::println);
56+
public static void main(String[] args) throws Exception {
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);
8460
}
8561

86-
public static void main(String[] args) throws Exception {
87-
copyDbFiles();
62+
@Override
63+
public void main(Provider p) throws Exception {
64+
copyNssCertKeyToClassesDir();
8865

89-
String nssConfig = null;
90-
try {
91-
nssConfig = PKCS11Test.getNssConfig();
92-
} catch (SkippedException exc) {
93-
System.out.println("Skipping test: " + exc.getMessage());
94-
}
66+
String nssConfig = getNssConfig();
9567

9668
if (nssConfig == null) {
9769
// No test framework support yet. Ignore
98-
System.out.println("No NSS config found. Skipping.");
99-
return;
70+
throw new SkippedException("No NSS config found. Skipping.");
10071
}
10172

102-
for (int i =0; i < NUM_PROVIDERS; i++) {
73+
for (int i = 0; i < NUM_PROVIDERS; i++) {
10374
// loop to set up test without security manger
104-
providers[i] = (SunPKCS11)PKCS11Test.newPKCS11Provider();
75+
providers[i] = (SunPKCS11)newPKCS11Provider();
10576
}
10677

10778
for (int i =0; i < NUM_PROVIDERS; i++) {
@@ -111,7 +82,7 @@ public static void main(String[] args) throws Exception {
11182
}
11283

11384
WeakReference<SunPKCS11>[] weakRef = new WeakReference[NUM_PROVIDERS];
114-
for (int i =0; i < NUM_PROVIDERS; i++) {
85+
for (int i = 0; i < NUM_PROVIDERS; i++) {
11586
weakRef[i] = new WeakReference<>(providers[i]);
11687
providers[i].logout();
11788

@@ -138,7 +109,7 @@ public static void main(String[] args) throws Exception {
138109
}
139110

140111
private static void test(SunPKCS11 p) throws Exception {
141-
KeyStore ks = KeyStore.getInstance(KS_TYPE, p);
112+
KeyStore ks = KeyStore.getInstance(PKCS11, p);
142113
p.setCallbackHandler(new PasswordCallbackHandler());
143114
try {
144115
ks.load(null, (char[]) null);
@@ -154,23 +125,23 @@ private static void test(SunPKCS11 p) throws Exception {
154125
try {
155126
ks.load(null, (char[]) null);
156127
} catch (IOException e) {
157-
if (e.getCause() instanceof LoginException &&
158-
e.getCause().getMessage().contains("No token present")) {
159-
// expected
160-
} else {
128+
if (!(e.getCause() instanceof LoginException) ||
129+
!(e.getCause().getMessage().contains("No token present"))) {
130+
161131
throw new RuntimeException("Token was present", e);
162-
}
132+
} // else expected
163133
}
164134
}
165135

166136
public static class PasswordCallbackHandler implements CallbackHandler {
167137
public void handle(Callback[] callbacks)
168138
throws IOException, UnsupportedCallbackException {
169-
if (!(callbacks[0] instanceof PasswordCallback)) {
139+
if (callbacks[0] instanceof PasswordCallback pc) {
140+
pc.setPassword(null);
141+
} else {
170142
throw new UnsupportedCallbackException(callbacks[0]);
171143
}
172-
PasswordCallback pc = (PasswordCallback)callbacks[0];
173-
pc.setPassword(null);
144+
174145
}
175146
}
176147
}

0 commit comments

Comments
 (0)