tests: always make bundles, adapt build and tests

Make test bundles the default. Drop non-bundle build mode.
Also do all the optimizations and tidy-ups this allows, simpler builds,
less bundle exceptions, streamlined build mechanics.

Also rework the init/deinit macro magic for unit tests. The new method
allows using unique init/deinit function names, and calling them with
arguments. This is in turn makes it possible to reduce the use of global
variables.

Note this drop existing build options `-DCURL_TEST_BUNDLES=` from cmake
and `--enable-test-bundles` / `--disable-test-bundles` from autotools.

Also:
- rename test entry functions to have unique names: `test_<testname>`
  This removes the last exception that was handled in the generator.
- fix `make dist` to not miss test sources with test bundles enabled.
- sync and merge `tests/mk-bundle.pl` into `scripts/mk-unity.pl`.
- mk-unity.pl: add `--embed` option and use it when `CURL_CLANG_TIDY=ON`
  to ensure that `clang-tidy` does not miss external test C sources.
  (because `clang-tidy` ignores code that's #included.)
- tests/unit: drop no-op setup/stop functions.
- tests: reduce symbol scopes, global macros, other fixes and tidy-ups.
- tool1621: fix to run, also fix it to pass.
- sockfilt: fix Windows compiler warning in certain unity include order,
  by explicitly including `warnless.h`.

Follow-up to 6897aeb105 #17468

Closes #17590
This commit is contained in:
Viktor Szakats 2025-06-11 11:31:14 +02:00
parent 1cdac95e2e
commit 2c27a67daa
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
386 changed files with 2996 additions and 4164 deletions

View file

@ -2,6 +2,5 @@
#
# SPDX-License-Identifier: curl
/tool[0-9][0-9][0-9][0-9]
tool_bundle.c
tunits
tunits.c

View file

@ -22,41 +22,29 @@
#
###########################################################################
# Get 'TOOLPROGS', '*_SOURCES', 'TOOLFILES' variables
# Get BUNDLE, BUNDLE_SRC, FIRSTFILES, UTILS, TESTFILES variables
curl_transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
if(CURL_TEST_BUNDLES)
add_custom_command(
OUTPUT "tool_bundle.c"
COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/tests/mk-bundle.pl" "${CMAKE_CURRENT_SOURCE_DIR}" > "tool_bundle.c"
DEPENDS
"${PROJECT_SOURCE_DIR}/tests/mk-bundle.pl" ${TOOLFILES}
"${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc"
VERBATIM)
add_custom_command(
OUTPUT "${BUNDLE_SRC}"
COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" --test ${TESTFILES}
${CURL_MK_UNITY_OPTION} --srcdir "${CMAKE_CURRENT_SOURCE_DIR}" > "${BUNDLE_SRC}"
DEPENDS
"${PROJECT_SOURCE_DIR}/scripts/mk-unity.pl" "${CMAKE_CURRENT_SOURCE_DIR}/Makefile.inc" ${FIRSTFILES} ${TESTFILES}
VERBATIM)
set(TOOLPROGS "tunits")
set(tunits_SOURCES "tool_bundle.c")
endif()
foreach(_target IN LISTS TOOLPROGS)
set(_target_name "${_target}")
add_executable(${_target_name} EXCLUDE_FROM_ALL ${${_target}_SOURCES})
add_dependencies(testdeps ${_target_name})
target_link_libraries(${_target_name} curltool curlu)
target_include_directories(${_target_name} PRIVATE
"${PROJECT_BINARY_DIR}/lib" # for "curl_config.h"
"${PROJECT_SOURCE_DIR}/lib" # for "curl_setup.h"
"${PROJECT_SOURCE_DIR}/lib/curlx" # for curlx
"${PROJECT_SOURCE_DIR}/src"
"${PROJECT_SOURCE_DIR}/tests/libtest"
"${PROJECT_SOURCE_DIR}/tests/unit" # for curlcheck.h
)
set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "${CURL_DEBUG_MACROS}")
if(CURL_TEST_BUNDLES)
target_include_directories(${_target_name} PRIVATE "${PROJECT_SOURCE_DIR}/tests/tunit")
endif()
set_target_properties(${_target_name} PROPERTIES
OUTPUT_NAME "${_target}"
PROJECT_LABEL "Test tunit ${_target}")
endforeach()
add_executable(tunits EXCLUDE_FROM_ALL ${UTILS} "${BUNDLE_SRC}")
add_dependencies(testdeps tunits)
target_link_libraries(tunits curltool curlu)
target_include_directories(tunits PRIVATE
"${PROJECT_BINARY_DIR}/lib" # for "curl_config.h"
"${PROJECT_SOURCE_DIR}/lib" # for "curl_setup.h"
"${PROJECT_SOURCE_DIR}/lib/curlx" # for curlx
"${PROJECT_SOURCE_DIR}/src"
"${PROJECT_SOURCE_DIR}/tests/libtest"
"${PROJECT_SOURCE_DIR}/tests/unit" # for curlcheck.h
"${PROJECT_SOURCE_DIR}/tests/tunit"
)
set_property(TARGET tunits APPEND PROPERTY COMPILE_DEFINITIONS "${CURL_DEBUG_MACROS}" "CURL_NO_OLDIES" "CURL_DISABLE_DEPRECATION")
set_target_properties(tunits PROPERTIES OUTPUT_NAME "${BUNDLE}" PROJECT_LABEL "Test ${BUNDLE}")

