Skip to content

Commit f75b947

Browse files
committed
Tests: Use loopback address for local TCP test sockets
1 parent aed6c20 commit f75b947

3 files changed

Lines changed: 41 additions & 24 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: 29 additions & 12 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,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

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)