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