2015-07-01 11:12:54 -07:00
|
|
|
/* unit.c API unit tests driver
|
|
|
|
|
*
|
2021-03-11 13:42:46 +07:00
|
|
|
* Copyright (C) 2006-2021 wolfSSL Inc.
|
2015-07-01 11:12:54 -07:00
|
|
|
*
|
2016-03-17 16:02:13 -06:00
|
|
|
* This file is part of wolfSSL.
|
2015-07-01 11:12:54 -07:00
|
|
|
*
|
|
|
|
|
* wolfSSL is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* wolfSSL is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2016-03-17 16:02:13 -06:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
2015-07-01 11:12:54 -07:00
|
|
|
*/
|
2014-12-29 10:27:03 -07:00
|
|
|
|
2016-03-17 16:02:13 -06:00
|
|
|
|
2015-02-17 14:20:10 -07:00
|
|
|
/* Name change compatibility layer no longer need to be included here */
|
2014-12-29 10:27:03 -07:00
|
|
|
|
2012-09-21 13:29:04 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-02-25 13:34:29 -08:00
|
|
|
#include <wolfssl/wolfcrypt/settings.h>
|
2013-04-10 12:17:23 -07:00
|
|
|
|
2011-12-14 09:20:46 -08:00
|
|
|
#include <stdio.h>
|
2012-08-06 17:14:31 -07:00
|
|
|
#include <tests/unit.h>
|
2021-04-06 08:54:45 -07:00
|
|
|
#include <wolfssl/wolfcrypt/fips_test.h>
|
2011-12-14 09:20:46 -08:00
|
|
|
|
2012-08-02 13:41:40 -07:00
|
|
|
|
|
|
|
|
int myoptind = 0;
|
|
|
|
|
char* myoptarg = NULL;
|
2014-05-08 15:52:20 -07:00
|
|
|
int unit_test(int argc, char** argv);
|
2012-08-02 13:41:40 -07:00
|
|
|
|
2014-05-08 15:52:20 -07:00
|
|
|
#ifndef NO_TESTSUITE_MAIN_DRIVER
|
2011-12-14 09:20:46 -08:00
|
|
|
int main(int argc, char** argv)
|
2014-05-08 15:52:20 -07:00
|
|
|
{
|
|
|
|
|
return unit_test(argc, argv);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int unit_test(int argc, char** argv)
|
2011-12-14 09:20:46 -08:00
|
|
|
{
|
2017-04-07 15:46:32 -07:00
|
|
|
int ret = 0;
|
2011-12-14 09:20:46 -08:00
|
|
|
|
2012-09-20 15:39:15 -07:00
|
|
|
(void)argc;
|
|
|
|
|
(void)argv;
|
2018-07-27 10:16:14 -07:00
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
|
|
|
|
|
if (argc > 1) {
|
2021-09-14 15:58:14 +10:00
|
|
|
int memFailCount = atoi(argv[1]);
|
2018-07-27 10:16:14 -07:00
|
|
|
printf("\n--- SET RNG MALLOC FAIL AT %d---\n", memFailCount);
|
|
|
|
|
wolfSSL_SetMemFailCount(memFailCount);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-10-29 15:39:42 -07:00
|
|
|
printf("starting unit tests...\n");
|
2011-12-14 10:02:05 -08:00
|
|
|
|
2015-09-23 14:42:48 -07:00
|
|
|
#if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
|
|
|
|
|
wolfSSL_Debugging_ON();
|
|
|
|
|
#endif
|
2013-02-01 12:21:38 -08:00
|
|
|
|
2021-03-24 16:45:19 -07:00
|
|
|
#ifdef WC_RNG_SEED_CB
|
|
|
|
|
wc_SetSeed_Cb(wc_GenerateSeed);
|
|
|
|
|
#endif
|
2016-05-05 15:31:25 -06:00
|
|
|
#ifdef HAVE_WNR
|
|
|
|
|
if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
|
|
|
|
|
err_sys("Whitewood netRandom global config failed");
|
|
|
|
|
#endif /* HAVE_WNR */
|
|
|
|
|
|
2015-01-07 13:33:10 -07:00
|
|
|
#ifndef WOLFSSL_TIRTOS
|
2015-10-28 23:54:08 -07:00
|
|
|
ChangeToWolfRoot();
|
2014-05-08 15:52:20 -07:00
|
|
|
#endif
|
2012-12-06 10:01:01 -08:00
|
|
|
|
2021-04-06 08:54:45 -07:00
|
|
|
#if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 5)
|
2021-12-10 14:44:20 -07:00
|
|
|
#if !defined(NO_AES) && !defined(NO_AES_CBC)
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_AES_CBC) != 0) {
|
|
|
|
|
err_sys("AES-CBC CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_AESGCM
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_AES_GCM) != 0) {
|
|
|
|
|
err_sys("AES-GCM CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifndef NO_SHA
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA1) != 0) {
|
|
|
|
|
err_sys("HMAC-SHA1 CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
/* the only non-optional CAST */
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_256) != 0) {
|
|
|
|
|
err_sys("HMAC-SHA2-256 CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#ifdef WOLFSSL_SHA512
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA2_512) != 0) {
|
|
|
|
|
err_sys("HMAC-SHA2-512 CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef WOLFSSL_SHA3
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_HMAC_SHA3_256) != 0) {
|
|
|
|
|
err_sys("HMAC-SHA3-256 CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_HASHDRBG
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_DRBG) != 0) {
|
|
|
|
|
err_sys("Hash_DRBG CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifndef NO_RSA
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_RSA_SIGN_PKCS1v15) != 0) {
|
|
|
|
|
err_sys("RSA sign CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_CDH_CAST)
|
|
|
|
|
if (wc_RunCast_fips(FIPS_CAST_ECC_CDH) != 0) {
|
|
|
|
|
err_sys("RSA sign CAST failed");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_ECC_DHE
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_ECC_PRIMITIVE_Z) != 0) {
|
|
|
|
|
err_sys("ECC Primitive Z CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_ECC
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_ECDSA) != 0) {
|
|
|
|
|
err_sys("ECDSA CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifndef NO_DH
|
|
|
|
|
if (wc_RunCast_fips(FIPS_CAST_DH_PRIMITIVE_Z) != 0) {
|
|
|
|
|
err_sys("DH Primitive Z CAST failed");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef WOLFSSL_HAVE_PRF
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_KDF_TLS12) != 0) {
|
|
|
|
|
err_sys("KDF TLSv1.2 CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#if defined(WOLFSSL_HAVE_PRF) && defined(WOLFSSL_TLS13)
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_KDF_TLS13) != 0) {
|
|
|
|
|
err_sys("KDF TLSv1.3 CAST failed");
|
|
|
|
|
}
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef WOLFSSL_WOLFSSH
|
2021-04-06 08:54:45 -07:00
|
|
|
if (wc_RunCast_fips(FIPS_CAST_KDF_SSH) != 0) {
|
|
|
|
|
err_sys("KDF SSHv2.0 CAST failed");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-12-10 14:44:20 -07:00
|
|
|
#endif /* HAVE_FIPS && HAVE_FIPS_VERSION == 5 */
|
2021-10-28 14:46:15 +02:00
|
|
|
#ifdef WOLFSSL_ALLOW_SKIP_UNIT_TESTS
|
|
|
|
|
if (argc == 1)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
ApiTest();
|
2012-08-06 17:14:31 -07:00
|
|
|
|
2021-10-28 14:46:15 +02:00
|
|
|
if ( (ret = HashTest()) != 0){
|
|
|
|
|
printf("hash test failed with %d\n", ret);
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
2022-05-30 11:17:03 +02:00
|
|
|
|
|
|
|
|
#ifdef WOLFSSL_W64_WRAPPER
|
|
|
|
|
if ((ret = w64wrapper_test()) != 0) {
|
|
|
|
|
printf("w64wrapper test failed with %d\n", ret);
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
#endif /* WOLFSSL_W64_WRAPPER */
|
|
|
|
|
|
2012-08-06 17:14:31 -07:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 23:43:14 -05:00
|
|
|
#ifndef NO_WOLFSSL_CIPHER_SUITE_TEST
|
2018-06-13 11:42:16 +10:00
|
|
|
#if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
|
2014-02-11 18:59:20 -08:00
|
|
|
#ifndef SINGLE_THREADED
|
2018-12-21 09:33:54 -08:00
|
|
|
if ( (ret = SuiteTest(argc, argv)) != 0){
|
2012-08-06 17:14:31 -07:00
|
|
|
printf("suite test failed with %d\n", ret);
|
2017-04-07 15:46:32 -07:00
|
|
|
goto exit;
|
2012-08-06 17:14:31 -07:00
|
|
|
}
|
2018-06-13 11:42:16 +10:00
|
|
|
#endif
|
2014-02-11 18:59:20 -08:00
|
|
|
#endif
|
2021-07-14 23:43:14 -05:00
|
|
|
#endif /* NO_WOLFSSL_CIPHER_SUITE_TEST */
|
2011-12-14 10:55:19 -08:00
|
|
|
|
2015-07-07 12:01:47 -03:00
|
|
|
SrpTest();
|
|
|
|
|
|
2017-04-07 15:46:32 -07:00
|
|
|
exit:
|
2016-05-05 15:31:25 -06:00
|
|
|
#ifdef HAVE_WNR
|
|
|
|
|
if (wc_FreeNetRandom() < 0)
|
|
|
|
|
err_sys("Failed to free netRandom context");
|
|
|
|
|
#endif /* HAVE_WNR */
|
|
|
|
|
|
2017-04-07 15:46:32 -07:00
|
|
|
return ret;
|
2011-12-14 09:20:46 -08:00
|
|
|
}
|
2012-08-06 17:14:31 -07:00
|
|
|
|
|
|
|
|
|
2014-02-11 18:59:20 -08:00
|
|
|
|
2012-08-06 17:14:31 -07:00
|
|
|
void wait_tcp_ready(func_args* args)
|
|
|
|
|
{
|
2014-02-11 18:59:20 -08:00
|
|
|
#ifdef SINGLE_THREADED
|
|
|
|
|
(void)args;
|
|
|
|
|
#elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
|
2012-08-06 17:14:31 -07:00
|
|
|
pthread_mutex_lock(&args->signal->mutex);
|
2015-07-07 12:01:47 -03:00
|
|
|
|
2012-08-06 17:14:31 -07:00
|
|
|
if (!args->signal->ready)
|
|
|
|
|
pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
|
|
|
|
|
args->signal->ready = 0; /* reset */
|
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&args->signal->mutex);
|
2013-05-01 14:17:11 -06:00
|
|
|
#else
|
|
|
|
|
(void)args;
|
2012-08-06 17:14:31 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
|
|
|
|
|
{
|
2014-02-11 18:59:20 -08:00
|
|
|
#ifdef SINGLE_THREADED
|
|
|
|
|
(void)fun;
|
|
|
|
|
(void)args;
|
|
|
|
|
(void)thread;
|
|
|
|
|
#elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
|
2012-08-06 17:14:31 -07:00
|
|
|
pthread_create(thread, 0, fun, args);
|
|
|
|
|
return;
|
2015-01-07 13:33:10 -07:00
|
|
|
#elif defined (WOLFSSL_TIRTOS)
|
2014-05-08 15:52:20 -07:00
|
|
|
/* Initialize the defaults and set the parameters. */
|
|
|
|
|
Task_Params taskParams;
|
|
|
|
|
Task_Params_init(&taskParams);
|
|
|
|
|
taskParams.arg0 = (UArg)args;
|
|
|
|
|
taskParams.stackSize = 65535;
|
|
|
|
|
*thread = Task_create((Task_FuncPtr)fun, &taskParams, NULL);
|
|
|
|
|
if (*thread == NULL) {
|
|
|
|
|
printf("Failed to create new Task\n");
|
|
|
|
|
}
|
|
|
|
|
Task_yield();
|
2012-08-06 17:14:31 -07:00
|
|
|
#else
|
|
|
|
|
*thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void join_thread(THREAD_TYPE thread)
|
|
|
|
|
{
|
2014-02-11 18:59:20 -08:00
|
|
|
#ifdef SINGLE_THREADED
|
|
|
|
|
(void)thread;
|
|
|
|
|
#elif defined(_POSIX_THREADS) && !defined(__MINGW32__)
|
2012-08-06 17:14:31 -07:00
|
|
|
pthread_join(thread, 0);
|
2015-01-07 13:33:10 -07:00
|
|
|
#elif defined (WOLFSSL_TIRTOS)
|
2014-05-08 15:52:20 -07:00
|
|
|
while(1) {
|
|
|
|
|
if (Task_getMode(thread) == Task_Mode_TERMINATED) {
|
|
|
|
|
Task_sleep(5);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Task_yield();
|
|
|
|
|
}
|
2012-08-06 17:14:31 -07:00
|
|
|
#else
|
2013-05-01 14:17:11 -06:00
|
|
|
int res = WaitForSingleObject((HANDLE)thread, INFINITE);
|
2012-08-06 17:14:31 -07:00
|
|
|
assert(res == WAIT_OBJECT_0);
|
2013-05-01 14:17:11 -06:00
|
|
|
res = CloseHandle((HANDLE)thread);
|
2012-08-06 17:14:31 -07:00
|
|
|
assert(res);
|
2016-04-01 15:45:53 -07:00
|
|
|
(void)res; /* Suppress un-used variable warning */
|
2012-08-06 17:14:31 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|