View file

@ -41,7 +41,10 @@ AM_CPPFLAGS = -I$(top_srcdir)/include \
-I$(top_srcdir)/tests/unit \
-I$(top_srcdir)/tests/tunit
EXTRA_DIST = CMakeLists.txt README.md
# Get BUNDLE, BUNDLE_SRC, FIRSTFILES, UTILS, TESTFILES variables
include Makefile.inc
EXTRA_DIST = CMakeLists.txt README.md $(UTILS) $(TESTFILES)
CFLAGS += @CURL_CFLAG_EXTRAS@
@ -49,7 +52,7 @@ CFLAGS += @CURL_CFLAG_EXTRAS@
LIBS = $(BLANK_AT_MAKETIME)
LDADD = $(top_builddir)/src/libcurltool.la \
$(top_builddir)/lib/libcurl.la \
$(top_builddir)/lib/libcurl.la \
@LIBCURL_PC_LDFLAGS_PRIVATE@ @LIBCURL_PC_LIBS_PRIVATE@
AM_CPPFLAGS += -DCURL_STATICLIB -DUNITTESTS
@ -60,21 +63,15 @@ if CURLDEBUG
AM_CPPFLAGS += -DCURLDEBUG
endif
BUNDLE=tunits
AM_CPPFLAGS += -DCURL_NO_OLDIES -DCURL_DISABLE_DEPRECATION
if BUILD_UNITTESTS
if USE_TEST_BUNDLES
tool_bundle.c: $(top_srcdir)/tests/mk-bundle.pl Makefile.inc
@PERL@ $(top_srcdir)/tests/mk-bundle.pl $(srcdir) > tool_bundle.c
$(BUNDLE_SRC): $(top_srcdir)/scripts/mk-unity.pl Makefile.inc $(FIRSTFILES) $(UTILS) $(TESTFILES)
@PERL@ $(top_srcdir)/scripts/mk-unity.pl --include $(UTILS) --test $(TESTFILES) > $(BUNDLE_SRC)
noinst_PROGRAMS = $(BUNDLE)
nodist_tunits_SOURCES = tool_bundle.c
CLEANFILES = tool_bundle.c
else
# Makefile.inc provides neat definitions
include Makefile.inc
noinst_PROGRAMS = $(TOOLPROGS)
endif
nodist_tunits_SOURCES = $(BUNDLE_SRC)
CLEANFILES = $(BUNDLE_SRC)
else
noinst_PROGRAMS =
endif
@ -87,7 +84,7 @@ CS_ = $(CS_0)
# ignore generated C files since they play by slightly different rules!
checksrc:
$(CHECKSRC)(@PERL@ $(top_srcdir)/scripts/checksrc.pl -D$(srcdir) \
-W$(srcdir)/tool_bundle.c \
-W$(srcdir)/$(BUNDLE_SRC) \
$(srcdir)/*.[ch])
if NOT_CURL_CI

View file

@ -21,18 +21,19 @@
# SPDX-License-Identifier: curl
#
###########################################################################
# Shared between CMakeLists.txt and Makefile.am
# these files are used in every single unit test program
BUNDLE = tunits
BUNDLE_SRC = tunits.c
FIRSTFILES = ../libtest/first.c
# Files referenced from the bundle source
FIRSTFILES = ../libtest/first.c ../libtest/first.h
TOOLFILES = $(FIRSTFILES)
# Common files used by test programs
UTILS = ../unit/curlcheck.h
# These are all tool unit test programs
TOOLPROGS = tool1394 tool1604 tool1621
tool1394_SOURCES = tool1394.c $(TOOLFILES)
tool1604_SOURCES = tool1604.c $(TOOLFILES)
tool1621_SOURCES = tool1621.c $(TOOLFILES)
# All tool unit test programs
TESTFILES = \
tool1394.c \
tool1604.c \
tool1621.c

View file

@ -31,17 +31,9 @@
#include "memdebug.h" /* LAST include file */
static CURLcode unit_setup(void)
static CURLcode test_tool1394(char *arg)
{
return CURLE_OK;
}
static void unit_stop(void)
{
}
UNITTEST_START
UNITTEST_BEGIN_SIMPLE
const char *values[] = {
/* -E parameter */ /* exp. cert name */ /* exp. passphrase */
@ -130,4 +122,5 @@ UNITTEST_START
free(passphrase);
}
UNITTEST_STOP
UNITTEST_END_SIMPLE
}

View file

