unit.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2023 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 allTesting = 1;
  30. int myoptind = 0;
  31. char* myoptarg = NULL;
  32. int unit_test(int argc, char** argv);
  33. #ifndef NO_TESTSUITE_MAIN_DRIVER
  34. int main(int argc, char** argv)
  35. {
  36. return unit_test(argc, argv);
  37. }
  38. #endif
  39. /* Print usage options for unit test.
  40. */
  41. static void UnitTest_Usage(void)
  42. {
  43. printf("Usage: ./tests/unit.test <options>\n");
  44. printf(" -?, --help Display this usage information.\n");
  45. printf(" --list List the API tests.\n");
  46. printf(" --api Only perform API tests.\n");
  47. printf(" -<number> Run the API test identified by number.\n");
  48. printf(" Can be specified multiple times.\n");
  49. printf(" -<string> Run the API test identified by name.\n");
  50. printf(" Can be specified multiple times.\n");
  51. printf(" <filename> Name of cipher suite testing file.\n");
  52. }
  53. int unit_test(int argc, char** argv)
  54. {
  55. int ret = 0;
  56. (void)argc;
  57. (void)argv;
  58. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  59. if (argc > 1) {
  60. int memFailCount = atoi(argv[1]);
  61. fprintf(stderr, "\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
  62. wolfSSL_SetMemFailCount(memFailCount);
  63. }
  64. #endif
  65. printf("starting unit tests...\n");
  66. fflush(stdout);
  67. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  68. wolfSSL_Debugging_ON();
  69. #endif
  70. #ifdef WC_RNG_SEED_CB
  71. wc_SetSeed_Cb(wc_GenerateSeed);
  72. #endif
  73. #ifdef HAVE_WNR
  74. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  75. err_sys("Whitewood netRandom global config failed");
  76. #endif /* HAVE_WNR */
  77. #ifndef WOLFSSL_TIRTOS
  78. ChangeToWolfRoot();
  79. #endif
  80. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
  81. #if !defined(NO_AES) && !defined(NO_AES_CBC)
  82. if (wc_RunCast_fips(FIPS_CAST_AES_CBC) != 0) {
  83. err_sys("AES-CBC CAST failed");
  84. }
  85. #endif
  86. #ifdef HAVE_AESGCM
  87. if (wc_RunCast_fips(FIPS_CAST_AES_GCM) != 0) {
  88. err_sys("AES-GCM CAST failed");
  89. }
  90. #endif
  91. #ifndef NO_SHA
  92. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA1) != 0) {
  93. err_sys("HMAC-SHA1 CAST failed");
  94. }
  95. #endif
  96. /* the only non-optional CAST */
  97. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_256) != 0) {
  98. err_sys("HMAC-SHA2-256 CAST failed");
  99. }
  100. #ifdef WOLFSSL_SHA512
  101. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_512) != 0) {
  102. err_sys("HMAC-SHA2-512 CAST failed");
  103. }
  104. #endif
  105. #ifdef WOLFSSL_SHA3
  106. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA3_256) != 0) {
  107. err_sys("HMAC-SHA3-256 CAST failed");
  108. }
  109. #endif
  110. #ifdef HAVE_HASHDRBG
  111. if (wc_RunCast_fips(FIPS_CAST_DRBG) != 0) {
  112. err_sys("Hash_DRBG CAST failed");
  113. }
  114. #endif
  115. #ifndef NO_RSA
  116. if (wc_RunCast_fips(FIPS_CAST_RSA_SIGN_PKCS1v15) != 0) {
  117. err_sys("RSA sign CAST failed");
  118. }
  119. #endif
  120. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_CDH_CAST)
  121. if (wc_RunCast_fips(FIPS_CAST_ECC_CDH) != 0) {
  122. err_sys("RSA sign CAST failed");
  123. }
  124. #endif
  125. #ifdef HAVE_ECC_DHE
  126. if (wc_RunCast_fips(FIPS_CAST_ECC_PRIMITIVE_Z) != 0) {
  127. err_sys("ECC Primitive Z CAST failed");
  128. }
  129. #endif
  130. #ifdef HAVE_ECC
  131. if (wc_RunCast_fips(FIPS_CAST_ECDSA) != 0) {
  132. err_sys("ECDSA CAST failed");
  133. }
  134. #endif
  135. #ifndef NO_DH
  136. if (wc_RunCast_fips(FIPS_CAST_DH_PRIMITIVE_Z) != 0) {
  137. err_sys("DH Primitive Z CAST failed");
  138. }
  139. #endif
  140. #ifdef WOLFSSL_HAVE_PRF
  141. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS12) != 0) {
  142. err_sys("KDF TLSv1.2 CAST failed");
  143. }
  144. #endif
  145. #if defined(WOLFSSL_HAVE_PRF) && defined(WOLFSSL_TLS13)
  146. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS13) != 0) {
  147. err_sys("KDF TLSv1.3 CAST failed");
  148. }
  149. #endif
  150. #ifdef WOLFSSL_WOLFSSH
  151. if (wc_RunCast_fips(FIPS_CAST_KDF_SSH) != 0) {
  152. err_sys("KDF SSHv2.0 CAST failed");
  153. }
  154. #endif
  155. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
  156. while (argc > 1) {
  157. if (argv[1][0] != '-') {
  158. break;
  159. }
  160. if (XSTRCMP(argv[1], "-?") == 0 || XSTRCMP(argv[1], "--help") == 0) {
  161. UnitTest_Usage();
  162. goto exit;
  163. }
  164. else if (XSTRCMP(argv[1], "--list") == 0) {
  165. ApiTest_PrintTestCases();
  166. goto exit;
  167. }
  168. else if (XSTRCMP(argv[1], "--api") == 0) {
  169. }
  170. else if (argv[1][1] >= '0' && argv[1][1] <= '9') {
  171. ret = ApiTest_RunIdx(atoi(argv[1] + 1));
  172. if (ret != 0) {
  173. goto exit;
  174. }
  175. }
  176. else {
  177. ret = ApiTest_RunName(argv[1] + 1);
  178. if (ret != 0) {
  179. goto exit;
  180. }
  181. }
  182. allTesting = 0;
  183. argc--;
  184. argv++;
  185. }
  186. #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
  187. if (argc == 1)
  188. #endif
  189. {
  190. ApiTest();
  191. if (!allTesting) {
  192. goto exit;
  193. }
  194. if ((ret = HashTest()) != 0) {
  195. fprintf(stderr, "hash test failed with %d\n", ret);
  196. goto exit;
  197. }
  198. #ifdef WOLFSSL_W64_WRAPPER
  199. if ((ret = w64wrapper_test()) != 0) {
  200. fprintf(stderr, "w64wrapper test failed with %d\n", ret);
  201. goto exit;
  202. }
  203. #endif /* WOLFSSL_W64_WRAPPER */
  204. #ifdef WOLFSSL_QUIC
  205. if ((ret = QuicTest()) != 0) {
  206. printf("quic test failed with %d\n", ret);
  207. goto exit;
  208. }
  209. #endif
  210. SrpTest();
  211. }
  212. #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
  213. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  214. #ifndef SINGLE_THREADED
  215. if ((ret = SuiteTest(argc, argv)) != 0) {
  216. fprintf(stderr, "suite test failed with %d\n", ret);
  217. goto exit;
  218. }
  219. #endif
  220. #endif
  221. #endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
  222. exit:
  223. #ifdef HAVE_WNR
  224. if (wc_FreeNetRandom() < 0)
  225. err_sys("Failed to free netRandom context");
  226. #endif /* HAVE_WNR */
  227. if (ret == 0) {
  228. puts("\nunit_test: Success for all configured tests.");
  229. fflush(stdout);
  230. }
  231. return ret;
  232. }
  233. void wait_tcp_ready(func_args* args)
  234. {
  235. #ifdef SINGLE_THREADED
  236. (void)args;
  237. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  238. PTHREAD_CHECK_RET(pthread_mutex_lock(&args->signal->mutex));
  239. if (!args->signal->ready)
  240. PTHREAD_CHECK_RET(pthread_cond_wait(&args->signal->cond,
  241. &args->signal->mutex));
  242. args->signal->ready = 0; /* reset */
  243. PTHREAD_CHECK_RET(pthread_mutex_unlock(&args->signal->mutex));
  244. #else
  245. (void)args;
  246. #endif
  247. }
  248. void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
  249. {
  250. #ifdef SINGLE_THREADED
  251. (void)fun;
  252. (void)args;
  253. (void)thread;
  254. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  255. PTHREAD_CHECK_RET(pthread_create(thread, 0, fun, args));
  256. return;
  257. #elif defined (WOLFSSL_TIRTOS)
  258. /* Initialize the defaults and set the parameters. */
  259. Task_Params taskParams;
  260. Task_Params_init(&taskParams);
  261. taskParams.arg0 = (UArg)args;
  262. taskParams.stackSize = 65535;
  263. *thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
  264. if (*thread == NULL) {
  265. fprintf(stderr, "Failed to create new Task\n");
  266. }
  267. Task_yield();
  268. #else
  269. *thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
  270. #endif
  271. }
  272. void join_thread(THREAD_TYPE thread)
  273. {
  274. #ifdef SINGLE_THREADED
  275. (void)thread;
  276. #elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
  277. PTHREAD_CHECK_RET(pthread_join(thread, 0));
  278. #elif defined (WOLFSSL_TIRTOS)
  279. while(1) {
  280. if (Task_getMode(thread) == Task_Mode_TERMINATED) {
  281. Task_sleep(5);
  282. break;
  283. }
  284. Task_yield();
  285. }
  286. #else
  287. int res = WaitForSingleObject((HANDLE)thread, INFINITE);
  288. assert(res == WAIT_OBJECT_0);
  289. res = CloseHandle((HANDLE)thread);
  290. assert(res);
  291. (void)res; /* Suppress un-used variable warning */
  292. #endif
  293. }