unit tests: extract "private" prototypes at build time

In order to do unit tests for private functions, functions that are
marked UNITTEST but without a global scope in the library, functions
that do not have prototypes in their corresponding header file, unit
tests previously brought their own private prototype *copy* into the
unit test.

This was error-prone when the internal function changes but the change
might be missed in the unit test which then uses an outdated prototype
copy for testing.

This change removes the private prototypes from unit tests and instead
introduces a C file parser that parses the specific C files and extracts
the necessary unit test prototypes into a generated header file for unit
tests to use. This geneated lib/unitprotos.h header is then included by
unit tests that need private prototypes.

Assisted-by: Viktor Szakats
Closes #17750
This commit is contained in:
Daniel Stenberg 2025-06-29 14:33:13 +02:00
parent 48d3407d7c
commit c9bb9cd165
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 125 additions and 13 deletions

1
lib/.gitignore vendored
View file

@ -7,3 +7,4 @@ curl_config.h
curl_config.h.in
libcurl.vers
libcurl_unity.c
unitprotos.h

View file

@ -49,6 +49,14 @@ if(CURL_BUILD_TESTING)
# There is plenty of parallelism when building the testdeps target.
# Override the curlu batch size with the maximum to optimize performance.
set_target_properties(curlu PROPERTIES UNITY_BUILD_BATCH_SIZE 0 C_CLANG_TIDY "")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/unitprotos.h"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND ${PERL_EXECUTABLE} "${PROJECT_SOURCE_DIR}/scripts/extract-unit-protos"
${CSOURCES} > "${CMAKE_CURRENT_BINARY_DIR}/unitprotos.h"
DEPENDS "${PROJECT_SOURCE_DIR}/scripts/extract-unit-protos" ${CSOURCES}
VERBATIM)
add_custom_target(curlu-unitprotos ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/unitprotos.h")
endif()
## Library definition

View file

@ -39,6 +39,10 @@ lib_LTLIBRARIES = libcurl.la
if BUILD_UNITTESTS
noinst_LTLIBRARIES = libcurlu.la
# generate a file with "private" prototypes for unit testing
UNITPROTOS = unitprotos.h
else
noinst_LTLIBRARIES =
endif
@ -88,8 +92,11 @@ CLEANFILES = libcurl_unity.c
else
libcurl_la_SOURCES = $(CSOURCES) $(HHEADERS)
libcurlu_la_SOURCES = $(CSOURCES) $(HHEADERS)
CLEANFILES =
endif
CLEANFILES += $(UNITPROTOS)
libcurl_la_CPPFLAGS_EXTRA =
libcurl_la_LDFLAGS_EXTRA =
libcurl_la_CFLAGS_EXTRA =
@ -151,10 +158,21 @@ checksrc:
if NOT_CURL_CI
if DEBUGBUILD
# for debug builds, we scan the sources on all regular make invokes
all-local: checksrc
CHECKSOURCES = checksrc
endif
endif
all-local: $(CHECKSOURCES) $(UNITPROTOS)
UNIT_V = $(UNITV_$(V))
UNITV_0 = @echo " UNITPR " $@;
UNITV_1 =
UNITV_ = $(UNITV_0)
# UNITPROTOS depends on every C file in the lib/ dir
$(UNITPROTOS): $(CSOURCES)
$(UNIT_V)(cd $(srcdir) && @PERL@ ../scripts/extract-unit-protos $(CSOURCES) > $(top_builddir)/lib/$(UNITPROTOS))
# disable the tests that are mostly causing false positives
TIDYFLAGS := -checks=-clang-analyzer-security.insecureAPI.bzero,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-optin.performance.Padding,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling -quiet
if CURL_WERROR