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# If SKIP_TEST_SUITES is not defined with -D, get it from the environment. 30if((NOT DEFINED SKIP_TEST_SUITES) AND (DEFINED ENV{SKIP_TEST_SUITES})) 31 set(SKIP_TEST_SUITES $ENV{SKIP_TEST_SUITES}) 32endif() 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". 36string(REGEX REPLACE "[ ,;]" "|" SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES}") 37string(REPLACE "." "\\." SKIP_TEST_SUITES_REGEX "${SKIP_TEST_SUITES_REGEX}") 38set(SKIP_TEST_SUITES_REGEX "^(${SKIP_TEST_SUITES_REGEX})(\$|\\.)") 39 40function(add_test_suite suite_name) 41 if(ARGV1) 42 set(data_name ${ARGV1}) 43 else() 44 set(data_name ${suite_name}) 45 endif() 46 47 add_custom_command( 48 OUTPUT test_suite_${data_name}.c 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 . 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 51 ) 52 53 add_executable(test_suite_${data_name} test_suite_${data_name}.c 54 $<TARGET_OBJECTS:mbedtls_test> 55 $<TARGET_OBJECTS:mbedtls_test_helpers>) 56 target_link_libraries(test_suite_${data_name} ${libs}) 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. 61 target_include_directories(test_suite_${data_name} 62 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include 63 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../library) 64 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() 70endfunction(add_test_suite) 71 72if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) 73 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function") 74endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG) 75 76if(CMAKE_COMPILER_IS_CLANG) 77 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wdocumentation -Wno-documentation-deprecated-sync -Wunreachable-code") 78endif(CMAKE_COMPILER_IS_CLANG) 79 80if(MSVC) 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-") 84endif(MSVC) 85 86file(GLOB test_suites RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" suites/*.data) 87list(SORT test_suites) 88foreach(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}) 94endforeach(test_suite) 95 96# Make scripts and data files needed for testing available in an 97# out-of-source build. 98if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) 99 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/seedfile") 100 link_to_source(seedfile) 101 endif() 102 link_to_source(Descriptions.txt) 103 link_to_source(compat.sh) 104 link_to_source(context-info.sh) 105 link_to_source(data_files) 106 link_to_source(scripts) 107 link_to_source(ssl-opt.sh) 108 link_to_source(suites) 109endif() 110