blob: dcc5de0ffb237c61dce730172d24c5f34f131740 [file] [log] [blame]
Paul Bakkerb06819b2011-01-18 16:18:38 +00001set(libs
Raef Coles995c66f2020-10-13 16:30:41 +01002 ${mbedtls_target}
Paul Bakkerb06819b2011-01-18 16:18:38 +00003)
4
Ashley Duncand85a7e92019-04-29 20:35:06 +12005# 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.
8if(NOT DEFINED MBEDTLS_DIR)
9 set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
10endif()
11
okhowang(王沛文)3c1b0902020-03-25 19:55:32 +080012if(NOT MBEDTLS_PYTHON_EXECUTABLE)
13 message(FATAL_ERROR "Cannot build test suites without Python 3")
Nicholas Wilson733676b2015-11-14 13:09:01 +000014endif()
15
Manuel Pégourié-Gonnard313bcfc2021-09-07 12:16:49 +020016# generated .data files will go there
17file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/suites)
18
David Horstmann7570d242021-10-20 16:27:24 +010019# Get base names for generated files (starting at "suites/")
David Horstmann1732b5d2021-10-19 16:43:53 +010020execute_process(
21 COMMAND
David Horstmann3e30ad92021-10-20 16:53:58 +010022 ${MBEDTLS_PYTHON_EXECUTABLE}
Werner Lewis8b2df742022-07-08 13:54:57 +010023 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py
24 --list-for-cmake
25 --directory suites
26 WORKING_DIRECTORY
27 ${CMAKE_CURRENT_SOURCE_DIR}/..
28 OUTPUT_VARIABLE
29 bignum_generated_data_files)
30
31execute_process(
32 COMMAND
33 ${MBEDTLS_PYTHON_EXECUTABLE}
David Horstmann1732b5d2021-10-19 16:43:53 +010034 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py
35 --list-for-cmake
David Horstmann7570d242021-10-20 16:27:24 +010036 --directory suites
David Horstmann1732b5d2021-10-19 16:43:53 +010037 WORKING_DIRECTORY
38 ${CMAKE_CURRENT_SOURCE_DIR}/..
39 OUTPUT_VARIABLE
Werner Lewis8b2df742022-07-08 13:54:57 +010040 psa_generated_data_files)
David Horstmann7570d242021-10-20 16:27:24 +010041
42# Derive generated file paths in the build directory
Werner Lewis75ef9442022-07-21 16:57:22 +010043set(base_generated_data_files ${bignum_generated_data_files} ${psa_generated_data_files})
David Horstmann7570d242021-10-20 16:27:24 +010044set(generated_data_files "")
Werner Lewis75ef9442022-07-21 16:57:22 +010045foreach(file ${base_generated_data_files})
David Horstmann7570d242021-10-20 16:27:24 +010046 list(APPEND generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/${file})
47endforeach()
David Horstmann1732b5d2021-10-19 16:43:53 +010048
David Horstmanna8d14062021-10-20 17:14:23 +010049if(GEN_FILES)
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020050 add_custom_command(
51 OUTPUT
David Horstmannae7bd352021-10-19 19:05:42 +010052 ${generated_data_files}
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020053 WORKING_DIRECTORY
54 ${CMAKE_CURRENT_SOURCE_DIR}/..
55 COMMAND
56 ${MBEDTLS_PYTHON_EXECUTABLE}
57 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py
58 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites
Werner Lewis8b2df742022-07-08 13:54:57 +010059 COMMAND
60 ${MBEDTLS_PYTHON_EXECUTABLE}
61 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py
62 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020063 DEPENDS
64 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py
Werner Lewis8b2df742022-07-08 13:54:57 +010065 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py
Gilles Peskine26f90542022-03-15 16:39:51 +010066 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/crypto_knowledge.py
67 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/macro_collector.py
68 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/psa_storage.py
69 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020070 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_config.h
71 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_values.h
72 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_extra.h
73 )
Jerry Yu80bc8602022-01-17 18:26:52 +080074
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020075else()
David Horstmann7570d242021-10-20 16:27:24 +010076 foreach(file ${base_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010077 link_to_source(${file})
78 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020079endif()
Jerry Yu3b86d1b2022-03-02 10:40:41 +080080# CMake generates sub-makefiles for each target and calls them in subprocesses.
81# Without this command, cmake will generate rules in each sub-makefile. As a result,
82# they can cause race conditions in parallel builds.
83# With this line, only 4 sub-makefiles include the above command, that reduces
84# the risk of a race.
Jerry Yu80bc8602022-01-17 18:26:52 +080085add_custom_target(test_suite_generated_data DEPENDS ${generated_data_files})
Gilles Peskineac372cc2018-11-29 10:15:06 +000086# Test suites caught by SKIP_TEST_SUITES are built but not executed.
87# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
88# but not "test_suite_foobar".
89string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
90string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
91set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
92
Paul Bakker73043762011-07-13 15:03:10 +000093function(add_test_suite suite_name)
94 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020095 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +000096 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020097 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +000098 endif()
99
David Horstmannae7bd352021-10-19 19:05:42 +0100100 # Get the test names of the tests with generated .data files
101 # from the generated_data_files list in parent scope.
102 set(generated_data_names "")
103 foreach(generated_data_file ${generated_data_files})
104 # Get the plain filename
105 get_filename_component(generated_data_name ${generated_data_file} NAME)
106 # Remove the ".data" extension
107 get_name_without_last_ext(generated_data_name ${generated_data_name})
108 # Remove leading "test_suite_"
109 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
110 list(APPEND generated_data_names ${generated_data_name})
111 endforeach()
112
113 if(";${generated_data_names};" MATCHES ";${data_name};")
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200114 set(data_file
115 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
116 else()
117 set(data_file
118 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
119 endif()
120
Paul Bakker367dae42009-06-28 21:50:27 +0000121 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200122 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +0100123 # The output filename of generate_test_code.py is derived from the -d
124 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200125 test_suite_${data_name}.c
126 COMMAND
127 ${MBEDTLS_PYTHON_EXECUTABLE}
128 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
129 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200130 -d ${data_file}
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200131 -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
132 -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
133 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
134 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
135 -o .
136 DEPENDS
137 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
138 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200139 ${data_file}
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200140 ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
141 ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
142 ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
143 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200144 BYPRODUCTS
145 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000146 )
Paul Bakker19343182013-08-16 13:31:10 +0200147
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200148 add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>)
Jerry Yu80bc8602022-01-17 18:26:52 +0800149 add_dependencies(test_suite_${data_name} test_suite_generated_data)
Paul Bakker73043762011-07-13 15:03:10 +0000150 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200151 # Include test-specific header files from ./include and private header
152 # files (used by some invasive tests) from ../library. Public header
153 # files are automatically included because the library targets declare
154 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +0200155 target_include_directories(test_suite_${data_name}
156 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
157 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
158
Gilles Peskineac372cc2018-11-29 10:15:06 +0000159 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
160 message(STATUS "The test suite ${data_name} will not be executed.")
161 else()
162 add_test(${data_name}-suite test_suite_${data_name} --verbose)
163 endif()
Paul Bakker367dae42009-06-28 21:50:27 +0000164endfunction(add_test_suite)
165
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200166# Enable definition of various functions used throughout the testsuite
167# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
168# on non-POSIX platforms.
169add_definitions("-D_POSIX_C_SOURCE=200809L")
170
Gilles Peskinef29019f2021-02-23 13:44:41 +0100171if(CMAKE_COMPILER_IS_CLANG)
172 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
173endif(CMAKE_COMPILER_IS_CLANG)
174
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200175if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000176 # If a warning level has been defined, suppress all warnings for test code
177 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
178 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200179endif(MSVC)
180
Gilles Peskine29080e82022-01-25 22:30:25 +0100181file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
182list(APPEND test_suites ${base_generated_data_files})
183# If the generated .data files are present in the source tree, we just added
184# them twice, both through GLOB and through ${base_generated_data_files}.
185list(REMOVE_DUPLICATES test_suites)
186list(SORT test_suites)
187foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100188 get_filename_component(data_name ${test_suite} NAME)
Gilles Peskinea18fb9d2022-01-25 23:24:34 +0100189 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100190 string(REPLACE "test_suite_" "" data_name "${data_name}")
191 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100192 add_test_suite(${function_name} ${data_name})
193endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100194
Gilles Peskine84052572018-03-21 12:12:47 +0100195# Make scripts and data files needed for testing available in an
196# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100197if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400198 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
199 link_to_source(seedfile)
200 endif()
Gilles Peskine84052572018-03-21 12:12:47 +0100201 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600202 link_to_source(context-info.sh)
Gilles Peskine84052572018-03-21 12:12:47 +0100203 link_to_source(data_files)
204 link_to_source(scripts)
205 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800206 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100207endif()