Blame SOURCES/0009-TESTS-Unit-tests-for-server_setup.patch

905b4d
From dd8c2ad0efbd6bb4d1f942ce45e3b87d5ecb83b7 Mon Sep 17 00:00:00 2001
905b4d
From: Jakub Hrozek <jhrozek@redhat.com>
905b4d
Date: Fri, 3 Oct 2014 16:12:01 +0200
905b4d
Subject: [PATCH 09/22] TESTS: Unit tests for server_setup
905b4d
905b4d
We changed server_setup, so we must make sure the function continues to
905b4d
work as expected.
905b4d
905b4d
Reviewed-by: Pavel Reichl <preichl@redhat.com>
905b4d
---
905b4d
 src/tests/cwrap/Makefile.am   |  63 ++++++++++++-
905b4d
 src/tests/cwrap/test_server.c | 204 ++++++++++++++++++++++++++++++++++++++++++
905b4d
 2 files changed, 266 insertions(+), 1 deletion(-)
905b4d
 create mode 100644 src/tests/cwrap/test_server.c
905b4d
905b4d
diff --git a/src/tests/cwrap/Makefile.am b/src/tests/cwrap/Makefile.am
905b4d
index 96b9a52435d9e80bea3ec132cc86c3e6abd0adf2..28d60e7f5bc346543f51740ec4b2781a99a48909 100644
905b4d
--- a/src/tests/cwrap/Makefile.am
905b4d
+++ b/src/tests/cwrap/Makefile.am
905b4d
@@ -3,7 +3,10 @@ AM_CPPFLAGS = \
905b4d
     -Wall \
905b4d
     -I$(top_srcdir)/src \
905b4d
     -I. \
905b4d
+    -DLOCALEDIR=\"$(localedir)\" \
905b4d
+    -DVARDIR=\"$(localstatedir)\" \
905b4d
     $(DBUS_CFLAGS) \
905b4d
+    $(GLIB2_CFLAGS) \
905b4d
     $(NULL)
905b4d
 
905b4d
 TESTS_ENVIRONMENT = \
905b4d
@@ -16,6 +19,20 @@ dist_noinst_SCRIPTS = \
905b4d
     cwrap_test_setup.sh \
905b4d
     $(NULL)
905b4d
 
905b4d
+SSSD_LIBS = \
905b4d
+    $(TALLOC_LIBS) \
905b4d
+    $(TEVENT_LIBS) \
905b4d
+    $(POPT_LIBS) \
905b4d
+    $(LDB_LIBS) \
905b4d
+    $(DBUS_LIBS) \
905b4d
+    $(PCRE_LIBS) \
905b4d
+    $(INI_CONFIG_LIBS) \
905b4d
+    $(COLLECTION_LIBS) \
905b4d
+    $(DHASH_LIBS) \
905b4d
+    $(SSS_CRYPT_LIBS) \
905b4d
+    $(OPENLDAP_LIBS) \
905b4d
+    $(TDB_LIBS)
905b4d
+
905b4d
 dist_noinst_DATA = \
905b4d
     group \
905b4d
     passwd \
905b4d
@@ -25,7 +42,10 @@ check_PROGRAMS =
905b4d
 if HAVE_CMOCKA
905b4d
 if HAVE_NSS_WRAPPER
905b4d
 if HAVE_UID_WRAPPER
905b4d
-check_PROGRAMS += become_user-tests
905b4d
+check_PROGRAMS = \
905b4d
+	become_user-tests \
905b4d
+	server-tests \
905b4d
+	$(NULL)
905b4d
 endif # HAVE_UID_WRAPPER
905b4d
 endif # HAVE_NSS_WRAPPER
905b4d
 endif # HAVE_CMOCKA
905b4d
@@ -45,4 +65,45 @@ become_user_tests_LDADD = \
905b4d
     $(abs_top_builddir)/libsss_test_common.la \
905b4d
     $(NULL)
905b4d
 
