From 1657efe255b2bcebfebae9f67d1575d8ad6d2ef1 Mon Sep 17 00:00:00 2001
From: Petr Cech <pcech@redhat.com>
Date: Thu, 25 Feb 2016 03:28:13 -0500
Subject: [PATCH] TESTS: WIP Test of sysdb_search_sudo_rules

This is test of new function sysdb_search_sudo_rules.
At the moment, it is not working, there is memory leak.

Resolves:
https://fedorahosted.org/sssd/ticket/2081
---
 Makefile.am                        |  18 +++
 src/tests/cmocka/test_sysdb_sudo.c | 277 +++++++++++++++++++++++++++++++++++++
 2 files changed, 295 insertions(+)
 create mode 100644 src/tests/cmocka/test_sysdb_sudo.c

diff --git a/Makefile.am b/Makefile.am
index 4e4f38a5eaf5bfa2cfafa88b9b32848e6c27131b..34d50505e2119821a757c1e2ae068c3bae4e868d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -230,6 +230,7 @@ if HAVE_CMOCKA
         sdap-tests \
         test_sysdb_views \
         test_sysdb_subdomains \
+        test_sysdb_sudo \
         test_sysdb_utils \
         test_be_ptask \
         test_copy_ccache \
@@ -2360,6 +2361,23 @@ test_sysdb_subdomains_LDADD = \
     libsss_test_common.la \
     $(NULL)
 
+test_sysdb_sudo_SOURCES = \
+    src/tests/cmocka/test_sysdb_sudo.c \
+    src/db/sysdb_sudo.c \
+    src/util/debug.c \
+    $(NULL)
+test_sysdb_sudo_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(NULL)
+test_sysdb_sudo_LDADD = \
+    $(CMOCKA_LIBS) \
+    $(LDB_LIBS) \
+    $(POPT_LIBS) \
+    $(TALLOC_LIBS) \
+    $(SSSD_INTERNAL_LTLIBS) \
+    libsss_test_common.la \
+    $(NULL)
+
 test_sysdb_utils_SOURCES = \
     src/tests/cmocka/test_sysdb_utils.c \
     $(NULL)
