blob: 854ef19d4307cf3bb4acc3acbc73701e9adf5d90 [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)
80
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020081else()
Gilles Peskinecca6ce82022-09-18 23:08:38 +020082 foreach(file ${all_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010083 link_to_source(${file})
84 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020085endif()
Jerry Yu3b86d1b2022-03-02 10:40:41 +080086# CMake generates sub-makefiles for each target and calls them in subprocesses.
87# Without this command, cmake will generate rules in each sub-makefile. As a result,
88# they can cause race conditions in parallel builds.
89# With this line, only 4 sub-makefiles include the above command, that reduces
90# the risk of a race.
Gilles Peskineef822c12024-05-28 19:07:24 +020091add_custom_target(test_suite_config_generated_data DEPENDS ${config_generated_data_files})
David Horstmann38610622022-11-28 10:18:05 +000092# If SKIP_TEST_SUITES is not defined with -D, get it from the environment.
93if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES}))
94 set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES})
95endif()
Gilles Peskineac372cc2018-11-29 10:15:06 +000096# Test suites caught by SKIP_TEST_SUITES are built but not executed.
97# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
98# but not "test_suite_foobar".
99string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
100string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
101set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
102
Paul Bakker73043762011-07-13 15:03:10 +0000103function(add_test_suite suite_name)
104 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200105 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +0000106 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +0200107 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +0000108 endif()
109
David Horstmannae7bd352021-10-19 19:05:42 +0100110 # Get the test names of the tests with generated .data files
111 # from the generated_data_files list in parent scope.
Ronald Cron7f410db2024-07-09 14:20:34 +0200112 set(config_generated_data_names "")
Ronald Cron7f410db2024-07-09 14:20:34 +0200113 foreach(generated_data_file ${config_generated_data_files})
114 # Get the plain filename
115 get_filename_component(generated_data_name ${generated_data_file} NAME)
116 # Remove the ".data" extension
117 get_name_without_last_ext(generated_data_name ${generated_data_name})
118 # Remove leading "test_suite_"
119 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
120 list(APPEND config_generated_data_names ${generated_data_name})
121 endforeach()
David Horstmannae7bd352021-10-19 19:05:42 +0100122
Ronald Cronf76cd222024-07-09 14:24:35 +0200123 if(";${config_generated_data_names};" MATCHES ";${data_name};")
Gilles Peskineef822c12024-05-28 19:07:24 +0200124 set(data_file
125 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
Ronald Cron7f410db2024-07-09 14:20:34 +0200126 set(dependency test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200127 else()
128 set(data_file
129 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
Gilles Peskineef822c12024-05-28 19:07:24 +0200130 set(dependency
Ronald Cronf76cd222024-07-09 14:24:35 +0200131 test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200132 endif()
133
Paul Bakker367dae42009-06-28 21:50:27 +0000134 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200135 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +0100136 # The output filename of generate_test_code.py is derived from the -d
137 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200138 test_suite_${data_name}.c
139 COMMAND
140 ${MBEDTLS_PYTHON_EXECUTABLE}
David Horstmannf6f3bca2024-05-29 17:57:08 +0100141 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200142 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200143 -d ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200144 -t ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function
145 -p ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200146 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
Ronald Cronf76cd222024-07-09 14:24:35 +0200147 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200148 -o .
149 DEPENDS
David Horstmannf6f3bca2024-05-29 17:57:08 +0100150 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200151 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200152 ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200153 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/main_test.function
154 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/host_test.function
155 ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200156 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200157 BYPRODUCTS
158 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000159 )
Paul Bakker19343182013-08-16 13:31:10 +0200160
Yanray Wangb458b8c2022-10-28 11:49:33 +0800161 add_executable(test_suite_${data_name} test_suite_${data_name}.c
162 $<TARGET_OBJECTS:mbedtls_test>
163 $<TARGET_OBJECTS:mbedtls_test_helpers>)
Werner Lewis383461c2022-08-23 11:29:05 +0100164 add_dependencies(test_suite_${data_name} ${dependency})
Paul Bakker73043762011-07-13 15:03:10 +0000165 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200166 # Include test-specific header files from ./include and private header
167 # files (used by some invasive tests) from ../library. Public header
168 # files are automatically included because the library targets declare
169 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +0200170 target_include_directories(test_suite_${data_name}
171 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Ronald Cronc29afb62024-07-01 14:50:54 +0200172 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library
173 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/core
174 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/drivers/builtin/src)
David Horstmannc2ab3982024-01-17 12:25:19 +0000175 # Request C11, which is needed for memory poisoning tests
176 set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11)
Ronald Cron85527412020-05-15 17:20:47 +0200177
Gilles Peskineac372cc2018-11-29 10:15:06 +0000178 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
179 message(STATUS "The test suite ${data_name} will not be executed.")
180 else()
181 add_test(${data_name}-suite test_suite_${data_name} --verbose)
182 endif()
Paul Bakker367dae42009-06-28 21:50:27 +0000183endfunction(add_test_suite)
184
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200185# Enable definition of various functions used throughout the testsuite
186# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
187# on non-POSIX platforms.
188add_definitions("-D_POSIX_C_SOURCE=200809L")
189
Gilles Peskinef29019f2021-02-23 13:44:41 +0100190if(CMAKE_COMPILER_IS_CLANG)
191 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
192endif(CMAKE_COMPILER_IS_CLANG)
193
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200194if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000195 # If a warning level has been defined, suppress all warnings for test code
196 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
197 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200198endif(MSVC)
199
Gilles Peskine29080e82022-01-25 22:30:25 +0100200file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200201list(APPEND test_suites ${all_generated_data_files})
Gilles Peskine29080e82022-01-25 22:30:25 +0100202# If the generated .data files are present in the source tree, we just added
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200203# them twice, both through GLOB and through ${all_generated_data_files}.
Gilles Peskine29080e82022-01-25 22:30:25 +0100204list(REMOVE_DUPLICATES test_suites)
205list(SORT test_suites)
206foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100207 get_filename_component(data_name ${test_suite} NAME)
Gilles Peskinea18fb9d2022-01-25 23:24:34 +0100208 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100209 string(REPLACE "test_suite_" "" data_name "${data_name}")
210 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100211 add_test_suite(${function_name} ${data_name})
212endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100213
Gilles Peskine84052572018-03-21 12:12:47 +0100214# Make scripts and data files needed for testing available in an
215# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100216if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400217 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
218 link_to_source(seedfile)
219 endif()
Gilles Peskined7db8832022-11-30 17:52:54 +0100220 link_to_source(Descriptions.txt)
Gilles Peskine84052572018-03-21 12:12:47 +0100221 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600222 link_to_source(context-info.sh)
David Horstmanndcf18dd2024-06-11 17:44:00 +0100223 link_to_source(../framework/data_files)
Gilles Peskine84052572018-03-21 12:12:47 +0100224 link_to_source(scripts)
225 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800226 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100227endif()