Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 1 | set(libs |
Raef Coles | 995c66f | 2020-10-13 16:30:41 +0100 | [diff] [blame] | 2 | ${mbedtls_target} |
Paul Elliott | 2667eda | 2023-11-29 15:53:00 +0000 | [diff] [blame] | 3 | ${CMAKE_THREAD_LIBS_INIT} |
Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 4 | ) |
| 5 | |
Ashley Duncan | d85a7e9 | 2019-04-29 20:35:06 +1200 | [diff] [blame] | 6 | # 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. |
| 9 | if(NOT DEFINED MBEDTLS_DIR) |
| 10 | set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR}) |
| 11 | endif() |
| 12 | |
okhowang(王沛文) | 3c1b090 | 2020-03-25 19:55:32 +0800 | [diff] [blame] | 13 | if(NOT MBEDTLS_PYTHON_EXECUTABLE) |
| 14 | message(FATAL_ERROR "Cannot build test suites without Python 3") |
Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 15 | endif() |
| 16 | |
Manuel Pégourié-Gonnard | 313bcfc | 2021-09-07 12:16:49 +0200 | [diff] [blame] | 17 | # generated .data files will go there |
| 18 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/suites) |
| 19 | |
Gilles Peskine | cca6ce8 | 2022-09-18 23:08:38 +0200 | [diff] [blame] | 20 | # Get base names for generated files |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 21 | execute_process( |
| 22 | COMMAND |
David Horstmann | 3e30ad9 | 2021-10-20 16:53:58 +0100 | [diff] [blame] | 23 | ${MBEDTLS_PYTHON_EXECUTABLE} |
Gilles Peskine | e154e6f | 2024-05-23 16:31:22 +0200 | [diff] [blame] | 24 | ${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 Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 29 | base_config_generated_data_files_raw) |
Gilles Peskine | e154e6f | 2024-05-23 16:31:22 +0200 | [diff] [blame] | 30 | string(REGEX REPLACE "[^;]*/" "" |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 31 | 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 |
| 33 | foreach(file ${base_config_generated_data_files_raw}) |
| 34 | if(${file} MATCHES "mbedtls") |
| 35 | list(APPEND base_config_generated_data_files ${file}) |
| 36 | endif() |
| 37 | endforeach() |
David Horstmann | 7570d24 | 2021-10-20 16:27:24 +0100 | [diff] [blame] | 38 | |
Gilles Peskine | cca6ce8 | 2022-09-18 23:08:38 +0200 | [diff] [blame] | 39 | # Derive generated file paths in the build directory. The generated data |
| 40 | # files go into the suites/ subdirectory. |
| 41 | set(base_generated_data_files |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 42 | ${base_config_generated_data_files}) |
Gilles Peskine | cca6ce8 | 2022-09-18 23:08:38 +0200 | [diff] [blame] | 43 | string(REGEX REPLACE "([^;]+)" "suites/\\1" |
| 44 | all_generated_data_files "${base_generated_data_files}") |
Gilles Peskine | e154e6f | 2024-05-23 16:31:22 +0200 | [diff] [blame] | 45 | set(config_generated_data_files "") |
Gilles Peskine | e154e6f | 2024-05-23 16:31:22 +0200 | [diff] [blame] | 46 | foreach(file ${base_config_generated_data_files}) |
| 47 | list(APPEND config_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) |
| 48 | endforeach() |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 49 | |
David Horstmann | a8d1406 | 2021-10-20 17:14:23 +0100 | [diff] [blame] | 50 | if(GEN_FILES) |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 51 | add_custom_command( |
| 52 | OUTPUT |
Gilles Peskine | e154e6f | 2024-05-23 16:31:22 +0200 | [diff] [blame] | 53 | ${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 Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 60 | ${config_generated_data_files} |
Gilles Peskine | e154e6f | 2024-05-23 16:31:22 +0200 | [diff] [blame] | 61 | 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 | ) |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 67 | else() |
Gilles Peskine | cca6ce8 | 2022-09-18 23:08:38 +0200 | [diff] [blame] | 68 | foreach(file ${all_generated_data_files}) |
David Horstmann | 1732b5d | 2021-10-19 16:43:53 +0100 | [diff] [blame] | 69 | link_to_source(${file}) |
| 70 | endforeach() |
Manuel Pégourié-Gonnard | e90e405 | 2021-09-08 13:27:09 +0200 | [diff] [blame] | 71 | endif() |
Jerry Yu | 3b86d1b | 2022-03-02 10:40:41 +0800 | [diff] [blame] | 72 | # CMake generates sub-makefiles for each target and calls them in subprocesses. |
| 73 | # Without this command, cmake will generate rules in each sub-makefile. As a result, |
| 74 | # they can cause race conditions in parallel builds. |
| 75 | # With this line, only 4 sub-makefiles include the above command, that reduces |
| 76 | # the risk of a race. |
Gilles Peskine | ef822c1 | 2024-05-28 19:07:24 +0200 | [diff] [blame] | 77 | add_custom_target(test_suite_config_generated_data DEPENDS ${config_generated_data_files}) |
David Horstmann | 3861062 | 2022-11-28 10:18:05 +0000 | [diff] [blame] | 78 | # If SKIP_TEST_SUITES is not defined with -D, get it from the environment. |
| 79 | if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES})) |
| 80 | set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES}) |
| 81 | endif() |
Gilles Peskine | ac372cc | 2018-11-29 10:15:06 +0000 | [diff] [blame] | 82 | # Test suites caught by SKIP_TEST_SUITES are built but not executed. |
| 83 | # "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar" |
| 84 | # but not "test_suite_foobar". |
| 85 | string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}") |
| 86 | string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}") |
| 87 | set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)") |
| 88 | |
Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 89 | function(add_test_suite suite_name) |
| 90 | if(ARGV1) |
Manuel Pégourié-Gonnard | 216a183 | 2015-06-25 09:20:03 +0200 | [diff] [blame] | 91 | set(data_name ${ARGV1}) |
Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 92 | else() |
Manuel Pégourié-Gonnard | 216a183 | 2015-06-25 09:20:03 +0200 | [diff] [blame] | 93 | set(data_name ${suite_name}) |
Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 94 | endif() |
| 95 | |
David Horstmann | ae7bd35 | 2021-10-19 19:05:42 +0100 | [diff] [blame] | 96 | # Get the test names of the tests with generated .data files |
| 97 | # from the generated_data_files list in parent scope. |
Ronald Cron | 7f410db | 2024-07-09 14:20:34 +0200 | [diff] [blame] | 98 | set(config_generated_data_names "") |
Ronald Cron | 7f410db | 2024-07-09 14:20:34 +0200 | [diff] [blame] | 99 | foreach(generated_data_file ${config_generated_data_files}) |
| 100 | # Get the plain filename |
| 101 | get_filename_component(generated_data_name ${generated_data_file} NAME) |
| 102 | # Remove the ".data" extension |
| 103 | get_name_without_last_ext(generated_data_name ${generated_data_name}) |
| 104 | # Remove leading "test_suite_" |
| 105 | string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name) |
| 106 | list(APPEND config_generated_data_names ${generated_data_name}) |
| 107 | endforeach() |
David Horstmann | ae7bd35 | 2021-10-19 19:05:42 +0100 | [diff] [blame] | 108 | |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 109 | if(";${config_generated_data_names};" MATCHES ";${data_name};") |
Gilles Peskine | ef822c1 | 2024-05-28 19:07:24 +0200 | [diff] [blame] | 110 | set(data_file |
| 111 | ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data) |
Ronald Cron | 7f410db | 2024-07-09 14:20:34 +0200 | [diff] [blame] | 112 | set(dependency test_suite_config_generated_data) |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 113 | else() |
| 114 | set(data_file |
| 115 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data) |
Gilles Peskine | ef822c1 | 2024-05-28 19:07:24 +0200 | [diff] [blame] | 116 | set(dependency |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 117 | test_suite_config_generated_data) |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 118 | endif() |
| 119 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 120 | add_custom_command( |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 121 | OUTPUT |
David Horstmann | f602eb1 | 2021-10-20 16:40:56 +0100 | [diff] [blame] | 122 | # The output filename of generate_test_code.py is derived from the -d |
| 123 | # input argument. |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 124 | test_suite_${data_name}.c |
| 125 | COMMAND |
| 126 | ${MBEDTLS_PYTHON_EXECUTABLE} |
David Horstmann | f6f3bca | 2024-05-29 17:57:08 +0100 | [diff] [blame] | 127 | ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 128 | -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 129 | -d ${data_file} |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 130 | -t ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function |
| 131 | -p ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 132 | -s ${CMAKE_CURRENT_SOURCE_DIR}/suites |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 133 | --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 134 | -o . |
| 135 | DEPENDS |
David Horstmann | f6f3bca | 2024-05-29 17:57:08 +0100 | [diff] [blame] | 136 | ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 137 | ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function |
Manuel Pégourié-Gonnard | ce3ba8f | 2021-05-14 12:03:37 +0200 | [diff] [blame] | 138 | ${data_file} |
Ronald Cron | f76cd22 | 2024-07-09 14:24:35 +0200 | [diff] [blame^] | 139 | ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function |
| 140 | ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function |
| 141 | ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 142 | ${mbedtls_target} |
Manuel Pégourié-Gonnard | 389150d | 2021-09-09 10:51:16 +0200 | [diff] [blame] | 143 | BYPRODUCTS |
| 144 | test_suite_${data_name}.datax |
Paul Bakker | 4c14a25 | 2010-06-18 22:54:05 +0000 | [diff] [blame] | 145 | ) |
Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 146 | |
Yanray Wang | b458b8c | 2022-10-28 11:49:33 +0800 | [diff] [blame] | 147 | add_executable(test_suite_${data_name} test_suite_${data_name}.c |
| 148 | $<TARGET_OBJECTS:mbedtls_test> |
| 149 | $<TARGET_OBJECTS:mbedtls_test_helpers>) |
Werner Lewis | 383461c | 2022-08-23 11:29:05 +0100 | [diff] [blame] | 150 | add_dependencies(test_suite_${data_name} ${dependency}) |
Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 151 | target_link_libraries(test_suite_${data_name} ${libs}) |
Gilles Peskine | 76dd3aa | 2020-07-02 15:58:37 +0200 | [diff] [blame] | 152 | # Include test-specific header files from ./include and private header |
| 153 | # files (used by some invasive tests) from ../library. Public header |
| 154 | # files are automatically included because the library targets declare |
| 155 | # them as PUBLIC. |
Ronald Cron | 8552741 | 2020-05-15 17:20:47 +0200 | [diff] [blame] | 156 | target_include_directories(test_suite_${data_name} |
| 157 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include |
Ronald Cron | c29afb6 | 2024-07-01 14:50:54 +0200 | [diff] [blame] | 158 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library |
| 159 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/core |
| 160 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/drivers/builtin/src) |
David Horstmann | c2ab398 | 2024-01-17 12:25:19 +0000 | [diff] [blame] | 161 | # Request C11, which is needed for memory poisoning tests |
| 162 | set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11) |
Ronald Cron | 8552741 | 2020-05-15 17:20:47 +0200 | [diff] [blame] | 163 | |
Gilles Peskine | ac372cc | 2018-11-29 10:15:06 +0000 | [diff] [blame] | 164 | if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) |
| 165 | message(STATUS "The test suite ${data_name} will not be executed.") |
| 166 | else() |
| 167 | add_test(${data_name}-suite test_suite_${data_name} --verbose) |
| 168 | endif() |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 169 | endfunction(add_test_suite) |
| 170 | |
Manuel Pégourié-Gonnard | 42681f3 | 2021-05-14 10:53:43 +0200 | [diff] [blame] | 171 | # Enable definition of various functions used throughout the testsuite |
| 172 | # (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless |
| 173 | # on non-POSIX platforms. |
| 174 | add_definitions("-D_POSIX_C_SOURCE=200809L") |
| 175 | |
Gilles Peskine | f29019f | 2021-02-23 13:44:41 +0100 | [diff] [blame] | 176 | if(CMAKE_COMPILER_IS_CLANG) |
| 177 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") |
| 178 | endif(CMAKE_COMPILER_IS_CLANG) |
| 179 | |
Manuel Pégourié-Gonnard | 3083053 | 2015-07-01 17:06:28 +0200 | [diff] [blame] | 180 | if(MSVC) |
Simon B | 9b3e3c4 | 2016-11-03 01:12:50 +0000 | [diff] [blame] | 181 | # If a warning level has been defined, suppress all warnings for test code |
| 182 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0") |
| 183 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-") |
Manuel Pégourié-Gonnard | 3083053 | 2015-07-01 17:06:28 +0200 | [diff] [blame] | 184 | endif(MSVC) |
| 185 | |
Gilles Peskine | 29080e8 | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 186 | file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data) |
Gilles Peskine | cca6ce8 | 2022-09-18 23:08:38 +0200 | [diff] [blame] | 187 | list(APPEND test_suites ${all_generated_data_files}) |
Gilles Peskine | 29080e8 | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 188 | # If the generated .data files are present in the source tree, we just added |
Gilles Peskine | cca6ce8 | 2022-09-18 23:08:38 +0200 | [diff] [blame] | 189 | # them twice, both through GLOB and through ${all_generated_data_files}. |
Gilles Peskine | 29080e8 | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 190 | list(REMOVE_DUPLICATES test_suites) |
| 191 | list(SORT test_suites) |
| 192 | foreach(test_suite ${test_suites}) |
Gilles Peskine | cd55fe0 | 2022-01-25 23:13:49 +0100 | [diff] [blame] | 193 | get_filename_component(data_name ${test_suite} NAME) |
Gilles Peskine | a18fb9d | 2022-01-25 23:24:34 +0100 | [diff] [blame] | 194 | string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}") |
Gilles Peskine | cd55fe0 | 2022-01-25 23:13:49 +0100 | [diff] [blame] | 195 | string(REPLACE "test_suite_" "" data_name "${data_name}") |
| 196 | string(REGEX MATCH "[^.]*" function_name "${data_name}") |
Gilles Peskine | 29080e8 | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 197 | add_test_suite(${function_name} ${data_name}) |
| 198 | endforeach(test_suite) |
Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 200 | # Make scripts and data files needed for testing available in an |
| 201 | # out-of-source build. |
Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 202 | if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) |
Andrzej Kurek | 4a71cfa | 2019-05-06 05:06:06 -0400 | [diff] [blame] | 203 | if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile") |
| 204 | link_to_source(seedfile) |
| 205 | endif() |
Gilles Peskine | d7db883 | 2022-11-30 17:52:54 +0100 | [diff] [blame] | 206 | link_to_source(Descriptions.txt) |
Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 207 | link_to_source(compat.sh) |
David Brown | c744418 | 2020-10-16 13:19:49 -0600 | [diff] [blame] | 208 | link_to_source(context-info.sh) |
David Horstmann | dcf18dd | 2024-06-11 17:44:00 +0100 | [diff] [blame] | 209 | link_to_source(../framework/data_files) |
Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 210 | link_to_source(scripts) |
| 211 | link_to_source(ssl-opt.sh) |
Jerry Yu | 55ee769 | 2021-11-29 13:26:55 +0800 | [diff] [blame] | 212 | link_to_source(opt-testcases) |
Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 213 | endif() |