unit.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2022 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. fprintf(stderr, "\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
  47. wolfSSL_SetMemFailCount(memFailCount);
  48. }
  49. #endif
  50. printf("starting unit tests...\n");
  51. fflush(stdout);
  52. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  53. wolfSSL_Debugging_ON();
  54. #endif
  55. #ifdef WC_RNG_SEED_CB
  56. wc_SetSeed_Cb(wc_GenerateSeed);
  57. #endif
  58. #ifdef HAVE_WNR
  59. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  60. err_sys("Whitewood netRandom global config failed");
  61. #endif /* HAVE_WNR */
  62. #ifndef WOLFSSL_TIRTOS
  63. ChangeToWolfRoot();
  64. #endif
  65. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
  66. #if !defined(NO_AES) && !defined(NO_AES_CBC)
  67. if (wc_RunCast_fips(FIPS_CAST_AES_CBC) != 0) {
  68. err_sys("AES-CBC CAST failed");
  69. }
  70. #endif
  71. #ifdef HAVE_AESGCM
  72. if (wc_RunCast_fips(FIPS_CAST_AES_GCM) != 0) {
  73. err_sys("AES-GCM CAST failed");
  74. }
  75. #endif
  76. #ifndef NO_SHA
  77. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA1) != 0) {
  78. err_sys("HMAC-SHA1 CAST failed");
  79. }
  80. #endif
  81. /* the only non-optional CAST */
  82. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_256) != 0) {
  83. err_sys("HMAC-SHA2-256 CAST failed");
  84. }
  85. #ifdef WOLFSSL_SHA512
  86. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_512) != 0) {
  87. err_sys("HMAC-SHA2-512 CAST failed");
  88. }
  89. #endif
  90. #ifdef WOLFSSL_SHA3
  91. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA3_256) != 0) {
  92. err_sys("HMAC-SHA3-256 CAST failed");
  93. }
  94. #endif
  95. #ifdef HAVE_HASHDRBG
  96. if (wc_RunCast_fips(FIPS_CAST_DRBG) != 0) {
  97. err_sys("Hash_DRBG CAST failed");
  98. }
  99. #endif
  100. #ifndef NO_RSA
  101. if (wc_RunCast_fips(FIPS_CAST_RSA_SIGN_PKCS1v15) != 0) {
  102. err_sys("RSA sign CAST failed");
  103. }
  104. #endif
  105. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_CDH_CAST)
  106. if (wc_RunCast_fips(FIPS_CAST_ECC_CDH) != 0) {
  107. err_sys("RSA sign CAST failed");
  108. }
  109. #endif
  110. #ifdef HAVE_ECC_DHE
  111. if (wc_RunCast_fips(FIPS_CAST_ECC_PRIMITIVE_Z) != 0) {
  112. err_sys("ECC Primitive Z CAST failed");
  113. }
  114. #endif
  115. #ifdef HAVE_ECC
  116. if (wc_RunCast_fips(FIPS_CAST_ECDSA) != 0) {
  117. err_sys("ECDSA CAST failed");
  118. }
  119. #endif
  120. #ifndef NO_DH
  121. if (wc_RunCast_fips(FIPS_CAST_DH_PRIMITIVE_Z) != 0) {
  122. err_sys("DH Primitive Z CAST failed");
  123. }
  124. #endif
  125. #ifdef WOLFSSL_HAVE_PRF
  126. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS12) != 0) {
  127. err_sys("KDF TLSv1.2 CAST failed");
  128. }
  129. #endif
  130. #if defined(WOLFSSL_HAVE_PRF) && defined(WOLFSSL_TLS13)
  131. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS13) != 0) {
  132. err_sys("KDF TLSv1.3 CAST failed");
  133. }
  134. #endif
  135. #ifdef WOLFSSL_WOLFSSH
  136. if (wc_RunCast_fips(FIPS_CAST_KDF_SSH) != 0) {
  137. err_sys("KDF SSHv2.0 CAST failed");
  138. }
  139. #endif
  140. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
  141. #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
  142. if (argc == 1)
  143. #endif
  144. {
  145. ApiTest();
  146. if ( (ret = HashTest()) != 0){
  147. fprintf(stderr, "hash test failed with %d\n", ret);
  148. goto exit;
  149. }
  150. #ifdef WOLFSSL_W64_WRAPPER
  151. if ((ret = w64wrapper_test()) != 0) {
  152. fprintf(stderr, "w64wrapper test failed with %d\n", ret);
  153. goto exit;
  154. }
  155. #endif /* WOLFSSL_W64_WRAPPER */
  156. }
  157. #ifdef WOLFSSL_QUIC
  158. if ( (ret = QuicTest()) != 0){
  159. printf("quic test failed with %d\n", ret);
  160. goto exit;
  161. }
  162. #endif
  163. SrpTest();
  164. #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
  165. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  166. #ifndef SINGLE_THREADED
  167. if ( (ret = SuiteTest(argc, argv)) != 0){
  168. fprintf(stderr, "suite test failed with %d\n", ret);
  169. goto exit;
  170. }
  171. #endif
  172. #endif
  173. #endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
  174. exit:
  175. #ifdef HAVE_WNR
  176. if (wc_FreeNetRandom() < 0)
  177. err_sys("Failed to free netRandom context");
  178. #endif /* HAVE_WNR */
  179. if (ret == 0) {
  180. puts("\nunit_test: Success for all configured tests.");
  181. fflush(stdout);
  182. }
  183. return ret;
  184. }
  185. void wait_tcp_ready(func_args* args)
  186. {
  187. #ifdef SINGLE_THREADED
  188. (void)args;
  189. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  190. PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));
  191. if (!args->signal->ready)
  192. PTHREAD_CHECK_RET(pthread_cond_wait(&args->signal->cond,
  193. &args->signal->mutex));
  194. args->signal->ready = 0; /* reset */
  195. PTHREAD_CHECK_RET(pthread_mutex_unlock(&args->signal->mutex));
  196. #else
  197. (void)args;
  198. #endif
  199. }
  200. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  201. {
  202. #ifdef SINGLE_THREADED
  203. (void)fun;
  204. (void)args;
  205. (void)thread;
  206. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  207. PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args));
  208. return;
  209. #elif defined (WOLFSSL_TIRTOS)
  210. /* Initialize the defaults and set the parameters. */
  211. Task_Params taskParams;
  212. Task_Params_init(&taskParams);
  213. taskParams.arg0 = (UArg)args;
  214. taskParams.stackSize = 65535;
  215. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  216. if (*thread == NULL) {
  217. fprintf(stderr, "Failed to create new Task\n");
  218. }
  219. Task_yield();
  220. #else
  221. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  222. #endif
  223. }
  224. void join_thread(THREAD_TYPE thread)
  225. {
  226. #ifdef SINGLE_THREADED
  227. (void)thread;
  228. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  229. PTHREAD_CHECK_RET(pthread_join(thread, 0));
  230. #elif defined (WOLFSSL_TIRTOS)
  231. while(1) {
  232. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  233. Task_sleep(5);
  234. break;
  235. }
  236. Task_yield();
  237. }
  238. #else
  239. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  240. assert(res == WAIT_OBJECT_0);
  241. res = CloseHandle((HANDLE)thread);
  242. assert(res);
  243. (void)res; /* Suppress un-used variable warning */
  244. #endif
  245. }