3737 * @library /test/lib /test/jdk/java/net/httpclient/lib
3838 * @modules java.base/sun.security.x509
3939 *
40- * @run junit/othervm -Djavax.net.debug=ssl:keymanager NullCases
40+ * @run junit NullCases
4141 */
4242
4343import jdk .test .lib .Asserts ;
4848
4949import javax .net .ssl .KeyManagerFactory ;
5050import javax .net .ssl .X509KeyManager ;
51- import java .io .ByteArrayOutputStream ;
52- import java .io .PrintStream ;
5351import java .security .KeyPair ;
5452import java .security .SecureRandom ;
5553import java .security .KeyStore ;
6361
6462
6563public class NullCases {
66- private static final String KEY_MGR_EXCEPTION_MESSAGE =
67- "Exception thrown while getting an alias" ;
6864
6965 private static KeyManagerFactory kmf ;
7066 private static X509KeyManager km ;
71- private final PrintStream initialErrStream = System .err ;
7267
7368 @ BeforeAll
7469 public static void beforeAll () throws Exception {
@@ -145,44 +140,26 @@ public void JDK6302271Test() {
145140 * The following tests are testing JDK-8369995
146141 */
147142
148- private ByteArrayOutputStream replaceSystemError () {
149- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
150- final PrintStream newErrStream = new PrintStream (outputStream );
151- System .setErr (newErrStream );
152-
153- return outputStream ;
154- }
155-
156143 @ Test
157144 public void incompleteChainAndKeyTest () {
158145 final X509Certificate [] certs = km .getCertificateChain ("1.1" );
159146 final PrivateKey priv = km .getPrivateKey ("1.1" );
160147
161148 Asserts .assertNull (certs ,
162- "Should return null if the alias can't be found " );
149+ "Should return null if the alias is incomplete " );
163150 Asserts .assertNull (priv ,
164- "Should return null if the alias can't be found " );
151+ "Should return null if the alias is incomplete " );
165152 }
166153
167154 @ Test
168155 public void nonexistentBuilderTest () {
169- // recording logs to the output stream
170- final ByteArrayOutputStream outputStream = replaceSystemError ();
171-
172156 final X509Certificate [] certs = km .getCertificateChain ("RSA.1.1" );
173157 final PrivateKey priv = km .getPrivateKey ("RSA.1.1" );
174158
175159 Asserts .assertNull (certs ,
176- "Should return null if the alias can 't be found " );
160+ "Should return null if builder doesn 't exist " );
177161 Asserts .assertNull (priv ,
178- "Should return null if the alias can't be found" );
179-
180- System .setErr (initialErrStream );
181- System .err .println (" => nonexistentBuilderTest: \n " + outputStream );
182-
183- Asserts .assertFalse (
184- !outputStream .toString ().contains (KEY_MGR_EXCEPTION_MESSAGE ),
185- "No log triggered" );
162+ "Should return null if builder doesn't exist" );
186163 }
187164
188165 @ Test
@@ -192,53 +169,36 @@ public void nonexistentKSTest() {
192169 final PrivateKey priv = km .getPrivateKey ("RSA.0.1" );
193170
194171 Asserts .assertNull (certs ,
195- "Should return null if the alias can 't be found " );
172+ "Should return null if KS doesn 't exist " );
196173 Asserts .assertNull (priv ,
197- "Should return null if the alias can 't be found " );
174+ "Should return null if KS doesn 't exist " );
198175 }
199176
200177 @ Test
201178 public void wrongNumberFormatTest () {
202- // recording logs to the output stream
203- final ByteArrayOutputStream outputStream = replaceSystemError ();
204179 final X509Certificate [] certs =
205180 km .getCertificateChain ("RSA.not.exist" );
206181 final PrivateKey priv = km .getPrivateKey ("RSA.not.exist" );
207182
208183 Asserts .assertNull (certs ,
209- "Should return null if the alias can't be found " );
184+ "Should return null if number format is wrong in alias " );
210185 Asserts .assertNull (priv ,
211- "Should return null if the alias can't be found" );
212-
213- System .setErr (initialErrStream );
214- System .err .println (" => wrongNumberFormatTest: \n " + outputStream );
215-
216- Asserts .assertFalse (
217- !outputStream .toString ().contains (KEY_MGR_EXCEPTION_MESSAGE ),
218- "No log triggered" );
219-
186+ "Should return null if number format is wrong in alias" );
220187 }
221188
222189 @ ParameterizedTest
223- @ ValueSource (strings = {"1.." ,".1." , "..1" , ".9.123456789" })
190+ @ ValueSource (strings = {"1..1" , "1.. " ,".1." , "..1" , ".9.123456789" })
224191 public void invalidAliasTest (final String alias ) {
225- // recording logs to the output stream
226- final ByteArrayOutputStream outputStream = replaceSystemError ();
227192 final X509Certificate [] certs = km .getCertificateChain (alias );
228193 final PrivateKey priv = km .getPrivateKey (alias );
229194
230195 Asserts .assertNull (certs ,
231- "Should return null if the alias can't be found" );
196+ String .format (
197+ "Should return null if the alias is invalid <%s>" ,
198+ alias ));
232199 Asserts .assertNull (priv ,
233- "Should return null if the alias can't be found" );
234-
235- System .setErr (initialErrStream );
236- System .err .println (" => wrongNumberFormatTest alias<" + alias + ">: \n "
237- + outputStream );
238-
239- Asserts .assertFalse (!outputStream .toString ()
240- .contains ("Invalid alias format:" ),
241- "No log triggered" );
242-
200+ String .format (
201+ "Should return null if the alias is invalid <%s>" ,
202+ alias ));
243203 }
244204}
0 commit comments