]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
test/common: validate the compile-time guards of dencoding's struct_v
authorRadoslaw Zarzynski <rzarzyns@redhat.com>
Sat, 15 Feb 2025 15:22:19 +0000 (15:22 +0000)
committerRadoslaw Zarzynski <rzarzyns@redhat.com>
Tue, 18 Mar 2025 12:11:33 +0000 (12:11 +0000)
Signed-off-by: Radoslaw Zarzynski <rzarzyns@redhat.com>
src/test/common/CMakeLists.txt
src/test/common/test_decode_start_v_checker.cpp [new file with mode: 0644]

index 27de0d8ccd919a5b8eb0dcddc1261b185db5890b..34f041e61de5d74e6ad1322b3dca086cc70762f4 100644 (file)
@@ -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 $<CONFIGURATION>
+  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 $<CONFIGURATION>
+         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 (file)
index 0000000..e56ad06
--- /dev/null
@@ -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);
+}