Skip to content

Commit ccfcd92

Browse files
fgualliniXueleiFan
authored andcommittedJun 1, 2021
8180571: Refactor sun/security/pkcs11 shell tests to plain java tests and fix failures
Reviewed-by: xuelei
1 parent 40e4171 commit ccfcd92

18 files changed

+256
-881
lines changed
 

‎test/jdk/ProblemList.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ javax/net/ssl/DTLS/CipherSuite.java 8202059 macosx-x
662662

663663
sun/security/provider/KeyStore/DKSTest.sh 8180266 windows-all
664664

665-
sun/security/pkcs11/KeyStore/SecretKeysBasic.sh 8209398 generic-all
665+
sun/security/pkcs11/KeyStore/SecretKeysBasic.java 8209398 generic-all
666666

667667
security/infra/java/security/cert/CertPathValidator/certification/ActalisCA.java 8224768 generic-all
668668
security/infra/java/security/cert/CertPathValidator/certification/BuypassCA.java 8243543 generic-all

‎test/jdk/sun/security/pkcs11/Config/ReadConfInUTF16Env.java

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2021, 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
@@ -21,17 +21,36 @@
2121
* questions.
2222
*/
2323

24+
/* @test
25+
* @bug 8187023
26+
* @summary Pkcs11 config file should be assumed in ISO-8859-1
27+
* @library /test/lib
28+
* @run testng/othervm ReadConfInUTF16Env
29+
*/
30+
31+
import jdk.test.lib.process.ProcessTools;
32+
import org.testng.annotations.Test;
33+
2434
import java.security.Provider;
2535
import java.security.Security;
2636

