blob: 2f5426469b4cf0da022279bf68a9e45a7ff97a12 [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}
David Horstmann1732b5d2021-10-19 16:43:53 +010023 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py
24 --list-for-cmake
David Horstmann7570d242021-10-20 16:27:24 +010025 --directory suites
David Horstmann1732b5d2021-10-19 16:43:53 +010026 WORKING_DIRECTORY
27 ${CMAKE_CURRENT_SOURCE_DIR}/..
28 OUTPUT_VARIABLE
David Horstmann7570d242021-10-20 16:27:24 +010029 base_generated_data_files)
30
31# Derive generated file paths in the build directory
32set(generated_data_files "")
33foreach(file ${base_generated_data_files})
34 list(APPEND generated_data_files ${CMAKE_CURRENT_BINARY_DIR}/${file})
35endforeach()
David Horstmann1732b5d2021-10-19 16:43:53 +010036
David Horstmanna8d14062021-10-20 17:14:23 +010037if(GEN_FILES)
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020038 add_custom_command(
39 OUTPUT
David Horstmannae7bd352021-10-19 19:05:42 +010040 ${generated_data_files}
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020041 WORKING_DIRECTORY
42 ${CMAKE_CURRENT_SOURCE_DIR}/..
43 COMMAND
44 ${MBEDTLS_PYTHON_EXECUTABLE}
45 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py
46 --directory ${CMAKE_CURRENT_BINARY_DIR}/suites
47 DEPENDS
48 ${CMAKE_CURRENT_SOURCE_DIR}/../tests/scripts/generate_psa_tests.py
49 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_config.h
50 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_values.h
51 ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_extra.h
52 )
Jerry Yu89e76542022-01-13 16:45:33 +080053 # CMake generate sub-makefile for each target and call it in subprocess. Without
54 # this command, cmake will generate ruler in each sub makefile. As a result,
55 # they will conflict under parllel build.
56 # With this line, only 4 sub-makefiles include above command, that reduces
57 # conflict risk.
58 add_custom_target(test_suite_generated_data DEPENDS ${generated_data_files})
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020059else()
David Horstmann7570d242021-10-20 16:27:24 +010060 foreach(file ${base_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010061 link_to_source(${file})
62 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020063endif()
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +020064
Gilles Peskineac372cc2018-11-29 10:15:06 +000065# Test suites caught by SKIP_TEST_SUITES are built but not executed.
66# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
67# but not "test_suite_foobar".
68string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
69string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
70set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
71
Paul Bakker73043762011-07-13 15:03:10 +000072function(add_test_suite suite_name)
73 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020074 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +000075 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020076 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +000077 endif()
78
David Horstmannae7bd352021-10-19 19:05:42 +010079 # Get the test names of the tests with generated .data files
80 # from the generated_data_files list in parent scope.
81 set(generated_data_names "")
82 foreach(generated_data_file ${generated_data_files})
83 # Get the plain filename
84 get_filename_component(generated_data_name ${generated_data_file} NAME)
85 # Remove the ".data" extension
86 get_name_without_last_ext(generated_data_name ${generated_data_name})
87 # Remove leading "test_suite_"
88 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
89 list(APPEND generated_data_names ${generated_data_name})
90 endforeach()
91
92 if(";${generated_data_names};" MATCHES ";${data_name};")
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +020093 set(data_file
94 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
95 else()
96 set(data_file
97 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
98 endif()
99
Paul Bakker367dae42009-06-28 21:50:27 +0000100 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200101 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +0100102 # The output filename of generate_test_code.py is derived from the -d
103 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200104 test_suite_${data_name}.c
105 COMMAND
106 ${MBEDTLS_PYTHON_EXECUTABLE}
107 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
108 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200109 -d ${data_file}
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200110 -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
111 -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
112 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
113 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
114 -o .
115 DEPENDS
116 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
117 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200118 ${data_file}
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200119 ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
120 ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
121 ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
122 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200123 BYPRODUCTS
124 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000125 )
Paul Bakker19343182013-08-16 13:31:10 +0200126
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200127 add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>)
Paul Bakker73043762011-07-13 15:03:10 +0000128 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200129 # Include test-specific header files from ./include and private header
130 # files (used by some invasive tests) from ../library. Public header
131 # files are automatically included because the library targets declare
132 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +0200133 target_include_directories(test_suite_${data_name}
134 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
135 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
136
Gilles Peskineac372cc2018-11-29 10:15:06 +0000137 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
138 message(STATUS "The test suite ${data_name} will not be executed.")
139 else()
140 add_test(${data_name}-suite test_suite_${data_name} --verbose)
141 endif()
Paul Bakker367dae42009-06-28 21:50:27 +0000142endfunction(add_test_suite)
143
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200144# Enable definition of various functions used throughout the testsuite
145# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
146# on non-POSIX platforms.
147add_definitions("-D_POSIX_C_SOURCE=200809L")
148
Gilles Peskinef29019f2021-02-23 13:44:41 +0100149if(CMAKE_COMPILER_IS_CLANG)
150 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
151endif(CMAKE_COMPILER_IS_CLANG)
152
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200153if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000154 # If a warning level has been defined, suppress all warnings for test code
155 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
156 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200157endif(MSVC)
158
Gilles Peskine29080e82022-01-25 22:30:25 +0100159file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
160list(APPEND test_suites ${base_generated_data_files})
161# If the generated .data files are present in the source tree, we just added
162# them twice, both through GLOB and through ${base_generated_data_files}.
163list(REMOVE_DUPLICATES test_suites)
164list(SORT test_suites)
165foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100166 get_filename_component(data_name ${test_suite} NAME)
Gilles Peskinea18fb9d2022-01-25 23:24:34 +0100167 string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100168 string(REPLACE "test_suite_" "" data_name "${data_name}")
169 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100170 add_test_suite(${function_name} ${data_name})
171endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100172
Gilles Peskine84052572018-03-21 12:12:47 +0100173# Make scripts and data files needed for testing available in an
174# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100175if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400176 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
177 link_to_source(seedfile)
178 endif()
Gilles Peskine84052572018-03-21 12:12:47 +0100179 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600180 link_to_source(context-info.sh)
Gilles Peskine84052572018-03-21 12:12:47 +0100181 link_to_source(data_files)
182 link_to_source(scripts)
183 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800184 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100185endif()