blob: f0a909b97f82fb41e7f151ed11ab6a659e61c58d [file] [log] [blame]
Michal Vaskob0e3ec42021-05-26 09:48:31 +02001# headers test for including compat.h
2add_test(NAME headers
3 COMMAND ${CMAKE_SOURCE_DIR}/compat/check_includes.sh ${CMAKE_SOURCE_DIR}/src/)
4
5# format
roman41a11e42022-06-22 09:27:08 +02006if(${SOURCE_FORMAT_ENABLED})
Jan Kundrát70299452021-12-17 07:11:48 +01007 add_test(NAME format WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND cmake --build ${CMAKE_BINARY_DIR} --target format-check)
Michal Vaskob0e3ec42021-05-26 09:48:31 +02008endif()
9
roman98066c72023-10-26 16:18:22 +020010foreach(src IN LISTS libsrc)
11 list(APPEND test_srcs "../${src}")
12endforeach()
13add_library(testobj OBJECT ${test_srcs} ${compatsrc})
14
romanabe69902024-08-13 14:43:49 +020015set(TEST_SRC "ln2_test.c")
16set(NEXT_TEST_PORT 10050)
17
18macro(get_test_ports PORT_COUNT PORT_DEFINITIONS)
19 if (NOT ${PORT_COUNT})
20 set(${PORT_COUNT} 1)
21 endif()
22
23 SET(PORT_INDEX 0)
24 while(PORT_INDEX LESS ${${PORT_COUNT}})
25 list(APPEND ${PORT_DEFINITIONS} "TEST_PORT_${PORT_INDEX}=${NEXT_TEST_PORT}")
26 math(EXPR PORT_INDEX "${PORT_INDEX} + 1")
27 math(EXPR NEXT_TEST_PORT "${NEXT_TEST_PORT} + 1")
28 endwhile()
29 set(NEXT_TEST_PORT ${NEXT_TEST_PORT} PARENT_SCOPE)
30endmacro()
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020031
Jan Kundrát60e31622024-04-08 11:26:33 +020032function(libnetconf2_test)
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020033 cmake_parse_arguments(TEST "" "NAME;PORT_COUNT" "WRAP_FUNCS" ${ARGN})
David Sedlákddde4492018-09-30 21:34:38 +020034
romanabe69902024-08-13 14:43:49 +020035 add_executable(${TEST_NAME} $<TARGET_OBJECTS:testobj> ${TEST_SRC} ${TEST_NAME}.c)
Jan Kundrát60e31622024-04-08 11:26:33 +020036 target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARIES} ${LIBYANG_LIBRARIES} netconf2)
37 target_include_directories(${TEST_NAME} PRIVATE ${CMOCKA_INCLUDE_DIR})
romanabe69902024-08-13 14:43:49 +020038
39 # wrap functions
Jan Kundrát60e31622024-04-08 11:26:33 +020040 if(TEST_WRAP_FUNCS)
41 set(wrap_link_flags "-Wl")
42 foreach(mock_func IN LISTS TEST_WRAP_FUNCS)
43 set(wrap_link_flags "${wrap_link_flags},--wrap=${mock_func}")
44 endforeach()
45 set_target_properties(${TEST_NAME} PROPERTIES LINK_FLAGS "${wrap_link_flags}")
46 endif()
David Sedlák6fdf1ec2018-09-30 21:42:31 +020047
romanabe69902024-08-13 14:43:49 +020048 # create a test, generate port numbers and set them as env vars for the test
49 add_test(NAME ${TEST_NAME} COMMAND $<TARGET_FILE:${TEST_NAME}>)
50 get_test_ports(TEST_PORT_COUNT TEST_PORT_DEFINITIONS)
51 set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${TEST_PORT_DEFINITIONS}")
52
53 # do the same for valgrind tests
Jan Kundrát60e31622024-04-08 11:26:33 +020054 if(ENABLE_VALGRIND_TESTS)
55 add_test(${TEST_NAME}_valgrind valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1
56 --suppressions=${PROJECT_SOURCE_DIR}/tests/library_valgrind.supp ${CMAKE_BINARY_DIR}/tests/${TEST_NAME})
romanabe69902024-08-13 14:43:49 +020057 get_test_ports(TEST_PORT_COUNT VALGRIND_TEST_PORT_DEFINITIONS)
58 set_tests_properties(${TEST_NAME}_valgrind PROPERTIES ENVIRONMENT "${VALGRIND_TEST_PORT_DEFINITIONS}")
Jan Kundrát60e31622024-04-08 11:26:33 +020059 endif()
60endfunction()
David Sedlákddde4492018-09-30 21:34:38 +020061
Jan Kundrát60e31622024-04-08 11:26:33 +020062# all the tests that don't require SSH and TLS
roman2ca058f2024-08-14 10:35:36 +020063libnetconf2_test(NAME test_client_messages)
Jan Kundrát60e31622024-04-08 11:26:33 +020064libnetconf2_test(NAME test_client_thread)
65libnetconf2_test(NAME test_fd_comm)
Jan Kundrát60e31622024-04-08 11:26:33 +020066libnetconf2_test(NAME test_io)
67libnetconf2_test(NAME test_thread_messages)
roman2ca058f2024-08-14 10:35:36 +020068libnetconf2_test(NAME test_unix_socket)
Jan Kundrát60e31622024-04-08 11:26:33 +020069
70# tests depending on SSH/TLS
71if(ENABLE_SSH_TLS)
roman1920cd52024-08-14 13:05:23 +020072 libnetconf2_test(NAME test_auth_ssh)
roman2ca058f2024-08-14 10:35:36 +020073 libnetconf2_test(NAME test_authkeys)
74 libnetconf2_test(NAME test_ch PORT_COUNT 2)
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020075 libnetconf2_test(NAME test_endpt_share_clients PORT_COUNT 4)
roman2ca058f2024-08-14 10:35:36 +020076 libnetconf2_test(NAME test_ks_ts)
Jan Kundrát60e31622024-04-08 11:26:33 +020077 if (LIBPAM_HAVE_CONFDIR)
78 libnetconf2_test(NAME test_pam WRAP_FUNCS pam_start)
79 endif()
roman2ca058f2024-08-14 10:35:36 +020080 libnetconf2_test(NAME test_replace)
81 libnetconf2_test(NAME test_runtime_changes PORT_COUNT 2)
82 libnetconf2_test(NAME test_tls)
83 libnetconf2_test(NAME test_two_channels)
Fred Gan5eda9cb2020-12-11 16:32:08 +080084endif()
85
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020086include_directories(${CMAKE_SOURCE_DIR}/src ${PROJECT_BINARY_DIR})
87configure_file("${PROJECT_SOURCE_DIR}/tests/config.h.in" "${PROJECT_BINARY_DIR}/tests/config.h" ESCAPE_QUOTES @ONLY)
roman7d136682023-12-14 10:43:36 +010088
89# compile PAM test module
90add_library(pam_netconf SHARED ${CMAKE_SOURCE_DIR}/tests/pam/pam_netconf.c)
91set_target_properties(pam_netconf PROPERTIES PREFIX "")
92target_link_libraries(pam_netconf ${LIBPAM_LIBRARIES})
93
94# generate PAM configuration file
95file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/netconf.conf
96 "#%PAM-1.4\n"
97 "auth required ${CMAKE_CURRENT_BINARY_DIR}/pam_netconf.so\n"
98 "account required ${CMAKE_CURRENT_BINARY_DIR}/pam_netconf.so\n"
99 "password required ${CMAKE_CURRENT_BINARY_DIR}/pam_netconf.so\n"
100)