diff --git a/src/tests/cmocka/test_sysdb_sudo.c b/src/tests/cmocka/test_sysdb_sudo.c
new file mode 100644
index 0000000000000000000000000000000000000000..09dabfd56e148bc191adff5ad3f3f0dc305d8e40
--- /dev/null
+++ b/src/tests/cmocka/test_sysdb_sudo.c
@@ -0,0 +1,277 @@
+/*
+    Authors:
+        Petr Čech <pcech@redhat.com>
+
+    Copyright (C) 2016 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/>.
+*/
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <popt.h>
+
+#include "tests/cmocka/common_mock.h"
+#include "src/db/sysdb_sudo.h"
+#include "responder/sudo/sudosrv_private.h" /* for struct sudo_cmd_ctx, enum sss_sudo_type */
+#include "db/sysdb_private.h" /* for sysdb->ldb member */
+
+#define TESTS_PATH "tp_" BASE_FILE_STEM
+#define TEST_CONF_FILE "tests_conf.ldb"
+
+#define TEST_DOM_NAME "test_domain"
+
+#define TEST_RULE_NAME  "test_rule"
+
+struct sysdb_test_ctx {
+    struct sysdb_ctx *sysdb;
+    struct sysdb_attrs **rules;
+    struct confdb_ctx *confdb;
+    struct sss_domain_info *domain;
+    struct tevent_context *ev;
+};
+
+static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
+{
+    struct sysdb_test_ctx *test_ctx;
+    char *conf_db;
+    int ret;
+
+    const char *val[2];
+    val[1] = NULL;
+
+    /* Create tests directory if it doesn't exist */
+    /* (relative to current dir) */
+    ret = mkdir(TESTS_PATH, 0775);
+    assert_true(ret == 0 || errno == EEXIST);
+
+    test_ctx = talloc_zero(global_talloc_context, struct sysdb_test_ctx);
+    assert_non_null(test_ctx);
+
+    /* Create an event context
+     * It will not be used except in confdb_init and sysdb_init
+     */
+    test_ctx->ev = tevent_context_init(test_ctx);
+    assert_non_null(test_ctx->ev);
+
+    conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
+    assert_non_null(conf_db);
+    DEBUG(SSSDBG_MINOR_FAILURE, "CONFDB: %s\n", conf_db);
+
+    /* Connect to the conf db */
+    ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
+    assert_int_equal(ret, EOK);
+
+    /* Configuration */
+    val[0] = TEST_DOM_NAME;
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/sssd", "domains", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = "sudo";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/sssd", "services", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = "ipa";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/domain/" TEST_DOM_NAME, "id_provider", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = TEST_DOM_NAME;
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/domain/" TEST_DOM_NAME, "ipa_domain", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = "TRUE";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/domain/" TEST_DOM_NAME, "use_fully_qualified_names", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = enumerate ? "TRUE" : "FALSE";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/domain/" TEST_DOM_NAME, "enumerate", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = "TRUE";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/domain/" TEST_DOM_NAME, "cache_credentials", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = "1";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/domain/" TEST_DOM_NAME, "pwd_expiration_warning", val);
+    assert_int_equal(ret, EOK);
+
+
+    /* SUDO RULES Configuration */
+    val[0] = "0xFFF0";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/sudo", "debug_level", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = "20";
+    ret = confdb_add_param(test_ctx->confdb, true,
+                           "config/sudo", "entry_cache_sudo_timeout", val);
+    assert_int_equal(ret, EOK);
+
+    /* DOMAIN */
+    ret = sssd_domain_init(test_ctx, test_ctx->confdb, TEST_DOM_NAME,
+                           TESTS_PATH, &test_ctx->domain);
+    assert_int_equal(ret, EOK);
+
+    /* Rules */
+    test_ctx->rules = NULL;
+
+    *ctx = test_ctx;
+    return EOK;
+}
+
+#define setup_sysdb_tests(ctx) _setup_sysdb_tests((ctx), false)
+
+static int test_sysdb_setup(void **state)
+{
+    int ret;
+    struct sysdb_test_ctx *test_ctx;
+
+    assert_true(leak_check_setup());
+    check_leaks_push(global_talloc_context);
+
+    ret = setup_sysdb_tests(&test_ctx);
+    assert_int_equal(ret, EOK);
+
+    check_leaks_push(test_ctx);
+    *state = (void *) test_ctx;
+    return 0;
+}
+
+static int test_sysdb_teardown(void **state)
+{
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    assert_true(check_leaks_pop(test_ctx));
+    talloc_free(test_ctx);
+    assert_true(check_leaks_pop(global_talloc_context));
+    assert_true(leak_check_teardown());
+    return 0;
+}
+
+void test_store_sudo(void **state)
+{
+    errno_t ret;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+
+    test_ctx->rules = talloc_array(test_ctx, struct sysdb_attrs *, 1);
+    test_ctx->rules[0] = sysdb_new_attrs(test_ctx);
+    assert_non_null(test_ctx->rules[0]);
+
+    ret = sysdb_attrs_add_string_safe(test_ctx->rules[0], SYSDB_SUDO_CACHE_AT_CN,
+                                      TEST_RULE_NAME);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string_safe(test_ctx->rules[0], SYSDB_SUDO_CACHE_AT_HOST,
+                                      "localhost");
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string_safe(test_ctx->rules[0], "name",
+                                      "one_rule");
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string_safe(test_ctx->rules[0], "dn",
+                                      "name=one_rule,cn=sudorules,cn=custom,cn=" TEST_DOM_NAME ",cn=sysdb");
+    assert_int_equal(ret, EOK);
+
+
+    fprintf(stderr, "\n\n");
+    fprintf(stderr, "There is call of `sysdb_sudo_store(test_ctx->domain, test_ctx->rules, 1);`\n");
+    fprintf(stderr, "\n\n");
+    fprintf(stderr, "talloc_report(test_ctx->domain) BEFORE call:\n");
+    talloc_report(test_ctx->domain, stderr);
+    fprintf(stderr, "\n");
+    fprintf(stderr, "talloc_report(test_ctx->domain->sysdb) BEFORE call:\n");
+    talloc_report(test_ctx->domain->sysdb, stderr);
+    fprintf(stderr, "\n");
+    fprintf(stderr, "talloc_report(test_ctx->domain->sysdb->ldb_file) BEFORE call:\n");
+    talloc_report(test_ctx->domain->sysdb->ldb_file, stderr);
+    fprintf(stderr, "\n");
+
+    ret = sysdb_sudo_store(test_ctx->domain, test_ctx->rules, 1);
+    assert_int_equal(ret, EOK);
+
+    fprintf(stderr, "talloc_report(test_ctx->domain) AFTER call:\n");
+    talloc_report(test_ctx->domain, stderr);
+    fprintf(stderr, "\n");
+    fprintf(stderr, "talloc_report(test_ctx->domain->sysdb) AFTER call:\n");
+    talloc_report(test_ctx->domain->sysdb, stderr);
+    fprintf(stderr, "\n");
+    fprintf(stderr, "talloc_report(test_ctx->domain->sysdb->ldb_file) AFTER call:\n");
+    talloc_report(test_ctx->domain->sysdb->ldb_file, stderr);
+    fprintf(stderr, "\n");
+
+
+    talloc_zfree(test_ctx->rules[0]);
+    talloc_zfree(test_ctx->rules);
+}
+
+int main(int argc, const char *argv[])
+{
+    int rv;
+    int no_cleanup = 0;
+    poptContext pc;
+    int opt;
+    struct poptOption long_options[] = {
+        POPT_AUTOHELP
+        SSSD_DEBUG_OPTS
+        {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
+         _("Do not delete the test database after a test run"), NULL },
+        POPT_TABLEEND
+    };
+
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test_setup_teardown(test_store_sudo,
+                                        test_sysdb_setup, test_sysdb_teardown),
+    };
+
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
+    debug_level = SSSDBG_TRACE_LIBS;
+
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
+    while((opt = poptGetNextOpt(pc)) != -1) {
+        switch(opt) {
+        default:
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
+                    poptBadOption(pc, 0), poptStrerror(opt));
+            poptPrintUsage(pc, stderr, 0);
+            return 1;
+        }
+    }
+    poptFreeContext(pc);
+
+    DEBUG_CLI_INIT(debug_level);
+
+    tests_set_cwd();
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, TEST_DOM_NAME);
+    test_dom_suite_setup(TESTS_PATH);
+    rv = cmocka_run_group_tests(tests, NULL, NULL);
+
+    if (rv == 0 && no_cleanup == 0) {
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, TEST_DOM_NAME);
+    }
+    return rv;
+}
-- 
2.5.0

