Skip to content

Commit f983d75

Browse files
committed
test/common: validate the compile-time guards of dencoding's struct_v
Signed-off-by: Radoslaw Zarzynski <[email protected]>
1 parent fbecd5d commit f983d75

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/test/common/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,39 @@ if(WITH_SYSTEMD)
446446
target_link_libraries(unittest_journald_logger ceph-common)
447447
add_ceph_unittest(unittest_journald_logger)
448448
endif()
449+
450+
# Validation of the DECODE_START's struct_v compile-time checker.
451+
# First, ensure buildability of the test program itself. This is
452+
# useful to avoid false positives coming from other-than-the-assert
453+
# build failures.
454+
add_executable(unittest_decode_start_v_checker
455+
test_decode_start_v_checker.cpp)
456+
target_link_libraries(unittest_decode_start_v_checker global)
457+
set_target_properties(unittest_decode_start_v_checker
458+
PROPERTIES
459+
EXCLUDE_FROM_ALL TRUE
460+
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
461+
add_test(
462+
NAME unittest_decode_start_v_checker
463+
COMMAND ${CMAKE_COMMAND} --build . --target unittest_decode_start_v_checker --config $<CONFIGURATION>
464+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
465+
466+
# Second, ensure that adding a single, wrongly versioned comparison
467+
# fails the build.
468+
add_executable(unittest_decode_start_v_checker_expect_failure
469+
test_decode_start_v_checker.cpp)
470+
target_link_libraries(unittest_decode_start_v_checker_expect_failure global)
471+
target_compile_definitions(unittest_decode_start_v_checker_expect_failure
472+
PRIVATE SHOULD_FAIL)
473+
set_target_properties(
474+
unittest_decode_start_v_checker_expect_failure
475+
PROPERTIES
476+
EXCLUDE_FROM_ALL TRUE
477+
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
478+
# Add the test bypassing the macros from AddCephTest.cmake. The idea is to
479+
# invoke "cmake --build ..." as the actual test and check whether it fails.
480+
add_test(NAME unittest_decode_start_v_checker_expect_failure
481+
COMMAND ${CMAKE_COMMAND} --build . --target unittest_decode_start_v_checker_expect_failure --config $<CONFIGURATION>
482+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
483+
set_tests_properties(unittest_decode_start_v_checker_expect_failure
484+
PROPERTIES WILL_FAIL TRUE)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2+
// vim: ts=8 sw=2 smarttab ft=cpp
3+
4+
/*
5+
* Ceph - scalable distributed file system
6+
*
7+
* Copyright contributors to the Ceph project
8+
*
9+
* This is free software; you can redistribute it and/or
10+
* modify it under the terms of the GNU Lesser General Public
11+
* License version 2.1, as published by the Free Software
12+
* Foundation. See file COPYING.
13+
*
14+
*/
15+
16+
#include "include/encoding.h"
17+
#include "include/denc.h"
18+
19+
int main (void) {
20+
ceph::buffer::list::const_iterator dummy;
21+
DECODE_START(42, dummy);
22+
if (struct_v >= 42)
23+
/* OK */;
24+
#ifdef SHOULD_FAIL
25+
if (struct_v >= 43)
26+
/* NOK */;
27+
#endif
28+
DECODE_FINISH(dummy);
29+
}

0 commit comments

Comments
 (0)