>From 5f4739c9494e3e3986d6b9ae8c02b13bab3748e2 Mon Sep 17 00:00:00 2001
From: Simo Sorce <simo@redhat.com>
Date: Thu, 5 Sep 2013 11:52:08 -0400
Subject: [PATCH] tests: Add dlopen test to make sure modules works

This tests dlopens and resolves all symbols to make sure there are no missing
symbols in our provider modules.
---
 Makefile.am              |  31 +++++++++
 src/conf_macros.m4       |   1 +
 src/external/krb5.m4     |   3 +-
 src/tests/dlopen-tests.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 193 insertions(+), 1 deletion(-)
 create mode 100644 src/tests/dlopen-tests.c

diff --git a/Makefile.am b/Makefile.am
index d21c5a717ac98239300886da0bd6dc74e4f42afd..1f7270076c32b7c306a594ef5d5150ee67452cbf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -114,6 +114,7 @@ endif
 
 if HAVE_CHECK
     non_interactive_check_based_tests = \
+        dlopen-tests \
         sysdb-tests \
         strtonum-tests \
         resolv-tests \
@@ -890,6 +891,36 @@ if HAVE_CHECK
 libsss_test_common_la_SOURCES += \
     src/tests/common_check.c
 
+check_LTLIBRARIES = \
+    libdlopen_test_providers.la
+
+libdlopen_test_providers_la_SOURCES = \
+    $(sssd_be_SOURCES)
+libdlopen_test_providers_la_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(CHECK_CFLAGS) \
+    -DUNIT_TESTING
+libdlopen_test_providers_la_LIBADD = \
+    $(PAM_LIBS) \
+    $(SSSD_LIBS) \
+    $(CARES_LIBS) \
+    $(SSSD_INTERNAL_LTLIBS)
+libdlopen_test_providers_la_LDFLAGS = \
+    -module \
+    -avoid-version \
+    -Wl,--version-script,$(srcdir)/src/providers/sssd_be.exports \
+    -rpath $(abs_top_builddir) \
+    -export-dynamic
+
+dlopen_tests_SOURCES = \
+    src/tests/dlopen-tests.c
+dlopen_tests_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(CHECK_CFLAGS)
+dlopen_tests_LDADD = \
+    -ldl \
+    $(CHECK_LIBS)
+
 sysdb_tests_DEPENDENCIES = \
     $(ldblib_LTLIBRARIES)
 sysdb_tests_SOURCES = \
diff --git a/src/conf_macros.m4 b/src/conf_macros.m4
index 433e99cef7bb415d9da775dfdba7c0ec5ca3e193..b4db0b4a2b34e69734402f2f4a4c2ccadbf94fb8 100644
--- a/src/conf_macros.m4
+++ b/src/conf_macros.m4
@@ -329,6 +329,7 @@ AC_DEFUN([WITH_PYTHON_BINDINGS],
     if test x"$with_python_bindings" = xyes; then
         HAVE_PYTHON_BINDINGS=1
         AC_SUBST(HAVE_PYTHON_BINDINGS)
+        AC_DEFINE_UNQUOTED(HAVE_PYTHON_BINDINGS, 1, [Build with python bindings])
     fi
     AM_CONDITIONAL([BUILD_PYTHON_BINDINGS], [test x"$with_python_bindings" = xyes])
   ])
diff --git a/src/external/krb5.m4 b/src/external/krb5.m4
index 1c1c3e5e2d08fd20b8945438b9885ee8def1df52..1a50bf1c72fd4df8d39fe3798d9fb4e4a6c1222f 100644
--- a/src/external/krb5.m4
+++ b/src/external/krb5.m4
@@ -91,4 +91,5 @@ AC_CHECK_HEADER([krb5/locate_plugin.h],
                 ])
 AM_CONDITIONAL([BUILD_KRB5_LOCATOR_PLUGIN],
                [test x$have_locate_plugin = xyes -a x$build_locator = xyes])
