From 7c59c74e0732f0d95b805fb4176ff13670075ea0 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 29 Jul 2020 10:34:15 -0700 Subject: [PATCH 1/5] Added unit tests for wolfmath.c --- tests/api.c | 226 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 225 insertions(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index af55ff12c..deb612ee8 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33597,8 +33597,226 @@ static int test_wc_RNG_GenerateBlock(void) } #endif /* - * Testing wc_InitRngNonce + * Testing get_rand_digit */ +static int test_get_rand_digit (void) +{ + int ret = 0; +#if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP) + + WC_RNG rng; + mp_digit d; + + printf(testingFmt, "get_rand_digit()"); + + ret = wc_InitRng(&rng); + + if (ret == 0) { + ret = get_rand_digit(&rng, &d); + } + if (ret == 0) { + ret = wc_FreeRng(&rng); + } + + printf(resultFmt, ret == 0 ? passed : failed); +#endif + return ret; + +}/* End test_get_rand_digit*/ +/* + * Testing get_digit_count + */ +static int test_get_digit_count (void) +{ + int ret = 0; +#if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP) + mp_int a; + + printf(testingFmt, "get_digit_count()"); + + if (mp_init(&a) != MP_OKAY) { + ret = -1; + } + if (ret == 0) { + ret = get_digit_count(NULL); + } + if (ret == 0) { + ret = get_digit_count(&a); + } + + printf(resultFmt, ret == 0 ? passed : failed); + mp_clear(&a); +#endif + return ret; + +}/* End test_get_digit_count*/ +/* + * Testing mp_cond_copy + */ +static int test_mp_cond_copy (void) +{ + int ret = 0; +#if defined(WOLFSSL_PUBLIC_MP) + mp_int a; + mp_int b; + int copy = 0; + + printf(testingFmt, "mp_cond_copy()"); + + if (mp_init(&a) != MP_OKAY) { + ret = -1; + } + if (ret == 0) { + if (mp_init(&b) != MP_OKAY) { + ret = -1; + } + } + if (ret == 0) { + ret = mp_cond_copy(NULL, copy, NULL); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = mp_cond_copy(NULL, copy, &b); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = mp_cond_copy(&a, copy, NULL); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = mp_cond_copy(&a, copy, &b); + } + + printf(resultFmt, ret == 0 ? passed : failed); + mp_clear(&a); + mp_clear(&b); +#endif + return ret; + +}/* End test_mp_cond_copy*/ +/* + * Testing mp_rand + */ +static int test_mp_rand (void) +{ + int ret = 0; +#if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP) + mp_int a; + int digits = 1; /*Setting equal to 0 causes a segmentation fault, line 183*/ + WC_RNG rng; + + printf(testingFmt, "mp_rand()"); + + if (mp_init(&a) != MP_OKAY) { + ret = -1; + } + if (ret == 0) { + ret = wc_InitRng(&rng); + } + + if (ret == 0) { + ret = mp_rand(&a, digits, NULL); + if (ret == MISSING_RNG_E) { + ret = 0; + } + } + if (ret == 0) { + ret = mp_rand(NULL, digits, &rng); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = mp_rand(&a, digits, &rng); + } + + printf(resultFmt, ret == 0 ? passed : failed); + mp_clear(&a); +#endif + return ret; +}/* End test_mp_rand*/ +/* + * Testing mp_rand + */ +static int test_get_digit (void) +{ + int ret = 0; +#if defined(WOLFSSL_PUBLIC_MP) + mp_int a; + int n = 0; + + printf(testingFmt, "get_digit()"); + + if (mp_init(&a) != MP_OKAY) { + ret = -1; + } + if (ret == 0) { + ret = get_digit(NULL, n); + } + + if (ret == 0) { + ret = get_digit(&a, n); + } + + + printf(resultFmt, ret == 0 ? passed : failed); + mp_clear(&a); +#endif + return ret; +}/* End test_get_digit*/ +/* + * Testing wc_export_int + */ +static int test_wc_export_int (void) +{ + int ret = 0; +#if defined(WOLFSSL_PUBLIC_MP) + mp_int mp; + byte buf[256]; + word32 keySz = (word32)sizeof(buf); + word32 len = (word32)sizeof(buf); + + + int encType = WC_TYPE_UNSIGNED_BIN; + + printf(testingFmt, "wc_export_int()"); + + if (mp_init(&mp) != MP_OKAY) { + ret = -1; + } + if (ret == 0) { + ret = wc_export_int(NULL, buf, &len, keySz, encType); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + len = 255; + if (ret == 0) { + ret = wc_export_int(&mp, buf, &len, keySz, encType); + if (ret == BUFFER_E) { + ret = 0; + } + } + len = 256; + if (ret == 0) { + ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_HEX_STR); + } + if (ret == 0) { + ret = wc_export_int(&mp, buf, &len, keySz, encType); + } + + printf(resultFmt, ret == 0 ? passed : failed); + mp_clear(&mp); +#endif + return ret; + +}/* End test_wc_export_int*/ static int test_wc_InitRngNonce(void) { int ret=0; @@ -35524,6 +35742,12 @@ void ApiTest(void) AssertIntEQ(test_wc_RNG_GenerateBlock(), 0); #endif + AssertIntEQ(test_get_rand_digit(), 0); + AssertIntEQ(test_get_digit_count(), 0); + AssertIntEQ(test_mp_cond_copy(), 0); + AssertIntEQ(test_mp_rand(), 0); + AssertIntEQ(test_get_digit(), 0); + AssertIntEQ(test_wc_export_int(), 0); AssertIntEQ(test_wc_InitRngNonce(), 0); AssertIntEQ(test_wc_InitRngNonce_ex(), 0); AssertIntEQ(test_wc_ed25519_make_key(), 0); From b4cd0886bb1473f02acc7692f3d27d764ab3447b Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 29 Jul 2020 14:23:03 -0700 Subject: [PATCH 2/5] Changed test returns for 'get_digit' to remove implicit conversion errors --- tests/api.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tests/api.c b/tests/api.c index deb612ee8..d72942e0d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33757,11 +33757,24 @@ static int test_get_digit (void) ret = -1; } if (ret == 0) { - ret = get_digit(NULL, n); + if (get_digit(NULL, n) != 0) { /* Should not hit this*/ + ret = -1; + } } - if (ret == 0) { - ret = get_digit(&a, n); + if (get_digit(NULL, n) == 0) { /* Should hit this*/ + ret = 0; + } + } + if (ret == 0) { + if (get_digit(&a, n) != 0) { /* Should not hit this*/ + ret = -1; + } + } + if (ret == 0) { + if (get_digit(&a, n) == 0) { /* Should hit this*/ + ret = 0; + } } From c52930bb8ad761d3ff81d6bb5c7fdc12101c067a Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Thu, 30 Jul 2020 09:37:05 -0700 Subject: [PATCH 3/5] Added freerng to 'test_mp_rand' --- tests/api.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/api.c b/tests/api.c index d72942e0d..1c64b7395 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33738,6 +33738,7 @@ static int test_mp_rand (void) printf(resultFmt, ret == 0 ? passed : failed); mp_clear(&a); + wc_FreeRng(&rng); #endif return ret; }/* End test_mp_rand*/ From b627610cde8d3b1a31b69c0d059208ccb8384151 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Fri, 31 Jul 2020 09:44:10 -0700 Subject: [PATCH 4/5] Added bad test cases for get_digit and corrected comment formatting --- tests/api.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/api.c b/tests/api.c index 1c64b7395..e6596e019 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33614,6 +33614,25 @@ static int test_get_rand_digit (void) if (ret == 0) { ret = get_rand_digit(&rng, &d); } + if (ret == 0) { + ret = get_rand_digit(NULL, NULL); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = get_rand_digit(NULL, &d); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = get_rand_digit(&rng, NULL); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { ret = wc_FreeRng(&rng); } @@ -33743,7 +33762,7 @@ static int test_mp_rand (void) return ret; }/* End test_mp_rand*/ /* - * Testing mp_rand + * Testing get_digit */ static int test_get_digit (void) { @@ -33758,22 +33777,22 @@ static int test_get_digit (void) ret = -1; } if (ret == 0) { - if (get_digit(NULL, n) != 0) { /* Should not hit this*/ + if (get_digit(NULL, n) != 0) { /* Should not hit this */ ret = -1; } } if (ret == 0) { - if (get_digit(NULL, n) == 0) { /* Should hit this*/ + if (get_digit(NULL, n) == 0) { /* Should hit this */ ret = 0; } } if (ret == 0) { - if (get_digit(&a, n) != 0) { /* Should not hit this*/ + if (get_digit(&a, n) != 0) { /* Should not hit this */ ret = -1; } } if (ret == 0) { - if (get_digit(&a, n) == 0) { /* Should hit this*/ + if (get_digit(&a, n) == 0) { /* Should hit this */ ret = 0; } } From 7f381275b1c14fc4c6c3049a397c7932b6c702f2 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Mon, 3 Aug 2020 13:31:11 -0700 Subject: [PATCH 5/5] Removed comment and changed len equal to variables instead of numbers --- tests/api.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/api.c b/tests/api.c index e6596e019..9e10e55cb 100644 --- a/tests/api.c +++ b/tests/api.c @@ -33727,7 +33727,7 @@ static int test_mp_rand (void) int ret = 0; #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP) mp_int a; - int digits = 1; /*Setting equal to 0 causes a segmentation fault, line 183*/ + int digits = 1; WC_RNG rng; printf(testingFmt, "mp_rand()"); @@ -33829,14 +33829,14 @@ static int test_wc_export_int (void) ret = 0; } } - len = 255; + len = sizeof(buf)-1; if (ret == 0) { ret = wc_export_int(&mp, buf, &len, keySz, encType); if (ret == BUFFER_E) { ret = 0; } } - len = 256; + len = sizeof(buf); if (ret == 0) { ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_HEX_STR); }