blob: 8318e8bf3be9c093863aff4ea6285b05ec385292 [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
okhowang(王沛文)3c1b0902020-03-25 19:55:32 +08006if(NOT MBEDTLS_PYTHON_EXECUTABLE)
7 message(FATAL_ERROR "Cannot build test suites without Python 3")
Nicholas Wilson733676b2015-11-14 13:09:01 +00008endif()
9
Manuel Pégourié-Gonnard313bcfc2021-09-07 12:16:49 +020010# generated .data files will go there
11file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/suites)
12
Gilles Peskinecca6ce82022-09-18 23:08:38 +020013# Get base names for generated files
David Horstmann1732b5d2021-10-19 16:43:53 +010014execute_process(
15 COMMAND
David Horstmann3e30ad92021-10-20 16:53:58 +010016 ${MBEDTLS_PYTHON_EXECUTABLE}
Ronald Cronb6254f52024-07-25 14:52:19 +020017 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_config_tests.py
Gilles Peskinee154e6f2024-05-23 16:31:22 +020018 --list-for-cmake
19 WORKING_DIRECTORY
20 ${CMAKE_CURRENT_SOURCE_DIR}/..
21 OUTPUT_VARIABLE
Ronald Cronf76cd222024-07-09 14:24:35 +020022 base_config_generated_data_files_raw)
Gilles Peskinee154e6f2024-05-23 16:31:22 +020023string(REGEX REPLACE "[^;]*/" ""
Ronald Cronf76cd222024-07-09 14:24:35 +020024 base_config_generated_data_files_raw "${base_config_generated_data_files_raw}")
25# Can be replace by list(FILTER ...) when CI CMake version is >=3.6
26foreach(file ${base_config_generated_data_files_raw})
27 if(${file} MATCHES "mbedtls")
28 list(APPEND base_config_generated_data_files ${file})
29 endif()
30endforeach()
David Horstmann7570d242021-10-20 16:27:24 +010031
Gilles Peskinecca6ce82022-09-18 23:08:38 +020032# Derive generated file paths in the build directory. The generated data
33# files go into the suites/ subdirectory.
34set(base_generated_data_files
Ronald Cronf76cd222024-07-09 14:24:35 +020035 ${base_config_generated_data_files})
Gilles Peskinecca6ce82022-09-18 23:08:38 +020036string(REGEX REPLACE "([^;]+)" "suites/\\1"
37 all_generated_data_files "${base_generated_data_files}")
Gilles Peskinee154e6f2024-05-23 16:31:22 +020038set(config_generated_data_files "")
Gilles Peskinee154e6f2024-05-23 16:31:22 +020039foreach(file ${base_config_generated_data_files})
40 list(APPEND config_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file})
41endforeach()
David Horstmann1732b5d2021-10-19 16:43:53 +010042
David Horstmanna8d14062021-10-20 17:14:23 +010043if(GEN_FILES)
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020044 add_custom_command(
45 OUTPUT
Gilles Peskinee154e6f2024-05-23 16:31:22 +020046 ${config_generated_data_files}
47 WORKING_DIRECTORY
48 ${CMAKE_CURRENT_SOURCE_DIR}/..
49 COMMAND
50 ${MBEDTLS_PYTHON_EXECUTABLE}
Ronald Cronb6254f52024-07-25 14:52:19 +020051 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_config_tests.py
Gilles Peskinee154e6f2024-05-23 16:31:22 +020052 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites
Ronald Cronf76cd222024-07-09 14:24:35 +020053 ${config_generated_data_files}
Gilles Peskinee154e6f2024-05-23 16:31:22 +020054 DEPENDS
Ronald Cronb6254f52024-07-25 14:52:19 +020055 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_config_tests.py
Gilles Peskinee154e6f2024-05-23 16:31:22 +020056 # Do not declare the configuration files as dependencies: they
57 # change too often in ways that don't affect the result
58 # ((un)commenting some options).
59 )
Gilles Peskine40c090f2024-09-19 19:20:00 +020060 add_custom_command(
61 OUTPUT
62 ${CMAKE_CURRENT_SOURCE_DIR}/opt-testcases/tls13-compat.sh
63 WORKING_DIRECTORY
64 ${CMAKE_CURRENT_SOURCE_DIR}/..
65 COMMAND
66 "${MBEDTLS_PYTHON_EXECUTABLE}"
Elena Uziunaiteb74c3ea2024-10-08 13:02:48 +010067 "${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_tls13_compat_tests.py"
Gilles Peskine40c090f2024-09-19 19:20:00 +020068 DEPENDS
Elena Uziunaiteb74c3ea2024-10-08 13:02:48 +010069 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_tls13_compat_tests.py
Gilles Peskine40c090f2024-09-19 19:20:00 +020070 )
71 add_custom_target(tls13-compat.sh
72 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/opt-testcases/tls13-compat.sh)
Gilles Peskinefd6d2792024-09-19 19:27:31 +020073 set_target_properties(tls13-compat.sh PROPERTIES EXCLUDE_FROM_ALL NO)
Gilles Peskine63068d62024-09-24 19:09:57 +020074 add_dependencies(${ssl_opt_target} tls13-compat.sh)
Gilles Peskine40c090f2024-09-19 19:20:00 +020075
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020076else()
Gilles Peskinecca6ce82022-09-18 23:08:38 +020077 foreach(file ${all_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010078 link_to_source(${file})
79 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020080endif()
Jerry Yu3b86d1b2022-03-02 10:40:41 +080081# CMake generates sub-makefiles for each target and calls them in subprocesses.
82# Without this command, cmake will generate rules in each sub-makefile. As a result,
83# they can cause race conditions in parallel builds.
84# With this line, only 4 sub-makefiles include the above command, that reduces
85# the risk of a race.
Gilles Peskineef822c12024-05-28 19:07:24 +020086add_custom_target(test_suite_config_generated_data DEPENDS ${config_generated_data_files})
David Horstmann38610622022-11-28 10:18:05 +000087# If SKIP_TEST_SUITES is not defined with -D, get it from the environment.
88if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES}))
89 set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES})
90endif()
Gilles Peskineac372cc2018-11-29 10:15:06 +000091# Test suites caught by SKIP_TEST_SUITES are built but not executed.
92# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
93# but not "test_suite_foobar".
94string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
95string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
96set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
97
Paul Bakker73043762011-07-13 15:03:10 +000098function(add_test_suite suite_name)
99 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200100 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +0000101 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200102 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +0000103 endif()
104
David Horstmannae7bd352021-10-19 19:05:42 +0100105 # Get the test names of the tests with generated .data files
106 # from the generated_data_files list in parent scope.
Ronald Cron7f410db2024-07-09 14:20:34 +0200107 set(config_generated_data_names "")
Ronald Cron7f410db2024-07-09 14:20:34 +0200108 foreach(generated_data_file ${config_generated_data_files})
109 # Get the plain filename
110 get_filename_component(generated_data_name ${generated_data_file} NAME)
111 # Remove the ".data" extension
112 get_name_without_last_ext(generated_data_name ${generated_data_name})
113 # Remove leading "test_suite_"
114 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
115 list(APPEND config_generated_data_names ${generated_data_name})
116 endforeach()
David Horstmannae7bd352021-10-19 19:05:42 +0100117
Ronald Cronf76cd222024-07-09 14:24:35 +0200118 if(";${config_generated_data_names};" MATCHES ";${data_name};")
Gilles Peskineef822c12024-05-28 19:07:24 +0200119 set(data_file
120 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
Ronald Cron7f410db2024-07-09 14:20:34 +0200121 set(dependency test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200122 else()
123 set(data_file
124 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
Gilles Peskineef822c12024-05-28 19:07:24 +0200125 set(dependency
Ronald Cronf76cd222024-07-09 14:24:35 +0200126 test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200127 endif()
128
Paul Bakker367dae42009-06-28 21:50:27 +0000129 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200130 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +0100131 # The output filename of generate_test_code.py is derived from the -d
132 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200133 test_suite_${data_name}.c
134 COMMAND
135 ${MBEDTLS_PYTHON_EXECUTABLE}
Ronald Cronb6254f52024-07-25 14:52:19 +0200136 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200137 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200138 -d ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200139 -t ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function
140 -p ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200141 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
Ronald Cronf76cd222024-07-09 14:24:35 +0200142 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200143 -o .
144 DEPENDS
Ronald Cronb6254f52024-07-25 14:52:19 +0200145 ${MBEDTLS_FRAMEWORK_DIR}/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200146 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200147 ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200148 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function
149 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function
150 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200151 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200152 BYPRODUCTS
153 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000154 )
Paul Bakker19343182013-08-16 13:31:10 +0200155
Yanray Wangb458b8c2022-10-28 11:49:33 +0800156 add_executable(test_suite_${data_name} test_suite_${data_name}.c
157 $<TARGET_OBJECTS:mbedtls_test>
158 $<TARGET_OBJECTS:mbedtls_test_helpers>)
Ronald Cronb2478982024-10-07 16:17:07 +0200159 set_base_compile_options(test_suite_${data_name})
Ronald Crond093edd2024-10-10 11:57:10 +0200160 target_compile_options(test_suite_${data_name} PRIVATE ${TEST_C_FLAGS})
Werner Lewis383461c2022-08-23 11:29:05 +0100161 add_dependencies(test_suite_${data_name} ${dependency})
Paul Bakker73043762011-07-13 15:03:10 +0000162 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200163 # Include test-specific header files from ./include and private header
164 # files (used by some invasive tests) from ../library. Public header
165 # files are automatically included because the library targets declare
166 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +0200167 target_include_directories(test_suite_${data_name}
168 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Ronald Cronc29afb62024-07-01 14:50:54 +0200169 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library
170 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/core
171 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/drivers/builtin/src)
David Horstmannc2ab3982024-01-17 12:25:19 +0000172 # Request C11, which is needed for memory poisoning tests
173 set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11)
Ronald Cron85527412020-05-15 17:20:47 +0200174
Gilles Peskineac372cc2018-11-29 10:15:06 +0000175 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
176 message(STATUS "The test suite ${data_name} will not be executed.")
177 else()
178 add_test(${data_name}-suite test_suite_${data_name} --verbose)
179 endif()
Paul Bakker367dae42009-06-28 21:50:27 +0000180endfunction(add_test_suite)
181
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200182# Enable definition of various functions used throughout the testsuite
183# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
184# on non-POSIX platforms.
185add_definitions("-D_POSIX_C_SOURCE=200809L")
186
Gilles Peskinef29019f2021-02-23 13:44:41 +0100187if(CMAKE_COMPILER_IS_CLANG)
Ronald Crond093edd2024-10-10 11:57:10 +0200188 set(TEST_C_FLAGS -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code)
Gilles Peskinef29019f2021-02-23 13:44:41 +0100189endif(CMAKE_COMPILER_IS_CLANG)
190
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200191if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000192 # If a warning level has been defined, suppress all warnings for test code
Ronald Crond093edd2024-10-10 11:57:10 +0200193 set(TEST_C_FLAGS /W0 /WX-)
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200194endif(MSVC)
195
Gilles Peskine29080e82022-01-25 22:30:25 +0100196file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200197list(APPEND test_suites ${all_generated_data_files})
Gilles Peskine29080e82022-01-25 22:30:25 +0100198# If the generated .data files are present in the source tree, we just added
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200199# them twice, both through GLOB and through ${all_generated_data_files}.
Gilles Peskine29080e82022-01-25 22:30:25 +0100200list(REMOVE_DUPLICATES test_suites)
201list(SORT test_suites)
202foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100203 get_filename_component(data_name ${test_suite} NAME)
Gilles Peskinea18fb9d2022-01-25 23:24:34 +0100204 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100205 string(REPLACE "test_suite_" "" data_name "${data_name}")
206 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100207 add_test_suite(${function_name} ${data_name})
208endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100209
Gilles Peskine84052572018-03-21 12:12:47 +0100210# Make scripts and data files needed for testing available in an
211# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100212if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400213 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
214 link_to_source(seedfile)
215 endif()
Gilles Peskined7db8832022-11-30 17:52:54 +0100216 link_to_source(Descriptions.txt)
Gilles Peskine84052572018-03-21 12:12:47 +0100217 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600218 link_to_source(context-info.sh)
David Horstmanndcf18dd2024-06-11 17:44:00 +0100219 link_to_source(../framework/data_files)
Gilles Peskine84052572018-03-21 12:12:47 +0100220 link_to_source(scripts)
221 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800222 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100223endif()