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