cmake: Support running the tests with AddressSanitizer
[resolv_wrapper.git] / tests / CMakeLists.txt
1 project(tests C)
2
3 include_directories(
4   ${CMAKE_BINARY_DIR}
5   ${CMAKE_CURRENT_SOURCE_DIR}
6   ${CMAKE_SOURCE_DIR}/src
7   ${CMOCKA_INCLUDE_DIR}
8 )
9
10 set(TORTURE_LIBRARY torture)
11
12 # A simple DNS server for testing
13 add_executable(dns_srv dns_srv.c)
14 target_compile_options(dns_srv PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
15 target_link_libraries(dns_srv ${RWRAP_REQUIRED_LIBRARIES})
16
17 add_executable(test_real_res_query test_real_res_query.c)
18 target_compile_options(test_real_res_query PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
19 target_link_libraries(test_real_res_query ${RWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
20
21 configure_file(fake_hosts.in ${CMAKE_CURRENT_BINARY_DIR}/fake_hosts @ONLY)
22
23 add_library(${TORTURE_LIBRARY} STATIC torture.c)
24 target_compile_options(${TORTURE_LIBRARY} PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
25 target_link_libraries(${TORTURE_LIBRARY}
26     ${CMOCKA_LIBRARY}
27     ${SWRAP_REQUIRED_LIBRARIES})
28
29
30 set(TESTSUITE_LIBRARIES ${RWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
31
32 # Some tests require socket_wrapper as well.
33 find_package(socket_wrapper REQUIRED)
34
35 set(RWRAP_TESTS test_res_init)
36
37 if (HAVE_LIBRESOLV)
38     set(RWRAP_TESTS ${RWRAP_TESTS} test_res_query_search)
39 endif()
40
41 function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME)
42     if (CMAKE_BUILD_TYPE)
43         string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
44         if (CMAKE_BUILD_TYPE_LOWER STREQUAL "addresssanitizer")
45             find_library(ASAN_LIBRARY
46                          NAMES asan)
47             if (NOT ASAN_LIBRARY)
48                 foreach(version RANGE 10 1)
49                     if (NOT ASAN_LIBRARY)
50                         find_library(ASAN_LIBRARY libasan.so.${version})
51                     endif()
52                 endforeach()
53             endif()
54         endif()
55     endif()
56
57     if (ASAN_LIBRARY)
58         list(APPEND PRELOAD_LIBRARIES ${ASAN_LIBRARY})
59     endif()
60     list(APPEND PRELOAD_LIBRARIES ${RESOLV_WRAPPER_LOCATION})
61     list(APPEND PRELOAD_LIBRARIES ${SOCKET_WRAPPER_LIBRARY})
62
63     if (OSX)
64         set(TORTURE_ENVIRONMENT "DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${RESOLV_WRAPPER_LOCATION}:${SOCKET_WRAPPER_LIBRARY}")
65     else ()
66         string(REPLACE ";" ":" _TMP_ENV "${PRELOAD_LIBRARIES}")
67         set(TORTURE_ENVIRONMENT "LD_PRELOAD=${_TMP_ENV}")
68     endif()
69
70     list(APPEND TORTURE_ENVIRONMENT RESOLV_WRAPPER=1)
71
72     foreach(_arg ${ARGN})
73         list(APPEND TORTURE_ENVIRONMENT ${_arg})
74     endforeach()
75
76     set_property(TEST
77                     ${_TEST_NAME}
78                 PROPERTY
79                     ENVIRONMENT "${TORTURE_ENVIRONMENT}")
80 endfunction()
81
82 foreach(_RWRAP_TEST ${RWRAP_TESTS})
83     add_cmocka_test(${_RWRAP_TEST} ${_RWRAP_TEST}.c ${TORTURE_LIBRARY} ${TESTSUITE_LIBRARIES})
84
85     add_cmocka_test_environment(${_RWRAP_TEST})
86 endforeach()
87
88 add_cmocka_test(test_dns_fake test_dns_fake.c ${TORTURE_LIBRARY} ${TESTSUITE_LIBRARIES})
89 add_cmocka_test_environment(test_dns_fake RESOLV_WRAPPER_HOSTS=${CMAKE_CURRENT_BINARY_DIR}/fake_hosts)