unit.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <wolfssl/wolfcrypt/fips_test.h>
  29. int myoptind = 0;
  30. char* myoptarg = NULL;
  31. int unit_test(int argc, char** argv);
  32. #ifndef NO_TESTSUITE_MAIN_DRIVER
  33. int main(int argc, char** argv)
  34. {
  35. return unit_test(argc, argv);
  36. }
  37. #endif
  38. int unit_test(int argc, char** argv)
  39. {
  40. int ret = 0;
  41. (void)argc;
  42. (void)argv;
  43. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  44. if (argc > 1) {
  45. int memFailCount = atoi(argv[1]);
  46. printf("\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
  47. wolfSSL_SetMemFailCount(memFailCount);
  48. }
  49. #endif
  50. printf("starting unit tests...\n");
  51. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  52. wolfSSL_Debugging_ON();
  53. #endif
  54. #ifdef WC_RNG_SEED_CB
  55. wc_SetSeed_Cb(wc_GenerateSeed);
  56. #endif
  57. #ifdef HAVE_WNR
  58. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  59. err_sys("Whitewood netRandom global config failed");
  60. #endif /* HAVE_WNR */
  61. #ifndef WOLFSSL_TIRTOS
  62. ChangeToWolfRoot();
  63. #endif
  64. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
  65. if (wc_RunCast_fips(FIPS_CAST_AES_CBC) != 0) {
  66. err_sys("AES-CBC CAST failed");
  67. }
  68. if (wc_RunCast_fips(FIPS_CAST_AES_GCM) != 0) {
  69. err_sys("AES-GCM CAST failed");
  70. }
  71. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA1) != 0) {
  72. err_sys("HMAC-SHA1 CAST failed");
  73. }
  74. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_256) != 0) {
  75. err_sys("HMAC-SHA2-256 CAST failed");
  76. }
  77. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_512) != 0) {
  78. err_sys("HMAC-SHA2-512 CAST failed");
  79. }
  80. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA3_256) != 0) {
  81. err_sys("HMAC-SHA3-256 CAST failed");
  82. }
  83. if (wc_RunCast_fips(FIPS_CAST_DRBG) != 0) {
  84. err_sys("Hash_DRBG CAST failed");
  85. }
  86. if (wc_RunCast_fips(FIPS_CAST_RSA_SIGN_PKCS1v15) != 0) {
  87. err_sys("RSA sign CAST failed");
  88. }
  89. if (wc_RunCast_fips(FIPS_CAST_ECC_PRIMITIVE_Z) != 0) {
  90. err_sys("ECC Primitive Z CAST failed");
  91. }
  92. if (wc_RunCast_fips(FIPS_CAST_DH_PRIMITIVE_Z) != 0) {
  93. err_sys("DH Primitive Z CAST failed");
  94. }
  95. if (wc_RunCast_fips(FIPS_CAST_ECDSA) != 0) {
  96. err_sys("ECDSA CAST failed");
  97. }
  98. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS12) != 0) {
  99. err_sys("KDF TLSv1.2 CAST failed");
  100. }
  101. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS13) != 0) {
  102. err_sys("KDF TLSv1.3 CAST failed");
  103. }
  104. if (wc_RunCast_fips(FIPS_CAST_KDF_SSH) != 0) {
  105. err_sys("KDF SSHv2.0 CAST failed");
  106. }
  107. #endif
  108. #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
  109. if (argc == 1)
  110. #endif
  111. {
  112. ApiTest();
  113. if ( (ret = HashTest()) != 0){
  114. printf("hash test failed with %d\n", ret);
  115. goto exit;
  116. }
  117. }
  118. #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
  119. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  120. #ifndef SINGLE_THREADED
  121. if ( (ret = SuiteTest(argc, argv)) != 0){
  122. printf("suite test failed with %d\n", ret);
  123. goto exit;
  124. }
  125. #endif
  126. #endif
  127. #endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
  128. SrpTest();
  129. exit:
  130. #ifdef HAVE_WNR
  131. if (wc_FreeNetRandom() < 0)
  132. err_sys("Failed to free netRandom context");
  133. #endif /* HAVE_WNR */
  134. return ret;
  135. }
  136. void wait_tcp_ready(func_args* args)
  137. {
  138. #ifdef SINGLE_THREADED
  139. (void)args;
  140. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  141. pthread_mutex_lock(&args->signal->mutex);
  142. if (!args->signal->ready)
  143. pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
  144. args->signal->ready = 0; /* reset */
  145. pthread_mutex_unlock(&args->signal->mutex);
  146. #else
  147. (void)args;
  148. #endif
  149. }
  150. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  151. {
  152. #ifdef SINGLE_THREADED
  153. (void)fun;
  154. (void)args;
  155. (void)thread;
  156. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  157. pthread_create(thread, 0, fun, args);
  158. return;
  159. #elif defined (WOLFSSL_TIRTOS)
  160. /* Initialize the defaults and set the parameters. */
  161. Task_Params taskParams;
  162. Task_Params_init(&taskParams);
  163. taskParams.arg0 = (UArg)args;
  164. taskParams.stackSize = 65535;
  165. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  166. if (*thread == NULL) {
  167. printf("Failed to create new Task\n");
  168. }
  169. Task_yield();
  170. #else
  171. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  172. #endif
  173. }
  174. void join_thread(THREAD_TYPE thread)
  175. {
  176. #ifdef SINGLE_THREADED
  177. (void)thread;
  178. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  179. pthread_join(thread, 0);
  180. #elif defined (WOLFSSL_TIRTOS)
  181. while(1) {
  182. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  183. Task_sleep(5);
  184. break;
  185. }
  186. Task_yield();
  187. }
  188. #else
  189. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  190. assert(res == WAIT_OBJECT_0);
  191. res = CloseHandle((HANDLE)thread);
  192. assert(res);
  193. (void)res; /* Suppress un-used variable warning */
  194. #endif
  195. }