libmicrohttpd2

HTTP server C library (MHD 2.x, alpha)
Log | Files | Refs | README | LICENSE

commit e4ff1d8b63f51fee31629516a1b4ab41def0beed
parent 463aff9e6fa2f19df5791a03ee2b77f5bc0a4cdd
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Thu, 16 Jul 2026 18:11:31 +0200

MD5/SHA unit tests: better handle early error exit

Diffstat:
Msrc/tests/unit/unit_md5.c | 49+++++++++++++++++++++++++++++++++++++++----------
Msrc/tests/unit/unit_sha256.c | 14++++++++++++++
Msrc/tests/unit/unit_sha512_256.c | 6++++++
3 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/src/tests/unit/unit_md5.c b/src/tests/unit/unit_md5.c @@ -249,6 +249,7 @@ test1_str (void) fprintf (stderr, "External hashing error: %d.\n", mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, @@ -429,6 +430,7 @@ test1_bin (void) fprintf (stderr, "External hashing error: %d.\n", mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, @@ -470,6 +472,7 @@ test2_str (void) fprintf (stderr, "External hashing error: %d.\n", mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, @@ -509,6 +512,7 @@ test2_bin (void) fprintf (stderr, "External hashing error: %d.\n", mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, @@ -539,7 +543,13 @@ test_unaligned (void) digest_buf = (uint8_t *)malloc (mhd_MD5_DIGEST_SIZE + MAX_OFFSET); if ((NULL == buf) || (NULL == digest_buf)) + { + fprintf (stderr, + "FAIL: test_unaligned - memory allocation failed.\n"); + free (digest_buf); + free (buf); exit (99); + } mhd_MD5_init (&ctx); @@ -566,6 +576,9 @@ test_unaligned (void) fprintf (stderr, "External hashing error: %d.\n", mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); + free (digest_buf); + free (buf); exit (99); } num_failed += check_result (MHD_FUNC_, @@ -703,15 +716,22 @@ main (int argc, } else { - printf ("FAIL: %s - digest function failed due to backend error\n", - t->name); + fprintf (stderr, + "FAIL: %s - digest function failed due to backend error: " \ + "%d.\n", + t->name, + mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } } else { - printf ("FAIL: %s - error in init\n", - t->name); + fprintf (stderr, + "FAIL: %s - backend initialisation error: %d.\n", + t->name, + mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } mhd_MD5_deinit (&ctx); @@ -749,15 +769,21 @@ main (int argc, } else { - printf ("FAIL: %s - backend reported error\n", - test_name); + fprintf (stderr, + "FAIL: %s - backend error: %d.\n", + test_name, + mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } } else { - printf ("FAIL: %s - backend reported initialisation error\n", - test_name); + fprintf (stderr, + "FAIL: %s - backend initialisation error: %d.\n", + test_name, + mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } mhd_MD5_deinit (&ctx); @@ -806,8 +832,11 @@ main (int argc, } else { - printf ("FAIL: %s - backend reported initialisation error\n", - test_name); + fprintf (stderr, + "FAIL: %s - backend error: %d.\n", + test_name, + mhd_MD5_get_err (&ctx)); + mhd_MD5_deinit (&ctx); exit (99); } mhd_MD5_deinit (&ctx); diff --git a/src/tests/unit/unit_sha256.c b/src/tests/unit/unit_sha256.c @@ -446,6 +446,7 @@ test1_str (void) fprintf (stderr, "External hashing error: %d.\n", mhd_SHA256_get_err (&ctx)); + mhd_SHA256_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, i, digest, @@ -477,6 +478,7 @@ test1_bin (void) fprintf (stderr, "External hashing error: %d.\n", mhd_SHA256_get_err (&ctx)); + mhd_SHA256_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, i, digest, @@ -515,6 +517,7 @@ test2_str (void) fprintf (stderr, "External hashing error: %d.\n", mhd_SHA256_get_err (&ctx)); + mhd_SHA256_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, i, digest, @@ -551,6 +554,7 @@ test2_bin (void) fprintf (stderr, "External hashing error: %d.\n", mhd_SHA256_get_err (&ctx)); + mhd_SHA256_deinit (&ctx); exit (99); } num_failed += check_result (MHD_FUNC_, i, digest, @@ -580,7 +584,14 @@ test_unaligned (void) buf = (uint8_t *)malloc (tdata->bin_l.len + MAX_OFFSET); digest_buf = (uint8_t *)malloc (mhd_SHA256_DIGEST_SIZE + MAX_OFFSET); if ((NULL == buf) || (NULL == digest_buf)) + { + fprintf (stderr, + "FAIL: test_unaligned - memory allocation failed.\n"); + mhd_SHA256_deinit (&ctx); + free (digest_buf); + free (buf); exit (99); + } for (offset = MAX_OFFSET; offset >= 1; --offset) { @@ -602,6 +613,9 @@ test_unaligned (void) fprintf (stderr, "External hashing error: %d.\n", mhd_SHA256_get_err (&ctx)); + mhd_SHA256_deinit (&ctx); + free (digest_buf); + free (buf); exit (99); } num_failed += check_result (MHD_FUNC_, MAX_OFFSET - offset, diff --git a/src/tests/unit/unit_sha512_256.c b/src/tests/unit/unit_sha512_256.c @@ -670,7 +670,13 @@ test_unaligned (void) buf = (uint8_t *)malloc (tdata->bin_l.len + MAX_OFFSET); digest_buf = (uint8_t *)malloc (mhd_SHA512_256_DIGEST_SIZE + MAX_OFFSET); if ((NULL == buf) || (NULL == digest_buf)) + { + fprintf (stderr, + "FAIL: test_unaligned - memory allocation failed.\n"); + free (digest_buf); + free (buf); exit (99); + } for (offset = MAX_OFFSET; offset >= 1; --offset) {