unit.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* unit.c API unit tests driver
  2. *
  3. * Copyright (C) 2006-2024 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(HAVE_HKDF) && !defined(NO_HMAC)
  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. #if FIPS_VERSION3_GT(5,2,0)
  159. if (wc_RunAllCast_fips() != 0) {
  160. err_sys("wc_RunAllCast_fips() failed\n");
  161. }
  162. #endif
  163. while (argc > 1) {
  164. if (argv[1][0] != '-') {
  165. break;
  166. }
  167. if (XSTRCMP(argv[1], "-?") == 0 || XSTRCMP(argv[1], "--help") == 0) {
  168. UnitTest_Usage();
  169. goto exit;
  170. }
  171. else if (XSTRCMP(argv[1], "--list") == 0) {
  172. ApiTest_PrintTestCases();
  173. goto exit;
  174. }
  175. else if (XSTRCMP(argv[1], "--api") == 0) {
  176. allTesting = 0;
  177. }
  178. else if (XSTRCMP(argv[1], "--no-api") == 0) {
  179. apiTesting = 0;
  180. }
  181. else if (argv[1][1] >= '0' && argv[1][1] <= '9') {
  182. ret = ApiTest_RunIdx(atoi(argv[1] + 1));
  183. if (ret != 0) {
  184. goto exit;
  185. }
  186. allTesting = 0;
  187. }
  188. else {
  189. ret = ApiTest_RunName(argv[1] + 1);
  190. if (ret != 0) {
  191. goto exit;
  192. }
  193. allTesting = 0;
  194. }
  195. argc--;
  196. argv++;
  197. }
  198. #ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
  199. if (argc == 1)
  200. #endif
  201. {
  202. if (apiTesting) {
  203. ret = ApiTest();
  204. if (ret != 0)
  205. goto exit;
  206. }
  207. if (!allTesting) {
  208. goto exit;
  209. }
  210. if ((ret = HashTest()) != 0) {
  211. fprintf(stderr, "hash test failed with %d\n", ret);
  212. goto exit;
  213. }
  214. #ifdef WOLFSSL_W64_WRAPPER
  215. if ((ret = w64wrapper_test()) != 0) {
  216. fprintf(stderr, "w64wrapper test failed with %d\n", ret);
  217. goto exit;
  218. }
  219. #endif /* WOLFSSL_W64_WRAPPER */
  220. #ifdef WOLFSSL_QUIC
  221. if ((ret = QuicTest()) != 0) {
  222. printf("quic test failed with %d\n", ret);
  223. goto exit;
  224. }
  225. #endif
  226. SrpTest();
  227. }
  228. #if !defined(NO_WOLFSSL_CIPHER_SUITE_TEST) && \
  229. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  230. !defined(SINGLE_THREADED)
  231. if ((ret = SuiteTest(argc, argv)) != 0) {
  232. fprintf(stderr, "suite test failed with %d\n", ret);
  233. goto exit;
  234. }
  235. #endif
  236. exit:
  237. #ifdef HAVE_WNR
  238. if (wc_FreeNetRandom() < 0)
  239. err_sys("Failed to free netRandom context");
  240. #endif /* HAVE_WNR */
  241. if (ret == 0) {
  242. puts("\nunit_test: Success for all configured tests.");
  243. fflush(stdout);
  244. }
  245. return ret;
  246. }