Blame SOURCES/0006-SSSD-Unit-test-sss_ldap_dn_in_search_bases.patch

2fc102
From 94c85df2d7ded82f2939d8fe29821e4c78ff000d Mon Sep 17 00:00:00 2001
2fc102
From: Pavel Reichl <pavel.reichl@redhat.com>
2fc102
Date: Thu, 14 Nov 2013 21:52:26 +0000
2fc102
Subject: [PATCH 6/6] SSSD: Unit test - sss_ldap_dn_in_search_bases
2fc102
2fc102
Unit test testing detection of the right domain when processing group with members from several domains
2fc102
2fc102
Resolves:
2fc102
https://fedorahosted.org/sssd/ticket/2132
2fc102
---
2fc102
 Makefile.am                          |  27 ++++-
2fc102
 src/tests/cmocka/test_search_bases.c | 191 +++++++++++++++++++++++++++++++++++
2fc102
 2 files changed, 217 insertions(+), 1 deletion(-)
2fc102
 create mode 100644 src/tests/cmocka/test_search_bases.c
2fc102
2fc102
diff --git a/Makefile.am b/Makefile.am
2fc102
index 2ba1ec0fd94e3292f05de0139d607b3626b5c6f7..583ccdb499306268640bfb894f673c42945e19ff 100644
2fc102
--- a/Makefile.am
2fc102
+++ b/Makefile.am
2fc102
@@ -151,7 +151,8 @@ if HAVE_CMOCKA
2fc102
         fqnames-tests \
2fc102
         test_sss_idmap \
2fc102
         test_utils \
2fc102
-        ad_access_filter_tests
2fc102
+        ad_access_filter_tests \
2fc102
+        test_search_bases
2fc102
 endif
2fc102
 
2fc102
 check_PROGRAMS = \
2fc102
@@ -1367,6 +1368,30 @@ test_utils_LDADD = \
2fc102
     $(SSSD_INTERNAL_LTLIBS) \
2fc102
     libsss_test_common.la
2fc102
 
2fc102
+test_search_bases_SOURCES = \
2fc102
+    $(sssd_be_SOURCES) \
2fc102
+    src/util/sss_ldap.c \
2fc102
+    src/util/sss_krb5.c \
2fc102
+    src/util/find_uid.c \
2fc102
+    src/util/user_info_msg.c \
2fc102
+    src/tests/cmocka/test_search_bases.c
2fc102
+test_search_bases_CFLAGS = \
2fc102
+    $(AM_CFLAGS) \
2fc102
+    -DUNIT_TESTING
2fc102
+test_search_bases_LDADD = \
2fc102
+    $(PAM_LIBS) \
2fc102
+    $(CMOCKA_LIBS) \
2fc102
+    $(POPT_LIBS) \
2fc102
+    $(SSSD_LIBS) \
2fc102
+    $(CARES_LIBS) \
2fc102
+    $(KRB5_LIBS) \
2fc102
+    $(SSSD_INTERNAL_LTLIBS) \
2fc102
+    $(SYSTEMD_LOGIN_LIBS) \
2fc102
+    libsss_ldap_common.la \
2fc102
+    libsss_idmap.la \
2fc102
+    libsss_krb5_common.la \
2fc102
+    libsss_test_common.la
2fc102
+
2fc102
 ad_access_filter_tests_SOURCES = \
2fc102
     $(sssd_be_SOURCES) \
2fc102
     src/util/sss_ldap.c \
