Blame SOURCES/0107-TESTS-Adding-tests-for-ad_enabled_domains-option.patch

b2d430
From 70c1809c7ad309d87fba7570cff5c5e9f10d17f1 Mon Sep 17 00:00:00 2001
b2d430
From: Petr Cech <pcech@redhat.com>
b2d430
Date: Mon, 27 Jun 2016 11:53:19 +0200
b2d430
Subject: [PATCH 107/108] TESTS: Adding tests for ad_enabled_domains option
b2d430
MIME-Version: 1.0
b2d430
Content-Type: text/plain; charset=UTF-8
b2d430
Content-Transfer-Encoding: 8bit
b2d430
b2d430
There is special logic around ad_enabled_domains option:
b2d430
 * option is disabled by default
b2d430
 * master domain is always added to enabled domains
b2d430
b2d430
Resolves:
b2d430
https://fedorahosted.org/sssd/ticket/2828
b2d430
b2d430
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
b2d430
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
b2d430
---
b2d430
 Makefile.am                           |  20 +++
b2d430
 src/tests/cmocka/test_ad_subdomains.c | 328 ++++++++++++++++++++++++++++++++++
b2d430
 2 files changed, 348 insertions(+)
b2d430
 create mode 100644 src/tests/cmocka/test_ad_subdomains.c
b2d430
b2d430
diff --git a/Makefile.am b/Makefile.am
b2d430
index e2e4c4c08f66ef15684e1b3b1fe17bfae4e4131b..4d90c7a46e2ee0fe652aa392cf647d056e06c7fc 100644
b2d430
--- a/Makefile.am
b2d430
+++ b/Makefile.am
b2d430
@@ -257,6 +257,7 @@ if HAVE_CMOCKA
b2d430
         test_sbus_opath \
b2d430
         test_fo_srv \
b2d430
         pam-srv-tests \
b2d430
+        test_ad_subdom \
b2d430
         test_ipa_subdom_util \
b2d430
         test_tools_colondb \
b2d430
         test_krb5_wait_queue \
b2d430
@@ -2817,6 +2818,25 @@ test_fo_srv_LDADD = \
b2d430
     libsss_test_common.la \
b2d430
     $(NULL)
b2d430
 
b2d430
+test_ad_subdom_SOURCES = \
b2d430
+    src/tests/cmocka/test_ad_subdomains.c \
b2d430
+    $(NULL)
b2d430
+test_ad_subdom_CFLAGS = \
b2d430
+    $(AM_CFLAGS) \
b2d430
+    $(NDR_NBT_CFLAGS) \
b2d430
+    $(NULL)
b2d430
+test_ad_subdom_LDADD = \
b2d430
+    $(CMOCKA_LIBS) \
b2d430
+    $(POPT_LIBS) \
b2d430
+    $(TALLOC_LIBS) \
b2d430
+    $(SSSD_INTERNAL_LTLIBS) \
b2d430
+    libsss_ldap_common.la \
b2d430
+    libsss_ad_tests.la \
b2d430
+    libsss_idmap.la \
b2d430
+    libsss_test_common.la \
b2d430
+    libdlopen_test_providers.la \
b2d430
+    $(NULL)
b2d430
+
b2d430
 test_ipa_subdom_util_SOURCES = \
b2d430
     src/tests/cmocka/test_ipa_subdomains_utils.c \
b2d430
     src/providers/ipa/ipa_subdomains_utils.c \
