blob: 5dfddb717abcd3fad3a5c1aa9ce188fc3864480a [file] [log] [blame]
Paul Bakkerb06819b2011-01-18 16:18:38 +00001set(libs
Raef Coles995c66f2020-10-13 16:30:41 +01002 ${mbedtls_target}
Paul Bakkerb06819b2011-01-18 16:18:38 +00003)
4
Ashley Duncand85a7e92019-04-29 20:35:06 +12005# Set the project root directory if it's not already defined, as may happen if
6# the tests folder is included directly by a parent project, without including
7# the top level CMakeLists.txt.
8if(NOT DEFINED MBEDTLS_DIR)
9 set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
10endif()
11
Paul Bakkerb06819b2011-01-18 16:18:38 +000012if(USE_PKCS11_HELPER_LIBRARY)
13 set(libs ${libs} pkcs11-helper)
14endif(USE_PKCS11_HELPER_LIBRARY)
15
Paul Bakker92eeea42012-07-03 15:10:33 +000016if(ENABLE_ZLIB_SUPPORT)
17 set(libs ${libs} ${ZLIB_LIBRARIES})
18endif(ENABLE_ZLIB_SUPPORT)
19
okhowang(王沛文)3c1b0902020-03-25 19:55:32 +080020if(NOT MBEDTLS_PYTHON_EXECUTABLE)
21 message(FATAL_ERROR "Cannot build test suites without Python 3")
Nicholas Wilson733676b2015-11-14 13:09:01 +000022endif()
23
Nicholas Wilson2682edf2017-12-05 12:08:15 +000024# Enable definition of various functions used throughout the testsuite
Nicholas Wilson61fa4362018-06-25 12:10:00 +010025# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
Nicholas Wilson2682edf2017-12-05 12:08:15 +000026# on non-POSIX platforms.
27add_definitions("-D_POSIX_C_SOURCE=200809L")
28
Gilles Peskineac372cc2018-11-29 10:15:06 +000029# Test suites caught by SKIP_TEST_SUITES are built but not executed.
30# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
31# but not "test_suite_foobar".
32string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
33string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
34set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
35
Paul Bakker73043762011-07-13 15:03:10 +000036function(add_test_suite suite_name)
37 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020038 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +000039 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020040 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +000041 endif()
42
Paul Bakker367dae42009-06-28 21:50:27 +000043 add_custom_command(
Paul Bakker73043762011-07-13 15:03:10 +000044 OUTPUT test_suite_${data_name}.c
okhowang(王沛文)3c1b0902020-03-25 19:55:32 +080045 COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function -d ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function -s ${CMAKE_CURRENT_SOURCE_DIR}/suites --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function -o .
Raef Coles995c66f2020-10-13 16:30:41 +010046 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py ${mbedtls_target} ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data
Paul Bakker4c14a252010-06-18 22:54:05 +000047 )
Paul Bakker19343182013-08-16 13:31:10 +020048
Ronald Cronf5ea29a2020-06-19 10:42:29 +020049 add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>)
Paul Bakker73043762011-07-13 15:03:10 +000050 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +020051 # Include test-specific header files from ./include and private header
52 # files (used by some invasive tests) from ../library. Public header
53 # files are automatically included because the library targets declare
54 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +020055 target_include_directories(test_suite_${data_name}
56 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
57 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
58
Gilles Peskineac372cc2018-11-29 10:15:06 +000059 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
60 message(STATUS "The test suite ${data_name} will not be executed.")
61 else()
62 add_test(${data_name}-suite test_suite_${data_name} --verbose)
63 endif()
Paul Bakker367dae42009-06-28 21:50:27 +000064endfunction(add_test_suite)
65
Manuel Pégourié-Gonnard7e2d68c2015-07-01 13:41:35 +020066if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
67 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
68endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
Paul Bakker367dae42009-06-28 21:50:27 +000069
Gilles Peskinef29019f2021-02-23 13:44:41 +010070if(CMAKE_COMPILER_IS_CLANG)
71 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
72endif(CMAKE_COMPILER_IS_CLANG)
73
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +020074if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +000075 # If a warning level has been defined, suppress all warnings for test code
76 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
77 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +020078endif(MSVC)
79
Gilles Peskine3df1dae2022-01-25 22:30:25 +010080file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
81list(SORT test_suites)
82foreach(test_suite ${test_suites})
83 get_filename_component(data_name ${test_suite} NAME)
84 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
85 string(REPLACE "test_suite_" "" data_name "${data_name}")
86 string(REGEX MATCH "[^.]*" function_name "${data_name}")
87 add_test_suite(${function_name} ${data_name})
88endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +010089
Gilles Peskine84052572018-03-21 12:12:47 +010090# Make scripts and data files needed for testing available in an
91# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +010092if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -040093 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
94 link_to_source(seedfile)
95 endif()
Gilles Peskine84052572018-03-21 12:12:47 +010096 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -060097 link_to_source(context-info.sh)
Gilles Peskine84052572018-03-21 12:12:47 +010098 link_to_source(data_files)
99 link_to_source(scripts)
100 link_to_source(ssl-opt.sh)
Gilles Peskine6a785732019-01-02 17:29:04 +0100101 link_to_source(suites)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100102endif()