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_knowledge.py
122            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/macro_collector.py
123            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/psa_storage.py
124            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_case.py
125            ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/mbedtls_dev/test_data_generation.py
126            ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_config.h
127            ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_values.h
128            ${CMAKE_CURRENT_SOURCE_DIR}/../include/psa/crypto_extra.h
129    )
130
131else()
132    foreach(file ${all_generated_data_files})
133        link_to_source(${file})
134    endforeach()
135endif()
136# CMake generates sub-makefiles for each target and calls them in subprocesses.
137# Without this command, cmake will generate rules in each sub-makefile. As a result,
138# they can cause race conditions in parallel builds.
139# With this line, only 4 sub-makefiles include the above command, that reduces
140# the risk of a race.
141add_custom_target(test_suite_bignum_generated_data DEPENDS ${bignum_generated_data_files})
142add_custom_target(test_suite_ecp_generated_data DEPENDS ${ecp_generated_data_files})
143add_custom_target(test_suite_psa_generated_data DEPENDS ${psa_generated_data_files})
144# If SKIP_TEST_SUITES is not defined with -D, get it from the environment.
145if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES}))
146    set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES})
147endif()
148# Test suites caught by SKIP_TEST_SUITES are built but not executed.
149# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
150# but not "test_suite_foobar".
151string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
152string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
153set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
154
155function(add_test_suite suite_name)
156    if(ARGV1)
157        set(data_name ${ARGV1})
158    else()
159        set(data_name ${suite_name})
160    endif()
161
162    # Get the test names of the tests with generated .data files
163    # from the generated_data_files list in parent scope.
164    set(bignum_generated_data_names "")
165    set(ecp_generated_data_names "")
166    set(psa_generated_data_names "")
167    foreach(generated_data_file ${bignum_generated_data_files})
168        # Get the plain filename
169        get_filename_component(generated_data_name ${generated_data_file} NAME)
170        # Remove the ".data" extension
171        get_name_without_last_ext(generated_data_name ${generated_data_name})
172        # Remove leading "test_suite_"
173        string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
174        list(APPEND bignum_generated_data_names ${generated_data_name})
175    endforeach()
176    foreach(generated_data_file ${ecp_generated_data_files})
177        # Get the plain filename
178        get_filename_component(generated_data_name ${generated_data_file} NAME)
179        # Remove the ".data" extension
180        get_name_without_last_ext(generated_data_name ${generated_data_name})
181        # Remove leading "test_suite_"
182        string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
183        list(APPEND ecp_generated_data_names ${generated_data_name})
184    endforeach()
185    foreach(generated_data_file ${psa_generated_data_files})
186        # Get the plain filename
187        get_filename_component(generated_data_name ${generated_data_file} NAME)
188        # Remove the ".data" extension
189        get_name_without_last_ext(generated_data_name ${generated_data_name})
190        # Remove leading "test_suite_"
191        string(SUBSTRING ${generated_data_name} 11 -1 generated_data_name)
192        list(APPEND psa_generated_data_names ${generated_data_name})
193    endforeach()
194
195    if(";${bignum_generated_data_names};" MATCHES ";${data_name};")
196        set(data_file
197            ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
198        set(dependency test_suite_bignum_generated_data)
199    elseif(";${ecp_generated_data_names};" MATCHES ";${data_name};")
200        set(data_file
201            ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
202        set(dependency test_suite_ecp_generated_data)
203    elseif(";${psa_generated_data_names};" MATCHES ";${data_name};")
204        set(data_file
205            ${CMAKE_CURRENT_BINARY_DIR}/suites/test_suite_${data_name}.data)
206        set(dependency test_suite_psa_generated_data)
207    else()
208        set(data_file
209            ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data)
210        set(dependency test_suite_bignum_generated_data test_suite_ecp_generated_data test_suite_psa_generated_data)
211    endif()
212
213    add_custom_command(
214        OUTPUT
215            # The output filename of generate_test_code.py is derived from the -d
216            # input argument.
217            test_suite_${data_name}.c
218        COMMAND
219            ${MBEDTLS_PYTHON_EXECUTABLE}
220            ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
221            -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
222            -d ${data_file}
223            -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
224            -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
225            -s ${CMAKE_CURRENT_SOURCE_DIR}/suites
226            --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
227            -o .
228        DEPENDS
229            ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py
230            ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function
231            ${data_file}
232            ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function
233            ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function
234            ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function
235            ${mbedtls_target}
236        BYPRODUCTS
237            test_suite_${data_name}.datax
238    )
239
240    add_executable(test_suite_${data_name} test_suite_${data_name}.c
241                   $<TARGET_OBJECTS:mbedtls_test>
242                   $<TARGET_OBJECTS:mbedtls_test_helpers>)
243    add_dependencies(test_suite_${data_name} ${dependency})
244    target_link_libraries(test_suite_${data_name} ${libs})
245    # Include test-specific header files from ./include and private header
246    # files (used by some invasive tests) from ../library. Public header
247    # files are automatically included because the library targets declare
248    # them as PUBLIC.
249    target_include_directories(test_suite_${data_name}
250        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
251        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
252
253    if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
254        message(STATUS "The test suite ${data_name} will not be executed.")
255    else()
256        add_test(${data_name}-suite test_suite_${data_name} --verbose)
257    endif()
258endfunction(add_test_suite)
259
260# Enable definition of various functions used throughout the testsuite
261# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
262# on non-POSIX platforms.
263add_definitions("-D_POSIX_C_SOURCE=200809L")
264
265if(CMAKE_COMPILER_IS_CLANG)
266    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
267endif(CMAKE_COMPILER_IS_CLANG)
268
269if(MSVC)
270    # If a warning level has been defined, suppress all warnings for test code
271    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
272    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
273endif(MSVC)
274
275file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data)
276list(APPEND test_suites ${all_generated_data_files})
277# If the generated .data files are present in the source tree, we just added
278# them twice, both through GLOB and through ${all_generated_data_files}.
279list(REMOVE_DUPLICATES test_suites)
280list(SORT test_suites)
281foreach(test_suite ${test_suites})
282    get_filename_component(data_name ${test_suite} NAME)
283    string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}")
284    string(REPLACE "test_suite_" "" data_name "${data_name}")
285    string(REGEX MATCH "[^.]*" function_name "${data_name}")
286    add_test_suite(${function_name} ${data_name})
287endforeach(test_suite)
288
289# Make scripts and data files needed for testing available in an
290# out-of-source build.
291if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
292    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
293        link_to_source(seedfile)
294    endif()
295    link_to_source(Descriptions.txt)
296    link_to_source(compat.sh)
297    link_to_source(context-info.sh)
298    link_to_source(data_files)
299    link_to_source(scripts)
300    link_to_source(ssl-opt.sh)
301    link_to_source(opt-testcases)
302endif()
303