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

There are tests functions of sysdb_sudo_rules.

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

diff --git a/Makefile.am b/Makefile.am
index f5d0400cd741135e4f57f272bc9a51ecc7ead2f2..8b99f3f760ca1814f2c04faf753b351c58f8f0ed 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -231,6 +231,7 @@ if HAVE_CMOCKA
         sdap-tests \
         test_sysdb_views \
         test_sysdb_subdomains \
+        test_sysdb_sudo \
         test_sysdb_utils \
         test_be_ptask \
         test_copy_ccache \
@@ -2374,6 +2375,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..27a4c1b728f659e5924e041f29b68925ce8dd620
--- /dev/null
+++ b/src/tests/cmocka/test_sysdb_sudo.c
@@ -0,0 +1,648 @@
+/*
+    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"
+
+#define TESTS_PATH "tp_" BASE_FILE_STEM
+#define TEST_CONF_DB "test_sysdb_sudorules.ldb"
+#define TEST_DOM_NAME "test_domain.test"
+
+#define TEST_CACHE_SUDO_TIMEOUT "20"
+
+#define TEST_USER_NON_EXIST "no_user"
+
+#define TEST_GROUP_NAME "test_sudo_group"
+#define TEST_GID 10001
+
+struct test_user {
+    const char *name;
+    uid_t uid;
+    gid_t gid;
+} users[] = {{"test_user1", 1001, 1001},
+             {"test_user2", 1002, 1002},
+             {"test_user3", 1003, 1003}};
+
+struct test_rule {
+    const char *name;
+    const char *host;
+    const char *as_user;
+} rules[] = {{"test_rule1", "test_host1.test_domain.test", "root"},
+             {"test_rule2", "test_host2.test_domain.test", "root"},
+             {"test_rule3", "test_host3.test_domain.test", "root"}};
+
+struct sysdb_test_ctx {
+    struct sss_test_ctx *tctx;
+    struct sysdb_attrs **rules;
+};
+
+static void create_groups(struct sss_domain_info *domain)
+{
+    errno_t ret;
+
+    ret = sysdb_add_group(domain, TEST_GROUP_NAME, TEST_GID,
+                          NULL, 30, time(NULL));
+    assert_int_equal(ret, EOK);
+}
+
+static void remove_groups(struct sss_domain_info *domain)
+{
+    errno_t ret;
+
+    ret = sysdb_delete_group(domain, TEST_GROUP_NAME, TEST_GID);
+    assert_int_equal(ret, EOK);
+
+}
+
+static void create_users(struct sss_domain_info *domain)
+{
+    errno_t ret;
+    int gid;
+
+    for (int i=0; i < 3; i++) {
+        gid = (i == 0) ? 0 : TEST_GID;
+        ret = sysdb_add_user(domain, users[i].name, users[i].uid, gid,
+                             users[i].name, NULL, "/bin/bash", domain->name,
+                             NULL, 30, time(NULL));
+        assert_int_equal(ret, EOK);
+    }
+}
+
+static void remove_users(struct sss_domain_info *domain)
+{
+    errno_t ret;
+
+    for (int i=0; i < 3; i++) {
+        ret = sysdb_delete_user(domain, users[i].name, users[i].uid);
+        assert_int_equal(ret, EOK);
+    }
+}
+
+static void create_rule(struct sysdb_attrs *rule,
+                        struct sss_domain_info *domain,
+                        const char *name,
+                        const char *host,
+                        const char *run_as_user,
+                        const char *sudo_user)
+{
+    errno_t ret;
+
+    ret = sysdb_attrs_add_string_safe(rule, SYSDB_SUDO_CACHE_AT_CN, name);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string_safe(rule, SYSDB_SUDO_CACHE_AT_HOST, host);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string_safe(rule, SYSDB_SUDO_CACHE_AT_RUNASUSER,
+                                      run_as_user);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_attrs_add_string_safe(rule, SYSDB_SUDO_CACHE_AT_USER,
+                                      sudo_user);
+    assert_int_equal(ret, EOK);
+}
+
+static void remove_rule(struct sysdb_test_ctx *test_ctx, const char *user_name)
+{
+    errno_t ret;
+    char *delete_filter;
+
+    ret = sysdb_get_sudo_filter(test_ctx, user_name, 0, NULL,
+                                SYSDB_SUDO_FILTER_USERNAME, &delete_filter);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_sudo_purge(test_ctx->tctx->dom, delete_filter, NULL, 0);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(delete_filter);
+}
+
+/* This function create set consisting of rules 2 and 3. */
+static void create_rules(struct sysdb_test_ctx *test_ctx)
+{
+    errno_t ret;
+
+    test_ctx->rules = talloc_array(test_ctx, struct sysdb_attrs *, 2);
+
+    for (int i=0; i < 2; i++) {
+        test_ctx->rules[i] = sysdb_new_attrs(test_ctx->rules);
+        assert_non_null(test_ctx->rules[i]);
+
+        create_rule(test_ctx->rules[i], test_ctx->tctx->dom,
+                    rules[i+1].name, rules[i+1].host,
+                    rules[i+1].as_user, users[i+1].name);
+    }
+
+    ret = sysdb_sudo_store(test_ctx->tctx->dom, test_ctx->rules, 2);
+    assert_int_equal(ret, EOK);
+}
+
+static void remove_rules(struct sysdb_test_ctx *test_ctx)
+{
+    for (int i=0; i < 3; i++) {
+    remove_rule(test_ctx, users[i].name);
+    }
+}
+
+static int test_sysdb_setup(void **state, bool rules_exist)
+{
+    int ret;
+    struct sysdb_test_ctx *test_ctx;
+    struct sss_test_conf_param params[] = {
+        { NULL, NULL },             /* Sentinel */
+    };
+    const char *val[2];
+    val[1] = NULL;
+
+    assert_true(leak_check_setup());
+    check_leaks_push(global_talloc_context);
+
+    test_ctx = talloc_zero(global_talloc_context, struct sysdb_test_ctx);
+    assert_non_null(test_ctx);
+
+    test_dom_suite_setup(TESTS_PATH);
+
+    test_ctx->tctx = create_dom_test_ctx(NULL, TESTS_PATH, TEST_CONF_DB,
+                                         TEST_DOM_NAME, "ipa", params);
+    assert_non_null(test_ctx->tctx);
+
+    /* config */
+    val[0] = "1";
+    ret = confdb_add_param(test_ctx->tctx->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->tctx->confdb, true,
+                           "config/sudo", "debug_level", val);
+    assert_int_equal(ret, EOK);
+
+    val[0] = TEST_CACHE_SUDO_TIMEOUT;
+   ret = confdb_add_param(test_ctx->tctx->confdb, true,
+                           "config/sudo", "entry_cache_sudo_timeout", val);
+    assert_int_equal(ret, EOK);
+
+    create_groups(test_ctx->tctx->dom);
+    create_users(test_ctx->tctx->dom);
+
+    if (rules_exist) {
+
+        create_rules(test_ctx);
+
+        ret = sysdb_sudo_set_last_full_refresh(test_ctx->tctx->dom, time(NULL));
+        assert_int_equal(ret, EOK);
+
+        /* TODO: It is necessary? */
+        talloc_zfree(test_ctx->rules);
+    }
+
+    /* Rules */
+    test_ctx->rules = NULL;
+
+    check_leaks_push(test_ctx);
+    *state = (void *) test_ctx;
+    return 0;
+}
+
+static int setup_sysdb_without_rules(void **state)
+{
+    return test_sysdb_setup(state, false);
+}
+
+static int setup_sysdb_with_rules(void **state)
+{
+    return test_sysdb_setup(state, true);
+}
+
+static int test_sysdb_teardown(void **state)
+{
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    remove_users(test_ctx->tctx->dom);
+    remove_groups(test_ctx->tctx->dom);
+    remove_rules(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->rules);
+    assert_non_null(test_ctx->rules[0]);
+
+    create_rule(test_ctx->rules[0], test_ctx->tctx->dom,
+                rules[0].name, rules[0].host,
+                rules[0].as_user, users[0].name);
+
+    ret = sysdb_sudo_store(test_ctx->tctx->dom, test_ctx->rules, 1);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(test_ctx->rules);
+}
+
+void test_sudo_purge_by_filter(void **state)
+{
+    errno_t ret;
+    char *delete_filter;
+    int uid = 0;
+    char **groupnames = NULL;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    ret = sysdb_get_sudo_filter(test_ctx, users[2].name,
+                                uid, groupnames, SYSDB_SUDO_FILTER_USERNAME,
+                                &delete_filter);
+    assert_int_equal(ret, EOK);
+
+
+    ret = sysdb_sudo_purge(test_ctx->tctx->dom, delete_filter,
+                         NULL, 0);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(delete_filter);
+}
+
+void test_sudo_purge_by_rules(void **state)
+{
+    errno_t ret;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    create_rules(test_ctx);
+
+    ret = sysdb_sudo_purge(test_ctx->tctx->dom, NULL, test_ctx->rules, 2);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(test_ctx->rules);
+}
+
+void test_sudo_set_get_last_full_refresh(void **state)
+{
+    errno_t ret;
+    time_t now;
+    time_t time_before;
+    time_t time_after;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    ret = sysdb_sudo_get_last_full_refresh(test_ctx->tctx->dom, &time_before);
+    assert_int_equal(ret, EOK);
+
+    sleep(1);
+    now = time(NULL);
+
+    ret = sysdb_sudo_set_last_full_refresh(test_ctx->tctx->dom, now);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_sudo_get_last_full_refresh(test_ctx->tctx->dom, &time_after);
+    assert_int_equal(ret, EOK);
+
+    assert_int_not_equal(time_before, now);
+    assert_int_equal(now, time_after);
+}
+
+void test_sudo_get_filter(void **state)
+{
+    /* TODO: Write other cases of filter! */
+
+    errno_t ret;
+    char *filter;
+    int uid = 0;
+    char **groupnames = NULL;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    ret = sysdb_get_sudo_filter(test_ctx, users[2].name,
+                                uid, groupnames, SYSDB_SUDO_FILTER_USERNAME,
+                                &filter);
+    assert_int_equal(ret, EOK);
+    assert_string_equal(filter,
+                        "(&(objectClass=sudoRule)(|(sudoUser=test_user3)))");
+
+    talloc_zfree(filter);
+}
+
+void test_get_sudo_user_info(void **state)
+{
+    errno_t ret;
+    char **groupnames = NULL;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    ret = sysdb_get_sudo_user_info(test_ctx, test_ctx->tctx->dom,
+                                   users[2].name, 0, &groupnames);
+    assert_int_equal(ret, EOK);
+    assert_string_equal(groupnames[0], TEST_GROUP_NAME);
+
+    talloc_zfree(groupnames);
+}
+
+void test_get_sudo_user_info_no_exist(void **state)
+{
+    errno_t ret;
+    char **groupnames = NULL;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    ret = sysdb_get_sudo_user_info(test_ctx, test_ctx->tctx->dom,
+                                   TEST_USER_NON_EXIST, 0, &groupnames);
+    assert_int_equal(ret, ENOENT);
+}
+
+void test_set_sudo_rule_attr_exist(void **state)
+{
+    errno_t ret;
+    struct sysdb_attrs *attrs = NULL;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    attrs = sysdb_new_attrs(test_ctx);
+    assert_non_null(attrs);
+
+    ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE, 1);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_set_sudo_rule_attr(test_ctx->tctx->dom, rules[2].name,
+                                   attrs, SYSDB_MOD_REP);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(attrs);
+}
+
+void test_set_sudo_rule_attr_no_exist(void **state)
+{
+    errno_t ret;
+    struct sysdb_attrs *attrs = NULL;
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    attrs = sysdb_new_attrs(test_ctx);
+    assert_non_null(attrs);
+
+    ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE, 1);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_set_sudo_rule_attr(test_ctx->tctx->dom, rules[2].name,
+                                   attrs, SYSDB_MOD_REP);
+    assert_int_equal(ret, ENOENT);
+
+    talloc_zfree(attrs);
+}
+
+void test_search_sudo_rules(void **state)
+{
+    errno_t ret;
+    char *filter;
+    const char *attrs[] = {SYSDB_NAME, NULL};
+    struct ldb_message **msgs = NULL;
+    size_t msgs_count;
+    size_t num_rules = 2;
+    const char *rule_names[num_rules];
+    const char *db_results[num_rules];
+    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+                                                         struct sysdb_test_ctx);
+
+    ret = sysdb_get_sudo_filter(test_ctx, NULL, 0, NULL,
+                                SYSDB_SUDO_FILTER_NONE, &filter);
+    assert_int_equal(ret, EOK);
+
+    ret = sysdb_search_sudo_rules(test_ctx, test_ctx->tctx->dom, filter,
+                                  attrs, &msgs_count, &msgs);
+    assert_int_equal(ret, EOK);
+
+    assert_int_equal(msgs_count, 2);
+
+    rule_names[0] = rules[1].name;
+    rule_names[1] = rules[2].name;
+
+    for (int i = 0; i < num_rules; ++i) {
+        db_results[i] = ldb_msg_find_attr_as_string(msgs[i], SYSDB_NAME, NULL);
+        assert_non_null(db_results[i]);
+    }
+
+    assert_string_not_equal(db_results[0], db_results[1]);
+    assert_true(are_values_in_array(rule_names, num_rules,
+                                    db_results, num_rules));
+
+    talloc_zfree(msgs);
+    talloc_zfree(filter);
+}
+
+/*
+void test_filter_rules_by_time(void **state)
+{
+    errno_t ret;
+    time_t time_before;
+    time_t now;
+    uint32_t _num_rules;
+    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]);
+
+    create_rule(test_ctx->rules[0], test_ctx->tctx->dom,
+                rules[0].name, rules[0].host,
+                rules[0].as_user, users[0].name);
+
+    ret = sysdb_sudo_store(test_ctx->tctx->dom, test_ctx->rules, 1);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(test_ctx->rules[0]);
+    talloc_zfree(test_ctx->rules);
+
+
+    time_before = time(NULL) - (2 * TEST_CACHE_SUDO_TIMEOUT);
+    ret = sysdb_sudo_set_last_full_refresh(test_ctx->tctx->dom, time_before);
+    assert_int_equal(ret, EOK);
+
+
+    test_ctx->rules = talloc_array(test_ctx, struct sysdb_attrs *, 2);
+    test_ctx->rules[0] = sysdb_new_attrs(test_ctx);
+    assert_non_null(test_ctx->rules[0]);
+
+    create_rule(test_ctx->rules[0], test_ctx->tctx->dom,
+                rules[1].name, rules[1].host,
+                rules[1].as_user, users[1].name);
+
+    create_rule(test_ctx->rules[1], test_ctx->tctx->dom,
+                rules[2].name, rules[2].host,
+                rules[2].as_user, users[2].name);
+
+
+    now = time(NULL);
+
+
+    ret = sysdb_sudo_store(test_ctx->tctx->dom, test_ctx->rules, 2);
+    assert_int_equal(ret, EOK);
+
+    talloc_zfree(test_ctx->rules[0]);
+    talloc_zfree(test_ctx->rules[1]);
+    talloc_zfree(test_ctx->rules);
+
+*/
+    /*
+    ret =  sysdb_sudo_filter_rules_by_time(test_ctx, 3,
+                                struct sysdb_attrs **in_rules,
+                                now,
+                                &_num_rules,
+                                struct sysdb_attrs ***_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[] = {
+
+        /* sysdb_sudo_store() */
+        cmocka_unit_test_setup_teardown(test_store_sudo,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_store_sudo,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        /* sysdb_sudo_purge() */
+        cmocka_unit_test_setup_teardown(test_sudo_purge_by_filter,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_sudo_purge_by_filter,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        cmocka_unit_test_setup_teardown(test_sudo_purge_by_rules,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_sudo_purge_by_rules,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        /*
+         * sysdb_sudo_set_last_full_refresh()
+         * sysdb_sudo_get_last_full_refresh()
+         */
+        cmocka_unit_test_setup_teardown(test_sudo_set_get_last_full_refresh,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_sudo_set_get_last_full_refresh,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        /* sysdb_get_sudo_filter() */
+        cmocka_unit_test_setup_teardown(test_sudo_get_filter,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_sudo_get_filter,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        /* sysdb_get_sudo_user_info() */
+        cmocka_unit_test_setup_teardown(test_get_sudo_user_info,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_get_sudo_user_info,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        cmocka_unit_test_setup_teardown(test_get_sudo_user_info_no_exist,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+
+        /* sysdb_set_sudo_rule_attr() */
+        cmocka_unit_test_setup_teardown(test_set_sudo_rule_attr_exist,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+        cmocka_unit_test_setup_teardown(test_set_sudo_rule_attr_no_exist,
+                                        setup_sysdb_without_rules,
+                                        test_sysdb_teardown),
+
+        /* sysdb_search_sudo_rules() */
+        cmocka_unit_test_setup_teardown(test_search_sudo_rules,
+                                        setup_sysdb_with_rules,
+                                        test_sysdb_teardown),
+
+        /* sysdb_sudo_filter_rules_by_time() */
+/*
+        cmocka_unit_test_setup_teardown(test_filter_rules_by_time,
+                                        setup_sysdb_without_rules,
+                                        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_DB, LOCAL_SYSDB_FILE);
+    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_DB, TEST_DOM_NAME);
+    }
+    return rv;
+}
-- 
2.5.0

