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