Skip to content

Commit bb5243e

Browse files
committed
Wait for spawned fork-test to avoid race condition
Each spawned testcase is now exited to avoid running the tests multiple times. This fixes a race condition seen with a newer GNU libc version. Signed-off-by: Björn Svensson <[email protected]>
1 parent ebf6ff7 commit bb5243e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/lib/test/ForkTests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "osmutex.h"
4141

4242
#include <sys/types.h>
43+
#include <sys/wait.h>
4344
#include <unistd.h>
4445

4546
CPPUNIT_TEST_SUITE_REGISTRATION(ForkTests);
@@ -64,6 +65,7 @@ void ForkTests::testFork()
6465
{
6566
CK_RV rv;
6667
pid_t pid;
68+
int status;
6769

6870
// Just make sure that we finalize any previous failed tests
6971
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
@@ -78,12 +80,20 @@ void ForkTests::testFork()
7880
CPPUNIT_FAIL("Fork failed");
7981
break;
8082
case 0:
83+
/* For the child, the token is expected to still be initialized. */
8184
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
8285
CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
86+
rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
87+
CPPUNIT_ASSERT(rv == CKR_OK);
88+
_exit(EXIT_SUCCESS);
8389
break;
8490
default:
91+
/* For the parent, the token is expected to still be initialized. */
8592
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
8693
CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
94+
/* Wait for the child process to finish and check its status. */
95+
CPPUNIT_ASSERT(waitpid(pid, &status, 0) == pid);
96+
CPPUNIT_ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
8797
break;
8898
}
8999

@@ -95,6 +105,7 @@ void ForkTests::testResetOnFork()
95105
{
96106
CK_RV rv;
97107
pid_t pid;
108+
int status;
98109

99110
// Just make sure that we finalize any previous failed tests
100111
CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
@@ -118,11 +129,17 @@ void ForkTests::testResetOnFork()
118129
/* For the child, the token is expected to be reset on fork */
119130
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
120131
CPPUNIT_ASSERT(rv == CKR_OK);
132+
rv = CRYPTOKI_F_PTR( C_Finalize(NULL_PTR) );
133+
CPPUNIT_ASSERT(rv == CKR_OK);
134+
_exit(EXIT_SUCCESS);
121135
break;
122136
default:
123137
/* For the parent, the token is expected to be still initialized */
124138
rv = CRYPTOKI_F_PTR( C_Initialize(NULL_PTR) );
125139
CPPUNIT_ASSERT(rv == CKR_CRYPTOKI_ALREADY_INITIALIZED);
140+
/* Wait for the child process to finish and check its status. */
141+
CPPUNIT_ASSERT(waitpid(pid, &status, 0) == pid);
142+
CPPUNIT_ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
126143
break;
127144
}
128145

0 commit comments

Comments
 (0)