Blame SOURCES/0047-SYSDB-TESTS-Add-tests-for-the-domain-s-resolution-or.patch

ecf709
From 22a10ea2b6b8a56fc040d852867040dce067548a Mon Sep 17 00:00:00 2001
ecf709
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
ecf709
Date: Fri, 24 Mar 2017 23:15:04 +0100
ecf709
Subject: [PATCH 47/54] SYSDB/TESTS: Add tests for the domain's resolution
ecf709
 order methods
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
Introduce a new and small set of tests for these new helper methods that
ecf709
are going to be used in different parts of the code in the follow-up
ecf709
patches.
ecf709
ecf709
Related:
ecf709
https://pagure.io/SSSD/sssd/issue/3001
ecf709
ecf709
Signed-off-by: Fabiano Fidêncio <fidencio@redhat.com>
ecf709
ecf709
Reviewed-by: Sumit Bose <sbose@redhat.com>
ecf709
Reviewed-by: Pavel Březina <pbrezina@redhat.com>
ecf709
---
ecf709
 Makefile.am                                        |  16 ++
ecf709
 .../cmocka/test_sysdb_domain_resolution_order.c    | 190 +++++++++++++++++++++
ecf709
 2 files changed, 206 insertions(+)
ecf709
 create mode 100644 src/tests/cmocka/test_sysdb_domain_resolution_order.c
ecf709
ecf709
diff --git a/Makefile.am b/Makefile.am
ecf709
index 8052150be32d89813764e9bc436dfcb211a738d6..450785bf4c482cce1e1440f1336879150537888e 100644
ecf709
--- a/Makefile.am
ecf709
+++ b/Makefile.am
ecf709
@@ -263,6 +263,7 @@ if HAVE_CMOCKA
ecf709
         test_sysdb_certmap \
ecf709
         test_sysdb_sudo \
ecf709
         test_sysdb_utils \
ecf709
+        test_sysdb_domain_resolution_order \
ecf709
         test_wbc_calls \
ecf709
         test_be_ptask \
ecf709
         test_copy_ccache \
ecf709
@@ -2875,6 +2876,21 @@ test_sysdb_utils_LDADD = \
ecf709
     libsss_test_common.la \
ecf709
     $(NULL)
ecf709
 
ecf709
+test_sysdb_domain_resolution_order_SOURCES = \
ecf709
+    src/tests/cmocka/test_sysdb_domain_resolution_order.c \
ecf709
+    $(NULL)
ecf709
+test_sysdb_domain_resolution_order_CFLAGS = \
ecf709
+    $(AM_CFLAGS) \
ecf709
+    $(NULL)
ecf709
+test_sysdb_domain_resolution_order_LDADD = \
ecf709
+    $(CMOCKA_LIBS) \
ecf709
+    $(LDB_LIBS) \
ecf709
+    $(POPT_LIBS) \
ecf709
+    $(TALLOC_LIBS) \
ecf709
+    $(SSSD_INTERNAL_LTLIBS) \
ecf709
+    libsss_test_common.la \
ecf709
+    $(NULL)
ecf709
+
ecf709
 test_wbc_calls_SOURCES = \
ecf709
     src/tests/cmocka/test_wbc_calls.c \
ecf709
     src/sss_client/idmap/sss_nss_idmap.c \
