| Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 1 | set(libs | 
| Raef Coles | 995c66f | 2020-10-13 16:30:41 +0100 | [diff] [blame] | 2 | ${mbedtls_target} | 
| Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 3 | ) | 
|  | 4 |  | 
| Ashley Duncan | d85a7e9 | 2019-04-29 20:35:06 +1200 | [diff] [blame] | 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. | 
|  | 8 | if(NOT DEFINED MBEDTLS_DIR) | 
|  | 9 | set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR}) | 
|  | 10 | endif() | 
|  | 11 |  | 
| Paul Bakker | b06819b | 2011-01-18 16:18:38 +0000 | [diff] [blame] | 12 | if(USE_PKCS11_HELPER_LIBRARY) | 
|  | 13 | set(libs ${libs} pkcs11-helper) | 
|  | 14 | endif(USE_PKCS11_HELPER_LIBRARY) | 
|  | 15 |  | 
| Paul Bakker | 92eeea4 | 2012-07-03 15:10:33 +0000 | [diff] [blame] | 16 | if(ENABLE_ZLIB_SUPPORT) | 
|  | 17 | set(libs ${libs} ${ZLIB_LIBRARIES}) | 
|  | 18 | endif(ENABLE_ZLIB_SUPPORT) | 
|  | 19 |  | 
| okhowang(王沛文) | 3c1b090 | 2020-03-25 19:55:32 +0800 | [diff] [blame] | 20 | if(NOT MBEDTLS_PYTHON_EXECUTABLE) | 
|  | 21 | message(FATAL_ERROR "Cannot build test suites without Python 3") | 
| Nicholas Wilson | 733676b | 2015-11-14 13:09:01 +0000 | [diff] [blame] | 22 | endif() | 
|  | 23 |  | 
| Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 24 | # Enable definition of various functions used throughout the testsuite | 
| Nicholas Wilson | 61fa436 | 2018-06-25 12:10:00 +0100 | [diff] [blame] | 25 | # (gethostname, strdup, fileno...) even when compiling with -std=c99. Harmless | 
| Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 26 | # on non-POSIX platforms. | 
|  | 27 | add_definitions("-D_POSIX_C_SOURCE=200809L") | 
|  | 28 |  | 
| David Horstmann | 04f25fc | 2022-11-28 10:18:05 +0000 | [diff] [blame] | 29 | # If SKIP_TEST_SUITES is not defined with -D, get it from the environment. | 
|  | 30 | if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES})) | 
|  | 31 | set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES}) | 
|  | 32 | endif() | 
| Gilles Peskine | ac372cc | 2018-11-29 10:15:06 +0000 | [diff] [blame] | 33 | # Test suites caught by SKIP_TEST_SUITES are built but not executed. | 
|  | 34 | # "foo" as a skip pattern skips "test_suite_foo" and "test_suite_foo.bar" | 
|  | 35 | # but not "test_suite_foobar". | 
|  | 36 | string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}") | 
|  | 37 | string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}") | 
|  | 38 | set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)") | 
|  | 39 |  | 
| Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 40 | function(add_test_suite suite_name) | 
|  | 41 | if(ARGV1) | 
| Manuel Pégourié-Gonnard | 216a183 | 2015-06-25 09:20:03 +0200 | [diff] [blame] | 42 | set(data_name ${ARGV1}) | 
| Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 43 | else() | 
| Manuel Pégourié-Gonnard | 216a183 | 2015-06-25 09:20:03 +0200 | [diff] [blame] | 44 | set(data_name ${suite_name}) | 
| Paul Bakker | 46c1794 | 2011-07-13 14:54:54 +0000 | [diff] [blame] | 45 | endif() | 
|  | 46 |  | 
| Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 47 | add_custom_command( | 
| Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 48 | OUTPUT test_suite_${data_name}.c | 
| okhowang(王沛文) | 3c1b090 | 2020-03-25 19:55:32 +0800 | [diff] [blame] | 49 | 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 . | 
| Raef Coles | 995c66f | 2020-10-13 16:30:41 +0100 | [diff] [blame] | 50 | 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 | 
| Paul Bakker | 4c14a25 | 2010-06-18 22:54:05 +0000 | [diff] [blame] | 51 | ) | 
| Paul Bakker | 1934318 | 2013-08-16 13:31:10 +0200 | [diff] [blame] | 52 |  | 
| Yanray Wang | 3742b12 | 2022-10-28 11:49:33 +0800 | [diff] [blame] | 53 | add_executable(test_suite_${data_name} test_suite_${data_name}.c | 
|  | 54 | $<TARGET_OBJECTS:mbedtls_test> | 
|  | 55 | $<TARGET_OBJECTS:mbedtls_test_helpers>) | 
| Paul Bakker | 7304376 | 2011-07-13 15:03:10 +0000 | [diff] [blame] | 56 | target_link_libraries(test_suite_${data_name} ${libs}) | 
| Gilles Peskine | 76dd3aa | 2020-07-02 15:58:37 +0200 | [diff] [blame] | 57 | # Include test-specific header files from ./include and private header | 
|  | 58 | # files (used by some invasive tests) from ../library. Public header | 
|  | 59 | # files are automatically included because the library targets declare | 
|  | 60 | # them as PUBLIC. | 
| Ronald Cron | 8552741 | 2020-05-15 17:20:47 +0200 | [diff] [blame] | 61 | target_include_directories(test_suite_${data_name} | 
|  | 62 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include | 
|  | 63 | PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library) | 
|  | 64 |  | 
| Gilles Peskine | ac372cc | 2018-11-29 10:15:06 +0000 | [diff] [blame] | 65 | if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) | 
|  | 66 | message(STATUS "The test suite ${data_name} will not be executed.") | 
|  | 67 | else() | 
|  | 68 | add_test(${data_name}-suite test_suite_${data_name} --verbose) | 
|  | 69 | endif() | 
| Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 70 | endfunction(add_test_suite) | 
|  | 71 |  | 
| Manuel Pégourié-Gonnard | 7e2d68c | 2015-07-01 13:41:35 +0200 | [diff] [blame] | 72 | if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) | 
|  | 73 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function") | 
|  | 74 | endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) | 
| Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 75 |  | 
| Gilles Peskine | f29019f | 2021-02-23 13:44:41 +0100 | [diff] [blame] | 76 | if(CMAKE_COMPILER_IS_CLANG) | 
|  | 77 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") | 
|  | 78 | endif(CMAKE_COMPILER_IS_CLANG) | 
|  | 79 |  | 
| Manuel Pégourié-Gonnard | 3083053 | 2015-07-01 17:06:28 +0200 | [diff] [blame] | 80 | if(MSVC) | 
| Simon B | 9b3e3c4 | 2016-11-03 01:12:50 +0000 | [diff] [blame] | 81 | # If a warning level has been defined, suppress all warnings for test code | 
|  | 82 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W0") | 
|  | 83 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX-") | 
| Manuel Pégourié-Gonnard | 3083053 | 2015-07-01 17:06:28 +0200 | [diff] [blame] | 84 | endif(MSVC) | 
|  | 85 |  | 
| Gilles Peskine | 3df1dae | 2022-01-25 22:30:25 +0100 | [diff] [blame] | 86 | file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data) | 
|  | 87 | list(SORT test_suites) | 
|  | 88 | foreach(test_suite ${test_suites}) | 
|  | 89 | get_filename_component(data_name ${test_suite} NAME) | 
|  | 90 | string(REGEX REPLACE "\\.data\$" "" data_name "${data_name}") | 
|  | 91 | string(REPLACE "test_suite_" "" data_name "${data_name}") | 
|  | 92 | string(REGEX MATCH "[^.]*" function_name "${data_name}") | 
|  | 93 | add_test_suite(${function_name} ${data_name}) | 
|  | 94 | endforeach(test_suite) | 
| Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 95 |  | 
| Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 96 | # Make scripts and data files needed for testing available in an | 
|  | 97 | # out-of-source build. | 
| Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 98 | if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) | 
| Andrzej Kurek | 4a71cfa | 2019-05-06 05:06:06 -0400 | [diff] [blame] | 99 | if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile") | 
|  | 100 | link_to_source(seedfile) | 
|  | 101 | endif() | 
| Gilles Peskine | b0f11f1 | 2022-11-30 17:52:54 +0100 | [diff] [blame] | 102 | link_to_source(Descriptions.txt) | 
| Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 103 | link_to_source(compat.sh) | 
| David Brown | c744418 | 2020-10-16 13:19:49 -0600 | [diff] [blame] | 104 | link_to_source(context-info.sh) | 
| Gilles Peskine | 8405257 | 2018-03-21 12:12:47 +0100 | [diff] [blame] | 105 | link_to_source(data_files) | 
|  | 106 | link_to_source(scripts) | 
|  | 107 | link_to_source(ssl-opt.sh) | 
| Gilles Peskine | 6a78573 | 2019-01-02 17:29:04 +0100 | [diff] [blame] | 108 | link_to_source(suites) | 
| Paul Bakker | cd6d69a | 2014-02-06 15:43:21 +0100 | [diff] [blame] | 109 | endif() |