unit.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2017 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. printf("starting unit tests...\n");
  43. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  44. wolfSSL_Debugging_ON();
  45. #endif
  46. #ifdef HAVE_WNR
  47. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  48. err_sys("Whitewood netRandom global config failed");
  49. #endif /* HAVE_WNR */
  50. #ifndef WOLFSSL_TIRTOS
  51. ChangeToWolfRoot();
  52. #endif
  53. ApiTest();
  54. if ( (ret = HashTest()) != 0){
  55. printf("hash test failed with %d\n", ret);
  56. goto exit;
  57. }
  58. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  59. #ifndef SINGLE_THREADED
  60. if ( (ret = SuiteTest()) != 0){
  61. printf("suite test failed with %d\n", ret);
  62. goto exit;
  63. }
  64. #endif
  65. #endif
  66. SrpTest();
  67. exit:
  68. #ifdef HAVE_WNR
  69. if (wc_FreeNetRandom() < 0)
  70. err_sys("Failed to free netRandom context");
  71. #endif /* HAVE_WNR */
  72. return ret;
  73. }
  74. void wait_tcp_ready(func_args* args)
  75. {
  76. #ifdef SINGLE_THREADED
  77. (void)args;
  78. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  79. pthread_mutex_lock(&args->signal->mutex);
  80. if (!args->signal->ready)
  81. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  82. args->signal->ready = 0; /* reset */
  83. pthread_mutex_unlock(&args->signal->mutex);
  84. #else
  85. (void)args;
  86. #endif
  87. }
  88. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  89. {
  90. #ifdef SINGLE_THREADED
  91. (void)fun;
  92. (void)args;
  93. (void)thread;
  94. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  95. pthread_create(thread, 0, fun, args);
  96. return;
  97. #elif defined (WOLFSSL_TIRTOS)
  98. /* Initialize the defaults and set the parameters. */
  99. Task_Params taskParams;
  100. Task_Params_init(&taskParams);
  101. taskParams.arg0 = (UArg)args;
  102. taskParams.stackSize = 65535;
  103. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  104. if (*thread == NULL) {
  105. printf("Failed to create new Task\n");
  106. }
  107. Task_yield();
  108. #else
  109. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  110. #endif
  111. }
  112. void join_thread(THREAD_TYPE thread)
  113. {
  114. #ifdef SINGLE_THREADED
  115. (void)thread;
  116. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  117. pthread_join(thread, 0);
  118. #elif defined (WOLFSSL_TIRTOS)
  119. while(1) {
  120. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  121. Task_sleep(5);
  122. break;
  123. }
  124. Task_yield();
  125. }
  126. #else
  127. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  128. assert(res == WAIT_OBJECT_0);
  129. res = CloseHandle((HANDLE)thread);
  130. assert(res);
  131. (void)res; /* Suppress un-used variable warning */
  132. #endif
  133. }