11/*
2- * Copyright (c) 2003, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2003, 2023 , 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
2828 * @test
2929 * @bug 8051498 8145849 8170282
3030 * @summary JEP 244: TLS Application-Layer Protocol Negotiation Extension
31+ * @library /javax/net/ssl/templates
3132 *
3233 * @run main/othervm SSLEngineAlpnTest h2 UNUSED h2 h2
3334 * @run main/othervm SSLEngineAlpnTest h2 UNUSED h2,http/1.1 h2
124125 */
125126import javax .net .ssl .*;
126127import javax .net .ssl .SSLEngineResult .*;
127- import java .io .*;
128- import java .security .*;
129- import java .nio .*;
130128import java .util .Arrays ;
131129
132- public class SSLEngineAlpnTest {
130+ public class SSLEngineAlpnTest extends SSLEngineTemplate {
133131
134132 /*
135133 * Enables logging of the SSLEngine operations.
@@ -147,41 +145,8 @@ public class SSLEngineAlpnTest {
147145 */
148146 private static final boolean debug = false ;
149147
150- private static boolean hasServerAPs ; // whether server APs are present
151148 private static boolean hasCallback ; // whether a callback is present
152149
153- private final SSLContext sslc ;
154-
155- private SSLEngine clientEngine ; // client Engine
156- private ByteBuffer clientOut ; // write side of clientEngine
157- private ByteBuffer clientIn ; // read side of clientEngine
158-
159- private SSLEngine serverEngine ; // server Engine
160- private ByteBuffer serverOut ; // write side of serverEngine
161- private ByteBuffer serverIn ; // read side of serverEngine
162-
163- /*
164- * For data transport, this example uses local ByteBuffers. This
165- * isn't really useful, but the purpose of this example is to show
166- * SSLEngine concepts, not how to do network transport.
167- */
168- private ByteBuffer cTOs ; // "reliable" transport client->server
169- private ByteBuffer sTOc ; // "reliable" transport server->client
170-
171- /*
172- * The following is to set up the keystores.
173- */
174- private static final String pathToStores = "../etc" ;
175- private static final String keyStoreFile = "keystore" ;
176- private static final String trustStoreFile = "truststore" ;
177- private static final String passwd = "passphrase" ;
178-
179- private static final String keyFilename
180- = System .getProperty ("test.src" , "." ) + "/" + pathToStores
181- + "/" + keyStoreFile ;
182- private static final String trustFilename
183- = System .getProperty ("test.src" , "." ) + "/" + pathToStores
184- + "/" + trustStoreFile ;
185150
186151 /*
187152 * Main entry point for this test.
@@ -198,10 +163,9 @@ public static void main(String args[]) throws Exception {
198163 throw new Exception ("Invalid number of test parameters" );
199164 }
200165
201- hasServerAPs = !args [0 ].equals ("UNUSED" ); // are server APs being used?
202166 hasCallback = !args [1 ].equals ("UNUSED" ); // is callback being used?
203167
204- SSLEngineAlpnTest test = new SSLEngineAlpnTest (args [ 3 ] );
168+ SSLEngineAlpnTest test = new SSLEngineAlpnTest ();
205169 try {
206170 test .runTest (convert (args [0 ]), args [1 ], convert (args [2 ]), args [3 ]);
207171 } catch (SSLHandshakeException she ) {
@@ -215,35 +179,8 @@ public static void main(String args[]) throws Exception {
215179 System .out .println ("Test Passed." );
216180 }
217181
218- /*
219- * Create an initialized SSLContext to use for these tests.
220- */
221- public SSLEngineAlpnTest (String expectedAP ) throws Exception {
222-
223- KeyStore ks = KeyStore .getInstance ("JKS" );
224- KeyStore ts = KeyStore .getInstance ("JKS" );
225-
226- char [] passphrase = "passphrase" .toCharArray ();
227-
228- ks .load (new FileInputStream (keyFilename ), passphrase );
229- ts .load (new FileInputStream (trustFilename ), passphrase );
230-
231- KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
232- kmf .init (ks , passphrase );
233-
234- KeyManager [] kms = kmf .getKeyManagers ();
235- if (!(kms [0 ] instanceof X509ExtendedKeyManager )) {
236- throw new Exception ("kms[0] not X509ExtendedKeyManager" );
237- }
238-
239- TrustManagerFactory tmf = TrustManagerFactory .getInstance ("SunX509" );
240- tmf .init (ts );
241-
242- SSLContext sslCtx = SSLContext .getInstance ("TLS" );
243-
244- sslCtx .init (kms , tmf .getTrustManagers (), null );
245-
246- sslc = sslCtx ;
182+ public SSLEngineAlpnTest () throws Exception {
183+ super ();
247184 }
248185
249186 /*
@@ -290,8 +227,7 @@ private void runTest(String[] serverAPs, String callbackAP,
290227
291228 boolean dataDone = false ;
292229
293- createSSLEngines (serverAPs , callbackAP , clientAPs );
294- createBuffers ();
230+ configureSSLEngines (serverAPs , callbackAP , clientAPs );
295231
296232 SSLEngineResult clientResult ; // results from client's last operation
297233 SSLEngineResult serverResult ; // results from server's last operation
@@ -311,12 +247,12 @@ private void runTest(String[] serverAPs, String callbackAP,
311247
312248 clientResult = clientEngine .wrap (clientOut , cTOs );
313249 log ("client wrap: " , clientResult );
314- runDelegatedTasks (clientResult , clientEngine );
250+ runDelegatedTasks (clientEngine );
315251 checkAPResult (clientEngine , clientResult , expectedAP );
316252
317253 serverResult = serverEngine .wrap (serverOut , sTOc );
318254 log ("server wrap: " , serverResult );
319- runDelegatedTasks (serverResult , serverEngine );
255+ runDelegatedTasks (serverEngine );
320256 checkAPResult (serverEngine , serverResult , expectedAP );
321257
322258 cTOs .flip ();
@@ -326,12 +262,12 @@ private void runTest(String[] serverAPs, String callbackAP,
326262
327263 clientResult = clientEngine .unwrap (sTOc , clientIn );
328264 log ("client unwrap: " , clientResult );
329- runDelegatedTasks (clientResult , clientEngine );
265+ runDelegatedTasks (clientEngine );
330266 checkAPResult (clientEngine , clientResult , expectedAP );
331267
332268 serverResult = serverEngine .unwrap (cTOs , serverIn );
333269 log ("server unwrap: " , serverResult );
334- runDelegatedTasks (serverResult , serverEngine );
270+ runDelegatedTasks (serverEngine );
335271 checkAPResult (serverEngine , serverResult , expectedAP );
336272
337273 cTOs .compact ();
@@ -403,13 +339,12 @@ private static void checkAPResult(SSLEngine engine, SSLEngineResult result,
403339 * Using the SSLContext created during object creation,
404340 * create/configure the SSLEngines we'll use for this test.
405341 */
406- private void createSSLEngines (String [] serverAPs , String callbackAP ,
342+ private void configureSSLEngines (String [] serverAPs , String callbackAP ,
407343 String [] clientAPs ) throws Exception {
408344 /*
409345 * Configure the serverEngine to act as a server in the SSL/TLS
410346 * handshake. Also, require SSL client authentication.
411347 */
412- serverEngine = sslc .createSSLEngine ();
413348 serverEngine .setUseClientMode (false );
414349
415350 SSLParameters sslp = serverEngine .getSSLParameters ();
@@ -454,7 +389,6 @@ private void createSSLEngines(String[] serverAPs, String callbackAP,
454389 /*
455390 * Similar to above, but using client mode instead.
456391 */
457- clientEngine = sslc .createSSLEngine ("client" , 80 );
458392 clientEngine .setUseClientMode (true );
459393 sslp = clientEngine .getSSLParameters ();
460394 if (clientAPs != null ) {
@@ -469,83 +403,10 @@ private void createSSLEngines(String[] serverAPs, String callbackAP,
469403 }
470404 }
471405
472- /*
473- * Create and size the buffers appropriately.
474- */
475- private void createBuffers () {
476-
477- /*
478- * We'll assume the buffer sizes are the same
479- * between client and server.
480- */
481- SSLSession session = clientEngine .getSession ();
482- int appBufferMax = session .getApplicationBufferSize ();
483- int netBufferMax = session .getPacketBufferSize ();
484-
485- /*
486- * We'll make the input buffers a bit bigger than the max needed
487- * size, so that unwrap()s following a successful data transfer
488- * won't generate BUFFER_OVERFLOWS.
489- *
490- * We'll use a mix of direct and indirect ByteBuffers for
491- * tutorial purposes only. In reality, only use direct
492- * ByteBuffers when they give a clear performance enhancement.
493- */
494- clientIn = ByteBuffer .allocate (appBufferMax + 50 );
495- serverIn = ByteBuffer .allocate (appBufferMax + 50 );
496-
497- cTOs = ByteBuffer .allocateDirect (netBufferMax );
498- sTOc = ByteBuffer .allocateDirect (netBufferMax );
499-
500- clientOut = ByteBuffer .wrap ("Hi Server, I'm Client" .getBytes ());
501- serverOut = ByteBuffer .wrap ("Hello Client, I'm Server" .getBytes ());
502- }
503-
504- /*
505- * If the result indicates that we have outstanding tasks to do,
506- * go ahead and run them in this thread.
507- */
508- private static void runDelegatedTasks (SSLEngineResult result ,
509- SSLEngine engine ) throws Exception {
510-
511- if (result .getHandshakeStatus () == HandshakeStatus .NEED_TASK ) {
512- Runnable runnable ;
513- while ((runnable = engine .getDelegatedTask ()) != null ) {
514- log ("\t running delegated task..." );
515- runnable .run ();
516- }
517- HandshakeStatus hsStatus = engine .getHandshakeStatus ();
518- if (hsStatus == HandshakeStatus .NEED_TASK ) {
519- throw new Exception (
520- "handshake shouldn't need additional tasks" );
521- }
522- log ("\t new HandshakeStatus: " + hsStatus );
523- }
524- }
525-
526406 private static boolean isEngineClosed (SSLEngine engine ) {
527407 return (engine .isOutboundDone () && engine .isInboundDone ());
528408 }
529409
530- /*
531- * Simple check to make sure everything came across as expected.
532- */
533- private static void checkTransfer (ByteBuffer a , ByteBuffer b )
534- throws Exception {
535- a .flip ();
536- b .flip ();
537-
538- if (!a .equals (b )) {
539- throw new Exception ("Data didn't transfer cleanly" );
540- } else {
541- log ("\t Data transferred cleanly" );
542- }
543-
544- a .position (a .limit ());
545- b .position (b .limit ());
546- a .limit (a .capacity ());
547- b .limit (b .capacity ());
548- }
549410
550411 /*
551412 * Logging code
0 commit comments