Skip to content

Commit b62fc9a

Browse files
committed
comments
1 parent 370a153 commit b62fc9a

File tree

3 files changed

+79
-55
lines changed

3 files changed

+79
-55
lines changed

src/java.base/share/classes/sun/security/ssl/X509KeyManagerImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ private PrivateKeyEntry getEntry(String alias) {
224224
int firstDot = alias.indexOf('.');
225225
int secondDot = alias.indexOf('.', firstDot + 1);
226226

227-
if (firstDot < 1 ||
228-
secondDot - firstDot < 2 ||
229-
alias.length() - secondDot < 2) {
227+
if ((firstDot < 1)
228+
|| (secondDot - firstDot < 2)
229+
|| (alias.length() - secondDot < 2)) {
230230

231-
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
231+
if (SSLLogger.isOn && SSLLogger.isOn("ssl,keymanager")) {
232232
SSLLogger.warning("Invalid alias format: " + alias);
233233
}
234234
return null;
@@ -255,7 +255,7 @@ private PrivateKeyEntry getEntry(String alias) {
255255
NoSuchAlgorithmException |
256256
IndexOutOfBoundsException e) {
257257
// ignore and only log exception
258-
if (SSLLogger.isOn && SSLLogger.isOn("keymanager")) {
258+
if (SSLLogger.isOn && SSLLogger.isOn("ssl,keymanager")) {
259259
SSLLogger.warning("Exception thrown while getting an alias " +
260260
alias + ": " + e);
261261
}

test/jdk/sun/security/ssl/X509KeyManager/NullCases.java

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
* @library /test/lib /test/jdk/java/net/httpclient/lib
3838
* @modules java.base/sun.security.x509
3939
*
40-
* @run junit NullCases
41-
* @run junit/othervm -Djavax.net.debug=ssl:keymanager -Darg=debug NullCases
40+
* @run junit/othervm -Djavax.net.debug=ssl:keymanager NullCases
4241
*/
4342

4443
import jdk.test.lib.Asserts;
@@ -61,29 +60,25 @@
6160
import static jdk.httpclient.test.lib.common.DynamicKeyStoreUtil.generateCert;
6261
import static jdk.httpclient.test.lib.common.DynamicKeyStoreUtil.generateKeyStore;
6362
import static jdk.httpclient.test.lib.common.DynamicKeyStoreUtil.generateRSAKeyPair;
64-
import static org.junit.jupiter.api.Assumptions.assumeFalse;
6563

6664

6765
public class NullCases {
68-
private static final String KEY_MGR_EXCEPTION_MESSAGE = "Exception thrown while getting an alias";
66+
private static final String KEY_MGR_EXCEPTION_MESSAGE =
67+
"Exception thrown while getting an alias";
6968

70-
private static boolean isDebugLogging;
7169
private static KeyManagerFactory kmf;
7270
private static X509KeyManager km;
7371
private final PrintStream initialErrStream = System.err;
7472

7573
@BeforeAll
7674
public static void beforeAll() throws Exception {
77-
final String arg = System.getProperty("arg");
78-
isDebugLogging = arg != null && arg.equals("debug");
79-
8075
kmf = KeyManagerFactory.getInstance("NewSunX509");
8176

8277
// creating a new keystore
8378
final SecureRandom secureRandom = new SecureRandom();
8479
final KeyPair keyPair = generateRSAKeyPair(secureRandom);
85-
final X509Certificate originServerCert = generateCert(keyPair, secureRandom,
86-
"subject");
80+
final X509Certificate originServerCert =
81+
generateCert(keyPair, secureRandom, "subject");
8782
final KeyStore ks = generateKeyStore(keyPair.getPrivate(),
8883
new Certificate[]{originServerCert});
8984

@@ -100,15 +95,13 @@ private X509KeyManager generateNullKm() throws Exception {
10095
@Test
10196
public void JDK6302126Test() throws Exception {
10297
// check for bug 6302126
103-
assumeFalse(isDebugLogging);
10498

10599
generateNullKm();
106100
}
107101

108102
@Test
109103
public void JDK6302304Test() throws Exception {
110104
// check for bug 6302304
111-
assumeFalse(isDebugLogging);
112105

113106
final X509KeyManager km = generateNullKm();
114107

@@ -123,24 +116,29 @@ public void JDK6302304Test() throws Exception {
123116
@Test
124117
public void JDK6302321Test() {
125118
// check for bug 6302321
126-
assumeFalse(isDebugLogging);
127119

128-
final X509Certificate[] certs = km.getCertificateChain("doesnotexist");
120+
final X509Certificate[] certs =
121+
km.getCertificateChain("doesnotexist");
129122
final PrivateKey priv = km.getPrivateKey("doesnotexist");
130-
Asserts.assertNull(certs, "Should return null if the alias can't be found");
131-
Asserts.assertNull(priv, "Should return null if the alias can't be found");
123+
Asserts.assertNull(certs,
124+
"Should return null if the alias can't be found");
125+
Asserts.assertNull(priv,
126+
"Should return null if the alias can't be found");
132127
}
133128

134129
@Test
135130
public void JDK6302271Test() {
136131
// check for 6302271
137-
assumeFalse(isDebugLogging);
138132

139-
final String[] clis = km.getClientAliases("doesnotexist", null);
140-
Asserts.assertFalse((clis != null && clis.length == 0), "Should return null instead of empty array");
133+
final String[] clis =
134+
km.getClientAliases("doesnotexist", null);
135+
Asserts.assertFalse((clis != null && clis.length == 0),
136+
"Should return null instead of empty array");
141137

142-
final String[] srvs = km.getServerAliases("doesnotexist", null);
143-
Asserts.assertFalse((srvs != null && srvs.length == 0), "Should return null instead of empty array");
138+
final String[] srvs =
139+
km.getServerAliases("doesnotexist", null);
140+
Asserts.assertFalse((srvs != null && srvs.length == 0),
141+
"Should return null instead of empty array");
144142
}
145143

146144
/**
@@ -157,13 +155,13 @@ private ByteArrayOutputStream replaceSystemError() {
157155

158156
@Test
159157
public void incompleteChainAndKeyTest() {
160-
assumeFalse(isDebugLogging);
161-
162158
final X509Certificate[] certs = km.getCertificateChain("1.1");
163159
final PrivateKey priv = km.getPrivateKey("1.1");
164160

165-
Asserts.assertNull(certs, "Should return null if the alias can't be found");
166-
Asserts.assertNull(priv, "Should return null if the alias can't be found");
161+
Asserts.assertNull(certs,
162+
"Should return null if the alias can't be found");
163+
Asserts.assertNull(priv,
164+
"Should return null if the alias can't be found");
167165
}
168166

169167
@Test
@@ -174,60 +172,72 @@ public void nonexistentBuilderTest() {
174172
final X509Certificate[] certs = km.getCertificateChain("RSA.1.1");
175173
final PrivateKey priv = km.getPrivateKey("RSA.1.1");
176174

177-
Asserts.assertNull(certs, "Should return null if the alias can't be found");
178-
Asserts.assertNull(priv, "Should return null if the alias can't be found");
175+
Asserts.assertNull(certs,
176+
"Should return null if the alias can't be found");
177+
Asserts.assertNull(priv,
178+
"Should return null if the alias can't be found");
179179

180180
System.setErr(initialErrStream);
181181
System.err.println(" => nonexistentBuilderTest: \n" + outputStream);
182182

183-
Asserts.assertFalse(isDebugLogging && !outputStream.toString().contains(KEY_MGR_EXCEPTION_MESSAGE),
183+
Asserts.assertFalse(
184+
!outputStream.toString().contains(KEY_MGR_EXCEPTION_MESSAGE),
184185
"No log triggered");
185186
}
186187

187188
@Test
188189
public void nonexistentKSTest() {
189-
assumeFalse(isDebugLogging);
190190

191191
final X509Certificate[] certs = km.getCertificateChain("RSA.0.1");
192192
final PrivateKey priv = km.getPrivateKey("RSA.0.1");
193193

194-
Asserts.assertNull(certs, "Should return null if the alias can't be found");
195-
Asserts.assertNull(priv, "Should return null if the alias can't be found");
194+
Asserts.assertNull(certs,
195+
"Should return null if the alias can't be found");
196+
Asserts.assertNull(priv,
197+
"Should return null if the alias can't be found");
196198
}
197199

198200
@Test
199201
public void wrongNumberFormatTest() {
200202
// recording logs to the output stream
201203
final ByteArrayOutputStream outputStream = replaceSystemError();
202-
final X509Certificate[] certs = km.getCertificateChain("RSA.not.exist");
204+
final X509Certificate[] certs =
205+
km.getCertificateChain("RSA.not.exist");
203206
final PrivateKey priv = km.getPrivateKey("RSA.not.exist");
204207

205-
Asserts.assertNull(certs, "Should return null if the alias can't be found");
206-
Asserts.assertNull(priv, "Should return null if the alias can't be found");
208+
Asserts.assertNull(certs,
209+
"Should return null if the alias can't be found");
210+
Asserts.assertNull(priv,
211+
"Should return null if the alias can't be found");
207212

208213
System.setErr(initialErrStream);
209214
System.err.println(" => wrongNumberFormatTest: \n" + outputStream);
210215

211-
Asserts.assertFalse(isDebugLogging && !outputStream.toString().contains(KEY_MGR_EXCEPTION_MESSAGE),
216+
Asserts.assertFalse(
217+
!outputStream.toString().contains(KEY_MGR_EXCEPTION_MESSAGE),
212218
"No log triggered");
213219

214220
}
215221

216222
@ParameterizedTest
217-
@ValueSource(strings = {"..1", ".9.123456789"})
223+
@ValueSource(strings = {"1..",".1.", "..1", ".9.123456789"})
218224
public void invalidAliasTest(final String alias) {
219225
// recording logs to the output stream
220226
final ByteArrayOutputStream outputStream = replaceSystemError();
221227
final X509Certificate[] certs = km.getCertificateChain(alias);
222228
final PrivateKey priv = km.getPrivateKey(alias);
223229

224-
Asserts.assertNull(certs, "Should return null if the alias can't be found");
225-
Asserts.assertNull(priv, "Should return null if the alias can't be found");
230+
Asserts.assertNull(certs,
231+
"Should return null if the alias can't be found");
232+
Asserts.assertNull(priv,
233+
"Should return null if the alias can't be found");
226234

227235
System.setErr(initialErrStream);
228-
System.err.println(" => wrongNumberFormatTest alias<" + alias + ">: \n" + outputStream);
236+
System.err.println(" => wrongNumberFormatTest alias<" + alias + ">: \n"
237+
+ outputStream);
229238

230-
Asserts.assertFalse(isDebugLogging && !outputStream.toString().contains("Invalid alias format:"),
239+
Asserts.assertFalse(!outputStream.toString()
240+
.contains("Invalid alias format:"),
231241
"No log triggered");
232242

233243
}

test/jdk/sun/security/ssl/X509KeyManager/X509KeyManagerNegativeTests.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.security.Key;
3636
import java.security.KeyStore;
3737
import java.security.KeyStoreSpi;
38+
import java.security.Provider;
3839
import java.security.Security;
3940
import java.security.cert.Certificate;
4041
import java.util.ConcurrentModificationException;
@@ -56,15 +57,19 @@ public static void beforeAll() throws Exception {
5657

5758
// initialising exception throwing ks
5859
// cleaned up after the tests are complete
59-
final KeyManagerFactory exceptionThrowingKMF = KeyManagerFactory.getInstance("NewSunX509");
60+
final KeyManagerFactory exceptionThrowingKMF =
61+
KeyManagerFactory.getInstance("NewSunX509");
6062

6163
// adding dummy provider
6264
Security.addProvider(new MyCustomKSProvider());
63-
final KeyStore exceptionThrowingKS = KeyStore.getInstance("MyExceptionKS");
65+
final KeyStore exceptionThrowingKS =
66+
KeyStore.getInstance("MyExceptionKS");
6467
exceptionThrowingKS.load(null, null);
6568

66-
exceptionThrowingKMF.init((KeyStore) exceptionThrowingKS, null);
67-
exceptionThrowingKM = (X509KeyManager) exceptionThrowingKMF.getKeyManagers()[0];
69+
exceptionThrowingKMF
70+
.init((KeyStore) exceptionThrowingKS, null);
71+
exceptionThrowingKM =
72+
(X509KeyManager) exceptionThrowingKMF.getKeyManagers()[0];
6873
}
6974

7075
@AfterAll
@@ -82,17 +87,20 @@ public void ksExceptionTest() {
8287
() -> exceptionThrowingKM.getPrivateKey("RSA.0.0"));
8388
}
8489

85-
public static class MyCustomKSProvider extends java.security.Provider {
90+
public static class MyCustomKSProvider extends Provider {
8691
public MyCustomKSProvider() {
87-
super("MyCustomKSProvider", 1.0, "My Custom KS Provider");
92+
super("MyCustomKSProvider",
93+
"1.0",
94+
"My Custom KS Provider");
8895
put("KeyStore.MyExceptionKS", MyExceptionKS.class.getName());
8996
}
9097
}
9198

9299
public static class MyExceptionKS extends KeyStoreSpi {
93100

94101
@Override
95-
public KeyStore.Entry engineGetEntry(String alias, KeyStore.ProtectionParameter param) {
102+
public KeyStore.Entry engineGetEntry(String alias,
103+
KeyStore.ProtectionParameter param) {
96104
throw new ConcurrentModificationException("getEntry exception");
97105
}
98106

@@ -155,15 +163,21 @@ public void engineLoad(InputStream stream, char[] password) {
155163
}
156164

157165
@Override
158-
public void engineSetKeyEntry(String alias, Key key, char[] password, Certificate[] chain) {
166+
public void engineSetKeyEntry(String alias,
167+
Key key,
168+
char[] password,
169+
Certificate[] chain) {
159170
}
160171

161172
@Override
162-
public void engineSetKeyEntry(String alias, byte[] key, Certificate[] chain) {
173+
public void engineSetKeyEntry(String alias,
174+
byte[] key,
175+
Certificate[] chain) {
163176
}
164177

165178
@Override
166-
public void engineSetCertificateEntry(String alias, Certificate cert) {
179+
public void engineSetCertificateEntry(String alias,
180+
Certificate cert) {
167181
}
168182

169183
@Override

0 commit comments

Comments
 (0)