diff --git a/.CMake/compiler_flags.cmake b/.CMake/compiler_flags.cmake index 5e48080ee..237e7a8f0 100644 --- a/.CMake/compiler_flags.cmake +++ b/.CMake/compiler_flags.cmake @@ -37,6 +37,10 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang") add_compile_options(-fomit-frame-pointer) endif() + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + set(OQS_USE_PTHREADS_IN_TESTS 1) + elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") add_compile_options(-Werror) add_compile_options(-Wall) @@ -75,6 +79,10 @@ elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU") endif () endif() + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + set(OQS_USE_PTHREADS_IN_TESTS 1) + elseif(CMAKE_C_COMPILER_ID STREQUAL "MSVC") # Warning C4146 is raised when a unary minus operator is applied to an # unsigned type; this has nonetheless been standard and portable for as diff --git a/src/oqsconfig.h.cmake b/src/oqsconfig.h.cmake index 58686b4e8..043bfa351 100644 --- a/src/oqsconfig.h.cmake +++ b/src/oqsconfig.h.cmake @@ -9,6 +9,8 @@ #cmakedefine OQS_USE_SHA2_OPENSSL 1 #cmakedefine OQS_USE_SHA3_OPENSSL 1 +#cmakedefine OQS_USE_PTHREADS_IN_TESTS 1 + #cmakedefine OQS_USE_AES_INSTRUCTIONS 1 #cmakedefine OQS_USE_AVX_INSTRUCTIONS 1 #cmakedefine OQS_USE_AVX2_INSTRUCTIONS 1 diff --git a/tests/test_kem.c b/tests/test_kem.c index a9421c4d1..8c748cabe 100644 --- a/tests/test_kem.c +++ b/tests/test_kem.c @@ -5,6 +5,10 @@ #include +#if OQS_USE_PTHREADS_IN_TESTS +#include +#endif + #include "system_info.c" /* Displays hexadecimal strings */ @@ -43,8 +47,8 @@ static OQS_STATUS kem_test_correctness(const char *method_name) { kem = OQS_KEM_new(method_name); if (kem == NULL) { - // should always succeed since we don't call this function on KEMs that aren't enabled - return OQS_ERROR; + fprintf(stderr, "ERROR: OQS_KEM_new failed\n"); + goto err; } printf("================================================================================\n"); @@ -123,6 +127,10 @@ cleanup: OQS_MEM_insecure_free(ciphertext); OQS_KEM_free(kem); +#ifdef OQS_USE_PTHREADS_IN_TESTS + pthread_exit((void *) ret); +#endif + return ret; } @@ -150,7 +158,20 @@ int main(int argc, char **argv) { if (!OQS_KEM_alg_is_enabled(alg_name)) { return EXIT_FAILURE; } - OQS_STATUS rc = kem_test_correctness(alg_name); + OQS_STATUS rc; +#if OQS_USE_PTHREADS_IN_TESTS + pthread_t thread; + void *status; + int trc = pthread_create(&thread, NULL, (void *(*)(void *)) &kem_test_correctness, alg_name); + if (trc) { + fprintf(stderr, "ERROR: Creating pthread\n"); + return EXIT_FAILURE; + } + pthread_join(thread, &status); + rc = (OQS_STATUS) status; +#else + rc = kem_test_correctness(alg_name); +#endif if (rc != OQS_SUCCESS) { return EXIT_FAILURE; } diff --git a/tests/test_sig.c b/tests/test_sig.c index 03d70438e..41ed23d93 100644 --- a/tests/test_sig.c +++ b/tests/test_sig.c @@ -8,6 +8,10 @@ #include +#if OQS_USE_PTHREADS_IN_TESTS +#include +#endif + #include "system_info.c" static OQS_STATUS sig_test_correctness(const char *method_name) { @@ -23,8 +27,8 @@ static OQS_STATUS sig_test_correctness(const char *method_name) { sig = OQS_SIG_new(method_name); if (sig == NULL) { - // should always succeed since we don't call this function on KEMs that aren't enabled - return OQS_ERROR; + fprintf(stderr, "ERROR: OQS_SIG_new failed\n"); + goto err; } printf("================================================================================\n"); @@ -84,6 +88,10 @@ cleanup: OQS_MEM_insecure_free(signature); OQS_SIG_free(sig); +#ifdef OQS_USE_PTHREADS_IN_TESTS + pthread_exit((void *) ret); +#endif + return ret; } @@ -111,7 +119,20 @@ int main(int argc, char **argv) { if (!OQS_SIG_alg_is_enabled(alg_name)) { return EXIT_FAILURE; } - OQS_STATUS rc = sig_test_correctness(alg_name); + OQS_STATUS rc; +#if OQS_USE_PTHREADS_IN_TESTS + pthread_t thread; + void *status; + int trc = pthread_create(&thread, NULL, (void *(*)(void *)) &sig_test_correctness, alg_name); + if (trc) { + fprintf(stderr, "ERROR: Creating pthread\n"); + return EXIT_FAILURE; + } + pthread_join(thread, &status); + rc = (OQS_STATUS) status; +#else + rc = sig_test_correctness(alg_name); +#endif if (rc != OQS_SUCCESS) { return EXIT_FAILURE; }