2
0

unit.c 7.1 KB

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