libmicrohttpd2

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

commit 20ebfbe86acb2305f5e4fa998837a0c9c247ea41
parent 6295bff6f7262e42e6f1da60c8756196f6e058f0
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Thu, 25 Dec 2025 11:32:25 +0100

unit tests: reduced code duplication

Diffstat:
Msrc/tests/unit/Makefile.am | 9++++++---
Dsrc/tests/unit/test_helpers.h | 92-------------------------------------------------------------------------------
Msrc/tests/unit/test_str_compare.c | 12++++++------
Msrc/tests/unit/test_str_from_value.c | 12++++++------
Msrc/tests/unit/test_str_to_value.c | 12++++++------
Msrc/tests/unit/unit_md5.c | 33++++-----------------------------
Msrc/tests/unit/unit_sha256.c | 33++++-----------------------------
Msrc/tests/unit/unit_sha512_256.c | 32+++-----------------------------
8 files changed, 35 insertions(+), 200 deletions(-)

diff --git a/src/tests/unit/Makefile.am b/src/tests/unit/Makefile.am @@ -71,13 +71,16 @@ TESTS = $(check_PROGRAMS) unit_str_SOURCES = unit_str.c test_str_compare_SOURCES = \ - test_str_compare.c test_helpers.h + test_str_compare.c \ + $(srcdir)/../mhdt_has_in_name.h test_str_to_value_SOURCES = \ - test_str_to_value.c test_helpers.h + test_str_to_value.c \ + $(srcdir)/../mhdt_has_in_name.h test_str_from_value_SOURCES = \ - test_str_from_value.c test_helpers.h + test_str_from_value.c \ + $(srcdir)/../mhdt_has_in_name.h test_str_token_SOURCES = \ test_str_token.c diff --git a/src/tests/unit/test_helpers.h b/src/tests/unit/test_helpers.h @@ -1,92 +0,0 @@ -/* - This file is part of libmicrohttpd - Copyright (C) 2016 Karlson2k (Evgeny Grin) - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -/** - * @file microhttpd/test_helpers.h - * @brief Static functions and macros helpers for testsuite. - * @author Karlson2k (Evgeny Grin) - */ - -#include <string.h> - -/** - * Check whether program name contains specific @a marker string. - * Only last component in pathname is checked for marker presence, - * all leading directories names (if any) are ignored. Directories - * separators are handled correctly on both non-W32 and W32 - * platforms. - * @param prog_name program name, may include path - * @param marker marker to look for. - * @return zero if any parameter is NULL or empty string or - * @a prog_name ends with slash or @a marker is not found in - * program name, non-zero if @a maker is found in program - * name. - */ -static int -has_in_name (const char *prog_name, const char *marker) -{ - size_t name_pos; - size_t pos; - - if (! prog_name || ! marker || ! prog_name[0] || ! marker[0]) - return 0; - - pos = 0; - name_pos = 0; - while (prog_name[pos]) - { - if ('/' == prog_name[pos]) - name_pos = pos + 1; -#if defined(_WIN32) || defined(__CYGWIN__) - else if ('\\' == prog_name[pos]) - name_pos = pos + 1; -#endif /* _WIN32 || __CYGWIN__ */ - pos++; - } - if (name_pos == pos) - return 0; - return strstr (prog_name + name_pos, marker) != (char *) 0; -} - - -/** - * Check whether one of strings in array is equal to @a param. - * String @a argv[0] is ignored. - * @param argc number of strings in @a argv, as passed to main function - * @param argv array of strings, as passed to main function - * @param param parameter to look for. - * @return zero if @a argv is NULL, @a param is NULL or empty string, - * @a argc is less then 2 or @a param is not found in @a argv, - * non-zero if one of strings in @a argv is equal to @a param. - */ -static int -has_param (int argc, char *const argv[], const char *param) -{ - int i; - if (! argv || ! param || ! param[0]) - return 0; - - for (i = 1; i < argc; i++) - { - if (argv[i] && (strcmp (argv[i], param) == 0) ) - return ! 0; - } - - return 0; -} diff --git a/src/tests/unit/test_str_compare.c b/src/tests/unit/test_str_compare.c @@ -27,6 +27,7 @@ #include "../mhd2/mhd_str.h" #include "../mhd2/mhd_str.c" #include "../mhdt_checks.h" +#include "../mhdt_has_param.h" #include <stdio.h> #include <locale.h> @@ -44,7 +45,6 @@ #endif /* HAVE_STDLIB_H */ #include "mhd_limits.h" #include "mhd_str.h" -#include "test_helpers.h" static int verbose = 0; /* verbose level (0-3)*/ @@ -867,12 +867,12 @@ int main (int argc, char *argv[]) { - if (has_param (argc, argv, "-v") || - has_param (argc, argv, "--verbose") || - has_param (argc, argv, "--verbose1")) + if (mhdt_has_param (argc, argv, "-v") || + mhdt_has_param (argc, argv, "--verbose") || + mhdt_has_param (argc, argv, "--verbose1")) MHDT_set_verbosity (MHDT_VERB_LVL_BASIC); - if (has_param (argc, argv, "-vv") || - has_param (argc, argv, "--verbose2")) + if (mhdt_has_param (argc, argv, "-vv") || + mhdt_has_param (argc, argv, "--verbose2")) MHDT_set_verbosity (MHDT_VERB_LVL_VERBOSE); return run_eq_neq_str_tests (); diff --git a/src/tests/unit/test_str_from_value.c b/src/tests/unit/test_str_from_value.c @@ -27,6 +27,7 @@ #include "../mhd2/mhd_str.h" #include "../mhd2/mhd_str.c" #include "../mhdt_checks.h" +#include "../mhdt_has_param.h" #include <stdio.h> #include <locale.h> @@ -44,7 +45,6 @@ #endif /* HAVE_STDLIB_H */ #include "mhd_limits.h" #include "mhd_str.h" -#include "test_helpers.h" static int verbose = 0; /* verbose level (0-3)*/ @@ -1803,12 +1803,12 @@ run_str_from_X_tests (void) int main (int argc, char *argv[]) { - if (has_param (argc, argv, "-v") || - has_param (argc, argv, "--verbose") || - has_param (argc, argv, "--verbose1")) + if (mhdt_has_param (argc, argv, "-v") || + mhdt_has_param (argc, argv, "--verbose") || + mhdt_has_param (argc, argv, "--verbose1")) MHDT_set_verbosity (MHDT_VERB_LVL_BASIC); - if (has_param (argc, argv, "-vv") || - has_param (argc, argv, "--verbose2")) + if (mhdt_has_param (argc, argv, "-vv") || + mhdt_has_param (argc, argv, "--verbose2")) MHDT_set_verbosity (MHDT_VERB_LVL_VERBOSE); return run_str_from_X_tests (); diff --git a/src/tests/unit/test_str_to_value.c b/src/tests/unit/test_str_to_value.c @@ -27,6 +27,7 @@ #include "../mhd2/mhd_str.h" #include "../mhd2/mhd_str.c" #include "../mhdt_checks.h" +#include "../mhdt_has_param.h" #include <stdio.h> #include <locale.h> @@ -44,7 +45,6 @@ #endif /* HAVE_STDLIB_H */ #include "mhd_limits.h" #include "mhd_str.h" -#include "test_helpers.h" static int verbose = 0; /* verbose level (0-3)*/ @@ -2602,12 +2602,12 @@ run_str_to_X_tests (void) int main (int argc, char *argv[]) { - if (has_param (argc, argv, "-v") || - has_param (argc, argv, "--verbose") || - has_param (argc, argv, "--verbose1")) + if (mhdt_has_param (argc, argv, "-v") || + mhdt_has_param (argc, argv, "--verbose") || + mhdt_has_param (argc, argv, "--verbose1")) MHDT_set_verbosity (MHDT_VERB_LVL_BASIC); - if (has_param (argc, argv, "-vv") || - has_param (argc, argv, "--verbose2")) + if (mhdt_has_param (argc, argv, "-vv") || + mhdt_has_param (argc, argv, "--verbose2")) MHDT_set_verbosity (MHDT_VERB_LVL_VERBOSE); return run_str_to_X_tests (); diff --git a/src/tests/unit/unit_md5.c b/src/tests/unit/unit_md5.c @@ -49,6 +49,8 @@ #include "mhd_sys_options.h" +#include "mhdt_has_param.h" + #if MHD_MD5_EXTR_OPENSSL #include "../mhd2/md5_ext_openssl.c" #elif MHD_MD5_EXTR_GNUTLS @@ -69,33 +71,6 @@ static int verbose; -/** - * Check whether one of strings in array is equal to @a param. - * String @a argv[0] is ignored. - * @param argc number of strings in @a argv, as passed to main function - * @param argv array of strings, as passed to main function - * @param param parameter to look for. - * @return zero if @a argv is NULL, @a param is NULL or empty string, - * @a argc is less then 2 or @a param is not found in @a argv, - * non-zero if one of strings in @a argv is equal to @a param. - */ -static int -has_param (int argc, char *const argv[], const char *param) -{ - int i; - if (! argv || ! param || ! param[0]) - return 0; - - for (i = 1; i < argc; i++) - { - if (argv[i] && (strcmp (argv[i], param) == 0) ) - return ! 0; - } - - return 0; -} - - /* Define to 1 if libmicrohttpd is compiled with MD5 hashing by OpenSSL. */ /* #undef MHD_MD5_EXTR_OPENSSL */ @@ -720,8 +695,8 @@ main (int argc, unsigned int total = 0; unsigned int num_failed; - if (has_param (argc, argv, "-v") || - has_param (argc, argv, "--verbose")) + if (mhdt_has_param (argc, argv, "-v") || + mhdt_has_param (argc, argv, "--verbose")) verbose = 1; while (NULL != tests[total].name) { diff --git a/src/tests/unit/unit_sha256.c b/src/tests/unit/unit_sha256.c @@ -50,6 +50,8 @@ #include "mhd_sys_options.h" +#include "mhdt_has_param.h" + #if MHD_SHA256_EXTR_OPENSSL #include "../mhd2/sha256_ext_openssl.c" #elif MHD_SHA256_EXTR_GNUTLS @@ -70,33 +72,6 @@ static int verbose; -/** - * Check whether one of strings in array is equal to @a param. - * String @a argv[0] is ignored. - * @param argc number of strings in @a argv, as passed to main function - * @param argv array of strings, as passed to main function - * @param param parameter to look for. - * @return zero if @a argv is NULL, @a param is NULL or empty string, - * @a argc is less then 2 or @a param is not found in @a argv, - * non-zero if one of strings in @a argv is equal to @a param. - */ -static int -has_param (int argc, char *const argv[], const char *param) -{ - int i; - if (! argv || ! param || ! param[0]) - return 0; - - for (i = 1; i < argc; i++) - { - if (argv[i] && (strcmp (argv[i], param) == 0) ) - return ! 0; - } - - return 0; -} - - /* Helper function to convert hex string to binary */ static size_t hex2bin (const char *hex, @@ -712,8 +687,8 @@ main (int argc, int total = 0; int num_failed; - if (has_param (argc, argv, "-v") || - has_param (argc, argv, "--verbose")) + if (mhdt_has_param (argc, argv, "-v") || + mhdt_has_param (argc, argv, "--verbose")) verbose = 1; diff --git a/src/tests/unit/unit_sha512_256.c b/src/tests/unit/unit_sha512_256.c @@ -50,6 +50,7 @@ #include <stdint.h> #include "mhd_sys_options.h" +#include "mhdt_has_param.h" #if MHD_SHA512_256_EXTR_OPENSSL #include "../mhd2/sha512_256_ext_openssl.c" @@ -69,33 +70,6 @@ static int verbose; -/** - * Check whether one of strings in array is equal to @a param. - * String @a argv[0] is ignored. - * @param argc number of strings in @a argv, as passed to main function - * @param argv array of strings, as passed to main function - * @param param parameter to look for. - * @return zero if @a argv is NULL, @a param is NULL or empty string, - * @a argc is less then 2 or @a param is not found in @a argv, - * non-zero if one of strings in @a argv is equal to @a param. - */ -static int -has_param (int argc, char *const argv[], const char *param) -{ - int i; - if (! argv || ! param || ! param[0]) - return 0; - - for (i = 1; i < argc; i++) - { - if (argv[i] && (strcmp (argv[i], param) == 0) ) - return ! 0; - } - - return 0; -} - - /* Helper function to convert hex string to binary */ static size_t hex2bin (const char *hex, @@ -786,8 +760,8 @@ main (int argc, unsigned int total = 0; int num_failed; - if (has_param (argc, argv, "-v") || - has_param (argc, argv, "--verbose")) + if (mhdt_has_param (argc, argv, "-v") || + mhdt_has_param (argc, argv, "--verbose")) verbose = 1;