]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use CTest to simplify testing options
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 11 Nov 2025 16:24:26 +0000 (17:24 +0100)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sun, 16 Nov 2025 11:15:05 +0000 (12:15 +0100)
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.

CMakeLists.txt
README.md
test/CMakeLists.txt
test/add-subdirectory-project/CMakeLists.txt
test/example.c
test/fuzz/CMakeLists.txt
test/infcover.c
test/test_shared_ng.h

index df83113386fa277d2557c10cf4f2d87205942b98..d2ef3c9faf435f97d444bb80110eeb3d4ba78c9a 100644 (file)
@@ -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")
index 922462cbc393131e28515e86d2f38871703c5761..9a48e8ea592d3b4a1154238b20f79ad800e4d956 100644 (file)
--- 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
index a9eb9f609dd534b929e94171eb1433a89c680077..a2304cf02bb8841620b488e2c0fe6a57d9c46e37 100644 (file)
@@ -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} $<TARGET_FILE:infcover>)
-endif()
+add_test(NAME infcover COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:infcover>)
 
 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
index 2c35218ac85b169e4bf277c09ea3d6a68df43aa0..7a25d1ae723405e145a61ccf29a10dca4fe802ee 100644 (file)
@@ -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)
 
index 5393fa5de9f40a6b0252c43e777d9b46249b7b3d..fd62bd5d7f30c48c448d70802de28027970c2534 100644 (file)
@@ -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;
index e5a7455dda7dbe8e31325ee46d6bf8cb7545f3b1..8f0972de7fde161a3458dec80a5def6791391b89 100644 (file)
@@ -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} $<TARGET_FILE:${FUZZER}> ${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} $<TARGET_FILE:${FUZZER}> ${FUZZER_TEST_FILES})
+    add_test(NAME ${FUZZER} COMMAND ${FUZZER_COMMAND})
 endforeach()
index 91b6b579610c5483963a2777a62b0d6bb65fe21e..ba922dfcb3696133653748ad9920660ddc6af759 100644 (file)
@@ -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;
index 81e451998f8e122879b31683c4e459bb2bd15642..f87ef027e28cbc60ddce9b67c2e9eb9d935de267 100644 (file)
@@ -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 */