blob: 644cb4618c97df74a805047932f00f1916a52701 [file] [log] [blame]
Paul Bakkerb06819b2011-01-18 16:18:38 +00001set(libs
Raef Coles995c66f2020-10-13 16:30:41 +01002 ${mbedtls_target}
Paul Elliott2667eda2023-11-29 15:53:00 +00003 ${CMAKE_THREAD_LIBS_INIT}
Paul Bakkerb06819b2011-01-18 16:18:38 +00004)
5
Ashley Duncand85a7e92019-04-29 20:35:06 +12006# Set the project root directory if it's not already defined, as may happen if
7# the tests folder is included directly by a parent project, without including
8# the top level CMakeLists.txt.
9if(NOT DEFINED MBEDTLS_DIR)
10 set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
11endif()
12
okhowang(王沛文)3c1b0902020-03-25 19:55:32 +080013if(NOT MBEDTLS_PYTHON_EXECUTABLE)
14 message(FATAL_ERROR "Cannot build test suites without Python 3")
Nicholas Wilson733676b2015-11-14 13:09:01 +000015endif()
16
Manuel Pégourié-Gonnard313bcfc2021-09-07 12:16:49 +020017# generated .data files will go there
18file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/suites)
19
Gilles Peskinecca6ce82022-09-18 23:08:38 +020020# Get base names for generated files
David Horstmann1732b5d2021-10-19 16:43:53 +010021execute_process(
22 COMMAND
David Horstmann3e30ad92021-10-20 16:53:58 +010023 ${MBEDTLS_PYTHON_EXECUTABLE}
Gilles Peskinee154e6f2024-05-23 16:31:22 +020024 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_config_tests.py
25 --list-for-cmake
26 WORKING_DIRECTORY
27 ${CMAKE_CURRENT_SOURCE_DIR}/..
28 OUTPUT_VARIABLE
Ronald Cronf76cd222024-07-09 14:24:35 +020029 base_config_generated_data_files_raw)
Gilles Peskinee154e6f2024-05-23 16:31:22 +020030string(REGEX REPLACE "[^;]*/" ""
Ronald Cronf76cd222024-07-09 14:24:35 +020031 base_config_generated_data_files_raw "${base_config_generated_data_files_raw}")
32# Can be replace by list(FILTER ...) when CI CMake version is >=3.6
33foreach(file ${base_config_generated_data_files_raw})
34 if(${file} MATCHES "mbedtls")
35 list(APPEND base_config_generated_data_files ${file})
36 endif()
37endforeach()
David Horstmann7570d242021-10-20 16:27:24 +010038
Gilles Peskinecca6ce82022-09-18 23:08:38 +020039# Derive generated file paths in the build directory. The generated data
40# files go into the suites/ subdirectory.
41set(base_generated_data_files
Ronald Cronf76cd222024-07-09 14:24:35 +020042 ${base_config_generated_data_files})
Gilles Peskinecca6ce82022-09-18 23:08:38 +020043string(REGEX REPLACE "([^;]+)" "suites/\\1"
44 all_generated_data_files "${base_generated_data_files}")
Gilles Peskinee154e6f2024-05-23 16:31:22 +020045set(config_generated_data_files "")
Gilles Peskinee154e6f2024-05-23 16:31:22 +020046foreach(file ${base_config_generated_data_files})
47 list(APPEND config_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file})
48endforeach()
David Horstmann1732b5d2021-10-19 16:43:53 +010049
David Horstmanna8d14062021-10-20 17:14:23 +010050if(GEN_FILES)
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020051 add_custom_command(
52 OUTPUT
Gilles Peskinee154e6f2024-05-23 16:31:22 +020053 ${config_generated_data_files}
54 WORKING_DIRECTORY
55 ${CMAKE_CURRENT_SOURCE_DIR}/..
56 COMMAND
57 ${MBEDTLS_PYTHON_EXECUTABLE}
58 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_config_tests.py
59 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites
Ronald Cronf76cd222024-07-09 14:24:35 +020060 ${config_generated_data_files}
Gilles Peskinee154e6f2024-05-23 16:31:22 +020061 DEPENDS
62 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_config_tests.py
63 # Do not declare the configuration files as dependencies: they
64 # change too often in ways that don't affect the result
65 # ((un)commenting some options).
66 )
Gilles Peskine40c090f2024-09-19 19:20:00 +020067 add_custom_command(
68 OUTPUT
69 ${CMAKE_CURRENT_SOURCE_DIR}/opt-testcases/tls13-compat.sh
70 WORKING_DIRECTORY
71 ${CMAKE_CURRENT_SOURCE_DIR}/..
72 COMMAND
73 "${MBEDTLS_PYTHON_EXECUTABLE}"
74 "${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_tls13_compat_tests.py"
75 DEPENDS
76 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_keys.py
77 )
78 add_custom_target(tls13-compat.sh
79 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/opt-testcases/tls13-compat.sh)
Gilles Peskinefd6d2792024-09-19 19:27:31 +020080 set_target_properties(tls13-compat.sh PROPERTIES EXCLUDE_FROM_ALL NO)
Gilles Peskine40c090f2024-09-19 19:20:00 +020081
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020082else()
Gilles Peskinecca6ce82022-09-18 23:08:38 +020083 foreach(file ${all_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010084 link_to_source(${file})
85 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020086endif()
Jerry Yu3b86d1b2022-03-02 10:40:41 +080087# CMake generates sub-makefiles for each target and calls them in subprocesses.
88# Without this command, cmake will generate rules in each sub-makefile. As a result,
89# they can cause race conditions in parallel builds.
90# With this line, only 4 sub-makefiles include the above command, that reduces
91# the risk of a race.
Gilles Peskineef822c12024-05-28 19:07:24 +020092add_custom_target(test_suite_config_generated_data DEPENDS ${config_generated_data_files})
David Horstmann38610622022-11-28 10:18:05 +000093# If SKIP_TEST_SUITES is not defined with -D, get it from the environment.
94if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES}))
95 set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES})
96endif()
Gilles Peskineac372cc2018-11-29 10:15:06 +000097# Test suites caught by SKIP_TEST_SUITES are built but not executed.
98# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
99# but not "test_suite_foobar".
100string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
101string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
102set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
103
Paul Bakker73043762011-07-13 15:03:10 +0000104function(add_test_suite suite_name)
105 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200106 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +0000107 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200108 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +0000109 endif()
110
David Horstmannae7bd352021-10-19 19:05:42 +0100111 # Get the test names of the tests with generated .data files
112 # from the generated_data_files list in parent scope.
Ronald Cron7f410db2024-07-09 14:20:34 +0200113 set(config_generated_data_names "")
Ronald Cron7f410db2024-07-09 14:20:34 +0200114 foreach(generated_data_file ${config_generated_data_files})
115 # Get the plain filename
116 get_filename_component(generated_data_name ${generated_data_file} NAME)
117 # Remove the ".data" extension
118 get_name_without_last_ext(generated_data_name ${generated_data_name})
119 # Remove leading "test_suite_"
120 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
121 list(APPEND config_generated_data_names ${generated_data_name})
122 endforeach()
David Horstmannae7bd352021-10-19 19:05:42 +0100123
Ronald Cronf76cd222024-07-09 14:24:35 +0200124 if(";${config_generated_data_names};" MATCHES ";${data_name};")
Gilles Peskineef822c12024-05-28 19:07:24 +0200125 set(data_file
126 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
Ronald Cron7f410db2024-07-09 14:20:34 +0200127 set(dependency test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200128 else()
129 set(data_file
130 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
Gilles Peskineef822c12024-05-28 19:07:24 +0200131 set(dependency
Ronald Cronf76cd222024-07-09 14:24:35 +0200132 test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200133 endif()
134
Paul Bakker367dae42009-06-28 21:50:27 +0000135 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200136 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +0100137 # The output filename of generate_test_code.py is derived from the -d
138 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200139 test_suite_${data_name}.c
140 COMMAND
141 ${MBEDTLS_PYTHON_EXECUTABLE}
David Horstmannf6f3bca2024-05-29 17:57:08 +0100142 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200143 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200144 -d ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200145 -t ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function
146 -p ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200147 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
Ronald Cronf76cd222024-07-09 14:24:35 +0200148 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200149 -o .
150 DEPENDS
David Horstmannf6f3bca2024-05-29 17:57:08 +0100151 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200152 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200153 ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200154 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function
155 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function
156 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200157 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200158 BYPRODUCTS
159 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000160 )
Paul Bakker19343182013-08-16 13:31:10 +0200161
Yanray Wangb458b8c2022-10-28 11:49:33 +0800162 add_executable(test_suite_${data_name} test_suite_${data_name}.c
163 $<TARGET_OBJECTS:mbedtls_test>
164 $<TARGET_OBJECTS:mbedtls_test_helpers>)
Werner Lewis383461c2022-08-23 11:29:05 +0100165 add_dependencies(test_suite_${data_name} ${dependency})
Paul Bakker73043762011-07-13 15:03:10 +0000166 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200167 # Include test-specific header files from ./include and private header
168 # files (used by some invasive tests) from ../library. Public header
169 # files are automatically included because the library targets declare
170 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +0200171 target_include_directories(test_suite_${data_name}
172 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Ronald Cronc29afb62024-07-01 14:50:54 +0200173 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library
174 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/core
175 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/drivers/builtin/src)
David Horstmannc2ab3982024-01-17 12:25:19 +0000176 # Request C11, which is needed for memory poisoning tests
177 set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11)
Ronald Cron85527412020-05-15 17:20:47 +0200178
Gilles Peskineac372cc2018-11-29 10:15:06 +0000179 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
180 message(STATUS "The test suite ${data_name} will not be executed.")
181 else()
182 add_test(${data_name}-suite test_suite_${data_name} --verbose)
183 endif()
Paul Bakker367dae42009-06-28 21:50:27 +0000184endfunction(add_test_suite)
185
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200186# Enable definition of various functions used throughout the testsuite
187# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
188# on non-POSIX platforms.
189add_definitions("-D_POSIX_C_SOURCE=200809L")
190
Gilles Peskinef29019f2021-02-23 13:44:41 +0100191if(CMAKE_COMPILER_IS_CLANG)
192 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
193endif(CMAKE_COMPILER_IS_CLANG)
194
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200195if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000196 # If a warning level has been defined, suppress all warnings for test code
197 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
198 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200199endif(MSVC)
200
Gilles Peskine29080e82022-01-25 22:30:25 +0100201file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200202list(APPEND test_suites ${all_generated_data_files})
Gilles Peskine29080e82022-01-25 22:30:25 +0100203# If the generated .data files are present in the source tree, we just added
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200204# them twice, both through GLOB and through ${all_generated_data_files}.
Gilles Peskine29080e82022-01-25 22:30:25 +0100205list(REMOVE_DUPLICATES test_suites)
206list(SORT test_suites)
207foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100208 get_filename_component(data_name ${test_suite} NAME)
Gilles Peskinea18fb9d2022-01-25 23:24:34 +0100209 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100210 string(REPLACE "test_suite_" "" data_name "${data_name}")
211 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100212 add_test_suite(${function_name} ${data_name})
213endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100214
Gilles Peskine84052572018-03-21 12:12:47 +0100215# Make scripts and data files needed for testing available in an
216# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100217if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400218 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
219 link_to_source(seedfile)
220 endif()
Gilles Peskined7db8832022-11-30 17:52:54 +0100221 link_to_source(Descriptions.txt)
Gilles Peskine84052572018-03-21 12:12:47 +0100222 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600223 link_to_source(context-info.sh)
David Horstmanndcf18dd2024-06-11 17:44:00 +0100224 link_to_source(../framework/data_files)
Gilles Peskine84052572018-03-21 12:12:47 +0100225 link_to_source(scripts)
226 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800227 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100228endif()