@@ -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,21 @@ public void testExtendedThreadingUse()
11791191 /* Wait for server thread to start up before connecting clients */
11801192 serverOpenLatch .await ();
11811193
1194+ /* Retrieve the actual bound port. Non-positive means the server
1195+ * failed to bind; fail fast instead of letting each client thread
1196+ * throw IllegalArgumentException on an invalid port. */
1197+ final int actualPort = server .getBoundPort ();
1198+ if (actualPort <= 0 ) {
1199+ fail ("Server failed to bind to a valid port, got: " +
1200+ actualPort );
1201+ }
1202+
11821203 /* Start up client threads */
11831204 for (int i = 0 ; i < numThreads ; i ++) {
11841205 service .submit (new Runnable () {
11851206 @ Override public void run () {
11861207 SSLSocketClient client =
1187- new SSLSocketClient (localCtx , "localhost" , svrPort );
1208+ new SSLSocketClient (localCtx , "localhost" , actualPort );
11881209 try {
11891210 client .connect ();
11901211 success .incrementAndGet (0 );
@@ -2359,7 +2380,7 @@ public void testSessionResumption() throws Exception {
23592380 SSLSocketFactory cliFactory = ctx .getSocketFactory ();
23602381
23612382 SSLSocket cs = (SSLSocket )cliFactory .createSocket ();
2362- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2383+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
23632384 ss .getLocalPort ()));
23642385
23652386 /* start server */
@@ -2390,8 +2411,8 @@ public Void call() throws Exception {
23902411
23912412 /* connection #2, should resume */
23922413 cs = (SSLSocket )cliFactory .createSocket ();
2393- cs .connect (new InetSocketAddress (InetAddress . getLocalHost (),
2394- ss .getLocalPort ()));
2414+ cs .connect (new InetSocketAddress (
2415+ InetAddress . getLoopbackAddress (), ss .getLocalPort ()));
23952416 cs .startHandshake ();
23962417 sessionID2 = cs .getSession ().getId ();
23972418 cs .close ();
@@ -2462,7 +2483,7 @@ public void testSessionResumptionSysPropDisabled() throws Exception {
24622483 SSLSocketFactory cliFactory = ctx .getSocketFactory ();
24632484
24642485 SSLSocket cs = (SSLSocket )cliFactory .createSocket ();
2465- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2486+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
24662487 ss .getLocalPort ()));
24672488
24682489 /* Start server */
@@ -2493,8 +2514,8 @@ public Void call() throws Exception {
24932514
24942515 /* connection #2, should NOT resume */
24952516 cs = (SSLSocket )cliFactory .createSocket ();
2496- cs .connect (new InetSocketAddress (InetAddress . getLocalHost (),
2497- ss .getLocalPort ()));
2517+ cs .connect (new InetSocketAddress (
2518+ InetAddress . getLoopbackAddress (), ss .getLocalPort ()));
24982519 cs .startHandshake ();
24992520 sessionID2 = cs .getSession ().getId ();
25002521 cs .close ();
@@ -2576,7 +2597,7 @@ public void testSessionResumptionWithTicketEnabled() throws Exception {
25762597
25772598 WolfSSLSocket cs = (WolfSSLSocket )cliFactory .createSocket ();
25782599 cs .setUseSessionTickets (true );
2579- cs .connect (new InetSocketAddress (InetAddress .getLocalHost (),
2600+ cs .connect (new InetSocketAddress (InetAddress .getLoopbackAddress (),
25802601 ss .getLocalPort ()));
25812602
25822603 /* start server */
@@ -2608,8 +2629,8 @@ public Void call() throws Exception {
26082629 /* connection #2, should resume */
26092630 cs = (WolfSSLSocket )cliFactory .createSocket ();
26102631 cs .setUseSessionTickets (true );
2611- cs .connect (new InetSocketAddress (InetAddress . getLocalHost (),
2612- ss .getLocalPort ()));
2632+ cs .connect (new InetSocketAddress (
2633+ InetAddress . getLoopbackAddress (), ss .getLocalPort ()));
26132634 cs .startHandshake ();
26142635 sessionID2 = cs .getSession ().getId ();
26152636 cs .close ();
@@ -4440,7 +4461,7 @@ public X509Certificate[] getAcceptedIssuers() {
44404461
44414462 SSLSocket cs = (SSLSocket )cliCtx .getSocketFactory ().createSocket ();
44424463 cs .connect (new InetSocketAddress (
4443- InetAddress .getLocalHost (), ss .getLocalPort ()));
4464+ InetAddress .getLoopbackAddress (), ss .getLocalPort ()));
44444465
44454466 final SSLSocket server = (SSLSocket )ss .accept ();
44464467
0 commit comments