blob: d1338ea66f7de4a43d18e03d877882f090646b4f [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 )
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020067else()
Gilles Peskinecca6ce82022-09-18 23:08:38 +020068 foreach(file ${all_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010069 link_to_source(${file})
70 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020071endif()
Jerry Yu3b86d1b2022-03-02 10:40:41 +080072# 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 Peskineef822c12024-05-28 19:07:24 +020077add_custom_target(test_suite_config_generated_data DEPENDS ${config_generated_data_files})
David Horstmann38610622022-11-28 10:18:05 +000078# If SKIP_TEST_SUITES is not defined with -D, get it from the environment.
79if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES}))
80 set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES})
81endif()
Gilles Peskineac372cc2018-11-29 10:15:06 +000082# 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".
85string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
86string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
87set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
88
Paul Bakker73043762011-07-13 15:03:10 +000089function(add_test_suite suite_name)
90 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020091 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +000092 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020093 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +000094 endif()
95
David Horstmannae7bd352021-10-19 19:05:42 +010096 # Get the test names of the tests with generated .data files
97 # from the generated_data_files list in parent scope.
Ronald Cron7f410db2024-07-09 14:20:34 +020098 set(config_generated_data_names "")
Ronald Cron7f410db2024-07-09 14:20:34 +020099 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 Horstmannae7bd352021-10-19 19:05:42 +0100108
Ronald Cronf76cd222024-07-09 14:24:35 +0200109 if(";${config_generated_data_names};" MATCHES ";${data_name};")
Gilles Peskineef822c12024-05-28 19:07:24 +0200110 set(data_file
111 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
Ronald Cron7f410db2024-07-09 14:20:34 +0200112 set(dependency test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200113 else()
114 set(data_file
115 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
Gilles Peskineef822c12024-05-28 19:07:24 +0200116 set(dependency
Ronald Cronf76cd222024-07-09 14:24:35 +0200117 test_suite_config_generated_data)
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200118 endif()
119
Paul Bakker367dae42009-06-28 21:50:27 +0000120 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200121 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +0100122 # The output filename of generate_test_code.py is derived from the -d
123 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200124 test_suite_${data_name}.c
125 COMMAND
126 ${MBEDTLS_PYTHON_EXECUTABLE}
David Horstmannf6f3bca2024-05-29 17:57:08 +0100127 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200128 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200129 -d ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200130 -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é-Gonnard42681f32021-05-14 10:53:43 +0200132 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
Ronald Cronf76cd222024-07-09 14:24:35 +0200133 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/../tf-psa-crypto/tests/suites/helpers.function
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200134 -o .
135 DEPENDS
David Horstmannf6f3bca2024-05-29 17:57:08 +0100136 ${CMAKE_CURRENT_SOURCE_DIR}/../framework/scripts/generate_test_code.py
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200137 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200138 ${data_file}
Ronald Cronf76cd222024-07-09 14:24:35 +0200139 ${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é-Gonnard42681f32021-05-14 10:53:43 +0200142 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200143 BYPRODUCTS
144 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000145 )
Paul Bakker19343182013-08-16 13:31:10 +0200146
Yanray Wangb458b8c2022-10-28 11:49:33 +0800147 add_executable(test_suite_${data_name} test_suite_${data_name}.c
148 $<TARGET_OBJECTS:mbedtls_test>
149 $<TARGET_OBJECTS:mbedtls_test_helpers>)
Werner Lewis383461c2022-08-23 11:29:05 +0100150 add_dependencies(test_suite_${data_name} ${dependency})
Paul Bakker73043762011-07-13 15:03:10 +0000151 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200152 # 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 Cron85527412020-05-15 17:20:47 +0200156 target_include_directories(test_suite_${data_name}
157 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
Ronald Cronc29afb62024-07-01 14:50:54 +0200158 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 Horstmannc2ab3982024-01-17 12:25:19 +0000161 # Request C11, which is needed for memory poisoning tests
162 set_target_properties(test_suite_${data_name} PROPERTIES C_STANDARD 11)
Ronald Cron85527412020-05-15 17:20:47 +0200163
Gilles Peskineac372cc2018-11-29 10:15:06 +0000164 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 Bakker367dae42009-06-28 21:50:27 +0000169endfunction(add_test_suite)
170
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200171# 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.
174add_definitions("-D_POSIX_C_SOURCE=200809L")
175
Gilles Peskinef29019f2021-02-23 13:44:41 +0100176if(CMAKE_COMPILER_IS_CLANG)
177 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
178endif(CMAKE_COMPILER_IS_CLANG)
179
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200180if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000181 # 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é-Gonnard30830532015-07-01 17:06:28 +0200184endif(MSVC)
185
Gilles Peskine29080e82022-01-25 22:30:25 +0100186file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200187list(APPEND test_suites ${all_generated_data_files})
Gilles Peskine29080e82022-01-25 22:30:25 +0100188# If the generated .data files are present in the source tree, we just added
Gilles Peskinecca6ce82022-09-18 23:08:38 +0200189# them twice, both through GLOB and through ${all_generated_data_files}.
Gilles Peskine29080e82022-01-25 22:30:25 +0100190list(REMOVE_DUPLICATES test_suites)
191list(SORT test_suites)
192foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100193 get_filename_component(data_name ${test_suite} NAME)
Gilles Peskinea18fb9d2022-01-25 23:24:34 +0100194 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100195 string(REPLACE "test_suite_" "" data_name "${data_name}")
196 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100197 add_test_suite(${function_name} ${data_name})
198endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100199
Gilles Peskine84052572018-03-21 12:12:47 +0100200# Make scripts and data files needed for testing available in an
201# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100202if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400203 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
204 link_to_source(seedfile)
205 endif()
Gilles Peskined7db8832022-11-30 17:52:54 +0100206 link_to_source(Descriptions.txt)
Gilles Peskine84052572018-03-21 12:12:47 +0100207 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600208 link_to_source(context-info.sh)
David Horstmanndcf18dd2024-06-11 17:44:00 +0100209 link_to_source(../framework/data_files)
Gilles Peskine84052572018-03-21 12:12:47 +0100210 link_to_source(scripts)
211 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800212 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100213endif()