blob: 7349994b1beae756f397b2cd71aaa95847aec3ce [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
63libnetconf2_test(NAME test_unix_socket)
64libnetconf2_test(NAME test_client_thread)
65libnetconf2_test(NAME test_fd_comm)
66libnetconf2_test(NAME test_init_destroy_client)
67libnetconf2_test(NAME test_init_destroy_server)
68libnetconf2_test(NAME test_io)
69libnetconf2_test(NAME test_thread_messages)
70libnetconf2_test(NAME test_client_messages)
71
72# tests depending on SSH/TLS
73if(ENABLE_SSH_TLS)
74 libnetconf2_test(NAME test_auth)
75 libnetconf2_test(NAME test_two_channels)
76 libnetconf2_test(NAME test_ks_ts)
77 libnetconf2_test(NAME test_ec)
78 libnetconf2_test(NAME test_ed25519)
79 libnetconf2_test(NAME test_replace)
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020080 libnetconf2_test(NAME test_endpt_share_clients PORT_COUNT 4)
Jan Kundrát60e31622024-04-08 11:26:33 +020081 libnetconf2_test(NAME test_tls)
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020082 libnetconf2_test(NAME test_ch PORT_COUNT 2)
83 libnetconf2_test(NAME test_runtime_changes PORT_COUNT 2)
Jan Kundrát60e31622024-04-08 11:26:33 +020084 libnetconf2_test(NAME test_authkeys)
85 if (LIBPAM_HAVE_CONFDIR)
86 libnetconf2_test(NAME test_pam WRAP_FUNCS pam_start)
87 endif()
Fred Gan5eda9cb2020-12-11 16:32:08 +080088endif()
89
Jan Kundrát60e31622024-04-08 11:26:33 +020090
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020091include_directories(${CMAKE_SOURCE_DIR}/src ${PROJECT_BINARY_DIR})
92configure_file("${PROJECT_SOURCE_DIR}/tests/config.h.in" "${PROJECT_BINARY_DIR}/tests/config.h" ESCAPE_QUOTES @ONLY)
roman7d136682023-12-14 10:43:36 +010093
94# compile PAM test module
95add_library(pam_netconf SHARED ${CMAKE_SOURCE_DIR}/tests/pam/pam_netconf.c)
96set_target_properties(pam_netconf PROPERTIES PREFIX "")
97target_link_libraries(pam_netconf ${LIBPAM_LIBRARIES})
98
99# generate PAM configuration file
100file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/netconf.conf
101 "#%PAM-1.4\n"
102 "auth required ${CMAKE_CURRENT_BINARY_DIR}/pam_netconf.so\n"
103 "account required ${CMAKE_CURRENT_BINARY_DIR}/pam_netconf.so\n"
104 "password required ${CMAKE_CURRENT_BINARY_DIR}/pam_netconf.so\n"
105)