unit.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* unit.c unit tests driver */
  2. /* Name change compatibility layer */
  3. #include <cyassl/ssl.h>
  4. #ifdef HAVE_CONFIG_H
  5. #include <config.h>
  6. #endif
  7. #include <cyassl/ctaocrypt/settings.h>
  8. #include <stdio.h>
  9. #include <tests/unit.h>
  10. int myoptind = 0;
  11. char* myoptarg = NULL;
  12. int unit_test(int argc, char** argv);
  13. #ifndef NO_TESTSUITE_MAIN_DRIVER
  14. int main(int argc, char** argv)
  15. {
  16. return unit_test(argc, argv);
  17. }
  18. #endif
  19. int unit_test(int argc, char** argv)
  20. {
  21. int ret;
  22. (void)argc;
  23. (void)argv;
  24. printf("starting unit tests...\n");
  25. #ifdef HAVE_CAVIUM
  26. ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  27. if (ret != 0)
  28. err_sys("Cavium OpenNitroxDevice failed");
  29. #endif /* HAVE_CAVIUM */
  30. #ifndef WOLFSSL_TIRTOS
  31. if (CurrentDir("tests") || CurrentDir("_build"))
  32. ChangeDirBack(1);
  33. else if (CurrentDir("Debug") || CurrentDir("Release"))
  34. ChangeDirBack(3);
  35. #endif
  36. ApiTest();
  37. if ( (ret = HashTest()) != 0){
  38. printf("hash test failed with %d\n", ret);
  39. return ret;
  40. }
  41. #ifndef SINGLE_THREADED
  42. if ( (ret = SuiteTest()) != 0){
  43. printf("suite test failed with %d\n", ret);
  44. return ret;
  45. }
  46. #endif
  47. #ifdef HAVE_CAVIUM
  48. CspShutdown(CAVIUM_DEV_ID);
  49. #endif
  50. return 0;
  51. }
  52. void wait_tcp_ready(func_args* args)
  53. {
  54. #ifdef SINGLE_THREADED
  55. (void)args;
  56. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  57. pthread_mutex_lock(&args->signal->mutex);
  58. if (!args->signal->ready)
  59. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  60. args->signal->ready = 0; /* reset */
  61. pthread_mutex_unlock(&args->signal->mutex);
  62. #else
  63. (void)args;
  64. #endif
  65. }
  66. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  67. {
  68. #ifdef SINGLE_THREADED
  69. (void)fun;
  70. (void)args;
  71. (void)thread;
  72. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  73. pthread_create(thread, 0, fun, args);
  74. return;
  75. #elif defined (WOLFSSL_TIRTOS)
  76. /* Initialize the defaults and set the parameters. */
  77. Task_Params taskParams;
  78. Task_Params_init(&taskParams);
  79. taskParams.arg0 = (UArg)args;
  80. taskParams.stackSize = 65535;
  81. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  82. if (*thread == NULL) {
  83. printf("Failed to create new Task\n");
  84. }
  85. Task_yield();
  86. #else
  87. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  88. #endif
  89. }
  90. void join_thread(THREAD_TYPE thread)
  91. {
  92. #ifdef SINGLE_THREADED
  93. (void)thread;
  94. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  95. pthread_join(thread, 0);
  96. #elif defined (WOLFSSL_TIRTOS)
  97. while(1) {
  98. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  99. Task_sleep(5);
  100. break;
  101. }
  102. Task_yield();
  103. }
  104. #else
  105. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  106. assert(res == WAIT_OBJECT_0);
  107. res = CloseHandle((HANDLE)thread);
  108. assert(res);
  109. #endif
  110. }
  111. void InitTcpReady(tcp_ready* ready)
  112. {
  113. ready->ready = 0;
  114. ready->port = 0;
  115. #ifdef SINGLE_THREADED
  116. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  117. pthread_mutex_init(&ready->mutex, 0);
  118. pthread_cond_init(&ready->cond, 0);
  119. #endif
  120. }
  121. void FreeTcpReady(tcp_ready* ready)
  122. {
  123. #ifdef SINGLE_THREADED
  124. (void)ready;
  125. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  126. pthread_mutex_destroy(&ready->mutex);
  127. pthread_cond_destroy(&ready->cond);
  128. #else
  129. (void)ready;
  130. #endif
  131. }