| 1 |
project(tests C) |
| 2 |
|
| 3 |
if (TARGET cmocka::cmocka) |
| 4 |
set(CMOCKA_LIBRARY cmocka::cmocka) |
| 5 |
endif() |
| 6 |
|
| 7 |
set(TESTSUITE_LIBRARIES nss_utils ${NWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY}) |
| 8 |
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) |
| 9 |
|
| 10 |
add_library(nss_nwrap SHARED nss_nwrap.c) |
| 11 |
target_compile_options(nss_nwrap |
| 12 |
PRIVATE |
| 13 |
${DEFAULT_C_COMPILE_FLAGS}) |
| 14 |
target_include_directories(nss_nwrap |
| 15 |
PRIVATE |
| 16 |
${CMAKE_BINARY_DIR} |
| 17 |
${CMOCKA_INCLUDE_DIR}) |
| 18 |
target_link_libraries(nss_nwrap PRIVATE nss_utils) |
| 19 |
|
| 20 |
set(HOMEDIR ${CMAKE_CURRENT_BINARY_DIR}) |
| 21 |
|
| 22 |
configure_file(passwd.in ${CMAKE_CURRENT_BINARY_DIR}/passwd @ONLY) |
| 23 |
configure_file(group.in ${CMAKE_CURRENT_BINARY_DIR}/group @ONLY) |
| 24 |
configure_file(hosts.in ${CMAKE_CURRENT_BINARY_DIR}/hosts @ONLY) |
| 25 |
configure_file(shadow.in ${CMAKE_CURRENT_BINARY_DIR}/shadow @ONLY) |
| 26 |
|
| 27 |
# If there is uid_wrapper, we can better test initgroups() since uid_wrapper |
| 28 |
# provides privilege for setgroups(). |
| 29 |
find_package(uid_wrapper 1.3.0) |
| 30 |
|
| 31 |
set(NWRAP_TESTS |
| 32 |
testsuite |
| 33 |
test_nwrap_vector |
| 34 |
test_getaddrinfo |
| 35 |
test_getnameinfo |
| 36 |
test_gethostby_name_addr |
| 37 |
test_gethostent |
| 38 |
test_getpwuid_module |
| 39 |
test_initgroups) |
| 40 |
|
| 41 |
if (HAVE_SHADOW_H) |
| 42 |
list(APPEND NWRAP_TESTS test_shadow) |
| 43 |
endif (HAVE_SHADOW_H) |
| 44 |
|
| 45 |
function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME) |
| 46 |
if (CMAKE_BUILD_TYPE) |
| 47 |
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) |
| 48 |
if (CMAKE_BUILD_TYPE_LOWER STREQUAL "addresssanitizer") |
| 49 |
find_library(ASAN_LIBRARY |
| 50 |
NAMES asan) |
| 51 |
if (NOT ASAN_LIBRARY) |
| 52 |
foreach(version RANGE 10 1) |
| 53 |
if (NOT ASAN_LIBRARY) |
| 54 |
find_library(ASAN_LIBRARY libasan.so.${version}) |
| 55 |
endif() |
| 56 |
endforeach() |
| 57 |
endif() |
| 58 |
endif() |
| 59 |
endif() |
| 60 |
|
| 61 |
if (ASAN_LIBRARY) |
| 62 |
list(APPEND PRELOAD_LIBRARIES ${ASAN_LIBRARY}) |
| 63 |
endif() |
| 64 |
if (uid_wrapper_FOUND) |
| 65 |
list(APPEND PRELOAD_LIBRARIES ${UID_WRAPPER_LIBRARY}) |
| 66 |
endif() |
| 67 |
list(APPEND PRELOAD_LIBRARIES ${NSS_WRAPPER_LOCATION}) |
| 68 |
|
| 69 |
if (OSX) |
| 70 |
set(TORTURE_ENVIRONMENT "DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${NSS_WRAPPER_LOCATION}") |
| 71 |
else () |
| 72 |
string(REPLACE ";" ":" _TMP_ENV "${PRELOAD_LIBRARIES}") |
| 73 |
set(TORTURE_ENVIRONMENT "LD_PRELOAD=${_TMP_ENV}") |
| 74 |
endif() |
| 75 |
|
| 76 |
if (uid_wrapper_FOUND) |
| 77 |
list(APPEND TORTURE_ENVIRONMENT UID_WRAPPER=1) |
| 78 |
endif() |
| 79 |
|
| 80 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_PASSWD=${CMAKE_CURRENT_BINARY_DIR}/passwd) |
| 81 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_GROUP=${CMAKE_CURRENT_BINARY_DIR}/group) |
| 82 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_SHADOW=${CMAKE_CURRENT_BINARY_DIR}/shadow) |
| 83 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_HOSTS=${CMAKE_CURRENT_BINARY_DIR}/hosts) |
| 84 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_MODULE_SO_PATH=${CMAKE_CURRENT_BINARY_DIR}/libnss_nwrap.so) |
| 85 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_MODULE_FN_PREFIX=nwrap) |
| 86 |
if (CMAKE_BUILD_TYPE) |
| 87 |
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) |
| 88 |
|
| 89 |
if (CMAKE_BUILD_TYPE_LOWER STREQUAL "threadsanitizer") |
| 90 |
list(APPEND TORTURE_ENVIRONMENT NSS_WRAPPER_DISABLE_DEEPBIND=1) |
| 91 |
if (uid_wrapper_FOUND) |
| 92 |
list(APPEND TORTURE_ENVIRONMENT UID_WRAPPER_DISABLE_DEEPBIND=1) |
| 93 |
endif() |
| 94 |
endif() |
| 95 |
endif() |
| 96 |
|
| 97 |
set_property(TEST |
| 98 |
${_TEST_NAME} |
| 99 |
PROPERTY |
| 100 |
ENVIRONMENT "${TORTURE_ENVIRONMENT}") |
| 101 |
endfunction() |
| 102 |
foreach(_NWRAP_TEST ${NWRAP_TESTS}) |
| 103 |
add_cmocka_test(${_NWRAP_TEST} |
| 104 |
SOURCES ${_NWRAP_TEST}.c |
| 105 |
COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} |
| 106 |
LINK_LIBRARIES ${TESTSUITE_LIBRARIES} |
| 107 |
LINK_OPTIONS ${DEFAULT_LINK_FLAGS}) |
| 108 |
if (BSD) |
| 109 |
target_compile_options(${_NWRAP_TEST} |
| 110 |
PRIVATE |
| 111 |
-DBSD) |
| 112 |
endif() |
| 113 |
target_include_directories(${_NWRAP_TEST} |
| 114 |
PRIVATE |
| 115 |
${CMAKE_BINARY_DIR} |
| 116 |
${CMOCKA_INCLUDE_DIR}) |
| 117 |
add_cmocka_test_environment(${_NWRAP_TEST}) |
| 118 |
endforeach() |
| 119 |
|
| 120 |
if (HAVE_SHADOW_H) |
| 121 |
# This is needed to check the hash in tests/shadow.in |
| 122 |
target_link_libraries(test_shadow PRIVATE crypt) |
| 123 |
endif (HAVE_SHADOW_H) |
| 124 |
|
| 125 |
target_link_libraries(test_nwrap_vector PRIVATE ${CMAKE_THREAD_LIBS_INIT}) |
| 126 |
target_link_libraries(test_gethostby_name_addr PRIVATE ${CMAKE_THREAD_LIBS_INIT}) |
| 127 |
|
| 128 |
# Test nwrap without wrapping so the libc functions are called |
| 129 |
if (NOT CMAKE_BUILD_TYPE_LOWER MATCHES "sanitizer") |
| 130 |
add_cmocka_test(test_nwrap_disabled |
| 131 |
SOURCES test_nwrap_disabled.c |
| 132 |
COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} |
| 133 |
LINK_LIBRARIES ${TESTSUITE_LIBRARIES} |
| 134 |
LINK_OPTIONS ${DEFAULT_LINK_FLAGS}) |
| 135 |
target_include_directories(test_nwrap_disabled |
| 136 |
PRIVATE |
| 137 |
${CMAKE_BINARY_DIR} |
| 138 |
${CMOCKA_INCLUDE_DIR}) |
| 139 |
set_property( |
| 140 |
TEST |
| 141 |
test_nwrap_disabled |
| 142 |
PROPERTY |
| 143 |
ENVIRONMENT LD_PRELOAD=${NSS_WRAPPER_LOCATION}) |
| 144 |
endif() |