unit.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <wolfssl/wolfcrypt/types.h>
  27. #include <stdio.h>
  28. #include <tests/unit.h>
  29. #include <wolfssl/wolfcrypt/fips_test.h>
  30. int allTesting = 1;
  31. int apiTesting = 1;
  32. int myoptind = 0;
  33. char* myoptarg = NULL;
  34. int unit_test(int argc, char** argv);
  35. #ifndef NO_TESTSUITE_MAIN_DRIVER
  36. int main(int argc, char** argv)
  37. {
  38. return unit_test(argc, argv);
  39. }
  40. #endif
  41. /* Print usage options for unit test.
  42. */
  43. static void UnitTest_Usage(void)
  44. {
  45. printf("Usage: ./tests/unit.test <options>\n");
  46. printf(" -?, --help Display this usage information.\n");
  47. printf(" --list List the API tests.\n");
  48. printf(" --api Only perform API tests.\n");
  49. printf(" -<number> Run the API test identified by number.\n");
  50. printf(" Can be specified multiple times.\n");
  51. printf(" -<string> Run the API test identified by name.\n");
  52. printf(" Can be specified multiple times.\n");
  53. printf(" <filename> Name of cipher suite testing file.\n");
  54. }
  55. int unit_test(int argc, char** argv)
  56. {
  57. int ret = 0;
  58. (void)argc;
  59. (void)argv;
  60. #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
  61. if (argc > 1) {
  62. int memFailCount = atoi(argv[1]);
  63. fprintf(stderr, "\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
  64. wolfSSL_SetMemFailCount(memFailCount);
  65. }
  66. #endif
  67. printf("starting unit tests...\n");
  68. fflush(stdout);
  69. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  70. wolfSSL_Debugging_ON();
  71. #endif
  72. #ifdef WC_RNG_SEED_CB
  73. wc_SetSeed_Cb(wc_GenerateSeed);
  74. #endif
  75. #ifdef HAVE_WNR
  76. if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
  77. err_sys("Whitewood netRandom global config failed");
  78. #endif /* HAVE_WNR */
  79. #ifndef WOLFSSL_TIRTOS
  80. ChangeToWolfRoot();
  81. #endif
  82. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
  83. #if !defined(NO_AES) && !defined(NO_AES_CBC)
  84. if (wc_RunCast_fips(FIPS_CAST_AES_CBC) != 0) {
  85. err_sys("AES-CBC CAST failed");
  86. }
  87. #endif
  88. #ifdef HAVE_AESGCM
  89. if (wc_RunCast_fips(FIPS_CAST_AES_GCM) != 0) {
  90. err_sys("AES-GCM CAST failed");
  91. }
  92. #endif
  93. #ifndef NO_SHA
  94. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA1) != 0) {
  95. err_sys("HMAC-SHA1 CAST failed");
  96. }
  97. #endif
  98. /* the only non-optional CAST */
  99. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_256) != 0) {
  100. err_sys("HMAC-SHA2-256 CAST failed");
  101. }
  102. #ifdef WOLFSSL_SHA512
  103. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_512) != 0) {
  104. err_sys("HMAC-SHA2-512 CAST failed");
  105. }
  106. #endif
  107. #ifdef WOLFSSL_SHA3
  108. if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA3_256) != 0) {
  109. err_sys("HMAC-SHA3-256 CAST failed");
  110. }
  111. #endif
  112. #ifdef HAVE_HASHDRBG
  113. if (wc_RunCast_fips(FIPS_CAST_DRBG) != 0) {
  114. err_sys("Hash_DRBG CAST failed");
  115. }
  116. #endif
  117. #ifndef NO_RSA
  118. if (wc_RunCast_fips(FIPS_CAST_RSA_SIGN_PKCS1v15) != 0) {
  119. err_sys("RSA sign CAST failed");
  120. }
  121. #endif
  122. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_CDH_CAST)
  123. if (wc_RunCast_fips(FIPS_CAST_ECC_CDH) != 0) {
  124. err_sys("RSA sign CAST failed");
  125. }
  126. #endif
  127. #ifdef HAVE_ECC_DHE
  128. if (wc_RunCast_fips(FIPS_CAST_ECC_PRIMITIVE_Z) != 0) {
  129. err_sys("ECC Primitive Z CAST failed");
  130. }
  131. #endif
  132. #ifdef HAVE_ECC
  133. if (wc_RunCast_fips(FIPS_CAST_ECDSA) != 0) {
  134. err_sys("ECDSA CAST failed");
  135. }
  136. #endif
  137. #ifndef NO_DH
  138. if (wc_RunCast_fips(FIPS_CAST_DH_PRIMITIVE_Z) != 0) {
  139. err_sys("DH Primitive Z CAST failed");
  140. }
  141. #endif
  142. #ifdef WOLFSSL_HAVE_PRF
  143. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS12) != 0) {
  144. err_sys("KDF TLSv1.2 CAST failed");
  145. }
  146. #endif
  147. #if defined(WOLFSSL_HAVE_PRF) && defined(WOLFSSL_TLS13)
  148. if (wc_RunCast_fips(FIPS_CAST_KDF_TLS13) != 0) {
  149. err_sys("KDF TLSv1.3 CAST failed");
  150. }
  151. #endif
  152. #ifdef WOLFSSL_WOLFSSH
  153. if (wc_RunCast_fips(FIPS_CAST_KDF_SSH) != 0) {
  154. err_sys("KDF SSHv2.0 CAST failed");
  155. }
  156. #endif
  157. #endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
  158. while (argc > 1) {
  159. if (argv[1][0] != '-') {
  160. break;
  161. }
  162. if (XSTRCMP(argv[1], "-?") == 0 || XSTRCMP(argv[1], "--help") == 0) {
  163. UnitTest_Usage();
  164. goto exit;
  165. }
  166. else if (XSTRCMP(argv[1], "--list") == 0) {
  167. ApiTest_PrintTestCases();
  168. goto exit;
  169. }
  170. else if (XSTRCMP(argv[1], "--api") == 0) {
  171. allTesting = 0;
  172. }
  173. else if (XSTRCMP(argv[1], "--no-api") == 0) {
  174. apiTesting = 0;
  175. }
  176. else if (argv[1][1] >= '0' && argv[1][1] <= '9') {
  177. ret = ApiTest_RunIdx(atoi(argv[1] + 1));
  178. if (ret != 0) {
  179. goto exit;
  180. }
  181. allTesting = 0;
  182. }
  183. else {
  184. ret = ApiTest_RunName(argv[1] + 1);
  185. if (ret != 0) {
  186. goto exit;
  187. }
  188. allTesting = 0;
  189. }
  190. argc--;
  191. argv++;
  192. }
  193. #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
  194. if (argc == 1)
  195. #endif
  196. {
  197. if (apiTesting) {
  198. ret = ApiTest();
  199. if (ret != 0)
  200. goto exit;
  201. }
  202. if (!allTesting) {
  203. goto exit;
  204. }
  205. if ((ret = HashTest()) != 0) {
  206. fprintf(stderr, "hash test failed with %d\n", ret);
  207. goto exit;
  208. }
  209. #ifdef WOLFSSL_W64_WRAPPER
  210. if ((ret = w64wrapper_test()) != 0) {
  211. fprintf(stderr, "w64wrapper test failed with %d\n", ret);
  212. goto exit;
  213. }
  214. #endif /* WOLFSSL_W64_WRAPPER */
  215. #ifdef WOLFSSL_QUIC
  216. if ((ret = QuicTest()) != 0) {
  217. printf("quic test failed with %d\n", ret);
  218. goto exit;
  219. }
  220. #endif
  221. SrpTest();
  222. }
  223. #ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
  224. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  225. #ifndef SINGLE_THREADED
  226. if ((ret = SuiteTest(argc, argv)) != 0) {
  227. fprintf(stderr, "suite test failed with %d\n", ret);
  228. goto exit;
  229. }
  230. #endif
  231. #endif
  232. #endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
  233. exit:
  234. #ifdef HAVE_WNR
  235. if (wc_FreeNetRandom() < 0)
  236. err_sys("Failed to free netRandom context");
  237. #endif /* HAVE_WNR */
  238. if (ret == 0) {
  239. puts("\nunit_test: Success for all configured tests.");
  240. fflush(stdout);
  241. }
  242. return ret;
  243. }