Skip to content

Commit 3fd7bc5

Browse files
committed
Tests: Use loopback address for local TCP test sockets
1 parent f9f4573 commit 3fd7bc5

3 files changed

Lines changed: 48 additions & 27 deletions

File tree

src/test/com/wolfssl/provider/jsse/test/WolfSSLEngineTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,14 +2323,14 @@ private void doNonBlockingIO(String protocol, int[] serverPort,
23232323
if (!isClient) {
23242324
ssc = ServerSocketChannel.open();
23252325
ssc.socket().bind(new InetSocketAddress(
2326-
InetAddress.getLocalHost(), 0));
2326+
InetAddress.getLoopbackAddress(), 0));
23272327
serverPort[0] = ssc.socket().getLocalPort();
23282328
serverReady[0] = true;
23292329
sc = ssc.accept();
23302330
} else {
23312331
sc = SocketChannel.open();
23322332
sc.connect(new InetSocketAddress(
2333-
InetAddress.getLocalHost(), serverPort[0]));
2333+
InetAddress.getLoopbackAddress(), serverPort[0]));
23342334
engine.setEnabledProtocols(new String[] { protocol });
23352335
}
23362336

src/test/com/wolfssl/provider/jsse/test/WolfSSLSocketTest.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/test/com/wolfssl/test/WolfSSLSessionTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ public Void call() throws Exception {
15501550

15511551
/* Client connection */
15521552
try {
1553-
cliSock = new Socket(InetAddress.getLocalHost(),
1553+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
15541554
srvSocket.getLocalPort());
15551555

15561556
cliSes = new WolfSSLSession(cliCtx);
@@ -1750,7 +1750,7 @@ public Void call() throws Exception {
17501750
/* -------------------------------------------------------------- */
17511751
/* Client connection #1 */
17521752
/* -------------------------------------------------------------- */
1753-
cliSock = new Socket(InetAddress.getLocalHost(),
1753+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
17541754
srvSocket.getLocalPort());
17551755

17561756
cliSes = new WolfSSLSession(cliCtx);
@@ -1810,7 +1810,7 @@ public Void call() throws Exception {
18101810
/* -------------------------------------------------------------- */
18111811
/* Client connection #2, set session and try resumption */
18121812
/* -------------------------------------------------------------- */
1813-
cliSock = new Socket(InetAddress.getLocalHost(),
1813+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
18141814
srvSocket.getLocalPort());
18151815
cliSes = new WolfSSLSession(cliCtx);
18161816

@@ -1997,7 +1997,7 @@ public Void call() throws Exception {
19971997
/* ------------------------------------------------------ */
19981998
/* Client connection #1 */
19991999
/* ------------------------------------------------------ */
2000-
cliSock = new Socket(InetAddress.getLocalHost(),
2000+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
20012001
srvSocket.getLocalPort());
20022002

20032003
cliSes = new WolfSSLSession(cliCtx);
@@ -2059,7 +2059,7 @@ public Void call() throws Exception {
20592059
/* ------------------------------------------------------ */
20602060
/* Client connection #2, set session and try resumption */
20612061
/* ------------------------------------------------------ */
2062-
cliSock = new Socket(InetAddress.getLocalHost(),
2062+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
20632063
srvSocket.getLocalPort());
20642064
cliSes = new WolfSSLSession(cliCtx);
20652065

@@ -2573,7 +2573,7 @@ public Void call() throws Exception {
25732573

25742574
try {
25752575
/* Client connection */
2576-
cliSock = new Socket(InetAddress.getLocalHost(),
2576+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
25772577
srvSocket.getLocalPort());
25782578

25792579
cliSes = new WolfSSLSession(cliCtx);
@@ -2770,7 +2770,7 @@ public Void call() throws Exception {
27702770

27712771
try {
27722772
/* Client connection */
2773-
cliSock = new Socket(InetAddress.getLocalHost(),
2773+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
27742774
srvSocket.getLocalPort());
27752775

27762776
cliSes = new WolfSSLSession(cliCtx);
@@ -2948,7 +2948,7 @@ public Void call() throws Exception {
29482948

29492949
try {
29502950
/* Client connection */
2951-
cliSock = new Socket(InetAddress.getLocalHost(),
2951+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
29522952
srvSocket.getLocalPort());
29532953

29542954
cliSes = new WolfSSLSession(cliCtx);
@@ -3117,7 +3117,7 @@ public Void call() throws Exception {
31173117
/* -------------------------------------------------------------- */
31183118
/* Client connection #1 - get session and serialize it */
31193119
/* -------------------------------------------------------------- */
3120-
cliSock = new Socket(InetAddress.getLocalHost(),
3120+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
31213121
srvSocket.getLocalPort());
31223122

31233123
cliSes = new WolfSSLSession(cliCtx);
@@ -3161,7 +3161,7 @@ public Void call() throws Exception {
31613161
/* -------------------------------------------------------------- */
31623162
/* Client connection #2 - deserialize session and resume */
31633163
/* -------------------------------------------------------------- */
3164-
cliSock = new Socket(InetAddress.getLocalHost(),
3164+
cliSock = new Socket(InetAddress.getLoopbackAddress(),
31653165
srvSocket.getLocalPort());
31663166
cliSes = new WolfSSLSession(cliCtx);
31673167

0 commit comments

Comments
 (0)