1set(libs 2 ${mbedtls_target} 3) 4 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. 8if(NOT DEFINED MBEDTLS_DIR) 9 set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR}) 10endif() 11 12if(NOT MBEDTLS_PYTHON_EXECUTABLE) 13 message(FATAL_ERROR "Cannot build test suites without Python 3") 14endif() 15 16# generated .data files will go there 17file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/suites) 18 19# Get base names for generated files 20execute_process( 21 COMMAND 22 ${MBEDTLS_PYTHON_EXECUTABLE} 23 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py 24 --list-for-cmake 25 WORKING_DIRECTORY 26 ${CMAKE_CURRENT_SOURCE_DIR}/.. 27 OUTPUT_VARIABLE 28 base_bignum_generated_data_files) 29string(REGEX REPLACE "[^;]*/" "" 30 base_bignum_generated_data_files "${base_bignum_generated_data_files}") 31 32execute_process( 33 COMMAND 34 ${MBEDTLS_PYTHON_EXECUTABLE} 35 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_ecp_tests.py 36 --list-for-cmake 37 WORKING_DIRECTORY 38 ${CMAKE_CURRENT_SOURCE_DIR}/.. 39 OUTPUT_VARIABLE 40 base_ecp_generated_data_files) 41string(REGEX REPLACE "[^;]*/" "" 42 base_ecp_generated_data_files "${base_ecp_generated_data_files}") 43 44execute_process( 45 COMMAND 46 ${MBEDTLS_PYTHON_EXECUTABLE} 47 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py 48 --list-for-cmake 49 WORKING_DIRECTORY 50 ${CMAKE_CURRENT_SOURCE_DIR}/.. 51 OUTPUT_VARIABLE 52 base_psa_generated_data_files) 53string(REGEX REPLACE "[^;]*/" "" 54 base_psa_generated_data_files "${base_psa_generated_data_files}") 55 56# Derive generated file paths in the build directory. The generated data 57# files go into the suites/ subdirectory. 58set(base_generated_data_files 59 ${base_bignum_generated_data_files} ${base_ecp_generated_data_files} ${base_psa_generated_data_files}) 60string(REGEX REPLACE "([^;]+)" "suites/\\1" 61 all_generated_data_files "${base_generated_data_files}") 62set(bignum_generated_data_files "") 63set(ecp_generated_data_files "") 64set(psa_generated_data_files "") 65foreach(file ${base_bignum_generated_data_files}) 66 list(APPEND bignum_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) 67endforeach() 68foreach(file ${base_ecp_generated_data_files}) 69 list(APPEND ecp_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) 70endforeach() 71foreach(file ${base_psa_generated_data_files}) 72 list(APPEND psa_generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/suites/${file}) 73endforeach() 74 75if(GEN_FILES) 76 add_custom_command( 77 OUTPUT 78 ${bignum_generated_data_files} 79 WORKING_DIRECTORY 80 ${CMAKE_CURRENT_SOURCE_DIR}/.. 81 COMMAND 82 ${MBEDTLS_PYTHON_EXECUTABLE} 83 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py 84 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites 85 DEPENDS 86 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_bignum_tests.py 87 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_common.py 88 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_core.py 89 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod_raw.py 90 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_mod.py 91 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py 92 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py 93 ) 94 add_custom_command( 95 OUTPUT 96 ${ecp_generated_data_files} 97 WORKING_DIRECTORY 98 ${CMAKE_CURRENT_SOURCE_DIR}/.. 99 COMMAND 100 ${MBEDTLS_PYTHON_EXECUTABLE} 101 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_ecp_tests.py 102 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites 103 DEPENDS 104 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_ecp_tests.py 105 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/bignum_common.py 106 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/ecp.py 107 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py 108 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py 109 ) 110 add_custom_command( 111 OUTPUT 112 ${psa_generated_data_files} 113 WORKING_DIRECTORY 114 ${CMAKE_CURRENT_SOURCE_DIR}/.. 115 COMMAND 116 ${MBEDTLS_PYTHON_EXECUTABLE} 117 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py 118 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites 119 DEPENDS 120 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py 121 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/crypto_data_tests.py 122 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/crypto_knowledge.py 123 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/macro_collector.py 124 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/psa_information.py 125 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/psa_storage.py 126 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py 127 ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py 128 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_config.h 129 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_values.h 130 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_extra.h 131 ) 132 133else() 134 foreach(file ${all_generated_data_files}) 135 link_to_source(${file}) 136 endforeach() 137endif() 138# CMake generates sub-makefiles for each target and calls them in subprocesses. 139# Without this command, cmake will generate rules in each sub-makefile. As a result, 140# they can cause race conditions in parallel builds. 141# With this line, only 4 sub-makefiles include the above command, that reduces 142# the risk of a race. 143add_custom_target(test_suite_bignum_generated_data DEPENDS ${bignum_generated_data_files}) 144add_custom_target(test_suite_ecp_generated_data DEPENDS ${ecp_generated_data_files}) 145add_custom_target(test_suite_psa_generated_data DEPENDS ${psa_generated_data_files}) 146# If SKIP_TEST_SUITES is not defined with -D, get it from the environment. 147if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES})) 148 set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES}) 149endif() 150# Test suites caught by SKIP_TEST_SUITES are built but not executed. 151# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar" 152# but not "test_suite_foobar". 153string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}") 154string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}") 155set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)") 156 157function(add_test_suite suite_name) 158 if(ARGV1) 159 set(data_name ${ARGV1}) 160 else() 161 set(data_name ${suite_name}) 162 endif() 163 164 # Get the test names of the tests with generated .data files 165 # from the generated_data_files list in parent scope. 166 set(bignum_generated_data_names "") 167 set(ecp_generated_data_names "") 168 set(psa_generated_data_names "") 169 foreach(generated_data_file ${bignum_generated_data_files}) 170 # Get the plain filename 171 get_filename_component(generated_data_name ${generated_data_file} NAME) 172 # Remove the ".data" extension 173 get_name_without_last_ext(generated_data_name ${generated_data_name}) 174 # Remove leading "test_suite_" 175 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name) 176 list(APPEND bignum_generated_data_names ${generated_data_name}) 177 endforeach() 178 foreach(generated_data_file ${ecp_generated_data_files}) 179 # Get the plain filename 180 get_filename_component(generated_data_name ${generated_data_file} NAME) 181 # Remove the ".data" extension 182 get_name_without_last_ext(generated_data_name ${generated_data_name}) 183 # Remove leading "test_suite_" 184 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name) 185 list(APPEND ecp_generated_data_names ${generated_data_name}) 186 endforeach() 187 foreach(generated_data_file ${psa_generated_data_files}) 188 # Get the plain filename 189 get_filename_component(generated_data_name ${generated_data_file} NAME) 190 # Remove the ".data" extension 191 get_name_without_last_ext(generated_data_name ${generated_data_name}) 192 # Remove leading "test_suite_" 193 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name) 194 list(APPEND psa_generated_data_names ${generated_data_name}) 195 endforeach() 196 197 if(";${bignum_generated_data_names};" MATCHES ";${data_name};") 198 set(data_file 199 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data) 200 set(dependency test_suite_bignum_generated_data) 201 elseif(";${ecp_generated_data_names};" MATCHES ";${data_name};") 202 set(data_file 203 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data) 204 set(dependency test_suite_ecp_generated_data) 205 elseif(";${psa_generated_data_names};" MATCHES ";${data_name};") 206 set(data_file 207 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data) 208 set(dependency test_suite_psa_generated_data) 209 else() 210 set(data_file 211 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data) 212 set(dependency test_suite_bignum_generated_data test_suite_ecp_generated_data test_suite_psa_generated_data) 213 endif() 214 215 add_custom_command( 216 OUTPUT 217 # The output filename of generate_test_code.py is derived from the -d 218 # input argument. 219 test_suite_${data_name}.c 220 COMMAND 221 ${MBEDTLS_PYTHON_EXECUTABLE} 222 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py 223 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function 224 -d ${data_file} 225 -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function 226 -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function 227 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites 228 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function 229 -o . 230 DEPENDS 231 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py 232 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function 233 ${data_file} 234 ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function 235 ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function 236 ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function 237 ${mbedtls_target} 238 BYPRODUCTS 239 test_suite_${data_name}.datax 240 ) 241 242 add_executable(test_suite_${data_name} test_suite_${data_name}.c 243 $<TARGET_OBJECTS:mbedtls_test> 244 $<TARGET_OBJECTS:mbedtls_test_helpers>) 245 add_dependencies(test_suite_${data_name} ${dependency}) 246 target_link_libraries(test_suite_${data_name} ${libs}) 247 # Include test-specific header files from ./include and private header 248 # files (used by some invasive tests) from ../library. Public header 249 # files are automatically included because the library targets declare 250 # them as PUBLIC. 251 target_include_directories(test_suite_${data_name} 252 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include 253 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library) 254 255 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) 256 message(STATUS "The test suite ${data_name} will not be executed.") 257 else() 258 add_test(${data_name}-suite test_suite_${data_name} --verbose) 259 endif() 260endfunction(add_test_suite) 261 262# Enable definition of various functions used throughout the testsuite 263# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless 264# on non-POSIX platforms. 265add_definitions("-D_POSIX_C_SOURCE=200809L") 266 267if(CMAKE_COMPILER_IS_CLANG) 268 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") 269endif(CMAKE_COMPILER_IS_CLANG) 270 271if(MSVC) 272 # If a warning level has been defined, suppress all warnings for test code 273 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0") 274 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-") 275endif(MSVC) 276 277file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data) 278list(APPEND test_suites ${all_generated_data_files}) 279# If the generated .data files are present in the source tree, we just added 280# them twice, both through GLOB and through ${all_generated_data_files}. 281list(REMOVE_DUPLICATES test_suites) 282list(SORT test_suites) 283foreach(test_suite ${test_suites}) 284 get_filename_component(data_name ${test_suite} NAME) 285 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}") 286 string(REPLACE "test_suite_" "" data_name "${data_name}") 287 string(REGEX MATCH "[^.]*" function_name "${data_name}") 288 add_test_suite(${function_name} ${data_name}) 289endforeach(test_suite) 290 291# Make scripts and data files needed for testing available in an 292# out-of-source build. 293if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) 294 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile") 295 link_to_source(seedfile) 296 endif() 297 link_to_source(Descriptions.txt) 298 link_to_source(compat.sh) 299 link_to_source(context-info.sh) 300 link_to_source(data_files) 301 link_to_source(scripts) 302 link_to_source(ssl-opt.sh) 303 link_to_source(opt-testcases) 304endif() 305