From c9bb9cd165c1b25c2fe005befdcfe479fc9b68e1 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 29 Jun 2025 14:33:13 +0200 Subject: [PATCH] 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 --- lib/.gitignore | 1 + lib/CMakeLists.txt | 8 ++++ lib/Makefile.am | 20 ++++++++- scripts/Makefile.am | 2 +- scripts/extract-unit-protos | 90 +++++++++++++++++++++++++++++++++++++ tests/unit/CMakeLists.txt | 3 +- tests/unit/unit1300.c | 3 +- tests/unit/unit1395.c | 5 +-- tests/unit/unit3212.c | 3 +- tests/unit/unit3213.c | 3 +- 10 files changed, 125 insertions(+), 13 deletions(-) create mode 100755 scripts/extract-unit-protos diff --git a/lib/.gitignore b/lib/.gitignore index e776bc899d..0f0bcd6465 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -7,3 +7,4 @@ curl_config.h curl_config.h.in libcurl.vers libcurl_unity.c +unitprotos.h diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 19db59f9e6..9b0fda5409 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -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 diff --git a/lib/Makefile.am b/lib/Makefile.am index d43d54979a..75fbbdf4b9 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 0b278e62b0..b4e3c0757b 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -25,7 +25,7 @@ EXTRA_DIST = coverage.sh completion.pl firefox-db2pem.sh checksrc.pl checksrc-all.sh \ mk-ca-bundle.pl mk-unity.pl schemetable.c cd2nroff nroff2cd cdall cd2cd managen \ dmaketgz maketgz release-tools.sh verify-release cmakelint.sh mdlinkcheck \ - CMakeLists.txt pythonlint.sh randdisable wcurl top-complexity + CMakeLists.txt pythonlint.sh randdisable wcurl top-complexity extract-unit-protos dist_bin_SCRIPTS = wcurl diff --git a/scripts/extract-unit-protos b/scripts/extract-unit-protos new file mode 100755 index 0000000000..9fc6c1b9a3 --- /dev/null +++ b/scripts/extract-unit-protos @@ -0,0 +1,90 @@ +#!/usr/bin/env perl +#*************************************************************************** +# _ _ ____ _ +# Project ___| | | | _ \| | +# / __| | | | |_) | | +# | (__| |_| | _ <| |___ +# \___|\___/|_| \_\_____| +# +# Copyright (C) Daniel Stenberg, , et al. +# +# This software is licensed as described in the file COPYING, which +# you should have received as part of this distribution. The terms +# are also available at https://curl.se/docs/copyright.html. +# +# You may opt to use, copy, modify, merge, publish, distribute and/or sell +# copies of the Software, and permit persons to whom the Software is +# furnished to do so, under the terms of the COPYING file. +# +# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY +# KIND, either express or implied. +# +# SPDX-License-Identifier: curl +# +########################################################################### + +sub scanfile { + my ($file) = @_; + open(F, "<$file") || die "$file failed"; + while() { + if($_ =~ /^UNITTEST .*\);/) { + push @proto, $_; + $inc{$file} = 1; + } + } + close(F); +} + +foreach my $f (@ARGV) { + scanfile($f); +} + +print <, et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + * SPDX-License-Identifier: curl + * + * Generated-by: extract-unit-protos + * + ***************************************************************************/ +HEAD + ; + +for my $f (sort keys %inc) { + # convert to suitable header file + $f =~ s/\.c/.h/; # .h extension + + if(-f $f) { + $f =~ s/.*\///; # cut the path off + print "#include \"$f\"\n"; + } +} + +for my $p (@proto) { + print $p; +} + +print <