From: Hans Kristian Rosbach Date: Tue, 11 Nov 2025 16:24:26 +0000 (+0100) Subject: Use CTest to simplify testing options X-Git-Tag: 2.3.0-rc2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8d4bd56217ab8f2701bc3f2fa643dcf45d460343;p=thirdparty%2Fzlib-ng.git Use CTest to simplify testing options Add CMake variable TEST_STOCK_ZLIB to disable some tests if attempting to run our testsuite on stock zlib. PR depends on CMP0077, introduced by CMake 3.13. Upped minimum compatible CMake version to 3.13, same as we have actually been telling people was the minumum for years on the wiki. Upped upper compatible CMake version to 3.31, my current version. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index df8311338..d2ef3c9fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12...3.29.0) +cmake_minimum_required(VERSION 3.14...3.31.0) message(STATUS "Using CMake version ${CMAKE_VERSION}") if(POLICY CMP0169) @@ -45,15 +45,21 @@ include(CheckCSourceRuns) include(CheckCCompilerFlag) include(CMakeDependentOption) include(CMakePackageConfigHelpers) +include(CTest) include(FeatureSummary) # We need to enable C++ before trying to check for coverage -option(ZLIB_ENABLE_TESTS "Build test binaries" ON) -cmake_dependent_option(ZLIBNG_ENABLE_TESTS "Test zlib-ng specific API" ON "ZLIB_ENABLE_TESTS" OFF) -cmake_dependent_option(WITH_GTEST "Build gtest_zlib" ON "ZLIB_ENABLE_TESTS" OFF) -cmake_dependent_option(WITH_FUZZERS "Build test/fuzz" OFF "ZLIB_ENABLE_TESTS" OFF) -cmake_dependent_option(WITH_BENCHMARKS "Build test/benchmarks" OFF "ZLIB_ENABLE_TESTS" OFF) -cmake_dependent_option(WITH_BENCHMARK_APPS "Build application benchmarks" OFF "WITH_BENCHMARKS" OFF) +option(BUILD_TESTING "Build test binaries." ON) # Depends on CMP0077 (3.13) +if (DEFINED ZLIB_ENABLE_TESTS) + message(DEPRECATION "ZLIB_ENABLE_TESTS is deprecated. Please use BUILD_TESTING instead.") + if (NOT ZLIB_ENABLE_TESTS) + set(BUILD_TESTING OFF) + endif() +endif() +cmake_dependent_option(WITH_GTEST "Build gtest_zlib" ON "BUILD_TESTING" OFF) +cmake_dependent_option(WITH_FUZZERS "Build test/fuzz" OFF "BUILD_TESTING" OFF) +cmake_dependent_option(WITH_BENCHMARKS "Build test/benchmarks" OFF "BUILD_TESTING" OFF) +cmake_dependent_option(WITH_BENCHMARK_APPS "Build application benchmarks" OFF "BUILD_TESTING" OFF) if(WITH_GTEST OR WITH_FUZZERS OR WITH_BENCHMARKS) enable_language(CXX) @@ -1574,17 +1580,17 @@ endif() # Example binaries #============================================================================ -if(ZLIB_ENABLE_TESTS) +if(BUILD_TESTING) enable_testing() if(BUILD_SHARED_LIBS) - if(ZLIBNG_ENABLE_TESTS) - message(STATUS "Disabling zlib-ng tests because shared libraries are enabled") - set(ZLIBNG_ENABLE_TESTS OFF) + if(WITH_GTEST) + message(STATUS "Disabling gtest because only shared libraries are enabled") + set(WITH_GTEST OFF) endif() if(WITH_BENCHMARKS OR WITH_BENCHMARK_APPS) - message(STATUS "Disabling benchmarks because shared libraries are enabled") + message(STATUS "Disabling google benchmarks because only shared libraries are enabled") set(WITH_BENCHMARKS OFF) set(WITH_BENCHMARK_APPS OFF) endif() @@ -1596,9 +1602,8 @@ endif() add_feature_info(WITH_GZFILEOP WITH_GZFILEOP "Compile with support for gzFile related functions") add_feature_info(ZLIB_COMPAT ZLIB_COMPAT "Compile with zlib compatible API") add_feature_info(ZLIB_ALIASES ZLIB_ALIASES "Compile with zlib compatible CMake targets") -add_feature_info(ZLIB_ENABLE_TESTS ZLIB_ENABLE_TESTS "Enable options for building tests") -add_feature_info(ZLIBNG_ENABLE_TESTS ZLIBNG_ENABLE_TESTS "Build test binaries") -add_feature_info(WITH_SANITIZER WITH_SANITIZER "Enable sanitizer support") +add_feature_info(BUILD_TESTING BUILD_TESTING "Build test binaries") +add_feature_info(WITH_SANITIZER WITH_SANITIZER "Enable sanitizer testing support") add_feature_info(WITH_GTEST WITH_GTEST "Build tests using Gtest framework") add_feature_info(WITH_FUZZERS WITH_FUZZERS "Build test/fuzz") add_feature_info(WITH_BENCHMARKS WITH_BENCHMARKS "Build benchmarks using Google Benchmark framework") diff --git a/README.md b/README.md index 922462cbc..9a48e8ea5 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,6 @@ Build Options |:---------------------------|:-------------------------|:------------------------------------------------------------------------------------|---------| | ZLIB_COMPAT | --zlib-compat | Compile with zlib compatible API | OFF | | ZLIB_ALIASES | | Provide zlib compatible CMake targets | ON | -| ZLIB_ENABLE_TESTS | | Tests masterswitch, does not enable any tests by itself | ON | -| ZLIBNG_ENABLE_TESTS | | Build test binaries | ON | | WITH_GZFILEOP | --without-gzfileops | Compile with support for gzFile related functions | ON | | WITH_OPTIM | --without-optimizations | Build with optimisations | ON | | WITH_NEW_STRATEGIES | --without-new-strategies | Use new strategies | ON | @@ -108,6 +106,7 @@ Build Options | WITH_GTEST | | Build tests using GTest framework | ON | | WITH_BENCHMARKS | | Build benchmarks using Google Benchmark framework | OFF | | INSTALL_UTILS | | Copy minigzip and minideflate during install | OFF | +| BUILD_TESTING | | Build test binaries | ON | Install diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a9eb9f609..a2304cf02 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,8 +7,8 @@ macro(configure_test_executable target) endif() endmacro() -if(ZLIBNG_ENABLE_TESTS) - add_definitions(-DZLIBNG_ENABLE_TESTS) +if(TEST_STOCK_ZLIB) + add_definitions(-DTEST_STOCK_ZLIB) endif() add_executable(example example.c) @@ -53,10 +53,7 @@ target_link_libraries(infcover zlib-ng) if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS) target_sources(infcover PRIVATE ${PROJECT_SOURCE_DIR}/inftrees.c) endif() -# infcover references zng_inflate_table() and struct inflate_state, which are internal to zlib-ng. -if(ZLIBNG_ENABLE_TESTS) - add_test(NAME infcover COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $) -endif() +add_test(NAME infcover COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $) add_executable(makefixed ${PROJECT_SOURCE_DIR}/tools/makefixed.c ${PROJECT_SOURCE_DIR}/inftrees.c) configure_test_executable(makefixed) @@ -177,7 +174,7 @@ if(WITH_GTEST) list(APPEND TEST_SRCS test_gzio.cc) endif() - if(ZLIBNG_ENABLE_TESTS) + if(NOT TEST_STOCK_ZLIB) list(APPEND TEST_SRCS test_adler32.cc # adler32_neon(), etc test_compare256.cc # compare256_neon(), etc diff --git a/test/add-subdirectory-project/CMakeLists.txt b/test/add-subdirectory-project/CMakeLists.txt index 2c35218ac..7a25d1ae7 100644 --- a/test/add-subdirectory-project/CMakeLists.txt +++ b/test/add-subdirectory-project/CMakeLists.txt @@ -5,7 +5,7 @@ project(zlib-ng-add-subdirecory-test C) include(CTest) set(BUILD_SHARED_LIBS OFF) -set(ZLIB_ENABLE_TESTS ON CACHE BOOL "Build test binaries" FORCE) +set(BUILD_TESTING ON CACHE BOOL "Build test binaries" FORCE) add_subdirectory(../.. zlib-ng) diff --git a/test/example.c b/test/example.c index 5393fa5de..fd62bd5d7 100644 --- a/test/example.c +++ b/test/example.c @@ -420,7 +420,7 @@ static void test_flush(unsigned char *compr, z_uintmax_t *comprLen) { *comprLen = (z_size_t)c_stream.total_out; } -#ifdef ZLIBNG_ENABLE_TESTS +#ifndef TEST_STOCK_ZLIB /* =========================================================================== * Test inflateSync() * We expect a certain compressed block layout, so skip this with the original zlib. @@ -979,7 +979,7 @@ int main(int argc, char *argv[]) { #endif test_flush(compr, &comprLen); -#ifdef ZLIBNG_ENABLE_TESTS +#ifndef TEST_STOCK_ZLIB test_sync(compr, comprLen, uncompr, uncomprLen); #endif comprLen = uncomprLen; diff --git a/test/fuzz/CMakeLists.txt b/test/fuzz/CMakeLists.txt index e5a7455dd..8f0972de7 100644 --- a/test/fuzz/CMakeLists.txt +++ b/test/fuzz/CMakeLists.txt @@ -39,9 +39,7 @@ foreach(FUZZER ${FUZZERS}) target_link_libraries(${FUZZER} ${FUZZING_ENGINE}) endif() - if(ZLIB_ENABLE_TESTS) - file(GLOB FUZZER_TEST_FILES ${PROJECT_SOURCE_DIR}/*) - set(FUZZER_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $ ${FUZZER_TEST_FILES}) - add_test(NAME ${FUZZER} COMMAND ${FUZZER_COMMAND}) - endif() + file(GLOB FUZZER_TEST_FILES ${PROJECT_SOURCE_DIR}/*) + set(FUZZER_COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $ ${FUZZER_TEST_FILES}) + add_test(NAME ${FUZZER} COMMAND ${FUZZER_COMMAND}) endforeach() diff --git a/test/infcover.c b/test/infcover.c index 91b6b5796..ba922dfcb 100644 --- a/test/infcover.c +++ b/test/infcover.c @@ -618,6 +618,7 @@ static void cover_inflate(void) { inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK); } +#ifndef TEST_STOCK_ZLIB /* cover remaining lines in inftrees.c */ static void cover_trees(void) { int ret; @@ -641,6 +642,7 @@ static void cover_trees(void) { fputs("inflate_table not enough errors\n", stderr); Z_UNUSED(ret); } +#endif /* cover remaining inffast.c decoding and window copying */ static void cover_fast(void) { @@ -672,7 +674,9 @@ int main(void) { cover_wrap(); cover_back(); cover_inflate(); +#ifndef TEST_STOCK_ZLIB cover_trees(); +#endif cover_fast(); cover_cve_2022_37434(); return 0; diff --git a/test/test_shared_ng.h b/test/test_shared_ng.h index 81e451998..f87ef027e 100644 --- a/test/test_shared_ng.h +++ b/test/test_shared_ng.h @@ -8,7 +8,7 @@ static inline int deflate_prime_32(PREFIX3(stream) *stream, uint32_t value) { int err; -#ifdef ZLIBNG_ENABLE_TESTS +#ifndef TEST_STOCK_ZLIB err = PREFIX(deflatePrime)(stream, 32, value); #else /* zlib's deflatePrime() takes at most 16 bits */