|
|
905b4d |
From 9d501b05b47a63e29c586c6dd3d40ac615a98886 Mon Sep 17 00:00:00 2001
|
|
|
905b4d |
From: Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
Date: Mon, 1 Dec 2014 12:04:25 +0100
|
|
|
905b4d |
Subject: [PATCH 126/128] TESTS: Basic child tests
|
|
|
905b4d |
|
|
|
905b4d |
The child_common.c module had no unit tests, yet we need to amend it.
|
|
|
905b4d |
|
|
|
905b4d |
Reviewed-by: Sumit Bose <sbose@redhat.com>
|
|
|
905b4d |
---
|
|
|
905b4d |
Makefile.am | 25 ++++++
|
|
|
905b4d |
src/tests/cmocka/test_child.c | 83 +++++++++++++++++++
|
|
|
905b4d |
src/tests/cmocka/test_child_common.c | 150 +++++++++++++++++++++++++++++++++++
|
|
|
905b4d |
3 files changed, 258 insertions(+)
|
|
|
905b4d |
create mode 100644 src/tests/cmocka/test_child.c
|
|
|
905b4d |
create mode 100644 src/tests/cmocka/test_child_common.c
|
|
|
905b4d |
|
|
|
905b4d |
diff --git a/Makefile.am b/Makefile.am
|
|
|
905b4d |
index 6f6db56f5d6229b530cc6f18f66c42f22140bdeb..b719c646721835096931819b10be6f1a766ef22e 100644
|
|
|
905b4d |
--- a/Makefile.am
|
|
|
905b4d |
+++ b/Makefile.am
|
|
|
905b4d |
@@ -216,6 +216,7 @@ if HAVE_CMOCKA
|
|
|
905b4d |
test_sysdb_views \
|
|
|
905b4d |
test_copy_ccache \
|
|
|
905b4d |
test_copy_keytab \
|
|
|
905b4d |
+ test_child_common \
|
|
|
905b4d |
$(NULL)
|
|
|
905b4d |
|
|
|
905b4d |
if BUILD_IFP
|
|
|
905b4d |
@@ -233,6 +234,7 @@ endif # HAVE_CMOCKA
|
|
|
905b4d |
check_PROGRAMS = \
|
|
|
905b4d |
stress-tests \
|
|
|
905b4d |
krb5-child-test \
|
|
|
905b4d |
+ test-child \
|
|
|
905b4d |
$(non_interactive_cmocka_based_tests) \
|
|
|
905b4d |
$(non_interactive_check_based_tests)
|
|
|
905b4d |
|
|
|
905b4d |
@@ -2115,6 +2117,29 @@ test_copy_keytab_LDADD = \
|
|
|
905b4d |
libsss_test_common.la \
|
|
|
905b4d |
$(NULL)
|
|
|
905b4d |
|
|
|
905b4d |
+test_child_SOURCES = \
|
|
|
905b4d |
+ src/tests/cmocka/test_child.c \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+test_child_LDADD = \
|
|
|
905b4d |
+ $(POPT_LIBS) \
|
|
|
905b4d |
+ $(SSSD_INTERNAL_LTLIBS) \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+
|
|
|
905b4d |
+test_child_common_SOURCES = \
|
|
|
905b4d |
+ src/tests/cmocka/test_child_common.c \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+test_child_common_CFLAGS = \
|
|
|
905b4d |
+ $(AM_CFLAGS) \
|
|
|
905b4d |
+ -DCHILD_DIR=\"$(builddir)\" \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+test_child_common_LDADD = \
|
|
|
905b4d |
+ $(CMOCKA_LIBS) \
|
|
|
905b4d |
+ $(POPT_LIBS) \
|
|
|
905b4d |
+ $(TALLOC_LIBS) \
|
|
|
905b4d |
+ $(SSSD_INTERNAL_LTLIBS) \
|
|
|
905b4d |
+ libsss_test_common.la \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+
|
|
|
905b4d |
endif # HAVE_CMOCKA
|
|
|
905b4d |
|
|
|
905b4d |
noinst_PROGRAMS = pam_test_client
|
|
|
905b4d |
diff --git a/src/tests/cmocka/test_child.c b/src/tests/cmocka/test_child.c
|
|
|
905b4d |
new file mode 100644
|
|
|
905b4d |
index 0000000000000000000000000000000000000000..a857afce1fe7c4f3949fe77fe4e0a24e15961a4f
|
|
|
905b4d |
--- /dev/null
|
|
|
905b4d |
+++ b/src/tests/cmocka/test_child.c
|
|
|
905b4d |
@@ -0,0 +1,83 @@
|
|
|
905b4d |
+/*
|
|
|
905b4d |
+ SSSD
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Tests -- a simple test process that echoes input back
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Authors:
|
|
|
905b4d |
+ Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Copyright (C) 2014 Red Hat
|
|
|
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 <unistd.h>
|
|
|
905b4d |
+#include <stdlib.h>
|
|
|
905b4d |
+#include <popt.h>
|
|
|
905b4d |
+
|
|
|
905b4d |
+#include "util/util.h"
|
|
|
905b4d |
+#include "util/child_common.h"
|
|
|
905b4d |
+
|
|
|
905b4d |
+int main(int argc, const char *argv[])
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ int opt;
|
|
|
905b4d |
+ int debug_fd = -1;
|
|
|
905b4d |
+ poptContext pc;
|
|
|
905b4d |
+ const char *action;
|
|
|
905b4d |
+ ssize_t len = 0;
|
|
|
905b4d |
+ ssize_t written;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+ uint8_t *buf[IN_BUF_SIZE];
|
|
|
905b4d |
+ uid_t uid;
|
|
|
905b4d |
+ gid_t gid;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ struct poptOption long_options[] = {
|
|
|
905b4d |
+ POPT_AUTOHELP
|
|
|
905b4d |
+ {"debug-level", 'd', POPT_ARG_INT, &debug_level, 0,
|
|
|
905b4d |
+ _("Debug level"), NULL},
|
|
|
905b4d |
+ {"debug-timestamps", 0, POPT_ARG_INT, &debug_timestamps, 0,
|
|
|
905b4d |
+ _("Add debug timestamps"), NULL},
|
|
|
905b4d |
+ {"debug-microseconds", 0, POPT_ARG_INT, &debug_microseconds, 0,
|
|
|
905b4d |
+ _("Show timestamps with microseconds"), NULL},
|
|
|
905b4d |
+ {"debug-fd", 0, POPT_ARG_INT, &debug_fd, 0,
|
|
|
905b4d |
+ _("An open file descriptor for the debug logs"), NULL},
|
|
|
905b4d |
+ {"debug-to-stderr", 0, POPT_ARG_NONE | POPT_ARGFLAG_DOC_HIDDEN, &debug_to_stderr, 0, \
|
|
|
905b4d |
+ _("Send the debug output to stderr directly."), NULL }, \
|
|
|
905b4d |
+ SSSD_SERVER_OPTS(uid, gid)
|
|
|
905b4d |
+ POPT_TABLEEND
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ /* Set debug level to invalid value so we can decide 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 |
+ _exit(1);
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ DEBUG(SSSDBG_TRACE_FUNC, "test_child completed successfully\n");
|
|
|
905b4d |
+ _exit(0);
|
|
|
905b4d |
+
|
|
|
905b4d |
+fail:
|
|
|
905b4d |
+ DEBUG(SSSDBG_TRACE_FUNC, "test_child completed successfully\n");
|
|
|
905b4d |
+ close(STDOUT_FILENO);
|
|
|
905b4d |
+ _exit(-1);
|
|
|
905b4d |
+}
|
|
|
905b4d |
diff --git a/src/tests/cmocka/test_child_common.c b/src/tests/cmocka/test_child_common.c
|
|
|
905b4d |
new file mode 100644
|
|
|
905b4d |
index 0000000000000000000000000000000000000000..f2cd8c0081c2e85432db1c9696102780dc990e75
|
|
|
905b4d |
--- /dev/null
|
|
|
905b4d |
+++ b/src/tests/cmocka/test_child_common.c
|
|
|
905b4d |
@@ -0,0 +1,150 @@
|
|
|
905b4d |
+/*
|
|
|
905b4d |
+ Authors:
|
|
|
905b4d |
+ Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Copyright (C) 2014 Red Hat
|
|
|
905b4d |
+
|
|
|
905b4d |
+ SSSD tests: Child handlers
|
|
|
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 <talloc.h>
|
|
|
905b4d |
+#include <tevent.h>
|
|
|
905b4d |
+#include <errno.h>
|
|
|
905b4d |
+#include <popt.h>
|
|
|
905b4d |
+
|
|
|
905b4d |
+#include "util/child_common.h"
|
|
|
905b4d |
+#include "tests/cmocka/common_mock.h"
|
|
|
905b4d |
+
|
|
|
905b4d |
+#define TEST_BIN "test-child"
|
|
|
905b4d |
+#define ECHO_STR "Hello child"
|
|
|
905b4d |
+
|
|
|
905b4d |
+struct child_test_ctx {
|
|
|
905b4d |
+ int pipefd_to_child[2];
|
|
|
905b4d |
+ int pipefd_from_child[2];
|
|
|
905b4d |
+
|
|
|
905b4d |
+ struct sss_test_ctx *test_ctx;
|
|
|
905b4d |
+};
|
|
|
905b4d |
+
|
|
|
905b4d |
+void child_test_setup(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ struct child_test_ctx *child_tctx;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ check_leaks_push(global_talloc_context);
|
|
|
905b4d |
+ child_tctx = talloc(global_talloc_context, struct child_test_ctx);
|
|
|
905b4d |
+ assert_non_null(child_tctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ child_tctx->test_ctx = create_ev_test_ctx(child_tctx);
|
|
|
905b4d |
+ assert_non_null(child_tctx->test_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = pipe(child_tctx->pipefd_from_child);
|
|
|
905b4d |
+ assert_int_not_equal(ret, -1);
|
|
|
905b4d |
+ DEBUG(SSSDBG_TRACE_LIBS, "from_child: %d:%d\n",
|
|
|
905b4d |
+ child_tctx->pipefd_from_child[0],
|
|
|
905b4d |
+ child_tctx->pipefd_from_child[1]);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = pipe(child_tctx->pipefd_to_child);
|
|
|
905b4d |
+ assert_int_not_equal(ret, -1);
|
|
|
905b4d |
+ DEBUG(SSSDBG_TRACE_LIBS, "to_child: %d:%d\n",
|
|
|
905b4d |
+ child_tctx->pipefd_to_child[0],
|
|
|
905b4d |
+ child_tctx->pipefd_to_child[1]);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ *state = child_tctx;
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void child_test_teardown(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ struct child_test_ctx *child_tctx = talloc_get_type(*state,
|
|
|
905b4d |
+ struct child_test_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ talloc_free(child_tctx);
|
|
|
905b4d |
+ check_leaks_pop(global_talloc_context);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+/* Just make sure the exec works. The child does nothing but exits */
|
|
|
905b4d |
+void test_exec_child(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+ pid_t child_pid;
|
|
|
905b4d |
+ int status;
|
|
|
905b4d |
+ struct child_test_ctx *child_tctx = talloc_get_type(*state,
|
|
|
905b4d |
+ struct child_test_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ child_pid = fork();
|
|
|
905b4d |
+ assert_int_not_equal(child_pid, -1);
|
|
|
905b4d |
+ if (child_pid == 0) {
|
|
|
905b4d |
+ ret = exec_child(child_tctx,
|
|
|
905b4d |
+ child_tctx->pipefd_to_child,
|
|
|
905b4d |
+ child_tctx->pipefd_from_child,
|
|
|
905b4d |
+ CHILD_DIR"/"TEST_BIN, 2);
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ } else {
|
|
|
905b4d |
+ do {
|
|
|
905b4d |
+ errno = 0;
|
|
|
905b4d |
+ ret = waitpid(child_pid, &status, 0);
|
|
|
905b4d |
+ } while (ret == -1 && errno == EINTR);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (ret > 0) {
|
|
|
905b4d |
+ ret = EIO;
|
|
|
905b4d |
+ if (WIFEXITED(status)) {
|
|
|
905b4d |
+ ret = WEXITSTATUS(status);
|
|
|
905b4d |
+ assert_int_equal(ret, 0);
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ } else {
|
|
|
905b4d |
+ DEBUG(SSSDBG_FUNC_DATA,
|
|
|
905b4d |
+ "Failed to wait for children %d\n", child_pid);
|
|
|
905b4d |
+ ret = EIO;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+int main(int argc, const char *argv[])
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ int rv;
|
|
|
905b4d |
+ poptContext pc;
|
|
|
905b4d |
+ int opt;
|
|
|
905b4d |
+ struct poptOption long_options[] = {
|
|
|
905b4d |
+ POPT_AUTOHELP
|
|
|
905b4d |
+ SSSD_DEBUG_OPTS
|
|
|
905b4d |
+ POPT_TABLEEND
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ const UnitTest tests[] = {
|
|
|
905b4d |
+ unit_test_setup_teardown(test_exec_child,
|
|
|
905b4d |
+ child_test_setup,
|
|
|
905b4d |
+ child_test_teardown),
|
|
|
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 |
+ rv = run_tests(tests);
|
|
|
905b4d |
+ return rv;
|
|
|
905b4d |
+}
|
|
|
905b4d |
--
|
|
|
905b4d |
1.9.3
|
|
|
905b4d |
|