2
0

unit.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2021 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Name change compatibility layer no longer need to be included here */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #include <stdio.h>
  27. #include <tests/unit.h>
  28. int myoptind = 0;
  29. char* myoptarg = NULL;
  30. int unit_test(int argc, char** argv);
  31. #ifndef NO_TESTSUITE_MAIN_DRIVER
  32. int main(int argc, char** argv)
  33. {
  34. return unit_test(argc, argv);
  35. }
  36. #endif
  37. int unit_test(int argc, char** argv)
  38. {
  39. int ret = 0;
  40. (void)argc;
  41. (void)argv;
  42. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  43. if (argc > 1) {
  44. word32 memFailCount = atoi(argv[1]);
  45. printf("\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
  46. wolfSSL_SetMemFailCount(memFailCount);
  47. }
  48. #endif
  49. printf("starting unit tests...\n");
  50. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  51. wolfSSL_Debugging_ON();
  52. #endif
  53. #ifdef HAVE_WNR
  54. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  55. err_sys("Whitewood netRandom global config failed");
  56. #endif /* HAVE_WNR */
  57. #ifndef WOLFSSL_TIRTOS
  58. ChangeToWolfRoot();
  59. #endif
  60. ApiTest();
  61. if ( (ret = HashTest()) != 0){
  62. printf("hash test failed with %d\n", ret);
  63. goto exit;
  64. }
  65. #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
  66. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  67. #ifndef SINGLE_THREADED
  68. if ( (ret = SuiteTest(argc, argv)) != 0){
  69. printf("suite test failed with %d\n", ret);
  70. goto exit;
  71. }
  72. #endif
  73. #endif
  74. #endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
  75. SrpTest();
  76. exit:
  77. #ifdef HAVE_WNR
  78. if (wc_FreeNetRandom() < 0)
  79. err_sys("Failed to free netRandom context");
  80. #endif /* HAVE_WNR */
  81. return ret;
  82. }
  83. void wait_tcp_ready(func_args* args)
  84. {
  85. #ifdef SINGLE_THREADED
  86. (void)args;
  87. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  88. pthread_mutex_lock(&args->signal->mutex);
  89. if (!args->signal->ready)
  90. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  91. args->signal->ready = 0; /* reset */
  92. pthread_mutex_unlock(&args->signal->mutex);
  93. #else
  94. (void)args;
  95. #endif
  96. }
  97. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  98. {
  99. #ifdef SINGLE_THREADED
  100. (void)fun;
  101. (void)args;
  102. (void)thread;
  103. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  104. pthread_create(thread, 0, fun, args);
  105. return;
  106. #elif defined (WOLFSSL_TIRTOS)
  107. /* Initialize the defaults and set the parameters. */
  108. Task_Params taskParams;
  109. Task_Params_init(&taskParams);
  110. taskParams.arg0 = (UArg)args;
  111. taskParams.stackSize = 65535;
  112. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  113. if (*thread == NULL) {
  114. printf("Failed to create new Task\n");
  115. }
  116. Task_yield();
  117. #else
  118. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  119. #endif
  120. }
  121. void join_thread(THREAD_TYPE thread)
  122. {
  123. #ifdef SINGLE_THREADED
  124. (void)thread;
  125. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  126. pthread_join(thread, 0);
  127. #elif defined (WOLFSSL_TIRTOS)
  128. while(1) {
  129. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  130. Task_sleep(5);
  131. break;
  132. }
  133. Task_yield();
  134. }
  135. #else
  136. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  137. assert(res == WAIT_OBJECT_0);
  138. res = CloseHandle((HANDLE)thread);
  139. assert(res);
  140. (void)res; /* Suppress un-used variable warning */
  141. #endif
  142. }