diff --git a/CMakeLists.txt b/CMakeLists.txt index 25df892c..4c489f4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,10 +67,6 @@ option(ENABLE_COVERAGE "Build code coverage report from tests" OFF) option(BUILD_SERVER "Build and install neotpeer2-server" ON) option(BUILD_CLI "Build and install neotpeer2-cli" ON) option(ENABLE_TESTS "Build tests" ON) -if(ENABLE_TESTS AND NOT BUILD_SERVER) - message(WARNING "Tests require the server executable, disabling them.") - set(ENABLE_TESTS OFF) -endif() if(ENABLE_TESTS AND "${BUILD_TYPE_UPPER}" STREQUAL "DEBUG" OR "${BUILD_TYPE_UPPER}" STREQUAL "RELWITHDEBINFO") option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON) else() @@ -78,15 +74,14 @@ else() endif() option(ENABLE_URL "Enable URL capability" ON) option(ENABLE_URL_FILE "Enable the 'file' URL protocol (security advisory: allows authorized users to access local FS as the server user)" OFF) +option(BUILD_NETOPEER2_LIB "Build netopeer2 library with sysrepo YANG module setup (functionality of setup.sh script)" OFF) +option(NETOPEER2_LIB_SERVER "Include server main in netopeer2 library" ON) +option(NETOPEER2_LIB_TESTS "Include server tests in netopeer2 library" ON) set(SSH_AUTHORIZED_KEYS_FORMAT "%h/.ssh/authorized_keys" CACHE STRING "sshd-like pattern (with '%h', '%u', '%U') for determining path to users' SSH authorized_keys file.") set(THREAD_COUNT 3 CACHE STRING "Number of threads accepting new sessions and handling requests") set(POLL_IO_TIMEOUT 10 CACHE STRING "Timeout in milliseconds of polling sessions for new data. It is also used for synchronization of low level IO such as sending a reply while a notification is being sent") set(YANG_MODULE_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/yang/modules/netopeer2" CACHE STRING "Directory where to copy the YANG modules to") set(DATA_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/netopeer2" CACHE STRING "Directory with shared netopeer2 data") -set(NETOPEER2_LIB "no" CACHE STRING "Build netopeer2 library with the setup function or also with the full server functionality, allowed values \"no\", \"setup\", and \"server\"") -if(NOT NETOPEER2_LIB STREQUAL "no" AND NOT NETOPEER2_LIB STREQUAL "setup" AND NOT NETOPEER2_LIB STREQUAL "server") - message(FATAL_ERROR "Invalid value \"${NETOPEER2_LIB}\" of NETOPEER2_LIB.") -endif() # script options option(SYSREPO_SETUP "Install required modules with their default configuration into sysrepo using a script" ON) @@ -196,6 +191,7 @@ if(ENABLE_TESTS) if(NOT CMOCKA_FOUND) message(STATUS "Disabling tests because of missing CMocka") set(ENABLE_TESTS OFF) + set(NETOPEER2_LIB_TESTS OFF) endif() endif() @@ -441,8 +437,8 @@ if(BUILD_CLI) add_subdirectory(cli) endif() -# netopeer2 lib -if(NOT NETOPEER2_LIB STREQUAL "no") +# netopeer2 lib (after tests, uses its vars) +if(BUILD_NETOPEER2_LIB) add_subdirectory(lib) endif() diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 63b25122..c66b23d6 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -8,12 +8,13 @@ include(GNUInstallDirs) # source files set(LIB_SRC - np2_sr_setup.c) + np2_sr_setup.c + ${compatsrc}) -if(NETOPEER2_LIB STREQUAL "server") - # build server main function as a library function - set(LIB_SRC ${LIB_SRC} $ ${PROJECT_SOURCE_DIR}/../src/main.c ${compatsrc}) - add_definitions(-Dmain=np2_server) +if(NETOPEER2_LIB_SERVER) + # include server main function as a library function + add_library(netopeer2_lib_server OBJECT $ ${PROJECT_SOURCE_DIR}/../src/main.c) + target_compile_definitions(netopeer2_lib_server PRIVATE main=np2_server) set(NETOPEER2_SERVER_FUNC "\ /** * @brief netopeer2-server main function. @@ -26,6 +27,29 @@ if(NETOPEER2_LIB STREQUAL "server") int np2_server(int argc, char *argv[]);") endif() +if(NETOPEER2_LIB_TESTS) + # include every test as a library function + foreach(TEST IN LISTS TESTS) + add_library(netopeer2_lib_${TEST} OBJECT "${TEST_SRC_DIR}/${TEST}.c") + target_compile_definitions(netopeer2_lib_${TEST} PRIVATE main=np2_${TEST}) + set(NETOPEER2_TESTS_FUNC ${NETOPEER2_TESTS_FUNC} " + +/** + * @brief netopeer2-server ${TEST} + * + * @param[in] argc Argument count, should be 0. + * @param[in] argv Argument list, should be NULL. + * @return EXIT_SUCCESS on success. + * @return EXIT_FAILURE on error. + */ +int np2_${TEST}(int argc, char *argv[]);") + endforeach() + + add_library(netopeer2_lib_test OBJECT "${TEST_SRC_DIR}/${TEST_SRC}") + include_directories(${TEST_BIN_DIR}) + include_directories(${TEST_SRC_DIR}) +endif() + configure_file(${PROJECT_SOURCE_DIR}/netopeer2.h.in ${PROJECT_BINARY_DIR}/include/netopeer2.h ESCAPE_QUOTES @ONLY) # generate YANG header files @@ -48,6 +72,15 @@ target_link_libraries(netopeer2 ${serverlibs}) if(TARGET CURL::libcurl) target_link_libraries(netopeer2 CURL::libcurl) endif() +if(NETOPEER2_LIB_SERVER) + target_link_libraries(netopeer2 netopeer2_lib_server) +endif() +if(NETOPEER2_LIB_TESTS) + foreach(TEST IN LISTS TESTS) + target_link_libraries(netopeer2 netopeer2_lib_${TEST}) + endforeach() + target_link_libraries(netopeer2 netopeer2_lib_test) +endif() # install install(TARGETS netopeer2 DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/lib/netopeer2.h.in b/lib/netopeer2.h.in index e335434c..793b56cb 100644 --- a/lib/netopeer2.h.in +++ b/lib/netopeer2.h.in @@ -34,4 +34,6 @@ int np2_sr_setup(const char *owner, const char *group, mode_t perm); @NETOPEER2_SERVER_FUNC@ +@NETOPEER2_TESTS_FUNC@ + #endif /* NETOPEER2_H_ */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 68eb1e9d..e5f0eecf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,27 +33,27 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) # base test source -set(test_sources "np_test.c") +set(TEST_SRC "np_test.c") # list of all the tests -set(tests test_rpc test_edit test_filter test_subscribe_filter test_subscribe_param test_parallel_sessions +set(TESTS test_rpc test_edit test_filter test_subscribe_filter test_subscribe_param test_parallel_sessions test_candidate test_with_defaults test_nacm test_sub_ntf test_sub_ntf_advanced test_sub_ntf_filter test_error) if(CMAKE_C_FLAGS MATCHES "-fsanitize=thread") message(WARNING "Features which use SIGEV_THREAD are known to be broken under TSAN, disabling tests for YANG-push and confirmed commit") else() # On TSAN, SIGEV_THREAD is known to not work: https://github.com/google/sanitizers/issues/1612 - list(APPEND tests test_yang_push test_yang_push_advanced test_confirmed_commit) + list(APPEND TESTS test_yang_push test_yang_push_advanced test_confirmed_commit) endif() # append url if supported if(NP2SRV_URL_CAPAB) - list(APPEND tests test_url) + list(APPEND TESTS test_url) endif() # build the executables -foreach(test_name IN LISTS tests) - add_executable(${test_name} ${test_sources} ${test_name}.c) +foreach(test_name IN LISTS TESTS) + add_executable(${test_name} ${TEST_SRC} ${test_name}.c) target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBNETCONF2_LIBRARIES} ${LIBYANG_LIBRARIES} ${SYSREPO_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) endforeach(test_name) @@ -69,7 +69,7 @@ if(${CMAKE_VERSION} VERSION_GREATER "3.7") endif() # add tests with their attributes -foreach(test_name IN LISTS tests) +foreach(test_name IN LISTS TESTS) add_test(NAME ${test_name} COMMAND $ WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3") set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "TEST_NAME=${test_name}") @@ -81,7 +81,7 @@ endforeach() # valgrind tests if(ENABLE_VALGRIND_TESTS) - foreach(test_name IN LISTS tests) + foreach(test_name IN LISTS TESTS) add_test(NAME ${test_name}_valgrind COMMAND valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 ${CMAKE_CURRENT_BINARY_DIR}/${test_name} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) set(test_name "${test_name}_valgrind") set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT "TEST_NAME=${test_name}") @@ -92,6 +92,12 @@ if(ENABLE_VALGRIND_TESTS) endforeach() endif() +# propagate vars to parent +set(TESTS ${TESTS} PARENT_SCOPE) +set(TEST_SRC ${TEST_SRC} PARENT_SCOPE) +set(TEST_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR} PARENT_SCOPE) +set(TEST_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE) + # phony target for clearing all sysrepo test data add_custom_target(test_clean COMMAND ${TEST_KILL_SERVER_COMMAND}