905b4d
+server_tests_SOURCES = \
905b4d
+    test_server.c \
905b4d
+    ../../../src/util/server.c \
905b4d
+    ../../../src/util/become_user.c \
905b4d
+    ../../../src/util/backup_file.c \
905b4d
+    ../../../src/util/domain_info_utils.c \
905b4d
+    ../../../src/util/atomic_io.c \
905b4d
+    ../../../src/util/signal.c \
905b4d
+    ../../../src/util/util.c \
905b4d
+    ../../../src/util/strtonum.c \
905b4d
+    ../../../src/util/util_errors.c \
905b4d
+    ../../../src/util/safe-format-string.c \
905b4d
+    ../../../src/util/sss_tc_utf8.c \
905b4d
+    ../../../src/util/sss_utf8.c \
905b4d
+    ../../../src/util/usertools.c \
905b4d
+    ../../../src/confdb/confdb.c \
905b4d
+    ../../../src/db/sysdb.c \
905b4d
+    ../../../src/db/sysdb_upgrade.c \
905b4d
+    ../../../src/db/sysdb_ops.c \
905b4d
+    ../../../src/db/sysdb_search.c \
905b4d
+    ../../../src/db/sysdb_autofs.c \
905b4d
+    ../../../src/db/sysdb_services.c \
905b4d
+    ../../../src/db/sysdb_views.c \
905b4d
+    $(NULL)
905b4d
+server_tests_CFLAGS = \
905b4d
+    $(AM_CFLAGS) \
905b4d
+    $(LIBCAPNG_CFLAGS) \
905b4d
+    -DTEST_DB_PATH=\"server_tests\" \
905b4d
+    -DTEST_PID_PATH=\"server_tests\" \
905b4d
+    -DUNIT_TESTING \
905b4d
+    $(NULL)
905b4d
+server_tests_LDADD = \
905b4d
+    $(CMOCKA_LIBS) \
905b4d
+    $(LIBCAPNG_LIBS) \
905b4d
+    $(UNICODE_LIBS) \
905b4d
+    $(SSSD_LIBS) \
905b4d
+    $(abs_top_builddir)/libsss_debug.la \
905b4d
+    $(abs_top_builddir)/libsss_crypt.la \
905b4d
+    $(abs_top_builddir)/libsss_test_common.la \
905b4d
+    $(NULL)
905b4d
+
905b4d
 tests: $(check_PROGRAMS)
