From b49be0e0aa60f9dc6b314cc1c4dcada4d8cfead6 Mon Sep 17 00:00:00 2001 From: John Bland Date: Mon, 16 Jan 2023 17:04:49 -0500 Subject: [PATCH] add thread local storage test to the unit tests --- configure.ac | 2 +- tests/unit_tests.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 9fdff35..1f6de94 100644 --- a/configure.ac +++ b/configure.ac @@ -85,7 +85,7 @@ AX_PTHREAD([ # -Xcompiler libtool will use it. Newer versions of clang don't need # the -Q flag when using pthreads. AS_CASE([$PTHREAD_CFLAGS],[-Qunused-arguments*],[PTHREAD_CFLAGS="-Xcompiler $PTHREAD_CFLAGS"]) - AM_CFLAGS="$AM_CFLAGS $PTHREAD_CFLAGS"]) + AM_CFLAGS="$AM_CFLAGS $PTHREAD_CFLAGS -DHAVE_PTHREAD"]) # Checks for typedefs, structures, and compiler characteristics. diff --git a/tests/unit_tests.c b/tests/unit_tests.c index 2066d1d..2d6e37b 100644 --- a/tests/unit_tests.c +++ b/tests/unit_tests.c @@ -327,6 +327,56 @@ static void test_wolfTPM2_CSR(void) #endif } +#if defined(HAVE_THREAD_LS) && defined(HAVE_PTHREAD) +#include +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +int secondRunner = 0; + +static void test_wolfTPM2_thread_local_storage_work_thread(void* args) +{ + TPM2_CTX tpm2Ctx; + + TPM2_Init(&tpm2Ctx, NULL, NULL); + + /* lock so that the other thread must wait while we set the ctx */ + pthread_mutex_lock(&mutex); + + /* ctx should be what was set in init, not set by other thread */ + if (secondRunner == 1) { + if (TPM2_GetActiveCtx() != &tpm2Ctx) + printf("Test TPM Wrapper:\tThread Local Storage\tFailed\n"); + else + printf("Test TPM Wrapper:\tThread Local Storage\tPassed\n"); + } + + /* set the active ctx, should not impact the other thread */ + TPM2_SetActiveCtx(&tpm2Ctx); + + secondRunner = 1; + + /* let the other thread run */ + pthread_mutex_unlock(&mutex); + + (void)args; +} +#endif /* HAVE_THREAD_LS && HAVE_PTHREAD */ + +static void test_wolfTPM2_thread_local_storage(void) +{ +#if defined(HAVE_THREAD_LS) && defined(HAVE_PTHREAD) + pthread_t thread_1; + pthread_t thread_2; + + pthread_create(&thread_1, NULL, + (void*)&test_wolfTPM2_thread_local_storage_work_thread, NULL); + pthread_create(&thread_2, NULL, + (void*)&test_wolfTPM2_thread_local_storage_work_thread, NULL); + + pthread_join(thread_1, NULL); + pthread_join(thread_2, NULL); +#endif /* HAVE_THREAD_LS && HAVE_PTHREAD */ +} + #endif /* !WOLFTPM2_NO_WRAPPER */ #ifndef NO_MAIN_DRIVER @@ -347,6 +397,7 @@ int unit_tests(int argc, char *argv[]) test_wolfTPM2_ReadPublicKey(); test_wolfTPM2_CSR(); test_wolfTPM2_Cleanup(); + test_wolfTPM2_thread_local_storage(); #endif /* !WOLFTPM2_NO_WRAPPER */ return 0;