2737
public class ReadConfInUTF16Env {
28-
public static void main(String argv[]) {
29-
Provider p = Security.getProvider("SunPKCS11");
30-
if (p == null) {
31-
System.out.println("Skipping test - no PKCS11 provider available");
32-
return;
33-
}
3438

35-
System.out.println(p.getName());
39+
@Test
40+
public void testReadConfInUTF16Env() throws Exception {
41+
String[] testCommand = new String[] { "-Dfile.encoding=UTF-16",
42+
TestSunPKCS11Provider.class.getName()};
43+
ProcessTools.executeTestJvm(testCommand).shouldHaveExitValue(0);
44+
}
45+
46+
static class TestSunPKCS11Provider {
47+
public static void main(String[] args) throws Exception {
48+
Provider p = Security.getProvider("SunPKCS11");
49+
if (p == null) {
50+
System.out.println("Skipping test - no PKCS11 provider available");
51+
return;
52+
}
53+
System.out.println(p.getName());
54+
}
3655
}
3756
}

‎test/jdk/sun/security/pkcs11/Config/ReadConfInUTF16Env.sh

-39
This file was deleted.

‎test/jdk/sun/security/pkcs11/KeyStore/Basic.java

+46-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, 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
@@ -21,19 +21,30 @@
2121
* questions.
2222
*/
2323

24+
/* @test
25+
* @bug 4938185
26+
* @summary KeyStore support for NSS cert/key databases
27+
* To run manually:
28+
* set environment variable:
29+
* <token> [activcard|ibutton|nss|sca1000]
30+
* <command> [list|basic]
31+
*
32+
* Note:
33+
* . 'list' lists the token aliases
34+
* . 'basic' does not run with activcard,
35+
* @library /test/lib ..
36+
* @run testng/othervm Basic
37+
*/
38+
2439
import java.io.*;
40+
import java.nio.file.Path;
2541
import java.util.*;
2642

2743
import java.security.KeyStore;
2844
import java.security.KeyStoreException;
2945
import java.security.KeyFactory;
30-
import java.security.KeyPairGenerator;
31-
import java.security.KeyPair;
32-
import java.security.SecureRandom;
33-
import java.security.AuthProvider;
3446
import java.security.PrivateKey;
3547
import java.security.Provider;
36-
import java.security.ProviderException;
3748
import java.security.Signature;
3849
import java.security.Security;
3950

@@ -44,17 +55,18 @@
4455
import javax.crypto.SecretKey;
4556

4657
import javax.security.auth.Subject;
47-
import javax.security.auth.login.LoginException;
4858

4959
import com.sun.security.auth.module.*;
5060
import com.sun.security.auth.callback.*;
61+
import org.testng.annotations.BeforeClass;
62+
import org.testng.annotations.Test;
5163

5264

5365
public class Basic extends PKCS11Test {
5466

55-
private static final char SEP = File.separatorChar;
56-
57-
private static String DIR = System.getProperty("DIR");
67+
private static final Path TEST_DATA_PATH = Path.of(BASE)
68+
.resolve("BasicData");
69+
private static final String DIR = TEST_DATA_PATH.toString();
5870
private static char[] tokenPwd;
5971
private static final char[] ibuttonPwd =
6072
new char[0];
@@ -88,6 +100,22 @@ public class Basic extends PKCS11Test {
88100
private static final String KS_TYPE = "PKCS11";
89101
private static Provider provider;
90102

103+
@BeforeClass
104+
public void setUp() throws Exception {
105+
copyNssCertKeyToClassesDir();
106+
setCommonSystemProps();
107+
System.setProperty("CUSTOM_P11_CONFIG",
108+
TEST_DATA_PATH.resolve("p11-nss.txt").toString());
109+
System.setProperty("TOKEN", "nss");
110+
System.setProperty("TEST", "basic");
111+
}
112+
113+
@Test
114+
public void testBasic() throws Exception {
115+
String[] args = {"sm", "Basic.policy"};
116+
main(new Basic(), args);
117+
}
118+
91119
private static class FooEntry implements KeyStore.Entry { }
92120

93121
private static class P11SecretKey implements SecretKey {
@@ -102,10 +130,6 @@ public P11SecretKey(String alg, int length) {
102130
public byte[] getEncoded() { return new byte[length/8]; }
103131
}
104132

105-
public static void main(String[] args) throws Exception {
106-
main(new Basic(), args);
107-
}
108-
109133
public void main(Provider p) throws Exception {
110134

111135
this.provider = p;
@@ -136,17 +160,17 @@ public void main(Provider p) throws Exception {
136160

137161
// get cert chains for private keys
138162
CertificateFactory cf = CertificateFactory.getInstance("X.509", "SUN");
139-
Certificate caCert = (X509Certificate)cf.generateCertificate
163+
Certificate caCert = cf.generateCertificate
140164
(new FileInputStream(new File(DIR, "ca.cert")));
141-
Certificate ca2Cert = (X509Certificate)cf.generateCertificate
165+
Certificate ca2Cert = cf.generateCertificate
142166
(new FileInputStream(new File(DIR, "ca2.cert")));
143-
Certificate pk1cert = (X509Certificate)cf.generateCertificate
167+
Certificate pk1cert = cf.generateCertificate
144168
(new FileInputStream(new File(DIR, "pk1.cert")));
145-
Certificate pk1cert2 = (X509Certificate)cf.generateCertificate
169+
Certificate pk1cert2 = cf.generateCertificate
146170
(new FileInputStream(new File(DIR, "pk1.cert2")));
147-
Certificate pk2cert = (X509Certificate)cf.generateCertificate
171+
Certificate pk2cert = cf.generateCertificate
148172
(new FileInputStream(new File(DIR, "pk2.cert")));
149-
Certificate pk3cert = (X509Certificate)cf.generateCertificate
173+
Certificate pk3cert = cf.generateCertificate
150174
(new FileInputStream(new File(DIR, "pk3.cert")));
151175
chain1 = new Certificate[] { pk1cert, caCert };
152176
chain2 = new Certificate[] { pk2cert, caCert };
@@ -373,12 +397,12 @@ private static void module() throws Exception {
373397

374398
KeyStoreLoginModule m = new KeyStoreLoginModule();
375399
Subject s = new Subject();
376-
Map options = new HashMap();
400+
Map<String, String> options = new HashMap<>();
377401
options.put("keyStoreURL", "NONE");
378402
options.put("keyStoreType", KS_TYPE);
379403
options.put("keyStoreProvider", KS_PROVIDER);
380404
options.put("debug", "true");
381-
m.initialize(s, new TextCallbackHandler(), new HashMap(), options);
405+
m.initialize(s, new TextCallbackHandler(), new HashMap<>(), options);
382406
m.login();
383407
m.commit();
384408
System.out.println("authenticated subject = " + s);

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jun 1, 2021

@openjdk-notifier[bot]
Please sign in to comment.