ecf709
diff --git a/src/tests/cmocka/test_sysdb_domain_resolution_order.c b/src/tests/cmocka/test_sysdb_domain_resolution_order.c
ecf709
new file mode 100644
ecf709
index 0000000000000000000000000000000000000000..59a85ce431be9ac27c1e8e6b5e4e5f8300af549e
ecf709
--- /dev/null
ecf709
+++ b/src/tests/cmocka/test_sysdb_domain_resolution_order.c
ecf709
@@ -0,0 +1,190 @@
ecf709
+/*
ecf709
+    SSSD
ecf709
+
ecf709
+    sysdb_domain_resolution_order - Tests for domain resolution order calls
ecf709
+
ecf709
+    Authors:
ecf709
+        Fabiano Fidêncio <fidencio@redhat.com>
ecf709
+
ecf709
+    Copyright (C) 2017 Red Hat
ecf709
+
ecf709
+    This program is free software; you can redistribute it and/or modify
ecf709
+    it under the terms of the GNU General Public License as published by
ecf709
+    the Free Software Foundation; either version 3 of the License, or
ecf709
+    (at your option) any later version.
ecf709
+
ecf709
+    This program is distributed in the hope that it will be useful,
ecf709
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
ecf709
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecf709
+    GNU General Public License for more details.
ecf709
+
ecf709
+    You should have received a copy of the GNU General Public License
ecf709
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
ecf709
+*/
ecf709
+
ecf709
+#include <stdarg.h>
ecf709
+#include <stddef.h>
ecf709
+#include <setjmp.h>
ecf709
+#include <cmocka.h>
ecf709
+#include <popt.h>
ecf709
+
ecf709
+#include "tests/cmocka/common_mock.h"
ecf709
+#include "tests/common.h"
ecf709
+#include "db/sysdb_domain_resolution_order.h"
ecf709
+#include "db/sysdb_private.h" /* for sysdb->ldb member */
ecf709
+
ecf709
+#define TESTS_PATH "tp_" BASE_FILE_STEM
ecf709
+#define TEST_CONF_DB "test_sysdb_domain_resolution_order.ldb"
ecf709
+
ecf709
+#define TEST_DOM_NAME "test_sysdb_domain_resolution_order"
ecf709
+
ecf709
+#define TEST_ID_PROVIDER "ldap"
ecf709
+
ecf709
+struct domain_resolution_order_test_ctx {
ecf709
+    struct sss_test_ctx *tctx;
ecf709
+};
ecf709
+
ecf709
+static int test_sysdb_domain_resolution_order_setup(void **state)
ecf709
+{
ecf709
+    struct domain_resolution_order_test_ctx *test_ctx;
ecf709
+
ecf709
+    assert_true(leak_check_setup());
ecf709
+
ecf709
+    test_ctx = talloc_zero(global_talloc_context,
ecf709
+                           struct domain_resolution_order_test_ctx);
ecf709
+    assert_non_null(test_ctx);
ecf709
+
ecf709
+    test_dom_suite_setup(TESTS_PATH);
ecf709
+
ecf709
+    test_ctx->tctx = create_dom_test_ctx(test_ctx, TESTS_PATH,
ecf709
+                                         TEST_CONF_DB, TEST_DOM_NAME,
ecf709
+                                         TEST_ID_PROVIDER, NULL);
ecf709
+    assert_non_null(test_ctx->tctx);
ecf709
+
ecf709
+    *state = test_ctx;
ecf709
+    return 0;
ecf709
+}
ecf709
+
ecf709
+static int test_sysdb_domain_resolution_order_teardown(void **state)
ecf709
+{
ecf709
+    struct domain_resolution_order_test_ctx *test_ctx =
ecf709
+        talloc_get_type(*state, struct domain_resolution_order_test_ctx);
ecf709
+
ecf709
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, TEST_DOM_NAME);
ecf709
+    talloc_free(test_ctx);
ecf709
+    assert_true(leak_check_teardown());
ecf709
+    return 0;
ecf709
+}
ecf709
+
ecf709
+static void test_sysdb_domain_resolution_order_ops(void **state)
ecf709
+{
ecf709
+    errno_t ret;
ecf709
+    struct domain_resolution_order_test_ctx *test_ctx =
ecf709
+        talloc_get_type(*state, struct domain_resolution_order_test_ctx);
ecf709
+    const char *domains_in = NULL;
ecf709
+    const char *domains_out = NULL;
ecf709
+    struct ldb_dn *dn;
ecf709
+
ecf709
+    dn = ldb_dn_new_fmt(test_ctx, test_ctx->tctx->dom->sysdb->ldb,
ecf709
+                        SYSDB_DOM_BASE, test_ctx->tctx->dom->name);
ecf709
+
ecf709
+    /* Adding domainResolutionOrder for the first time */
ecf709
+    domains_in = "foo:bar:foobar";
ecf709
+    ret = sysdb_update_domain_resolution_order(test_ctx->tctx->dom->sysdb,
ecf709
+                                               dn, domains_in);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_domain_resolution_order(test_ctx,
ecf709
+                                            test_ctx->tctx->dom->sysdb, dn,
ecf709
+                                            &domains_out);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_true(strcmp(domains_in, domains_out) == 0);
ecf709
+
ecf709
+    /* Setting the domainResolutionOrder to ":" ...
ecf709
+     *
ecf709
+     * It means, the domainResolutionOrder is set, but if there's another
ecf709
+     * domainResolutionOrder with lower precedence those must be ignored.
ecf709
+     */
ecf709
+    domains_in = ":";
ecf709
+    ret = sysdb_update_domain_resolution_order(test_ctx->tctx->dom->sysdb,
ecf709
+                                               dn, domains_in);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_domain_resolution_order(test_ctx,
ecf709
+                                            test_ctx->tctx->dom->sysdb, dn,
ecf709
+                                            &domains_out);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_true(strcmp(domains_in, domains_out) == 0);
ecf709
+
ecf709
+    /* Changing the domainResolutionOrder */
ecf709
+    domains_in = "bar:foobar:foo";
ecf709
+    ret = sysdb_update_domain_resolution_order(test_ctx->tctx->dom->sysdb,
ecf709
+                                               dn, domains_in);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_domain_resolution_order(test_ctx,
ecf709
+                                            test_ctx->tctx->dom->sysdb, dn,
ecf709
+                                            &domains_out);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+    assert_true(strcmp(domains_out, domains_out) == 0);
ecf709
+
ecf709
+    /* Removing the domainResolutionOrder attribute */
ecf709
+    domains_in = NULL;
ecf709
+    ret = sysdb_update_domain_resolution_order(test_ctx->tctx->dom->sysdb,
ecf709
+                                               dn, domains_in);
ecf709
+    assert_int_equal(ret, EOK);
ecf709
+
ecf709
+    ret = sysdb_get_domain_resolution_order(test_ctx,
ecf709
+                                            test_ctx->tctx->dom->sysdb, dn,
ecf709
+                                            &domains_out);
ecf709
+    assert_int_equal(ret, ENOENT);
ecf709
+    assert_true(domains_out == NULL);
ecf709
+}
ecf709
+
ecf709
+int main(int argc, const char *argv[])
ecf709
+{
ecf709
+    int rv;
ecf709
+    int no_cleanup = 0;
ecf709
+    poptContext pc;
ecf709
+    int opt;
ecf709
+    struct poptOption long_options[] = {
ecf709
+        POPT_AUTOHELP
ecf709
+        SSSD_DEBUG_OPTS
ecf709
+        {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
ecf709
+         _("Do not delete the test database after a test run"), NULL },
ecf709
+        POPT_TABLEEND
ecf709
+    };
ecf709
+
ecf709
+    const struct CMUnitTest tests[] = {
ecf709
+        cmocka_unit_test_setup_teardown(test_sysdb_domain_resolution_order_ops,
ecf709
+                                        test_sysdb_domain_resolution_order_setup,
ecf709
+                                        test_sysdb_domain_resolution_order_teardown),
ecf709
+    };
ecf709
+
ecf709
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
ecf709
+    debug_level = SSSDBG_INVALID;
ecf709
+
ecf709
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
ecf709
+    while((opt = poptGetNextOpt(pc)) != -1) {
ecf709
+        switch(opt) {
ecf709
+        default:
ecf709
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
ecf709
+                    poptBadOption(pc, 0), poptStrerror(opt));
ecf709
+            poptPrintUsage(pc, stderr, 0);
ecf709
+            return 1;
ecf709
+        }
ecf709
+    }
ecf709
+    poptFreeContext(pc);
ecf709
+
ecf709
+    DEBUG_CLI_INIT(debug_level);
ecf709
+
ecf709
+    tests_set_cwd();
ecf709
+    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, LOCAL_SYSDB_FILE);
ecf709
+    test_dom_suite_setup(TESTS_PATH);
ecf709
+    rv = cmocka_run_group_tests(tests, NULL, NULL);
ecf709
+
ecf709
+    if (rv == 0 && no_cleanup == 0) {
ecf709
+        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_DB, LOCAL_SYSDB_FILE);
ecf709
+    }
ecf709
+    return rv;
ecf709
+}
ecf709
-- 
ecf709
2.9.3
ecf709