David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 1 | # list of all the tests in each directory |
Michal Vasko | d7fb6df | 2021-05-19 11:27:35 +0200 | [diff] [blame^] | 2 | set(tests test_io test_fd_comm test_init_destroy_client test_init_destroy_server test_client_thread) |
David Sedlák | 19af266 | 2018-10-01 13:42:08 +0200 | [diff] [blame] | 3 | set(client_tests test_client test_client_messages) |
Michal Vasko | 061561f | 2016-02-08 15:36:22 +0100 | [diff] [blame] | 4 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 5 | # add -Wl,--wrap flags |
| 6 | set(test test_client_ssh) |
David Sedlák | 493f2b1 | 2018-10-08 21:50:33 +0200 | [diff] [blame] | 7 | set(${test}_mock_funcs connect ssh_connect ssh_userauth_none ssh_userauth_kbdint ssh_is_connected |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 8 | ssh_channel_open_session ssh_channel_request_subsystem ssh_channel_is_close ssh_channel_write |
| 9 | ssh_channel_poll_timeout ssh_userauth_password nc_handshake_io nc_ctx_check_and_fill |
Fred Gan | 3a736e0 | 2021-01-04 17:59:38 +0800 | [diff] [blame] | 10 | ssh_userauth_try_publickey ssh_userauth_publickey nc_sock_listen_inet nc_sock_accept_binds nc_accept_callhome_ssh_sock) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 11 | set(${test}_wrap_link_flags "-Wl") |
| 12 | foreach(mock_func IN LISTS ${test}_mock_funcs) |
| 13 | set(${test}_wrap_link_flags "${${test}_wrap_link_flags},--wrap=${mock_func}") |
| 14 | endforeach() |
| 15 | |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 16 | set(test test_client_tls) |
| 17 | set(${test}_mock_funcs connect SSL_connect nc_send_hello_io nc_handshake_io nc_ctx_check_and_fill) |
| 18 | set(${test}_wrap_link_flags "-Wl") |
| 19 | foreach(mock_func IN LISTS ${test}_mock_funcs) |
| 20 | set(${test}_wrap_link_flags "${${test}_wrap_link_flags},--wrap=${mock_func}") |
| 21 | endforeach() |
| 22 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 23 | #append tests depending on SSH/TLS |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 24 | if(ENABLE_SSH OR ENABLE_TLS) |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 25 | list(APPEND tests test_server_thread) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 26 | if(ENABLE_SSH) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 27 | list(APPEND client_tests test_client_ssh) |
| 28 | endif() |
| 29 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 30 | if(ENABLE_TLS) |
David Sedlák | 6fdf1ec | 2018-09-30 21:42:31 +0200 | [diff] [blame] | 31 | list(APPEND client_tests test_client_tls) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 32 | endif() |
Michal Vasko | 061561f | 2016-02-08 15:36:22 +0100 | [diff] [blame] | 33 | endif() |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 34 | |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 35 | foreach(src IN LISTS libsrc) |
| 36 | list(APPEND test_srcs "../${src}") |
| 37 | endforeach() |
| 38 | add_library(testobj OBJECT ${test_srcs}) |
| 39 | |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 40 | foreach(test_name IN LISTS tests) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 41 | add_executable(${test_name} $<TARGET_OBJECTS:testobj> ${test_name}.c) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 42 | target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBYANG_LIBRARIES} netconf2) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 43 | target_include_directories(${test_name} PRIVATE ${CMOCKA_INCLUDE_DIR}) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 44 | set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${${test_name}_wrap_link_flags}") |
| 45 | add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>) |
| 46 | endforeach() |
| 47 | |
| 48 | foreach(test_name IN LISTS client_tests) |
| 49 | add_executable(${test_name} $<TARGET_OBJECTS:testobj> ./client/${test_name}.c) |
| 50 | target_link_libraries(${test_name} ${CMOCKA_LIBRARIES} ${LIBYANG_LIBRARIES} netconf2) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 51 | target_include_directories(${test_name} PRIVATE ${CMOCKA_INCLUDE_DIR}) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 52 | set_target_properties(${test_name} PROPERTIES LINK_FLAGS "${${test_name}_wrap_link_flags}") |
| 53 | add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>) |
Michal Vasko | 061561f | 2016-02-08 15:36:22 +0100 | [diff] [blame] | 54 | endforeach() |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 55 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 56 | if(ENABLE_VALGRIND_TESTS) |
Michal Vasko | 294d4c6 | 2016-02-01 10:10:27 +0100 | [diff] [blame] | 57 | find_program(valgrind_FOUND valgrind) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 58 | if(valgrind_FOUND) |
| 59 | foreach(test_name IN LISTS tests) |
David Sedlák | 493f2b1 | 2018-10-08 21:50:33 +0200 | [diff] [blame] | 60 | add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 61 | --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp ${CMAKE_BINARY_DIR}/tests/${test_name}) |
Michal Vasko | 061561f | 2016-02-08 15:36:22 +0100 | [diff] [blame] | 62 | endforeach() |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 63 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 64 | foreach(test_name IN LISTS client_tests) |
David Sedlák | 493f2b1 | 2018-10-08 21:50:33 +0200 | [diff] [blame] | 65 | add_test(${test_name}_valgrind valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 66 | --suppressions=${PROJECT_SOURCE_DIR}/tests/ld.supp ${CMAKE_BINARY_DIR}/tests/${test_name}) |
David Sedlák | ddde449 | 2018-09-30 21:34:38 +0200 | [diff] [blame] | 67 | endforeach() |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 68 | else() |
| 69 | message("-- valgrind executable not found! Disabling memory leaks tests") |
Michal Vasko | 061561f | 2016-02-08 15:36:22 +0100 | [diff] [blame] | 70 | endif() |
Radek Krejci | ce24ab8 | 2015-10-08 15:37:02 +0200 | [diff] [blame] | 71 | endif() |
| 72 | |
Fred Gan | 5eda9cb | 2020-12-11 16:32:08 +0800 | [diff] [blame] | 73 | if(ENABLE_COVERAGE) |
| 74 | # Destination |
| 75 | set(COVERAGE_DIR "${CMAKE_BINARY_DIR}/tests/code_coverage/") |
| 76 | set(COVERAGE_FILE_RAW "${CMAKE_BINARY_DIR}/tests/coverage_raw.info") |
| 77 | set(COVERAGE_FILE_CLEAN "${CMAKE_BINARY_DIR}/tests/coverage_clean.info") |
| 78 | |
| 79 | # Add coverage target |
| 80 | add_custom_target(coverage |
| 81 | COMMENT "Generating code coverage..." |
| 82 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" |
| 83 | # Cleanup code counters |
| 84 | COMMAND "${PATH_LCOV}" --directory . --zerocounters --quiet |
| 85 | |
| 86 | # Run tests |
| 87 | COMMAND "${CMAKE_CTEST_COMMAND}" --quiet |
| 88 | |
| 89 | # Capture the counters |
| 90 | COMMAND "${PATH_LCOV}" |
| 91 | --directory . |
| 92 | --rc lcov_branch_coverage=1 |
| 93 | --rc 'lcov_excl_line=assert' |
| 94 | --capture --quiet |
| 95 | --output-file "${COVERAGE_FILE_RAW}" |
| 96 | # Remove coverage of tests, system headers, etc. |
| 97 | COMMAND "${PATH_LCOV}" |
| 98 | --remove "${COVERAGE_FILE_RAW}" '${CMAKE_SOURCE_DIR}/tests/*' |
| 99 | --rc lcov_branch_coverage=1 |
| 100 | --quiet --output-file "${COVERAGE_FILE_CLEAN}" |
| 101 | # Generate HTML report |
| 102 | COMMAND "${PATH_GENHTML}" |
| 103 | --branch-coverage --function-coverage --quiet --title "libnetconf2" |
| 104 | --legend --show-details --output-directory "${COVERAGE_DIR}" |
| 105 | "${COVERAGE_FILE_CLEAN}" |
| 106 | # Delete the counters |
| 107 | COMMAND "${CMAKE_COMMAND}" -E remove |
| 108 | ${COVERAGE_FILE_RAW} ${COVERAGE_FILE_CLEAN} |
| 109 | ) |
| 110 | |
| 111 | add_custom_command(TARGET coverage POST_BUILD |
| 112 | WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/tests" |
| 113 | COMMENT "To see the code coverage report, open ${COVERAGE_DIR}index.html" |
| 114 | COMMAND ; |
| 115 | ) |
| 116 | endif() |
| 117 | |
Jan Kundrát | cf15d6c | 2017-10-26 18:07:56 +0200 | [diff] [blame] | 118 | include_directories(${CMAKE_SOURCE_DIR}/src ${PROJECT_BINARY_DIR}) |
| 119 | configure_file("${PROJECT_SOURCE_DIR}/tests/config.h.in" "${PROJECT_BINARY_DIR}/tests/config.h" ESCAPE_QUOTES @ONLY) |