Skip to content

Commit 94281c3

Browse files
committed
JSSE: add CountDownLatch to WolfSSLSocketTest TestServer/Client to wait until threads are finished before checking for Exceptions
1 parent dc5e43c commit 94281c3

1 file changed

Lines changed: 76 additions & 24 deletions

File tree

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

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
468468
TestClient client = null;
469469
Exception srvException = null;
470470
Exception cliException = null;
471+
CountDownLatch sDoneLatch = null;
472+
CountDownLatch cDoneLatch = null;
471473

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

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

489491
TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
490492
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
491-
server = new TestServer(this.ctx, ss, sArgs, 1);
493+
494+
sDoneLatch = new CountDownLatch(1);
495+
cDoneLatch = new CountDownLatch(1);
496+
497+
server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
492498
server.start();
493-
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
499+
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
500+
cDoneLatch);
494501
client.start();
495502

503+
cDoneLatch.await();
504+
sDoneLatch.await();
505+
496506
srvException = server.getException();
497507
if (srvException != null) {
498508
Security.setProperty("wolfjsse.enabledSupportedCurves",
@@ -530,11 +540,19 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
530540

531541
TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
532542
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
533-
server = new TestServer(this.ctx, ss, sArgs, 1);
543+
544+
sDoneLatch = new CountDownLatch(1);
545+
cDoneLatch = new CountDownLatch(1);
546+
547+
server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
534548
server.start();
535-
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
549+
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
550+
cDoneLatch);
536551
client.start();
537552

553+
cDoneLatch.await();
554+
sDoneLatch.await();
555+
538556
srvException = server.getException();
539557
if (srvException != null) {
540558
Security.setProperty("wolfjsse.enabledSupportedCurves",
@@ -572,11 +590,19 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
572590

573591
TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
574592
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
575-
server = new TestServer(this.ctx, ss, sArgs, 1);
593+
594+
sDoneLatch = new CountDownLatch(1);
595+
cDoneLatch = new CountDownLatch(1);
596+
597+
server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
576598
server.start();
577-
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
599+
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
600+
cDoneLatch);
578601
client.start();
579602

603+
cDoneLatch.await();
604+
sDoneLatch.await();
605+
580606
srvException = server.getException();
581607
if (srvException != null) {
582608
Security.setProperty("wolfjsse.enabledSupportedCurves",
@@ -603,7 +629,9 @@ public void testEnabledSupportedCurvesProperty() throws Exception {
603629
}
604630
}
605631

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

615-
TestArgs sArgs = new TestArgs(null, null, true, true, true, null);
616643
TestArgs cArgs = new TestArgs(null, null, false, false, true, null);
617-
server = new TestServer(this.ctx, ss, sArgs, 1);
618-
server.start();
619-
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
644+
645+
cDoneLatch = new CountDownLatch(1);
646+
647+
client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
648+
cDoneLatch);
620649
client.start();
621650

622-
srvException = server.getException();
623-
if (srvException != null) {
624-
Security.setProperty("wolfjsse.enabledSupportedCurves",
625-
originalProperty);
626-
throw srvException;
627-
}
651+
cDoneLatch.await();
628652

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

634658
try {
635659
client.join(1000);
636-
server.join(1000);
660+
//server.join(1000);
637661

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

686+
CountDownLatch sDoneLatch = null;
687+
CountDownLatch cDoneLatch = null;
688+
662689
System.out.print("\tTesting basic client/server");
663690

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

674-
TestServer server = new TestServer(this.ctx, ss, sArgs, 1);
701+
sDoneLatch = new CountDownLatch(1);
702+
cDoneLatch = new CountDownLatch(1);
703+
704+
TestServer server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
675705
server.start();
676706

677-
TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
707+
TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
708+
cDoneLatch);
678709
client.start();
679710

711+
cDoneLatch.await();
712+
sDoneLatch.await();
680713

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

739+
CountDownLatch sDoneLatch = null;
740+
CountDownLatch cDoneLatch = null;
741+
706742
if (sArgs == null || cArgs == null) {
707743
throw new Exception("client/server TestArgs can not be null");
708744
}
@@ -713,12 +749,19 @@ public void alpnClientServerRunner(TestArgs sArgs, TestArgs cArgs,
713749
SSLServerSocket ss = (SSLServerSocket)ctx.getServerSocketFactory()
714750
.createServerSocket(0);
715751

716-
TestServer server = new TestServer(this.ctx, ss, sArgs, 1);
752+
sDoneLatch = new CountDownLatch(1);
753+
cDoneLatch = new CountDownLatch(1);
754+
755+
TestServer server = new TestServer(this.ctx, ss, sArgs, 1, sDoneLatch);
717756
server.start();
718757

719-
TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs);
758+
TestClient client = new TestClient(this.ctx, ss.getLocalPort(), cArgs,
759+
cDoneLatch);
720760
client.start();
721761

762+
cDoneLatch.await();
763+
sDoneLatch.await();
764+
722765
try {
723766
client.join(1000);
724767
server.join(1000);
@@ -3357,13 +3400,15 @@ protected class TestServer extends Thread
33573400
private int numConnections = 1;
33583401
WolfSSLSocketTest wst;
33593402
SSLServerSocket ss = null;
3403+
CountDownLatch doneLatch = null;
33603404

33613405
public TestServer(SSLContext ctx, SSLServerSocket ss,
3362-
TestArgs args, int numConnections) {
3406+
TestArgs args, int numConnections, CountDownLatch doneLatch) {
33633407
this.ctx = ctx;
33643408
this.ss = ss;
33653409
this.args = args;
33663410
this.numConnections = numConnections;
3411+
this.doneLatch = doneLatch;
33673412
}
33683413

33693414

@@ -3443,6 +3488,8 @@ public void run() {
34433488

34443489
} catch (Exception e) {
34453490
this.exception = e;
3491+
} finally {
3492+
this.doneLatch.countDown();
34463493
}
34473494
}
34483495

@@ -3470,11 +3517,14 @@ protected class TestClient extends Thread
34703517
private Exception exception = null;
34713518
private TestArgs args = null;
34723519
WolfSSLSocketTest wst;
3520+
CountDownLatch doneLatch = null;
34733521

3474-
public TestClient(SSLContext ctx, int port, TestArgs args) {
3522+
public TestClient(SSLContext ctx, int port, TestArgs args,
3523+
CountDownLatch doneLatch) {
34753524
this.ctx = ctx;
34763525
this.srvPort = port;
34773526
this.args = args;
3527+
this.doneLatch = doneLatch;
34783528
}
34793529

34803530
@Override
@@ -3539,6 +3589,8 @@ public void run() {
35393589

35403590
} catch (Exception e) {
35413591
this.exception = e;
3592+
} finally {
3593+
this.doneLatch.countDown();
35423594
}
35433595
}
35443596

0 commit comments

Comments
 (0)