-
+AM_COND_IF([BUILD_KRB5_LOCATOR_PLUGIN],
+           [AC_DEFINE_UNQUOTED(HAVE_KRB5_LOCATOR_PLUGIN, 1, [Build with krb5 locator plugin])])
diff --git a/src/tests/dlopen-tests.c b/src/tests/dlopen-tests.c
new file mode 100644
index 0000000000000000000000000000000000000000..67fc41c7a9187cd765553d7715a704ff260a0e1c
--- /dev/null
+++ b/src/tests/dlopen-tests.c
@@ -0,0 +1,159 @@
+/*
+    SSSD
+
+    debug-tests.c
+
+    Authors:
+        Simo Sorce <simo@redhat.com>
+
+    Copyright (C) 2013 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#define _GNU_SOURCE
+#include <stdbool.h>
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <check.h>
+#include "tests/common.h"
+
+#define LIBPFX ABS_BUILD_DIR"/.libs/"
+
+struct so {
+    const char *name;
+    const char *libs[6];
+} so[] = {
+    { "libsss_debug.so", { LIBPFX"libsss_debug.so", NULL } },
+    { "libipa_hbac.so", { LIBPFX"libipa_hbac.so", NULL } },
+    { "libsss_idmap.so", { LIBPFX"libsss_idmap.so", NULL } },
+    { "libsss_nss_idmap.so", { LIBPFX"libsss_nss_idmap.so", NULL } },
+    { "libnss_sss.so", { LIBPFX"libnss_sss.so", NULL } },
+    { "pam_sss.so", { LIBPFX"pam_sss.so", NULL } },
+#ifdef BUILD_SUDO
+    { "libsss_sudo.so", { LIBPFX"libsss_sudo.so", NULL } },
+#endif
+#ifdef BUILD_AUTOFS
+    { "libsss_autofs.so", { LIBPFX"libsss_autofs.so", NULL } },
+#endif
+#ifdef HAVE_KRB5_LOCATOR_PLUGIN
+    { "sssd_krb5_locator_plugin.so", { LIBPFX"sssd_krb5_locator_plugin.so",
+                                       NULL } },
+#endif
+#ifdef HAVE_PAC_RESPONDER
+    { "sssd_pac_plugin.so", { LIBPFX"sssd_pac_plugin.so", NULL } },
+#endif
+    { "memberof.so", { LIBPFX"memberof.so", NULL } },
+    { "libsss_child.so", { "libtevent.so",
+                           LIBPFX"libsss_debug.so",
+                           LIBPFX"libsss_crypt.so",
+                           LIBPFX"libsss_util.so",
+                           LIBPFX"libsss_child.so", NULL } },
+    { "libsss_crypt.so", { "libtalloc.so",
+                           LIBPFX"libsss_debug.so",
+                           LIBPFX"libsss_crypt.so", NULL } },
+    { "libsss_util.so", { "libtalloc.so",
+                           LIBPFX"libsss_debug.so",
+                           LIBPFX"libsss_crypt.so",
+                           LIBPFX"libsss_util.so", NULL } },
+    { "libsss_simple.so", { LIBPFX"libdlopen_test_providers.so",
+                            LIBPFX"libsss_simple.so", NULL } },
+    { "libsss_ad.so", { LIBPFX"libdlopen_test_providers.so",
+                        LIBPFX"libsss_ad.so", NULL } },
+    { "libsss_ipa.so", { LIBPFX"libdlopen_test_providers.so",
+                         LIBPFX"libsss_ipa.so", NULL } },
+    { "libsss_krb5.so", { LIBPFX"libdlopen_test_providers.so",
+                          LIBPFX"libsss_krb5.so", NULL } },
+    { "libsss_ldap.so", { LIBPFX"libdlopen_test_providers.so",
+                          LIBPFX"libsss_ldap.so", NULL } },
+    { "libsss_proxy.so", { LIBPFX"libdlopen_test_providers.so",
+                           LIBPFX"libsss_proxy.so", NULL } },
+#ifdef HAVE_PYTHON_BINDINGS
+    { "pyhbac.so", { LIBPFX"pyhbac.so", NULL } },
+    { "pysss.so", { LIBPFX"pysss.so", NULL } },
+    { "pysss_murmur.so", { LIBPFX"pysss_murmur.so", NULL } },
+    { "pysss_nss_idmap.so", { LIBPFX"pysss_nss_idmap.so", NULL } },
+#endif
+    { NULL }
+};
+
+static bool recursive_dlopen(const char **name, int round, char **errmsg)
+{
+    void *handle;
+    bool ok;
+
+    *errmsg = NULL;
+
+    handle = dlopen(name[round], RTLD_GLOBAL|RTLD_NOW);
+    if (!handle) {
+        asprintf(errmsg, "dlopen() failed: %s", dlerror());
+        return false;
+    }
+
+    round++;
+    if (name[round]) {
+        ok = recursive_dlopen(name, round, errmsg);
+    } else {
+        ok = true;
+    }
+
+    dlclose(handle);
+    return ok;
+}
+
+START_TEST(test_dlopen_base)
+{
+    char *errmsg;
+    bool ok;
+    int i;
+
+    for (i = 0; so[i].name != NULL; i++) {
+        ok = recursive_dlopen(so[i].libs, 0, &errmsg);
+        fail_unless(ok, "Error opening %s: [%s]", so[i].name, errmsg);
+    }
+}
+END_TEST
+
+Suite *dlopen_suite(void)
+{
+    Suite *s = suite_create("dlopen");
+
+    TCase *tc_dlopen = tcase_create("dlopen");
+
+    tcase_add_test(tc_dlopen, test_dlopen_base);
+    tcase_set_timeout(tc_dlopen, 10);
+
+    suite_add_tcase(s, tc_dlopen);
+
+    return s;
+}
+
+int main(int argc, const char *argv[])
+{
+    int number_failed;
+
+    Suite *s = dlopen_suite();
+    SRunner *sr = srunner_create(s);
+
+    srunner_run_all(sr, CK_NORMAL);
+    number_failed = srunner_ntests_failed(sr);
+    srunner_free(sr);
+
+    if (number_failed == 0)
+        return EXIT_SUCCESS;
+
+    return EXIT_FAILURE;
+}
-- 
1.8.3.1