b2d430
diff --git a/src/tests/cmocka/test_ad_subdomains.c b/src/tests/cmocka/test_ad_subdomains.c
b2d430
new file mode 100644
b2d430
index 0000000000000000000000000000000000000000..99908b5f8f0b89c2cc391b5496e24a247bfd7448
b2d430
--- /dev/null
b2d430
+++ b/src/tests/cmocka/test_ad_subdomains.c
b2d430
@@ -0,0 +1,328 @@
b2d430
+/*
b2d430
+    Authors:
b2d430
+        Petr Čech <pcech@redhat.com>
b2d430
+
b2d430
+    Copyright (C) 2016 Red Hat
b2d430
+
b2d430
+    SSSD tests: AD subdomain tests
b2d430
+
b2d430
+    This program is free software; you can redistribute it and/or modify
b2d430
+    it under the terms of the GNU General Public License as published by
b2d430
+    the Free Software Foundation; either version 3 of the License, or
b2d430
+    (at your option) any later version.
b2d430
+
b2d430
+    This program is distributed in the hope that it will be useful,
b2d430
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
b2d430
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b2d430
+    GNU General Public License for more details.
b2d430
+
b2d430
+    You should have received a copy of the GNU General Public License
b2d430
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
b2d430
+*/
b2d430
+
b2d430
+
b2d430
+
b2d430
+#include <talloc.h>
b2d430
+#include <tevent.h>
b2d430
+#include <errno.h>
b2d430
+#include <popt.h>
b2d430
+#include <arpa/inet.h>
b2d430
+#include <netinet/in.h>
b2d430
+#include <sys/types.h>
b2d430
+#include <stdarg.h>
b2d430
+#include <stdlib.h>
b2d430
+
b2d430
+#include "tests/cmocka/common_mock.h"
b2d430
+#include "tests/cmocka/common_mock_resp.h"
b2d430
+#include "providers/ad/ad_common.h"
b2d430
+
b2d430
+#include "providers/ad/ad_subdomains.c"
b2d430
+#include "providers/ad/ad_opts.c"
b2d430
+
b2d430
+#define AD_DOMAIN "ad_domain.domain.test"
b2d430
+#define DOMAIN_1 "one.domain.test"
b2d430
+#define DOMAIN_2 "two.domain.test"
b2d430
+
b2d430
+struct test_ad_subdom_ctx {
b2d430
+    struct ad_id_ctx *ad_id_ctx;
b2d430
+};
b2d430
+
b2d430
+static struct ad_id_ctx *
b2d430
+test_ad_subdom_init_ad_id_ctx(TALLOC_CTX *mem_ctx)
b2d430
+{
b2d430
+    struct ad_id_ctx *ad_id_ctx;
b2d430
+    struct ad_options *ad_options;
b2d430
+    errno_t ret;
b2d430
+
b2d430
+    ad_id_ctx = talloc_zero(mem_ctx, struct ad_id_ctx);
b2d430
+    assert_non_null(ad_id_ctx);
b2d430
+
b2d430
+    ad_options = talloc_zero(ad_id_ctx, struct ad_options);
b2d430
+    assert_non_null(ad_options);
b2d430
+
b2d430
+    ret = dp_copy_defaults(ad_options,
b2d430
+                           ad_basic_opts,
b2d430
+                           AD_OPTS_BASIC,
b2d430
+                           &ad_options->basic);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+
b2d430
+    ad_id_ctx->ad_options = ad_options;
b2d430
+
b2d430
+    return ad_id_ctx;
b2d430
+}
b2d430
+
b2d430
+static int test_ad_subdom_setup(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+
b2d430
+    assert_true(leak_check_setup());
b2d430
+
b2d430
+    test_ctx = talloc_zero(global_talloc_context, struct test_ad_subdom_ctx);
b2d430
+    assert_non_null(test_ctx);
b2d430
+
b2d430
+    test_ctx->ad_id_ctx = NULL;
b2d430
+
b2d430
+    check_leaks_push(test_ctx);
b2d430
+    *state = test_ctx;
b2d430
+    return 0;
b2d430
+}
b2d430
+
b2d430
+static int test_ad_subdom_teardown(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+
b2d430
+    test_ctx = talloc_get_type(*state, struct test_ad_subdom_ctx);
b2d430
+    assert_non_null(test_ctx);
b2d430
+
b2d430
+    assert_true(check_leaks_pop(test_ctx) == true);
b2d430
+    talloc_free(test_ctx);
b2d430
+    assert_true(leak_check_teardown());
b2d430
+    return 0;
b2d430
+}
b2d430
+
b2d430
+static void test_ad_subdom_default(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+    const char **ad_enabled_domains = NULL;
b2d430
+    errno_t ret;
b2d430
+
b2d430
+    test_ctx = talloc_get_type(*state, struct test_ad_subdom_ctx);
b2d430
+    test_ctx->ad_id_ctx = test_ad_subdom_init_ad_id_ctx(test_ctx);
b2d430
+    assert_non_null(test_ctx->ad_id_ctx);
b2d430
+
b2d430
+    ret = ad_get_enabled_domains(test_ctx, test_ctx->ad_id_ctx,
b2d430
+                                 AD_DOMAIN,
b2d430
+                                 &ad_enabled_domains);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+    assert_null(ad_enabled_domains);
b2d430
+
b2d430
+    talloc_zfree(test_ctx->ad_id_ctx);
b2d430
+}
b2d430
+
b2d430
+static void test_ad_subdom_add_one(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+    const char **ad_enabled_domains = NULL;
b2d430
+    int enabled_domains_count;
b2d430
+    int domain_count = 2;
b2d430
+    const char *domains[domain_count];
b2d430
+    errno_t ret;
b2d430
+
b2d430
+    test_ctx = talloc_get_type(*state, struct test_ad_subdom_ctx);
b2d430
+    test_ctx->ad_id_ctx = test_ad_subdom_init_ad_id_ctx(test_ctx);
b2d430
+    assert_non_null(test_ctx->ad_id_ctx);
b2d430
+
b2d430
+    ret = dp_opt_set_string(test_ctx->ad_id_ctx->ad_options->basic,
b2d430
+                            AD_ENABLED_DOMAINS, DOMAIN_1);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+
b2d430
+    ret = ad_get_enabled_domains(test_ctx, test_ctx->ad_id_ctx,
b2d430
+                                 AD_DOMAIN,
b2d430
+                                 &ad_enabled_domains);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+    assert_non_null(ad_enabled_domains);
b2d430
+
b2d430
+    for (enabled_domains_count = 0;
b2d430
+         ad_enabled_domains[enabled_domains_count] != NULL;
b2d430
+         enabled_domains_count++) {
b2d430
+    }
b2d430
+    assert_int_equal(domain_count, enabled_domains_count);
b2d430
+
b2d430
+    domains[0] = AD_DOMAIN;
b2d430
+    domains[1] = DOMAIN_1;
b2d430
+    assert_true(are_values_in_array(domains, domain_count,
b2d430
+                                    ad_enabled_domains, enabled_domains_count));
b2d430
+
b2d430
+    talloc_zfree(test_ctx->ad_id_ctx);
b2d430
+    talloc_zfree(ad_enabled_domains);
b2d430
+}
b2d430
+
b2d430
+static void test_ad_subdom_add_two(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+    const char **ad_enabled_domains = NULL;
b2d430
+    int enabled_domains_count;
b2d430
+    int domain_count = 3;
b2d430
+    const char *domains[domain_count];
b2d430
+    errno_t ret;
b2d430
+
b2d430
+    test_ctx = talloc_get_type(*state, struct test_ad_subdom_ctx);
b2d430
+    test_ctx->ad_id_ctx = test_ad_subdom_init_ad_id_ctx(test_ctx);
b2d430
+    assert_non_null(test_ctx->ad_id_ctx);
b2d430
+
b2d430
+    ret = dp_opt_set_string(test_ctx->ad_id_ctx->ad_options->basic,
b2d430
+                            AD_ENABLED_DOMAINS, DOMAIN_1","DOMAIN_2);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+
b2d430
+    ret = ad_get_enabled_domains(test_ctx, test_ctx->ad_id_ctx,
b2d430
+                                 AD_DOMAIN,
b2d430
+                                 &ad_enabled_domains);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+    assert_non_null(ad_enabled_domains);
b2d430
+
b2d430
+    for (enabled_domains_count = 0;
b2d430
+         ad_enabled_domains[enabled_domains_count] != NULL;
b2d430
+         enabled_domains_count++) {
b2d430
+    }
b2d430
+    assert_int_equal(domain_count, enabled_domains_count);
b2d430
+
b2d430
+    domains[0] = AD_DOMAIN;
b2d430
+    domains[1] = DOMAIN_1;
b2d430
+    domains[2] = DOMAIN_2;
b2d430
+    assert_true(are_values_in_array(domains, domain_count,
b2d430
+                                    ad_enabled_domains, enabled_domains_count));
b2d430
+
b2d430
+    talloc_zfree(test_ctx->ad_id_ctx);
b2d430
+    talloc_zfree(ad_enabled_domains);
b2d430
+}
b2d430
+
b2d430
+static void test_ad_subdom_add_master(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+    const char **ad_enabled_domains = NULL;
b2d430
+    int enabled_domains_count;
b2d430
+    int domain_count = 1;
b2d430
+    const char *domains[domain_count];
b2d430
+    errno_t ret;
b2d430
+
b2d430
+    test_ctx = talloc_get_type(*state, struct test_ad_subdom_ctx);
b2d430
+    test_ctx->ad_id_ctx = test_ad_subdom_init_ad_id_ctx(test_ctx);
b2d430
+    assert_non_null(test_ctx->ad_id_ctx);
b2d430
+
b2d430
+    ret = dp_opt_set_string(test_ctx->ad_id_ctx->ad_options->basic,
b2d430
+                            AD_ENABLED_DOMAINS, AD_DOMAIN);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+
b2d430
+    ret = ad_get_enabled_domains(test_ctx, test_ctx->ad_id_ctx,
b2d430
+                                 AD_DOMAIN,
b2d430
+                                 &ad_enabled_domains);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+    assert_non_null(ad_enabled_domains);
b2d430
+
b2d430
+    for (enabled_domains_count = 0;
b2d430
+         ad_enabled_domains[enabled_domains_count] != NULL;
b2d430
+         enabled_domains_count++) {
b2d430
+    }
b2d430
+    assert_int_equal(domain_count, enabled_domains_count);
b2d430
+
b2d430
+    domains[0] = AD_DOMAIN;
b2d430
+    assert_true(are_values_in_array(domains, domain_count,
b2d430
+                                    ad_enabled_domains, enabled_domains_count));
b2d430
+
b2d430
+    talloc_zfree(test_ctx->ad_id_ctx);
b2d430
+    talloc_zfree(ad_enabled_domains);
b2d430
+}
b2d430
+
b2d430
+static void test_ad_subdom_add_two_with_master(void **state)
b2d430
+{
b2d430
+    struct test_ad_subdom_ctx *test_ctx;
b2d430
+    const char **ad_enabled_domains = NULL;
b2d430
+    int enabled_domains_count;
b2d430
+    int domain_count = 3;
b2d430
+    const char *domains[domain_count];
b2d430
+    errno_t ret;
b2d430
+
b2d430
+    test_ctx = talloc_get_type(*state, struct test_ad_subdom_ctx);
b2d430
+    test_ctx->ad_id_ctx = test_ad_subdom_init_ad_id_ctx(test_ctx);
b2d430
+    assert_non_null(test_ctx->ad_id_ctx);
b2d430
+
b2d430
+    ret = dp_opt_set_string(test_ctx->ad_id_ctx->ad_options->basic,
b2d430
+                            AD_ENABLED_DOMAINS,
b2d430
+                            DOMAIN_1","AD_DOMAIN","DOMAIN_2);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+
b2d430
+    ret = ad_get_enabled_domains(test_ctx, test_ctx->ad_id_ctx,
b2d430
+                                 AD_DOMAIN,
b2d430
+                                 &ad_enabled_domains);
b2d430
+    assert_int_equal(ret, EOK);
b2d430
+    assert_non_null(ad_enabled_domains);
b2d430
+
b2d430
+    for (enabled_domains_count = 0;
b2d430
+         ad_enabled_domains[enabled_domains_count] != NULL;
b2d430
+         enabled_domains_count++) {
b2d430
+    }
b2d430
+    assert_int_equal(domain_count, enabled_domains_count);
b2d430
+
b2d430
+    domains[0] = AD_DOMAIN;
b2d430
+    domains[1] = DOMAIN_1;
b2d430
+    domains[2] = DOMAIN_2;
b2d430
+    assert_true(are_values_in_array(domains, domain_count,
b2d430
+                                    ad_enabled_domains, enabled_domains_count));
b2d430
+
b2d430
+    talloc_zfree(test_ctx->ad_id_ctx);
b2d430
+    talloc_zfree(ad_enabled_domains);
b2d430
+}
b2d430
+
b2d430
+int main(int argc, const char *argv[])
b2d430
+{
b2d430
+    int rv;
b2d430
+    poptContext pc;
b2d430
+    int opt;
b2d430
+    struct poptOption long_options[] = {
b2d430
+        POPT_AUTOHELP
b2d430
+        SSSD_DEBUG_OPTS
b2d430
+        POPT_TABLEEND
b2d430
+    };
b2d430
+
b2d430
+    const struct CMUnitTest tests[] = {
b2d430
+        cmocka_unit_test_setup_teardown(test_ad_subdom_default,
b2d430
+                                        test_ad_subdom_setup,
b2d430
+                                        test_ad_subdom_teardown),
b2d430
+        cmocka_unit_test_setup_teardown(test_ad_subdom_add_one,
b2d430
+                                        test_ad_subdom_setup,
b2d430
+                                        test_ad_subdom_teardown),
b2d430
+        cmocka_unit_test_setup_teardown(test_ad_subdom_add_two,
b2d430
+                                        test_ad_subdom_setup,
b2d430
+                                        test_ad_subdom_teardown),
b2d430
+        cmocka_unit_test_setup_teardown(test_ad_subdom_add_master,
b2d430
+                                        test_ad_subdom_setup,
b2d430
+                                        test_ad_subdom_teardown),
b2d430
+        cmocka_unit_test_setup_teardown(test_ad_subdom_add_two_with_master,
b2d430
+                                        test_ad_subdom_setup,
b2d430
+                                        test_ad_subdom_teardown),
b2d430
+    };
b2d430
+
b2d430
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
b2d430
+    debug_level = SSSDBG_INVALID;
b2d430
+
b2d430
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
b2d430
+    while((opt = poptGetNextOpt(pc)) != -1) {
b2d430
+        switch(opt) {
b2d430
+        default:
b2d430
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
b2d430
+                    poptBadOption(pc, 0), poptStrerror(opt));
b2d430
+            poptPrintUsage(pc, stderr, 0);
b2d430
+            return 1;
b2d430
+        }
b2d430
+    }
b2d430
+    poptFreeContext(pc);
b2d430
+
b2d430
+    DEBUG_CLI_INIT(debug_level);
b2d430
+
b2d430
+    /* Even though normally the tests should clean up after themselves
b2d430
+     * they might not after a failed run. Remove the old db to be sure */
b2d430
+    tests_set_cwd();
b2d430
+
b2d430
+    rv = cmocka_run_group_tests(tests, NULL, NULL);
b2d430
+    return rv;
b2d430
+}
b2d430
-- 
b2d430
2.4.11
b2d430