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(USE_PKCS11_HELPER_LIBRARY)
13    set(libs ${libs} pkcs11-helper)
14endif(USE_PKCS11_HELPER_LIBRARY)
15
16if(ENABLE_ZLIB_SUPPORT)
17    set(libs ${libs} ${ZLIB_LIBRARIES})
18endif(ENABLE_ZLIB_SUPPORT)
19
20if(NOT MBEDTLS_PYTHON_EXECUTABLE)
21    message(FATAL_ERROR "Cannot build test suites without Python 3")
22endif()
23
24# Enable definition of various functions used throughout the testsuite
25# (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless
26# on non-POSIX platforms.
27add_definitions("-D_POSIX_C_SOURCE=200809L")
28
29# Test suites caught by SKIP_TEST_SUITES are built but not executed.
30# "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar"
31# but not "test_suite_foobar".
32string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}")
33string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}")
34set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)")
35
36function(add_test_suite suite_name)
37    if(ARGV1)
38        set(data_name ${ARGV1})
39    else()
40        set(data_name ${suite_name})
41    endif()
42
43    add_custom_command(
44        OUTPUT test_suite_${data_name}.c
45        COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function -d ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function -s ${CMAKE_CURRENT_SOURCE_DIR}/suites --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function -o .
46        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py ${mbedtls_target} ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data
47    )
48
49    add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>)
50    target_link_libraries(test_suite_${data_name} ${libs})
51    # Include test-specific header files from ./include and private header
52    # files (used by some invasive tests) from ../library. Public header
53    # files are automatically included because the library targets declare
54    # them as PUBLIC.
55    target_include_directories(test_suite_${data_name}
56        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
57        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library)
58
59    if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
60        message(STATUS "The test suite ${data_name} will not be executed.")
61    else()
62        add_test(${data_name}-suite test_suite_${data_name} --verbose)
63    endif()
64endfunction(add_test_suite)
65
66if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
67    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
68endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
69
70if(CMAKE_COMPILER_IS_CLANG)
71    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code")
72endif(CMAKE_COMPILER_IS_CLANG)
73
74if(MSVC)
75    # If a warning level has been defined, suppress all warnings for test code
76    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0")
77    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-")
78endif(MSVC)
79
80add_test_suite(aes aes.cbc)
81add_test_suite(aes aes.cfb)
82add_test_suite(aes aes.ecb)
83add_test_suite(aes aes.ofb)
84add_test_suite(aes aes.rest)
85add_test_suite(aes aes.xts)
86add_test_suite(arc4)
87add_test_suite(aria)
88add_test_suite(asn1parse)
89add_test_suite(asn1write)
90add_test_suite(base64)
91add_test_suite(blowfish)
92add_test_suite(camellia)
93add_test_suite(ccm)
94add_test_suite(chacha20)
95add_test_suite(chachapoly)
96add_test_suite(cipher cipher.aes)
97add_test_suite(cipher cipher.arc4)
98add_test_suite(cipher cipher.blowfish)
99add_test_suite(cipher cipher.camellia)
100add_test_suite(cipher cipher.ccm)
101add_test_suite(cipher cipher.chacha20)
102add_test_suite(cipher cipher.chachapoly)
103add_test_suite(cipher cipher.des)
104add_test_suite(cipher cipher.gcm)
105add_test_suite(cipher cipher.misc)
106add_test_suite(cipher cipher.nist_kw)
107add_test_suite(cipher cipher.null)
108add_test_suite(cipher cipher.padding)
109add_test_suite(cmac)
110add_test_suite(ctr_drbg)
111add_test_suite(debug)
112add_test_suite(des)
113add_test_suite(dhm)
114add_test_suite(ecdh)
115add_test_suite(ecdsa)
116add_test_suite(ecjpake)
117add_test_suite(ecp)
118add_test_suite(entropy)
119add_test_suite(error)
120add_test_suite(gcm gcm.aes128_de)
121add_test_suite(gcm gcm.aes128_en)
122add_test_suite(gcm gcm.aes192_de)
123add_test_suite(gcm gcm.aes192_en)
124add_test_suite(gcm gcm.aes256_de)
125add_test_suite(gcm gcm.aes256_en)
126add_test_suite(gcm gcm.camellia)
127add_test_suite(gcm gcm.misc)
128add_test_suite(hkdf)
129add_test_suite(hmac_drbg hmac_drbg.misc)
130add_test_suite(hmac_drbg hmac_drbg.no_reseed)
131add_test_suite(hmac_drbg hmac_drbg.nopr)
132add_test_suite(hmac_drbg hmac_drbg.pr)
133add_test_suite(md)
134add_test_suite(mdx)
135add_test_suite(memory_buffer_alloc)
136add_test_suite(mpi)
137add_test_suite(mps)
138add_test_suite(net)
139add_test_suite(nist_kw)
140add_test_suite(oid)
141add_test_suite(pem)
142add_test_suite(pk)
143add_test_suite(pkcs1_v15)
144add_test_suite(pkcs1_v21)
145add_test_suite(pkcs5)
146add_test_suite(pkcs12)
147add_test_suite(pkparse)
148add_test_suite(pkwrite)
149add_test_suite(poly1305)
150add_test_suite(psa_crypto)
151add_test_suite(psa_crypto_attributes)
152add_test_suite(psa_crypto_entropy)
153add_test_suite(psa_crypto_hash)
154add_test_suite(psa_crypto_init)
155add_test_suite(psa_crypto_metadata)
156add_test_suite(psa_crypto_not_supported psa_crypto_not_supported.generated)
157add_test_suite(psa_crypto_not_supported psa_crypto_not_supported.misc)
158add_test_suite(psa_crypto_persistent_key)
159add_test_suite(psa_crypto_se_driver_hal)
160add_test_suite(psa_crypto_se_driver_hal_mocks)
161add_test_suite(psa_crypto_slot_management)
162add_test_suite(psa_crypto_storage_format psa_crypto_storage_format.misc)
163add_test_suite(psa_crypto_storage_format psa_crypto_storage_format.current)
164add_test_suite(psa_crypto_storage_format psa_crypto_storage_format.v0)
165add_test_suite(psa_its)
166add_test_suite(random)
167add_test_suite(rsa)
168add_test_suite(shax)
169add_test_suite(ssl)
170add_test_suite(timing)
171add_test_suite(version)
172add_test_suite(x509parse)
173add_test_suite(x509write)
174add_test_suite(xtea)
175
176# Make scripts and data files needed for testing available in an
177# out-of-source build.
178if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
179    if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile")
180        link_to_source(seedfile)
181    endif()
182    link_to_source(compat.sh)
183    link_to_source(context-info.sh)
184    link_to_source(data_files)
185    link_to_source(scripts)
186    link_to_source(ssl-opt.sh)
187    link_to_source(suites)
188endif()
189