From f983d755f09cd9d328cfb10c5957dd926112888f Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Sat, 15 Feb 2025 15:22:19 +0000 Subject: [PATCH] test/common: validate the compile-time guards of dencoding's struct_v Signed-off-by: Radoslaw Zarzynski --- src/test/common/CMakeLists.txt | 36 +++++++++++++++++++ .../common/test_decode_start_v_checker.cpp | 29 +++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/test/common/test_decode_start_v_checker.cpp diff --git a/src/test/common/CMakeLists.txt b/src/test/common/CMakeLists.txt index 27de0d8ccd9..34f041e61de 100644 --- a/src/test/common/CMakeLists.txt +++ b/src/test/common/CMakeLists.txt @@ -446,3 +446,39 @@ if(WITH_SYSTEMD) target_link_libraries(unittest_journald_logger ceph-common) add_ceph_unittest(unittest_journald_logger) endif() + +# Validation of the DECODE_START's struct_v compile-time checker. +# First, ensure buildability of the test program itself. This is +# useful to avoid false positives coming from other-than-the-assert +# build failures. +add_executable(unittest_decode_start_v_checker + test_decode_start_v_checker.cpp) +target_link_libraries(unittest_decode_start_v_checker global) +set_target_properties(unittest_decode_start_v_checker + PROPERTIES + EXCLUDE_FROM_ALL TRUE + EXCLUDE_FROM_DEFAULT_BUILD TRUE) +add_test( + NAME unittest_decode_start_v_checker + COMMAND ${CMAKE_COMMAND} --build . --target unittest_decode_start_v_checker --config $ + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) + +# Second, ensure that adding a single, wrongly versioned comparison +# fails the build. +add_executable(unittest_decode_start_v_checker_expect_failure + test_decode_start_v_checker.cpp) +target_link_libraries(unittest_decode_start_v_checker_expect_failure global) +target_compile_definitions(unittest_decode_start_v_checker_expect_failure + PRIVATE SHOULD_FAIL) +set_target_properties( + unittest_decode_start_v_checker_expect_failure + PROPERTIES + EXCLUDE_FROM_ALL TRUE + EXCLUDE_FROM_DEFAULT_BUILD TRUE) +# Add the test bypassing the macros from AddCephTest.cmake. The idea is to +# invoke "cmake --build ..." as the actual test and check whether it fails. +add_test(NAME unittest_decode_start_v_checker_expect_failure + COMMAND ${CMAKE_COMMAND} --build . --target unittest_decode_start_v_checker_expect_failure --config $ + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +set_tests_properties(unittest_decode_start_v_checker_expect_failure + PROPERTIES WILL_FAIL TRUE) diff --git a/src/test/common/test_decode_start_v_checker.cpp b/src/test/common/test_decode_start_v_checker.cpp new file mode 100644 index 00000000000..e56ad06a766 --- /dev/null +++ b/src/test/common/test_decode_start_v_checker.cpp @@ -0,0 +1,29 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab ft=cpp + +/* + * Ceph - scalable distributed file system + * + * Copyright contributors to the Ceph project + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "include/encoding.h" +#include "include/denc.h" + +int main (void) { + ceph::buffer::list::const_iterator dummy; + DECODE_START(42, dummy); + if (struct_v >= 42) + /* OK */; +#ifdef SHOULD_FAIL + if (struct_v >= 43) + /* NOK */; +#endif + DECODE_FINISH(dummy); +} -- 2.39.5