Codebase list openssl / 440bce8
Add a multithread rand test Reviewed-by: Tim Hudson <tjh@openssl.org> Reviewed-by: Paul Dale <paul.dale@oracle.com> Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com> (Merged from https://github.com/openssl/openssl/pull/5547) Kurt Roeckx authored 6 years ago Dr. Matthias St. Pierre committed 6 years ago
1 changed file(s) with 89 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1515 #include <openssl/evp.h>
1616 #include <openssl/aes.h>
1717 #include "../crypto/rand/rand_lcl.h"
18
19 #if defined(_WIN32)
20 # include <windows.h>
21 #endif
1822
1923 #include "testutil.h"
2024 #include "drbgtest.h"
761765 return rv;
762766 }
763767
768 #if defined(OPENSSL_THREADS)
769
770 static void run_multi_thread_test(void)
771 {
772 unsigned char buf[256];
773 time_t start = time(NULL);
774 RAND_DRBG *public, *private;
775
776 public = RAND_DRBG_get0_public();
777 private = RAND_DRBG_get0_private();
778 RAND_DRBG_set_reseed_time_interval(public, 1);
779 RAND_DRBG_set_reseed_time_interval(private, 1);
780
781 do {
782 RAND_bytes(buf, sizeof(buf));
783 RAND_priv_bytes(buf, sizeof(buf));
784 }
785 while(time(NULL) - start < 5);
786 }
787
788 # if defined(OPENSSL_SYS_WINDOWS)
789
790 typedef HANDLE thread_t;
791
792 static DWORD WINAPI thread_run(LPVOID arg)
793 {
794 run_multi_thread_test();
795 return 0;
796 }
797
798 static int run_thread(thread_t *t)
799 {
800 *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
801 return *t != NULL;
802 }
803
804 static int wait_for_thread(thread_t thread)
805 {
806 return WaitForSingleObject(thread, INFINITE) == 0;
807 }
808
809 # else
810
811 typedef pthread_t thread_t;
812
813 static void *thread_run(void *arg)
814 {
815 run_multi_thread_test();
816 return NULL;
817 }
818
819 static int run_thread(thread_t *t)
820 {
821 return pthread_create(t, NULL, thread_run, NULL) == 0;
822 }
823
824 static int wait_for_thread(thread_t thread)
825 {
826 return pthread_join(thread, NULL) == 0;
827 }
828
829 # endif
830
831 /*
832 * The main thread will also run the test, so we'll have THREADS+1 parallel
833 * tests running
834 */
835 #define THREADS 3
836
837 static int test_multi_thread(void)
838 {
839 thread_t t[THREADS];
840 int i;
841
842 for (i = 0; i < THREADS; i++)
843 run_thread(&t[i]);
844 run_multi_thread_test();
845 for (i = 0; i < THREADS; i++)
846 wait_for_thread(t[i]);
847 return 1;
848 }
849 #endif
764850
765851 int setup_tests(void)
766852 {
769855 ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
770856 ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
771857 ADD_TEST(test_rand_reseed);
858 #if defined(OPENSSL_THREADS)
859 ADD_TEST(test_multi_thread);
860 #endif
772861 return 1;
773862 }