Skip to content

Commit

Permalink
JSSE: add CountDownLatch to WolfSSLSocketTest TestServer/Client to wa…
Browse files Browse the repository at this point in the history
…it until threads are finished before checking for Exceptions
  • Loading branch information
cconlon committed Jan 20, 2025
1 parent f00b7c0 commit 4bc8162
Showing 1 changed file with 76 additions and 24 deletions.
100 changes: 76 additions & 24 deletions src/test/com/wolfssl/provider/jsse/test/WolfSSLSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
TestClient client = null;
Exception srvException = null;
Exception cliException = null;
CountDownLatch sDoneLatch = null;
CountDownLatch cDoneLatch = null;

System.out.print("\twolfjsse.enabledSupportedCurves");

Expand All @@ -488,11 +490,19 @@ public void testEnabledSupportedCurvesProperty() throws Exception {

TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
server = new TestServer(this.ctx, ss, sArgs, 1);

sDoneLatch = new CountDownLatch(1);
cDoneLatch = new CountDownLatch(1);

server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
server.start();
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
cDoneLatch);
client.start();

cDoneLatch.await();
sDoneLatch.await();

srvException = server.getException();
if (srvException != null) {
Security.setProperty("wolfjsse.enabledSupportedCurves",
Expand Down Expand Up @@ -530,11 +540,19 @@ public void testEnabledSupportedCurvesProperty() throws Exception {

TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
server = new TestServer(this.ctx, ss, sArgs, 1);

sDoneLatch = new CountDownLatch(1);
cDoneLatch = new CountDownLatch(1);

server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
server.start();
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
cDoneLatch);
client.start();

cDoneLatch.await();
sDoneLatch.await();

srvException = server.getException();
if (srvException != null) {
Security.setProperty("wolfjsse.enabledSupportedCurves",
Expand Down Expand Up @@ -572,11 +590,19 @@ public void testEnabledSupportedCurvesProperty() throws Exception {

TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
server = new TestServer(this.ctx, ss, sArgs, 1);

sDoneLatch = new CountDownLatch(1);
cDoneLatch = new CountDownLatch(1);

server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
server.start();
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
cDoneLatch);
client.start();

cDoneLatch.await();
sDoneLatch.await();

srvException = server.getException();
if (srvException != null) {
Security.setProperty("wolfjsse.enabledSupportedCurves",
Expand All @@ -603,7 +629,9 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
}
}

/* Test with invalid property entries */
/* Test with invalid property entries.
* Only need to start client thread, since it throws exception
* before connecting to server. */
{
Security.setProperty("wolfjsse.enabledSupportedCurves",
"badone, badtwo");
Expand All @@ -612,19 +640,15 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
ss = (SSLServerSocket)ctx.getServerSocketFactory()
.createServerSocket(0);

TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
server = new TestServer(this.ctx, ss, sArgs, 1);
server.start();
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);

cDoneLatch = new CountDownLatch(1);

client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
cDoneLatch);
client.start();

srvException = server.getException();
if (srvException != null) {
Security.setProperty("wolfjsse.enabledSupportedCurves",
originalProperty);
throw srvException;
}
cDoneLatch.await();

cliException = client.getException();
if (cliException != null) {
Expand All @@ -633,7 +657,7 @@ public void testEnabledSupportedCurvesProperty() throws Exception {

try {
client.join(1000);
server.join(1000);
//server.join(1000);

} catch (InterruptedException e) {
System.out.println("interrupt happened");
Expand All @@ -659,6 +683,9 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
@Test
public void testClientServerThreaded() throws Exception {

CountDownLatch sDoneLatch = null;
CountDownLatch cDoneLatch = null;

System.out.print("\tTesting basic client/server");

/* create new CTX */
Expand All @@ -671,12 +698,18 @@ public void testClientServerThreaded() throws Exception {
TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);

TestServer server = new TestServer(this.ctx, ss, sArgs, 1);
sDoneLatch = new CountDownLatch(1);
cDoneLatch = new CountDownLatch(1);

TestServer server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
server.start();

TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
cDoneLatch);
client.start();

cDoneLatch.await();
sDoneLatch.await();

Exception srvException = server.getException();
if (srvException != null) {
Expand All @@ -703,6 +736,9 @@ public void testClientServerThreaded() throws Exception {
public void alpnClientServerRunner(TestArgs sArgs, TestArgs cArgs,
boolean expectingException) throws Exception {

CountDownLatch sDoneLatch = null;
CountDownLatch cDoneLatch = null;

if (sArgs == null || cArgs == null) {
throw new Exception("client/server TestArgs can not be null");
}
Expand All @@ -713,12 +749,19 @@ public void alpnClientServerRunner(TestArgs sArgs, TestArgs cArgs,
SSLServerSocket ss = (SSLServerSocket)ctx.getServerSocketFactory()
.createServerSocket(0);

TestServer server = new TestServer(this.ctx, ss, sArgs, 1);
sDoneLatch = new CountDownLatch(1);
cDoneLatch = new CountDownLatch(1);

TestServer server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
server.start();

TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
cDoneLatch);
client.start();

cDoneLatch.await();
sDoneLatch.await();

try {
client.join(1000);
server.join(1000);
Expand Down Expand Up @@ -3357,13 +3400,15 @@ protected class TestServer extends Thread
private int numConnections = 1;
WolfSSLSocketTest wst;
SSLServerSocket ss = null;
CountDownLatch doneLatch = null;

public TestServer(SSLContext ctx, SSLServerSocket ss,
TestArgs args, int numConnections) {
TestArgs args, int numConnections, CountDownLatch doneLatch) {
this.ctx = ctx;
this.ss = ss;
this.args = args;
this.numConnections = numConnections;
this.doneLatch = doneLatch;
}


Expand Down Expand Up @@ -3443,6 +3488,8 @@ public void run() {

} catch (Exception e) {
this.exception = e;
} finally {
this.doneLatch.countDown();
}
}

Expand Down Expand Up @@ -3470,11 +3517,14 @@ protected class TestClient extends Thread
private Exception exception = null;
private TestArgs args = null;
WolfSSLSocketTest wst;
CountDownLatch doneLatch = null;

public TestClient(SSLContext ctx, int port, TestArgs args) {
public TestClient(SSLContext ctx, int port, TestArgs args,
CountDownLatch doneLatch) {
this.ctx = ctx;
this.srvPort = port;
this.args = args;
this.doneLatch = doneLatch;
}

@Override
Expand Down Expand Up @@ -3539,6 +3589,8 @@ public void run() {

} catch (Exception e) {
this.exception = e;
} finally {
this.doneLatch.countDown();
}
}

Expand Down

0 comments on commit 4bc8162

Please sign in to comment.