@ -32,18 +32,7 @@
#include "memdebug.h" /* LAST include file */
static CURLcode unit_setup(void)
{
return CURLE_OK;
}
static void unit_stop(void)
{
}
#if defined(_WIN32) || defined(MSDOS)
static char *getflagstr(int flags)
{
char *buf = malloc(256);
@ -71,16 +60,21 @@ static char *getcurlcodestr(int cc)
}
return buf;
}
#endif
struct data {
const char *input;
int flags;
const char *expected_output;
SANITIZEcode expected_result;
};
static CURLcode test_tool1604(char *arg)
{
UNITTEST_BEGIN_SIMPLE
UNITTEST_START
{ /* START sanitize_file_name */
#if defined(_WIN32) || defined(MSDOS)
struct data {
const char *input;
int flags;
const char *expected_output;
SANITIZEcode expected_result;
};
/* START sanitize_file_name */
struct data data[] = {
{ "", 0,
"", SANITIZE_ERR_OK
@ -256,13 +250,10 @@ UNITTEST_START
free(received_ccstr);
free(expected_ccstr);
}
} /* END sanitize_file_name */
/* END sanitize_file_name */
#else
UNITTEST_START
{
fprintf(stderr, "Skipped test not for this platform\n");
}
#endif /* _WIN32 || MSDOS */
UNITTEST_STOP
UNITTEST_END_SIMPLE
}

View file

@ -26,67 +26,64 @@
#include "urldata.h"
#include "url.h"
#include "tool_xattr.h"
#include "memdebug.h" /* LAST include file */
static CURLcode unit_setup(void)
static CURLcode test_tool1621(char *arg)
{
return CURLE_OK;
}
UNITTEST_BEGIN_SIMPLE
static void unit_stop(void)
{
}
#ifdef USE_XATTR /* Required for stripcredentials() */
#ifndef USE_XATTR
/* stripcredentials isn't available in this case */
struct checkthis {
const char *input;
const char *output;
};
UNITTEST_START
UNITTEST_STOP
#else
static const struct checkthis tests[] = {
{ "ninja://foo@example.com", "(null)" }, /* unsupported scheme */
#if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
{ "pop3s://foo@example.com", "pop3s://example.com/" },
#endif
#ifndef CURL_DISABLE_LDAP
{ "ldap://foo@example.com", "ldap://example.com/" },
#endif
#if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
{ "https://foo@example.com", "https://example.com/" },
{ "https://localhost:45", "https://localhost:45/" },
{ "https://foo@localhost:45", "https://localhost:45/" },
{ "https://user:pass@localhost:45", "https://localhost:45/" },
#endif
#ifndef CURL_DISABLE_HTTP
{ "http://daniel:password@localhost", "http://localhost/" },
{ "http://daniel@localhost", "http://localhost/" },
{ "http://localhost/", "http://localhost/" },
{ "http://odd%40host/", "(null)" }, /* bad host */
{ "http://user@odd%40host/", "(null)" }, /* bad host */
{ "http://host/@path/", "http://host/@path/" },
{ "http://emptypw:@host/", "http://host/" },
{ "http://:emptyuser@host/", "http://host/" },
{ "http://odd%40user@host/", "http://host/" },
{ "http://only%40one%40host/", "(null)" }, /* bad host */
{ "http://odder%3auser@host/", "http://host/" },
#endif
{ NULL, NULL } /* end marker */
};
char *stripcredentials(const char *url);
struct checkthis {
const char *input;
const char *output;
};
static const struct checkthis tests[] = {
{ "ninja://foo@example.com", "(null)" }, /* unsupported scheme */
{ "pop3s://foo@example.com", "pop3s://example.com/" },
{ "ldap://foo@example.com", "ldap://example.com/" },
{ "https://foo@example.com", "https://example.com/" },
{ "https://localhost:45", "https://localhost:45/" },
{ "https://foo@localhost:45", "https://localhost:45/" },
{ "http://daniel:password@localhost", "http://localhost/" },
{ "http://daniel@localhost", "http://localhost/" },
{ "https://user:pass@localhost:45", "https://localhost:45/" },
{ "http://localhost/", "http://localhost/" },
{ "http://odd%40host/", "(null)" }, /* bad host */
{ "http://user@odd%40host/", "(null)" }, /* bad host */
{ "http://host/@path/", "http://host/@path/" },
{ "http://emptypw:@host/", "http://host/" },
{ "http://:emptyuser@host/", "http://host/" },
{ "http://odd%40user@host/", "http://host/" },
{ "http://only%40one%40host/", "(null)" }, /* bad host */
{ "http://odder%3auser@host/", "http://host/" },
{ NULL, NULL } /* end marker */
};
UNITTEST_START
{
int i;
for(i = 0; tests[i].input; i++) {
const char *url = tests[i].input;
char *stripped = stripcredentials(url);
char *strippedstr = stripped ? stripped : "(null)";
printf("Test %u got input \"%s\", output: \"%s\"\n",
i, tests[i].input, strippedstr);
const char *strippedstr = stripped ? stripped : "(null)";
printf("Test %u got input \"%s\", output: \"%s\", expected: \"%s\"\n",
i, tests[i].input, strippedstr, tests[i].output);
fail_if(strcmp(tests[i].output, strippedstr), tests[i].output);
curl_free(stripped);
}
}
UNITTEST_STOP
#endif
UNITTEST_END_SIMPLE
}