2fc102
diff --git a/src/tests/cmocka/test_search_bases.c b/src/tests/cmocka/test_search_bases.c
2fc102
new file mode 100644
2fc102
index 0000000000000000000000000000000000000000..e03ef3662685d92335bce4a7023e1ac7e64432c8
2fc102
--- /dev/null
2fc102
+++ b/src/tests/cmocka/test_search_bases.c
2fc102
@@ -0,0 +1,191 @@
2fc102
+/*
2fc102
+    Authors:
2fc102
+        Pavel Reichl <preichl@redhat.com>
2fc102
+
2fc102
+    Copyright (C) 2013 Red Hat
2fc102
+
2fc102
+    SSSD tests - Search bases
2fc102
+
2fc102
+    This program is free software; you can redistribute it and/or modify
2fc102
+    it under the terms of the GNU General Public License as published by
2fc102
+    the Free Software Foundation; either version 3 of the License, or
2fc102
+    (at your option) any later version.
2fc102
+
2fc102
+    This program is distributed in the hope that it will be useful,
2fc102
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
2fc102
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2fc102
+    GNU General Public License for more details.
2fc102
+
2fc102
+    You should have received a copy of the GNU General Public License
2fc102
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
2fc102
+*/
2fc102
+
2fc102
+#include <stdarg.h>
2fc102
+#include <stdlib.h>
2fc102
+#include <stddef.h>
2fc102
+#include <setjmp.h>
2fc102
+#include <unistd.h>
2fc102
+#include <sys/types.h>
2fc102
+#include <cmocka.h>
2fc102
+#include <ldap.h>
2fc102
+
2fc102
+#include "util/find_uid.h"
2fc102
+#include "util/sss_ldap.h"
2fc102
+#include "tests/common.h"
2fc102
+#include "providers/ldap/ldap_common.h"
2fc102
+#include "providers/ldap/sdap.h"
2fc102
+#include "dhash.h"
2fc102
+#include "tests/common_check.h"
2fc102
+
2fc102
+enum sss_test_get_by_dn {
2fc102
+    DN_NOT_IN_DOMS, /* dn is not in any domain           */
2fc102
+    DN_IN_DOM1,     /* dn is in the domain based on dns  */
2fc102
+    DN_IN_DOM2,     /* dn is in the domain based on dns2 */
2fc102
+};
2fc102
+
2fc102
+static struct sdap_search_base** generate_bases(TALLOC_CTX *mem_ctx,
2fc102
+                                                const char** dns, size_t n)
2fc102
+{
2fc102
+    struct sdap_search_base **search_bases;
2fc102
+    errno_t err;
2fc102
+    int i;
2fc102
+
2fc102
+    search_bases = talloc_array(mem_ctx, struct sdap_search_base *, n + 1);
2fc102
+    assert_non_null(search_bases);
2fc102
+
2fc102
+    for (i=0; i < n; ++i) {
2fc102
+        err = sdap_create_search_base(mem_ctx, dns[i], LDAP_SCOPE_SUBTREE,
2fc102
+                                      NULL, &search_bases[i]);
2fc102
+        if (err != EOK) {
2fc102
+            fprintf(stderr, "Failed to create search base\n");
2fc102
+        }
2fc102
+        assert_int_equal(err, EOK);
2fc102
+    }
2fc102
+    search_bases[n] = NULL;
2fc102
+    return search_bases;
2fc102
+}
2fc102
+
2fc102
+static bool do_test_search_bases(const char* dn, const char** dns, size_t n)
2fc102
+{
2fc102
+    TALLOC_CTX *tmp_ctx;
2fc102
+    struct sdap_search_base **search_bases;
2fc102
+    bool ret;
2fc102
+
2fc102
+    tmp_ctx = talloc_new(NULL);
2fc102
+    assert_non_null(tmp_ctx);
2fc102
+
2fc102
+    search_bases = generate_bases(tmp_ctx, dns, n);
2fc102
+    check_leaks_push(tmp_ctx);
2fc102
+    ret = sss_ldap_dn_in_search_bases(tmp_ctx, dn, search_bases, NULL);
2fc102
+    assert_true(check_leaks_pop(tmp_ctx) == true);
2fc102
+
2fc102
+    talloc_free(tmp_ctx);
2fc102
+    return ret;
2fc102
+}
2fc102
+
2fc102
+void test_search_bases_fail(void **state)
2fc102
+{
2fc102
+    const char *dn = "cn=user, dc=sub, dc=ad, dc=pb";
2fc102
+    const char *dns[] = {"dc=example, dc=com", "dc=subdom, dc=ad, dc=pb"};
2fc102
+    bool ret;
2fc102
+
2fc102
+    ret = do_test_search_bases(dn, dns, 2);
2fc102
+    assert_false(ret);
2fc102
+}
2fc102
+
2fc102
+void test_search_bases_success(void **state)
2fc102
+{
2fc102
+    const char *dn = "cn=user, dc=sub, dc=ad, dc=pb";
2fc102
+    const char *dns[] = {"", "dc=ad, dc=pb", "dc=sub, dc=ad, dc=pb"};
2fc102
+    bool ret;
2fc102
+
2fc102
+    ret = do_test_search_bases(dn, dns, 3);
2fc102
+    assert_true(ret);
2fc102
+}
2fc102
+
2fc102
+static void do_test_get_by_dn(const char *dn, const char **dns, size_t n,
2fc102
+                              const char **dns2, size_t n2, int expected_result)
2fc102
+{
2fc102
+    TALLOC_CTX *tmp_ctx;
2fc102
+    struct sdap_options *opts;
2fc102
+    struct sdap_domain *sdom;
2fc102
+    struct sdap_domain *sdom2;
2fc102
+    struct sdap_domain *res_sdom;
2fc102
+    struct sdap_search_base **search_bases;
2fc102
+    struct sdap_search_base **search_bases2;
2fc102
+    tmp_ctx = talloc_new(NULL);
2fc102
+    assert_non_null(tmp_ctx);
2fc102
+
2fc102
+    search_bases = generate_bases(tmp_ctx, dns, n);
2fc102
+    search_bases2 = generate_bases(tmp_ctx, dns2, n2);
2fc102
+    sdom = talloc_zero(tmp_ctx, struct sdap_domain);
2fc102
+    assert_non_null(sdom);
2fc102
+    sdom2 = talloc_zero(tmp_ctx, struct sdap_domain);
2fc102
+    assert_non_null(sdom2);
2fc102
+
2fc102
+    sdom->search_bases = search_bases;
2fc102
+    sdom->next = sdom2;
2fc102
+    sdom->prev = NULL;
2fc102
+    sdom2->search_bases = search_bases2;
2fc102
+    sdom2->next = NULL;
2fc102
+    sdom2->prev = sdom;
2fc102
+
2fc102
+    opts = talloc(tmp_ctx, struct sdap_options);
2fc102
+    assert_non_null(opts);
2fc102
+    opts->sdom = sdom;
2fc102
+    res_sdom = sdap_domain_get_by_dn(opts, dn);
2fc102
+
2fc102
+    switch (expected_result) {
2fc102
+    case DN_NOT_IN_DOMS:
2fc102
+        assert_null(res_sdom);
2fc102
+        break;
2fc102
+    case DN_IN_DOM1:
2fc102
+        assert_true(res_sdom == sdom);
2fc102
+        break;
2fc102
+    case DN_IN_DOM2:
2fc102
+        assert_true(res_sdom == sdom2);
2fc102
+        break;
2fc102
+    }
2fc102
+
2fc102
+    talloc_free(tmp_ctx);
2fc102
+}
2fc102
+
2fc102
+void test_get_by_dn(void **state)
2fc102
+{
2fc102
+    const char *dn = "cn=user, dc=sub, dc=ad, dc=pb";
2fc102
+    const char *dns[] = {"dc=ad, dc=pb"};
2fc102
+    const char *dns2[] = {"dc=sub, dc=ad, dc=pb"};
2fc102
+
2fc102
+    do_test_get_by_dn(dn, dns, 1, dns2, 1, DN_IN_DOM2);
2fc102
+}
2fc102
+
2fc102
+void test_get_by_dn2(void **state)
2fc102
+{
2fc102
+    const char *dn = "cn=user, dc=ad, dc=com";
2fc102
+    const char *dns[] = {"dc=ad, dc=com"};
2fc102
+    const char *dns2[] = {"dc=sub, dc=ad, dc=pb"};
2fc102
+
2fc102
+    do_test_get_by_dn(dn, dns, 1, dns2, 1, DN_IN_DOM1);
2fc102
+}
2fc102
+
2fc102
+void test_get_by_dn_fail(void **state)
2fc102
+{
2fc102
+    const char *dn = "cn=user, dc=sub, dc=example, dc=com";
2fc102
+    const char *dns[] = {"dc=ad, dc=pb"};
2fc102
+    const char *dns2[] = {"dc=sub, dc=ad, dc=pb"};
2fc102
+
2fc102
+    do_test_get_by_dn(dn, dns, 1, dns2, 1, DN_NOT_IN_DOMS);
2fc102
+}
2fc102
+
2fc102
+int main(void)
2fc102
+{
2fc102
+    const UnitTest tests[] = {
2fc102
+        unit_test(test_search_bases_fail),
2fc102
+        unit_test(test_search_bases_success),
2fc102
+        unit_test(test_get_by_dn_fail),
2fc102
+        unit_test(test_get_by_dn),
2fc102
+        unit_test(test_get_by_dn2)
2fc102
+     };
2fc102
+
2fc102
+    return run_tests(tests);
2fc102
+}
2fc102
-- 
2fc102
1.8.4.2
2fc102