905b4d
diff --git a/src/tests/cwrap/test_server.c b/src/tests/cwrap/test_server.c
905b4d
new file mode 100644
905b4d
index 0000000000000000000000000000000000000000..26ecfee1e189b6a474ae52fdbfff6b8922b3f0d7
905b4d
--- /dev/null
905b4d
+++ b/src/tests/cwrap/test_server.c
905b4d
@@ -0,0 +1,204 @@
905b4d
+/*
905b4d
+    Authors:
905b4d
+        Jakub Hrozek <jhrozek@redhat.com>
905b4d
+
905b4d
+    Copyright (C) 2014 Red Hat
905b4d
+
905b4d
+    SSSD tests: Server instantiation
905b4d
+
905b4d
+    This program is free software; you can redistribute it and/or modify
905b4d
+    it under the terms of the GNU General Public License as published by
905b4d
+    the Free Software Foundation; either version 3 of the License, or
905b4d
+    (at your option) any later version.
905b4d
+
905b4d
+    This program is distributed in the hope that it will be useful,
905b4d
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
905b4d
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
905b4d
+    GNU General Public License for more details.
905b4d
+
905b4d
+    You should have received a copy of the GNU General Public License
905b4d
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
905b4d
+*/
905b4d
+
905b4d
+#include <sys/types.h>
905b4d
+#include <sys/stat.h>
905b4d
+#include <fcntl.h>
905b4d
+
905b4d
+#include <popt.h>
905b4d
+#include "util/util.h"
905b4d
+#include "tests/cmocka/common_mock.h"
905b4d
+
905b4d
+static void wait_for_fg_server(pid_t pid)
905b4d
+{
905b4d
+    pid_t wpid;
905b4d
+    int status;
905b4d
+
905b4d
+    assert_int_not_equal(pid, -1);
905b4d
+
905b4d
+    wpid = waitpid(pid, &status, 0);
905b4d
+    assert_int_equal(wpid, pid);
905b4d
+    assert_true(WIFEXITED(status));
905b4d
+    assert_int_equal(WEXITSTATUS(status), 0);
905b4d
+}
905b4d
+
905b4d
+static void wait_for_bg_server(const char *pidfile)
905b4d
+{
905b4d
+    int fd;
905b4d
+    long tmp;
905b4d
+    char buf[16];
905b4d
+    pid_t pid;
905b4d
+    int ret;
905b4d
+    int count;
905b4d
+
905b4d
+    count = 0;
905b4d
+    do {
905b4d
+        struct stat sb;
905b4d
+
905b4d
+        count++;
905b4d
+        if (count > 100) {
905b4d
+            break;
905b4d
+        }
905b4d
+
905b4d
+        ret = stat(pidfile, &sb);
905b4d
+        usleep(50);
905b4d
+    } while (ret != 0);
905b4d
+
905b4d
+    /* read the pidfile */
905b4d
+    fd = open(pidfile, O_RDONLY);
905b4d
+    assert_false(fd < 0);
905b4d
+
905b4d
+    ret = read(fd, buf, sizeof(buf));
905b4d
+    close(fd);
905b4d
+    assert_false(ret <= 0);
905b4d
+
905b4d
+    buf[sizeof(buf) - 1] = '\0';
905b4d
+
905b4d
+    tmp = strtol(buf, NULL, 10);
905b4d
+    assert_false(tmp == 0 || tmp > 0xFFFF || errno == ERANGE);
905b4d
+
905b4d
+    pid = (pid_t) (tmp & 0xFFFF);
905b4d
+
905b4d
+    /* Make sure the daemon goes away! */
905b4d
+    ret = kill(pid, SIGTERM);
905b4d
+    fprintf(stderr, "killing %u\n", pid);
905b4d
+    assert_true(ret == 0);
905b4d
+
905b4d
+    unlink(pidfile);
905b4d
+}
905b4d
+
905b4d
+void test_run_as_root_fg(void **state)
905b4d
+{
905b4d
+    int ret;
905b4d
+    struct main_context *main_ctx;
905b4d
+    pid_t pid;
905b4d
+
905b4d
+    /* Must root as root, real or fake */
905b4d
+    assert_int_equal(geteuid(), 0);
905b4d
+
905b4d
+    pid = fork();
905b4d
+    if (pid == 0) {
905b4d
+        ret = server_setup(__FUNCTION__, 0, 0, 0,
905b4d
+                           __FUNCTION__, &main_ctx);
905b4d
+        assert_int_equal(ret, 0);
905b4d
+        exit(0);
905b4d
+    }
905b4d
+    wait_for_fg_server(pid);
905b4d
+}
905b4d
+
905b4d
+void test_run_as_sssd_fg(void **state)
905b4d
+{
905b4d
+    int ret;
905b4d
+    struct main_context *main_ctx;
905b4d
+    struct passwd *sssd;
905b4d
+    pid_t pid;
905b4d
+
905b4d
+    /* Must root as root, real or fake */
905b4d
+    assert_int_equal(geteuid(), 0);
905b4d
+
905b4d
+    sssd = getpwnam("sssd");
905b4d
+    assert_non_null(sssd);
905b4d
+
905b4d
+    pid = fork();
905b4d
+    if (pid == 0) {
905b4d
+        ret = server_setup(__FUNCTION__, 0, sssd->pw_uid, sssd->pw_gid,
905b4d
+                           __FUNCTION__, &main_ctx);
905b4d
+        assert_int_equal(ret, 0);
905b4d
+        exit(0);
905b4d
+    }
905b4d
+    wait_for_fg_server(pid);
905b4d
+}
905b4d
+
905b4d
+void test_run_as_root_daemon(void **state)
905b4d
+{
905b4d
+    int ret;
905b4d
+    struct main_context *main_ctx;
905b4d
+    pid_t pid;
905b4d
+    char *pidfile;
905b4d
+
905b4d
+    /* Must root as root, real or fake */
905b4d
+    assert_int_equal(geteuid(), 0);
905b4d
+
905b4d
+    pidfile = talloc_asprintf(NULL, "%s/%s.pid", TEST_PID_PATH, __FUNCTION__);
905b4d
+
905b4d
+    /* Make sure there are no leftovers */
905b4d
+    unlink(pidfile);
905b4d
+
905b4d
+    pid = fork();
905b4d
+    if (pid == 0) {
905b4d
+        ret = server_setup(__FUNCTION__, FLAGS_PID_FILE,
905b4d
+                           0, 0, __FUNCTION__, &main_ctx);
905b4d
+        assert_int_equal(ret, 0);
905b4d
+
905b4d
+        server_loop(main_ctx);
905b4d
+        exit(0);
905b4d
+    }
905b4d
+
905b4d
+    wait_for_bg_server(pidfile);
905b4d
+    talloc_free(pidfile);
905b4d
+}
905b4d
+
905b4d
+int main(int argc, const char *argv[])
905b4d
+{
905b4d
+    poptContext pc;
905b4d
+    int opt;
905b4d
+    int rv;
905b4d
+    struct poptOption long_options[] = {
905b4d
+        POPT_AUTOHELP
905b4d
+        SSSD_DEBUG_OPTS
905b4d
+        POPT_TABLEEND
905b4d
+    };
905b4d
+
905b4d
+    const UnitTest tests[] = {
905b4d
+        unit_test(test_run_as_root_fg),
905b4d
+        unit_test(test_run_as_sssd_fg),
905b4d
+        unit_test(test_run_as_root_daemon),
905b4d
+    };
905b4d
+
905b4d
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
905b4d
+    debug_level = SSSDBG_INVALID;
905b4d
+
905b4d
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
905b4d
+    while((opt = poptGetNextOpt(pc)) != -1) {
905b4d
+        switch(opt) {
905b4d
+        default:
905b4d
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
905b4d
+                    poptBadOption(pc, 0), poptStrerror(opt));
905b4d
+            poptPrintUsage(pc, stderr, 0);
905b4d
+            return 1;
905b4d
+        }
905b4d
+    }
905b4d
+    poptFreeContext(pc);
905b4d
+
905b4d
+    DEBUG_CLI_INIT(debug_level);
905b4d
+
905b4d
+    /* Even though normally the tests should clean up after themselves
905b4d
+     * they might not after a failed run. Remove the old db to be sure */
905b4d
+    tests_set_cwd();
905b4d
+    test_dom_suite_cleanup(TEST_DB_PATH, CONFDB_FILE, NULL);
905b4d
+    test_dom_suite_setup(TEST_DB_PATH);
905b4d
+
905b4d
+    rv = run_tests(tests);
905b4d
+    test_dom_suite_cleanup(TEST_DB_PATH, CONFDB_FILE, NULL);
905b4d
+
905b4d
+    return rv;
905b4d
+}
905b4d
-- 
905b4d
1.9.3
905b4d