1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-08-08 19:02:06 +03:00

cmake: Rewritten AddCMockaTest.cmake

This changes add_cmocka_test() to receive compiler options, the
libraries to be linked to the test, and the linker options.  The way the
tests are declared in tests/unittests and tests/client were updated.

Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Anderson Toshiyuki Sasaki
2018-09-21 13:10:54 +02:00
committed by Andreas Schneider
parent 14f5624ff5
commit 667fb5f9a9
3 changed files with 173 additions and 90 deletions

View File

@@ -1,11 +1,63 @@
# - add_cmocka_test(test_name test_source linklib1 ... linklibN) #
# Copyright (c) 2007 Daniel Gollub <dgollub@suse.de> # Copyright (c) 2007 Daniel Gollub <dgollub@suse.de>
# Copyright (c) 2007-2018 Andreas Schneider <asn@cryptomilk.org> # Copyright (c) 2007-2018 Andreas Schneider <asn@cryptomilk.org>
# Copyright (c) 2018 Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
# #
# Redistribution and use is allowed according to the terms of the BSD license. # Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file. # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#.rst:
# AddCMockaTest
# -------------
#
# This file provides a function to add a test
#
# Functions provided
# ------------------
#
# ::
#
# add_cmocka_test(target_name
# SOURCES src1 src2 ... srcN
# [COMPILE_OPTIONS opt1 opt2 ... optN]
# [LINK_LIBRARIES lib1 lib2 ... libN]
# [LINK_OPTIONS lopt1 lop2 .. loptN]
# )
#
# ``target_name``:
# Required, expects the name of the test which will be used to define a target
#
# ``SOURCES``:
# Required, expects one or more source files names
#
# ``COMPILE_OPTIONS``:
# Optional, expects one or more options to be passed to the compiler
#
# ``LINK_LIBRARIES``:
# Optional, expects one or more libraries to be linked with the test
# executable.
#
# ``LINK_OPTIONS``:
# Optional, expects one or more options to be passed to the linker
#
#
# Example:
#
# .. code-block:: cmake
#
# add_cmocka_test(my_test
# SOURCES my_test.c other_source.c
# COMPILE_OPTIONS -g -Wall
# LINK_LIBRARIES mylib
# LINK_OPTIONS -Wl,--enable-syscall-fixup
# )
#
# Where ``my_test`` is the name of the test, ``my_test.c`` and
# ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler
# options to be used, ``mylib`` is a target of a library to be linked, and
# ``-Wl,--enable-syscall-fixup`` is an option passed to the linker.
#
enable_testing() enable_testing()
include(CTest) include(CTest)
@@ -17,10 +69,52 @@ if (CMAKE_CROSSCOMPILING)
endif() endif()
endif() endif()
function(ADD_CMOCKA_TEST _testName _testSource) function(ADD_CMOCKA_TEST _TARGET_NAME)
add_executable(${_testName} ${_testSource})
target_link_libraries(${_testName} ${ARGN}) set(one_value_arguments
)
set(multi_value_arguments
SOURCES
COMPILE_OPTIONS
LINK_LIBRARIES
LINK_OPTIONS
)
cmake_parse_arguments(_add_cmocka_test
""
"${one_value_arguments}"
"${multi_value_arguments}"
${ARGN}
)
if (NOT DEFINED _add_cmocka_test_SOURCES)
message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}")
endif()
add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES})
if (DEFINED _add_cmocka_test_COMPILE_OPTIONS)
target_compile_options(${_TARGET_NAME}
PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS}
)
endif()
if (DEFINED _add_cmocka_test_LINK_LIBRARIES)
target_link_libraries(${_TARGET_NAME}
PRIVATE ${_add_cmocka_test_LINK_LIBRARIES}
)
endif()
if (DEFINED _add_cmocka_test_LINK_OPTIONS)
set_target_properties(${_TARGET_NAME}
PROPERTIES LINK_FLAGS
${_add_cmocka_test_LINK_OPTIONS}
)
endif()
add_test(${_TARGET_NAME}
${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME}
)
add_test(${_testName} ${TARGET_SYSTEM_EMULATOR} ${CMAKE_CURRENT_BINARY_DIR}/${_testName}${CMAKE_EXECUTABLE_SUFFIX})
endfunction (ADD_CMOCKA_TEST) endfunction (ADD_CMOCKA_TEST)

View File

