libmicrohttpd2

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

commit 3bbf327c500cf202da6429a7b4c358dee73254e9
parent 789c33bdeefa58b0e5037ecc9bbb150b1ecd3cb7
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date:   Sat, 23 Aug 2025 23:52:31 +0200

tests: shard some helpers code to avoid duplications

Diffstat:
Msrc/tests/Makefile.am | 2++
Msrc/tests/basic/Makefile.am | 3++-
Msrc/tests/basic/test_basic_checks.c | 59+++++++++++------------------------------------------------
Asrc/tests/mhdt_has_in_name.h | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tests/mhdt_has_param.h | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/tests/upgrade/Makefile.am | 7+++----
Dsrc/tests/upgrade/test_helpers.h | 113-------------------------------------------------------------------------------
Msrc/tests/upgrade/test_upgrade.c | 23++++++++++++-----------
8 files changed, 192 insertions(+), 177 deletions(-)

diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am @@ -13,4 +13,6 @@ endif endif endif +noinst_HEADERS = mhdt_has_in_name.h + .NOTPARALLEL: diff --git a/src/tests/basic/Makefile.am b/src/tests/basic/Makefile.am @@ -4,6 +4,7 @@ EMPTY_ITEM = AM_CPPFLAGS = \ -I$(top_srcdir)/src/include \ -I$(top_srcdir)/src/mhd2 \ + -I$(top_srcdir)/src/tests \ -DMHD_CPU_COUNT=$(CPU_COUNT) \ $(CPPFLAGS_ac) @@ -107,7 +108,7 @@ check_PROGRAMS += test_lib_info_version TESTS = $(check_PROGRAMS) # The universal sources used in all tests -basic_test_sources = test_basic_checks.c +basic_test_sources = test_basic_checks.c $(srcdir)/../mhdt_has_in_name.h test_create_destroy_SOURCES = $(basic_test_sources) diff --git a/src/tests/basic/test_basic_checks.c b/src/tests/basic/test_basic_checks.c @@ -45,6 +45,7 @@ #include <stdlib.h> #include <string.h> #include "microhttpd2.h" +#include "mhdt_has_in_name.h" /* Helper macros */ @@ -91,44 +92,6 @@ #define tst_EXPECT_CHECK(sc,expect_ok) \ ((expect_ok) ? tst_EXPECT_OK ((sc)) : tst_EXPECT_FAIL ((sc))) -/* Helper functions */ - -/** - * 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 the program name, may include path - * @param marker the 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) -{ - const char *s; - const char *basename; - - if (! prog_name || ! marker || ! prog_name[0] || ! marker[0]) - return 0; - - basename = prog_name; - for (s = prog_name; *s; ++s) - { - if ('/' == *s) - basename = s + 1; -#if defined(_WIN32) || defined(__CYGWIN__) - else if ('\\' == *s) - basename = s + 1; -#endif /* _WIN32 || __CYGWIN__ */ - } - - return strstr (basename, marker) != NULL; -} - /* The test */ @@ -326,29 +289,29 @@ test_simple (void) static int init_test (const char *prog_name) { - if (has_in_name (prog_name, "_start")) + if (mhdt_has_in_name (prog_name, "_start")) use_start = ! 0; - if (has_in_name (prog_name, "_ipv4")) + if (mhdt_has_in_name (prog_name, "_ipv4")) use_ipv4 = ! 0; - if (has_in_name (prog_name, "_ipv6")) + if (mhdt_has_in_name (prog_name, "_ipv6")) use_ipv6 = ! 0; - if (has_in_name (prog_name, "_ipbest")) + if (mhdt_has_in_name (prog_name, "_ipbest")) use_ip_best = ! 0; - use_select = has_in_name (prog_name, "_select"); + use_select = mhdt_has_in_name (prog_name, "_select"); - use_poll = has_in_name (prog_name, "_poll"); + use_poll = mhdt_has_in_name (prog_name, "_poll"); - use_epoll = has_in_name (prog_name, "_epoll"); + use_epoll = mhdt_has_in_name (prog_name, "_epoll"); - use_int_thread = has_in_name (prog_name, "_int_thread"); + use_int_thread = mhdt_has_in_name (prog_name, "_int_thread"); - use_thread_per_conn = has_in_name (prog_name, "_thread_per_conn"); + use_thread_per_conn = mhdt_has_in_name (prog_name, "_thread_per_conn"); - use_thread_pool = has_in_name (prog_name, "_thread_pool"); + use_thread_pool = mhdt_has_in_name (prog_name, "_thread_pool"); return ! 0; } diff --git a/src/tests/mhdt_has_in_name.h b/src/tests/mhdt_has_in_name.h @@ -0,0 +1,86 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2016-2025 Evgeny Grin (Karlson2k) + + GNU libmicrohttpd 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. + + GNU libmicrohttpd 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. + + Alternatively, you can redistribute GNU libmicrohttpd 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, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file src/tests/mhdt_has_in_name.h + * @brief A simple helper function to detect tokens in the filename + * @author Karlson2k (Evgeny Grin) + */ +#ifndef MHDT_HAS_IN_NAME_H +#define MHDT_HAS_IN_NAME_H 1 + +#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 the program name, may include path + * @param marker the 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 inline int +mhdt_has_in_name (const char *prog_name, const char *marker) +{ + const char *dir_symb; + const char *basename; + + if (! prog_name || ! marker || ! prog_name[0] || ! marker[0]) + return 0; + + basename = prog_name; + dir_symb = strrchr (basename, '/'); + if (NULL != dir_symb) + basename = dir_symb + 1; + +#if defined(_WIN32) || defined(__CYGWIN__) + dir_symb = strrchr (basename, '\\'); + if (NULL != dir_symb) + basename = dir_symb + 1; +#endif /* _WIN32 || __CYGWIN__ */ + + return (NULL != strstr (basename, marker)); +} + + +#endif /* ! MHDT_HAS_IN_NAME_H */ diff --git a/src/tests/mhdt_has_param.h b/src/tests/mhdt_has_param.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ +/* + This file is part of GNU libmicrohttpd. + Copyright (C) 2016-2025 Karlson2k (Evgeny Grin) + + GNU libmicrohttpd 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. + + GNU libmicrohttpd 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. + + Alternatively, you can redistribute GNU libmicrohttpd 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, together + with the eCos exception, as follows: + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile this + file and link it with other works to produce a work based on this + file, this file does not by itself cause the resulting work to be + covered by the GNU General Public License. However the source code + for this file must still be made available in accordance with + section (3) of the GNU General Public License v2. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + + You should have received copies of the GNU Lesser General Public + License and the GNU General Public License along with this library; + if not, see <https://www.gnu.org/licenses/>. +*/ + +/** + * @file src/tests/mhdt_has_param.h + * @brief A simple helper function to detect parameter presence + * @author Karlson2k (Evgeny Grin) + */ +#ifndef MHDT_HAS_PARAM_H +#define MHDT_HAS_PARAM_H 1 + +#include <string.h> + +/** + * 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 inline int +mhdt_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; +} + + +#endif /* ! MHDT_HAS_PARAM_H */ diff --git a/src/tests/upgrade/Makefile.am b/src/tests/upgrade/Makefile.am @@ -3,6 +3,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/src/include \ -I$(top_srcdir)/src/incl_priv \ -I$(top_srcdir)/src/mhd2 \ + -I$(top_srcdir)/src/tests \ -DMHD_CPU_COUNT=$(CPU_COUNT) \ $(CPPFLAGS_ac) $(MHD_TLS_LIB_CPPFLAGS) @@ -22,16 +23,14 @@ $(top_builddir)/src/mhd2/libmicrohttpd2.la: $(top_builddir)/src/mhd2/Makefile @echo ' cd $(top_builddir)/src/mhd2 && $(MAKE) $(AM_MAKEFLAGS) libmicrohttpd2.la'; \ $(am__cd) $(top_builddir)/src/mhd2 && $(MAKE) $(AM_MAKEFLAGS) libmicrohttpd2.la -noinst_HEADERS = \ - test_helpers.h - check_PROGRAMS = \ test_upgrade \ test_upgrade_large \ test_upgrade_vlarge \ $(EMPTY_ITEM) -test_upgrade_SOURCES = test_upgrade.c test_helpers.h tls_test_keys.h +test_upgrade_SOURCES = \ + test_upgrade.c tls_test_keys.h $(srcdir)/../mhdt_has_in_name.h test_upgrade_large_SOURCES = $(test_upgrade_SOURCES) diff --git a/src/tests/upgrade/test_helpers.h b/src/tests/upgrade/test_helpers.h @@ -1,113 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ -/* - This file is part of GNU libmicrohttpd. - Copyright (C) 2016-2024 Karlson2k (Evgeny Grin) - - GNU libmicrohttpd 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. - - GNU libmicrohttpd 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. - - Alternatively, you can redistribute GNU libmicrohttpd 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, together - with the eCos exception, as follows: - - As a special exception, if other files instantiate templates or - use macros or inline functions from this file, or you compile this - file and link it with other works to produce a work based on this - file, this file does not by itself cause the resulting work to be - covered by the GNU General Public License. However the source code - for this file must still be made available in accordance with - section (3) of the GNU General Public License v2. - - This exception does not invalidate any other reasons why a work - based on this file might be covered by the GNU General Public - License. - - You should have received copies of the GNU Lesser General Public - License and the GNU General Public License along with this library; - if not, see <https://www.gnu.org/licenses/>. -*/ - -/** - * @file 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 *) (void *) 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; - } - - (void) has_in_name; /* Mute compiler warning */ - (void) has_param; /* Mute compiler warning */ - return 0; -} diff --git a/src/tests/upgrade/test_upgrade.c b/src/tests/upgrade/test_upgrade.c @@ -67,7 +67,8 @@ #include "microhttpd2.h" -#include "test_helpers.h" +#include "mhdt_has_in_name.h" +#include "mhdt_has_param.h" #ifdef HTTPS_SUPPORT # include <gnutls/gnutls.h> @@ -1926,16 +1927,16 @@ main (int argc, unsigned int error_count = 0; unsigned int res; - use_vlarge = (0 != has_in_name (argv[0], "_vlarge")); - use_large = (! use_vlarge) && (0 != has_in_name (argv[0], "_large")); + use_vlarge = (0 != mhdt_has_in_name (argv[0], "_vlarge")); + use_large = (! use_vlarge) && (0 != mhdt_has_in_name (argv[0], "_large")); use_tls_tool = TLS_CLI_NO_TOOL; - test_tls = has_in_name (argv[0], "_tls"); + test_tls = mhdt_has_in_name (argv[0], "_tls"); - verbose = ! (has_param (argc, argv, "-q") || - has_param (argc, argv, "--quiet") || - has_param (argc, argv, "-s") || - has_param (argc, argv, "--silent")); + verbose = ! (mhdt_has_param (argc, argv, "-q") || + mhdt_has_param (argc, argv, "--quiet") || + mhdt_has_param (argc, argv, "-s") || + mhdt_has_param (argc, argv, "--silent")); if ((((int) ((~((unsigned int) 0U)) >> 1)) / 1000) < test_timeout) { @@ -1950,11 +1951,11 @@ main (int argc, { use_tls_tool = TLS_LIB_GNUTLS; /* Should be always available as MHD uses it when TLS is supported. */ #ifdef HTTPS_SUPPORT - if (has_param (argc, argv, "--use-gnutls-cli")) + if (mhdt_has_param (argc, argv, "--use-gnutls-cli")) use_tls_tool = TLS_CLI_GNUTLS; - else if (has_param (argc, argv, "--use-openssl")) + else if (mhdt_has_param (argc, argv, "--use-openssl")) use_tls_tool = TLS_CLI_OPENSSL; - else if (has_param (argc, argv, "--use-gnutls-lib")) + else if (mhdt_has_param (argc, argv, "--use-gnutls-lib")) use_tls_tool = TLS_LIB_GNUTLS; #if defined(HAVE_FORK) && defined(HAVE_WAITPID) else if (0 == system ("gnutls-cli --version 1> /dev/null 2> /dev/null"))