blob: b3308cbb3230a1410f2171f8c58b22f6f68d0f92 [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 )
53else()
David Horstmann7570d242021-10-20 16:27:24 +010054 foreach(file ${base_generated_data_files})
David Horstmann1732b5d2021-10-19 16:43:53 +010055 link_to_source(${file})
56 endforeach()
Manuel Pégourié-Gonnarde90e4052021-09-08 13:27:09 +020057endif()
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +020058
Gilles Peskineac372cc2018-11-29 10:15:06 +000059# Test suites caught by SKIP_TEST_SUITES are built but not executed.
60# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
61# but not "test_suite_foobar".
62string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
63string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
64set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
65
Paul Bakker73043762011-07-13 15:03:10 +000066function(add_test_suite suite_name)
67 if(ARGV1)
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020068 set(data_name ${ARGV1})
Paul Bakker73043762011-07-13 15:03:10 +000069 else()
Manuel Pégourié-Gonnard216a1832015-06-25 09:20:03 +020070 set(data_name ${suite_name})
Paul Bakker46c17942011-07-13 14:54:54 +000071 endif()
72
David Horstmannae7bd352021-10-19 19:05:42 +010073 # Get the test names of the tests with generated .data files
74 # from the generated_data_files list in parent scope.
75 set(generated_data_names "")
76 foreach(generated_data_file ${generated_data_files})
77 # Get the plain filename
78 get_filename_component(generated_data_name ${generated_data_file} NAME)
79 # Remove the ".data" extension
80 get_name_without_last_ext(generated_data_name ${generated_data_name})
81 # Remove leading "test_suite_"
82 string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
83 list(APPEND generated_data_names ${generated_data_name})
84 endforeach()
85
86 if(";${generated_data_names};" MATCHES ";${data_name};")
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +020087 set(data_file
88 ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
89 else()
90 set(data_file
91 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
92 endif()
93
Paul Bakker367dae42009-06-28 21:50:27 +000094 add_custom_command(
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +020095 OUTPUT
David Horstmannf602eb12021-10-20 16:40:56 +010096 # The output filename of generate_test_code.py is derived from the -d
97 # input argument.
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +020098 test_suite_${data_name}.c
99 COMMAND
100 ${MBEDTLS_PYTHON_EXECUTABLE}
101 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
102 -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200103 -d ${data_file}
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200104 -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
105 -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
106 -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
107 --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
108 -o .
109 DEPENDS
110 ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
111 ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
Manuel Pégourié-Gonnardce3ba8f2021-05-14 12:03:37 +0200112 ${data_file}
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200113 ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
114 ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
115 ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
116 ${mbedtls_target}
Manuel Pégourié-Gonnard389150d2021-09-09 10:51:16 +0200117 BYPRODUCTS
118 test_suite_${data_name}.datax
Paul Bakker4c14a252010-06-18 22:54:05 +0000119 )
Paul Bakker19343182013-08-16 13:31:10 +0200120
Ronald Cronf5ea29a2020-06-19 10:42:29 +0200121 add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>)
Paul Bakker73043762011-07-13 15:03:10 +0000122 target_link_libraries(test_suite_${data_name} ${libs})
Gilles Peskine76dd3aa2020-07-02 15:58:37 +0200123 # Include test-specific header files from ./include and private header
124 # files (used by some invasive tests) from ../library. Public header
125 # files are automatically included because the library targets declare
126 # them as PUBLIC.
Ronald Cron85527412020-05-15 17:20:47 +0200127 target_include_directories(test_suite_${data_name}
128 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
129 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
130
Gilles Peskineac372cc2018-11-29 10:15:06 +0000131 if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
132 message(STATUS "The test suite ${data_name} will not be executed.")
133 else()
134 add_test(${data_name}-suite test_suite_${data_name} --verbose)
135 endif()
Paul Bakker367dae42009-06-28 21:50:27 +0000136endfunction(add_test_suite)
137
Manuel Pégourié-Gonnard42681f32021-05-14 10:53:43 +0200138# Enable definition of various functions used throughout the testsuite
139# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
140# on non-POSIX platforms.
141add_definitions("-D_POSIX_C_SOURCE=200809L")
142
Manuel Pégourié-Gonnard7e2d68c2015-07-01 13:41:35 +0200143if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
144 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
145endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
Paul Bakker367dae42009-06-28 21:50:27 +0000146
Gilles Peskinef29019f2021-02-23 13:44:41 +0100147if(CMAKE_COMPILER_IS_CLANG)
148 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
149endif(CMAKE_COMPILER_IS_CLANG)
150
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200151if(MSVC)
Simon B9b3e3c42016-11-03 01:12:50 +0000152 # If a warning level has been defined, suppress all warnings for test code
153 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
154 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
Manuel Pégourié-Gonnard30830532015-07-01 17:06:28 +0200155endif(MSVC)
156
Gilles Peskine29080e82022-01-25 22:30:25 +0100157file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
158list(APPEND test_suites ${base_generated_data_files})
159# If the generated .data files are present in the source tree, we just added
160# them twice, both through GLOB and through ${base_generated_data_files}.
161list(REMOVE_DUPLICATES test_suites)
162list(SORT test_suites)
163foreach(test_suite ${test_suites})
Gilles Peskinecd55fe02022-01-25 23:13:49 +0100164 get_filename_component(data_name ${test_suite} NAME)
165 string(REGEX REPLACE "\.data$" "" data_name "${data_name}")
166 string(REPLACE "test_suite_" "" data_name "${data_name}")
167 string(REGEX MATCH "[^.]*" function_name "${data_name}")
Gilles Peskine29080e82022-01-25 22:30:25 +0100168 add_test_suite(${function_name} ${data_name})
169endforeach(test_suite)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100170
Gilles Peskine84052572018-03-21 12:12:47 +0100171# Make scripts and data files needed for testing available in an
172# out-of-source build.
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100173if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Andrzej Kurek4a71cfa2019-05-06 05:06:06 -0400174 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
175 link_to_source(seedfile)
176 endif()
Gilles Peskine84052572018-03-21 12:12:47 +0100177 link_to_source(compat.sh)
David Brownc7444182020-10-16 13:19:49 -0600178 link_to_source(context-info.sh)
Gilles Peskine84052572018-03-21 12:12:47 +0100179 link_to_source(data_files)
180 link_to_source(scripts)
181 link_to_source(ssl-opt.sh)
Jerry Yu55ee7692021-11-29 13:26:55 +0800182 link_to_source(opt-testcases)
Paul Bakkercd6d69a2014-02-06 15:43:21 +0100183endif()