@@ -30,8 +30,11 @@ if (WITH_SFTP)
endif (WITH_SFTP) endif (WITH_SFTP)
foreach(_CLI_TEST ${LIBSSH_CLIENT_TESTS}) foreach(_CLI_TEST ${LIBSSH_CLIENT_TESTS})
add_cmocka_test(${_CLI_TEST} ${_CLI_TEST}.c ${TORTURE_LIBRARY}) add_cmocka_test(${_CLI_TEST}
target_compile_options(${_CLI_TEST} PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) SOURCES ${_CLI_TEST}.c
COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS}
LINK_LIBRARIES ${TORTURE_LIBRARY}
)
if (OSX) if (OSX)
set_property( set_property(

View File

@@ -2,95 +2,81 @@ project(unittests C)
include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIR})
add_cmocka_test(torture_buffer torture_buffer.c ${TEST_TARGET_LIBRARIES}) set(LIBSSH_UNIT_TESTS
target_compile_options(torture_buffer PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) torture_buffer
torture_callbacks
torture_crypto
torture_init
torture_list
torture_misc
torture_config
torture_options
torture_isipaddr
torture_knownhosts_parsing
torture_hashes
)
add_cmocka_test(torture_callbacks torture_callbacks.c ${TEST_TARGET_LIBRARIES}) set(LIBSSH_THREAD_UNIT_TESTS
target_compile_options(torture_callbacks PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) torture_rand
torture_threads_init
add_cmocka_test(torture_crypto torture_crypto.c ${TEST_TARGET_LIBRARIES}) torture_threads_buffer
target_compile_options(torture_crypto PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) torture_threads_crypto
)
add_cmocka_test(torture_init torture_init.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_init PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_list torture_list.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_list PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_misc torture_misc.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_misc PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_config torture_config.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_config PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_options torture_options.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_options PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_isipaddr torture_isipaddr.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_isipaddr PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_knownhosts_parsing torture_knownhosts_parsing.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_knownhosts_parsing PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
add_cmocka_test(torture_hashes torture_hashes.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_hashes PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
if (CMAKE_USE_PTHREADS_INIT)
add_cmocka_test(torture_rand torture_rand.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_rand PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
target_link_libraries(torture_rand Threads::Threads)
add_cmocka_test(torture_threads_init torture_threads_init.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_threads_init PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
target_link_libraries(torture_threads_init Threads::Threads)
add_cmocka_test(torture_threads_buffer torture_threads_buffer.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_threads_buffer PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
target_link_libraries(torture_threads_buffer Threads::Threads)
add_cmocka_test(torture_threads_crypto torture_threads_crypto.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_threads_crypto PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
target_link_libraries(torture_threads_crypto Threads::Threads)
endif ()
if (UNIX AND NOT WIN32) if (UNIX AND NOT WIN32)
# this uses a socketpair set(LIBSSH_UNIT_TESTS
add_cmocka_test(torture_packet torture_packet.c ${TEST_TARGET_LIBRARIES}) ${LIBSSH_UNIT_TESTS}
target_compile_options(torture_packet PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) # this uses a socketpair
torture_packet
# requires ssh-keygen # requires ssh-keygen
add_cmocka_test(torture_keyfiles torture_keyfiles.c ${TEST_TARGET_LIBRARIES}) torture_keyfiles
torture_pki
add_cmocka_test(torture_pki torture_pki.c ${TEST_TARGET_LIBRARIES}) torture_pki_rsa
target_compile_options(torture_pki PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) torture_pki_ed25519
# requires /dev/null
add_cmocka_test(torture_pki_rsa torture_pki_rsa.c ${TEST_TARGET_LIBRARIES}) torture_channel
target_compile_options(torture_pki_rsa PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) )
add_cmocka_test(torture_pki_ed25519 torture_pki_ed25519.c ${TEST_TARGET_LIBRARIES})
target_compile_options(torture_pki_ed25519 PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
if (HAVE_DSA) if (HAVE_DSA)
add_cmocka_test(torture_pki_dsa torture_pki_dsa.c ${TEST_TARGET_LIBRARIES}) set(LIBSSH_UNIT_TESTS
target_compile_options(torture_pki_dsa PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) ${LIBSSH_UNIT_TESTS}
torture_pki_dsa
)
endif() endif()
if (HAVE_ECC) if (HAVE_ECC)
add_cmocka_test(torture_pki_ecdsa torture_pki_ecdsa.c ${TEST_TARGET_LIBRARIES}) set(LIBSSH_UNIT_TESTS
target_compile_options(torture_pki_ecdsa PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) ${LIBSSH_UNIT_TESTS}
torture_pki_ecdsa
)
endif() endif()
# requires /dev/null set(LIBSSH_THREAD_UNIT_TESTS
add_cmocka_test(torture_channel torture_channel.c ${TEST_TARGET_LIBRARIES}) ${LIBSSH_THREAD_UNIT_TESTS}
target_compile_options(torture_channel PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) # requires pthread
# requires pthread torture_threads_pki_rsa
if (CMAKE_USE_PTHREADS_INIT) )
add_cmocka_test(torture_threads_pki_rsa torture_threads_pki_rsa.c ${TEST_TARGET_LIBRARIES}) # Not working correctly
target_compile_options(torture_threads_pki_rsa PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) #if (WITH_SERVER)
target_link_libraries(torture_threads_pki_rsa Threads::Threads) # add_cmocka_test(torture_server_x11 torture_server_x11.c ${TEST_TARGET_LIBRARIES})
#endif (WITH_SERVER)
# Not working correctly
#if (WITH_SERVER)
# add_cmocka_test(torture_server_x11 torture_server_x11.c ${TEST_TARGET_LIBRARIES})
#endif (WITH_SERVER)
endif ()
endif (UNIX AND NOT WIN32) endif (UNIX AND NOT WIN32)
foreach(_UNIT_TEST ${LIBSSH_UNIT_TESTS})
add_cmocka_test(${_UNIT_TEST}
SOURCES ${_UNIT_TEST}.c
COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS}
LINK_LIBRARIES ${TEST_TARGET_LIBRARIES}
)
endforeach()
if (CMAKE_USE_PTHREADS_INIT)
foreach(_UNIT_TEST ${LIBSSH_THREAD_UNIT_TESTS})
add_cmocka_test(${_UNIT_TEST}
SOURCES ${_UNIT_TEST}.c
COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS}
LINK_LIBRARIES ${TEST_TARGET_LIBRARIES} Threads::Threads
)
endforeach()
endif ()