@@ -1003,6 +1003,7 @@ public void testClientServerThreadedAlpnSelectCallback() throws Exception {
10031003 protected class InternalMultiThreadedSSLSocketServer extends Thread
10041004 {
10051005 private int serverPort ;
1006+ private volatile int boundPort = -1 ;
10061007 private CountDownLatch serverOpenLatch = null ;
10071008 private int clientConnections = 1 ;
10081009
@@ -1013,13 +1014,19 @@ public InternalMultiThreadedSSLSocketServer(
10131014 this .clientConnections = clientConnections ;
10141015 }
10151016
1017+ public int getBoundPort () {
1018+ return boundPort ;
1019+ }
1020+
10161021 @ Override
10171022 public void run () {
10181023 try {
10191024 SSLContext ctx = tf .createSSLContext ("TLS" , ctxProvider );
10201025 SSLServerSocket ss = (SSLServerSocket )ctx
10211026 .getServerSocketFactory ().createServerSocket (serverPort );
10221027
1028+ boundPort = ss .getLocalPort ();
1029+
10231030 while (clientConnections > 0 ) {
10241031 serverOpenLatch .countDown ();
10251032 SSLSocket sock = (SSLSocket )ss .accept ();
@@ -1030,6 +1037,10 @@ public void run() {
10301037
10311038 } catch (Exception e ) {
10321039 e .printStackTrace ();
1040+ /* Ensure awaiting main thread doesn't hang on bind failure. */
1041+ if (serverOpenLatch != null ) {
1042+ serverOpenLatch .countDown ();
1043+ }
10331044 }
10341045 }
10351046
@@ -1139,10 +1150,11 @@ public void testExtendedThreadingUse()
11391150 /* Number of SSLSocket client threads to start up */
11401151 int numThreads = 50 ;
11411152
1142- /* Port of internal HTTPS server. Using 11120 since SSLEngine
1143- * extended threading test uses 11119. If both tests end up running
1144- * concurrently by JUnit ports could conflict. */
1145- final int svrPort = 11120 ;
1153+ /* Bind server on an ephemeral port (0) so multiple JVMs running
1154+ * this test concurrently don't collide on a single hardcoded
1155+ * port. The actual bound port is retrieved after the server
1156+ * thread signals it's up. */
1157+ final int svrPort = 0 ;
11461158
11471159 /* Create ExecutorService to launch client SSLSocket threads */
11481160 ExecutorService service = Executors .newFixedThreadPool (numThreads );
@@ -1179,12 +1191,17 @@ public void testExtendedThreadingUse()
11791191 /* Wait for server thread to start up before connecting clients */
11801192 serverOpenLatch .await ();
11811193
1194+ /* Retrieve the actual bound port. -1 indicates server failed to
1195+ * bind, in which case clients won't be able to connect and the
1196+ * test will correctly fail via the latch timeout path below. */
1197+ final int actualPort = server .getBoundPort ();
1198+
11821199 /* Start up client threads */
11831200 for (int i = 0 ; i < numThreads ; i ++) {
11841201 service .submit (new Runnable () {
11851202 @ Override public void run () {
11861203 SSLSocketClient client =
1187- new SSLSocketClient (localCtx , "localhost" , svrPort );
1204+ new SSLSocketClient (localCtx , "localhost" , actualPort );
11881205 try {
11891206 client .connect ();
11901207 success .incrementAndGet (0 );
@@ -2359,7 +2376,7 @@ public void testSessionResumption() throws Exception {
23592376 SSLSocketFactory cliFactory = ctx .getSocketFactory ();
23602377
23612378 SSLSocket cs = (SSLSocket )cliFactory .createSocket ();
2362- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2379+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
23632380 ss .getLocalPort ()));
23642381
23652382 /* start server */
@@ -2390,7 +2407,7 @@ public Void call() throws Exception {
23902407
23912408 /* connection #2, should resume */
23922409 cs = (SSLSocket )cliFactory .createSocket ();
2393- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2410+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
23942411 ss .getLocalPort ()));
23952412 cs .startHandshake ();
23962413 sessionID2 = cs .getSession ().getId ();
@@ -2462,7 +2479,7 @@ public void testSessionResumptionSysPropDisabled() throws Exception {
24622479 SSLSocketFactory cliFactory = ctx .getSocketFactory ();
24632480
24642481 SSLSocket cs = (SSLSocket )cliFactory .createSocket ();
2465- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2482+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
24662483 ss .getLocalPort ()));
24672484
24682485 /* Start server */
@@ -2493,7 +2510,7 @@ public Void call() throws Exception {
24932510
24942511 /* connection #2, should NOT resume */
24952512 cs = (SSLSocket )cliFactory .createSocket ();
2496- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2513+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
24972514 ss .getLocalPort ()));
24982515 cs .startHandshake ();
24992516 sessionID2 = cs .getSession ().getId ();
@@ -2576,7 +2593,7 @@ public void testSessionResumptionWithTicketEnabled() throws Exception {
25762593
25772594 WolfSSLSocket cs = (WolfSSLSocket )cliFactory .createSocket ();
25782595 cs .setUseSessionTickets (true );
2579- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2596+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
25802597 ss .getLocalPort ()));
25812598
25822599 /* start server */
@@ -2608,7 +2625,7 @@ public Void call() throws Exception {
26082625 /* connection #2, should resume */
26092626 cs = (WolfSSLSocket )cliFactory .createSocket ();
26102627 cs .setUseSessionTickets (true );
2611- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2628+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
26122629 ss .getLocalPort ()));
26132630 cs .startHandshake ();
26142631 sessionID2 = cs .getSession ().getId ();
@@ -4440,7 +4457,7 @@ public X509Certificate[] getAcceptedIssuers() {
44404457
44414458 SSLSocket cs = (SSLSocket )cliCtx .getSocketFactory ().createSocket ();
44424459 cs .connect (new InetSocketAddress (
4443- InetAddress .getLocalHost (), ss .getLocalPort ()));
4460+ InetAddress .getLoopbackAddress (), ss .getLocalPort ()));
44444461
44454462 final SSLSocket server = (SSLSocket )ss .accept ();
44464463
0 commit comments