Blame SOURCES/0094-IPA-refactor-idmap-code-and-add-test.patch

2fc102
From 5ec1d31f32583761c05691c951576b6213037393 Mon Sep 17 00:00:00 2001
2fc102
From: Sumit Bose <sbose@redhat.com>
2fc102
Date: Fri, 7 Feb 2014 15:54:30 +0100
2fc102
Subject: [PATCH 94/97] IPA: refactor idmap code and add test
2fc102
2fc102
---
2fc102
 Makefile.am                       |  15 +++
2fc102
 src/providers/ipa/ipa_common.h    |  10 ++
2fc102
 src/providers/ipa/ipa_idmap.c     | 248 +++++++++++++++----------------------
2fc102
 src/tests/cmocka/test_ipa_idmap.c | 249 ++++++++++++++++++++++++++++++++++++++
2fc102
 4 files changed, 374 insertions(+), 148 deletions(-)
2fc102
 create mode 100644 src/tests/cmocka/test_ipa_idmap.c
2fc102
2fc102
diff --git a/Makefile.am b/Makefile.am
2fc102
index 16648f9aa2275b60ec84a95ff8a26b1225b97918..2e1a1e6bacfb79e4ef7068a22a64c21d23858cb9 100644
2fc102
--- a/Makefile.am
2fc102
+++ b/Makefile.am
2fc102
@@ -150,6 +150,7 @@ if HAVE_CMOCKA
2fc102
         dyndns-tests \
2fc102
         fqnames-tests \
2fc102
         test_sss_idmap \
2fc102
+        test_ipa_idmap \
2fc102
         test_utils \
2fc102
         ad_access_filter_tests \
2fc102
         ad_common_tests \
2fc102
@@ -1359,6 +1360,20 @@ test_sss_idmap_LDADD = \
2fc102
     $(SSSD_INTERNAL_LTLIBS) \
2fc102
     libsss_test_common.la
2fc102
 
2fc102
+test_ipa_idmap_SOURCES = \
2fc102
+    src/tests/cmocka/test_ipa_idmap.c \
2fc102
+    src/providers/ipa/ipa_idmap.c
2fc102
+test_ipa_idmap_CFLAGS = \
2fc102
+    $(AM_CFLAGS)
2fc102
+test_ipa_idmap_LDFLAGS = \
2fc102
+    -Wl,-wrap,sysdb_get_ranges
2fc102
+test_ipa_idmap_LDADD = \
2fc102
+    $(CMOCKA_LIBS) \
2fc102
+    $(POPT_LIBS) \
2fc102
+    libsss_idmap.la \
2fc102
+    $(SSSD_INTERNAL_LTLIBS) \
2fc102
+    libsss_test_common.la
2fc102
+
2fc102
 test_utils_SOURCES = \
2fc102
     src/tests/cmocka/test_utils.c
2fc102
 test_utils_CFLAGS = \
2fc102
diff --git a/src/providers/ipa/ipa_common.h b/src/providers/ipa/ipa_common.h
2fc102
index 02f0baf55f0d226eeb8956076b9bbcce285d4a94..0b8a17c532b7b0081dc749dcef1e6c0e684a7ed2 100644
2fc102
--- a/src/providers/ipa/ipa_common.h
2fc102
+++ b/src/providers/ipa/ipa_common.h
2fc102
@@ -195,6 +195,16 @@ int ipa_sudo_init(struct be_ctx *be_ctx,
2fc102
                   struct bet_ops **ops,
2fc102
                   void **pvt_data);
2fc102
 
2fc102
+errno_t get_idmap_data_from_range(struct range_info *r, char *domain_name,
2fc102
+                                  char **_name, char **_sid, uint32_t *_rid,
2fc102
+                                  struct sss_idmap_range *_range,
2fc102
+                                  bool *_external_mapping);
2fc102
+
2fc102
+errno_t ipa_idmap_get_ranges_from_sysdb(struct sdap_idmap_ctx *idmap_ctx,
2fc102
+                                        const char *dom_name,
2fc102
+                                        const char *dom_sid_str,
2fc102
+                                        bool allow_collisions);
2fc102
+
2fc102
 errno_t ipa_idmap_init(TALLOC_CTX *mem_ctx,
2fc102
                        struct sdap_id_ctx *id_ctx,
2fc102
                        struct sdap_idmap_ctx **_idmap_ctx);
2fc102
diff --git a/src/providers/ipa/ipa_idmap.c b/src/providers/ipa/ipa_idmap.c
2fc102
index eaca0ed3c3ce2622fbf80dff13d22e2e521f54fe..a65086af4cb4bec7ab85774f3ca1a3555056cee0 100644
2fc102
--- a/src/providers/ipa/ipa_idmap.c
2fc102
+++ b/src/providers/ipa/ipa_idmap.c
2fc102
@@ -156,9 +156,68 @@ done:
2fc102
     return ret;
2fc102
 }
2fc102
 
2fc102
-errno_t ipa_idmap_find_new_domain(struct sdap_idmap_ctx *idmap_ctx,
2fc102
-                                  const char *dom_name,
2fc102
-                                  const char *dom_sid_str)
2fc102
+errno_t get_idmap_data_from_range(struct range_info *r, char *domain_name,
2fc102
+                                  char **_name, char **_sid, uint32_t *_rid,
2fc102
+                                  struct sss_idmap_range *_range,
2fc102
+                                  bool *_external_mapping)
2fc102
+{
2fc102
+    if (r->range_type == NULL) {
2fc102
+        /* Older IPA servers might not have the range_type attribute, but
2fc102
+         * only support local ranges and trusts with algorithmic mapping. */
2fc102
+
2fc102
+        if (r->trusted_dom_sid == NULL && r->secondary_base_rid != 0) {
2fc102
+            /* local IPA domain */
2fc102
+            *_rid = 0;
2fc102
+            *_external_mapping = true;
2fc102
+            *_name = domain_name;
2fc102
+            *_sid = NULL;
2fc102
+        } else if (r->trusted_dom_sid != NULL
2fc102
+                && r->secondary_base_rid == 0) {
2fc102
+            /* trusted domain */
2fc102
+            *_rid = r->base_rid;
2fc102
+            *_external_mapping = false;
2fc102
+            *_name = r->trusted_dom_sid;
2fc102
+            *_sid = r->trusted_dom_sid;
2fc102
+        } else {
2fc102
+            DEBUG(SSSDBG_MINOR_FAILURE, ("Cannot determine range type, " \
2fc102
+                                         "for id range [%s].\n",
2fc102
+                                         r->name));
2fc102
+            return EINVAL;
2fc102
+        }
2fc102
+    } else {
2fc102
+        if (strcmp(r->range_type, IPA_RANGE_LOCAL) == 0) {
2fc102
+            *_rid = 0;
2fc102
+            *_external_mapping = true;
2fc102
+            *_name = domain_name;
2fc102
+            *_sid = NULL;
2fc102
+        } else if (strcmp(r->range_type, IPA_RANGE_AD_TRUST_POSIX) == 0) {
2fc102
+            *_rid = 0;
2fc102
+            *_external_mapping = true;
2fc102
+            *_name = r->trusted_dom_sid;
2fc102
+            *_sid = r->trusted_dom_sid;
2fc102
+        } else if (strcmp(r->range_type, IPA_RANGE_AD_TRUST) == 0) {
2fc102
+            *_rid = r->base_rid;
2fc102
+            *_external_mapping = false;
2fc102
+            *_name = r->trusted_dom_sid;
2fc102
+            *_sid = r->trusted_dom_sid;
2fc102
+        } else {
2fc102
+            DEBUG(SSSDBG_MINOR_FAILURE, ("Range type [%s] of id range " \
2fc102
+                                         "[%s] not supported.\n", \
2fc102
+                                         r->range_type, r->name));
2fc102
+            return EINVAL;
2fc102
+        }
2fc102
+    }
2fc102
+
2fc102
+    _range->min = r->base_id;
2fc102
+    _range->max = r->base_id + r->id_range_size -1;
2fc102
+
2fc102
+    return EOK;
2fc102
+}
2fc102
+
2fc102
+errno_t ipa_idmap_get_ranges_from_sysdb(struct sdap_idmap_ctx *idmap_ctx,
2fc102
+                                        const char *dom_name,
2fc102
+                                        const char *dom_sid_str,
2fc102
+                                        bool allow_collisions)
2fc102
 {
2fc102
     int ret;
2fc102
     size_t range_count;
2fc102
@@ -166,7 +225,6 @@ errno_t ipa_idmap_find_new_domain(struct sdap_idmap_ctx *idmap_ctx,
2fc102
     TALLOC_CTX *tmp_ctx;
2fc102
     size_t c;
2fc102
     enum idmap_error_code err;
2fc102
-    struct range_info *r;
2fc102
     struct sss_idmap_range range;
2fc102
     uint32_t rid;
2fc102
     bool external_mapping;
2fc102
@@ -187,74 +245,39 @@ errno_t ipa_idmap_find_new_domain(struct sdap_idmap_ctx *idmap_ctx,
2fc102
     }
2fc102
 
2fc102
     for (c = 0; c < range_count; c++) {
2fc102
-        r = range_list[c];
2fc102
-
2fc102
-        if (r->range_type == NULL) {
2fc102
-            /* Older IPA servers might not have the range_type attribute, but
2fc102
-             * only support local ranges and trusts with algorithmic mapping. */
2fc102
-
2fc102
-            if (r->trusted_dom_sid == NULL && r->secondary_base_rid != 0) {
2fc102
-                /* local IPA domain */
2fc102
-                rid = 0;
2fc102
-                external_mapping = true;
2fc102
-                name = idmap_ctx->id_ctx->be->domain->name;
2fc102
-                sid = NULL;
2fc102
-            } else if (r->trusted_dom_sid != NULL
2fc102
-                    && r->secondary_base_rid == 0) {
2fc102
-                /* trusted domain */
2fc102
-                rid = r->base_rid;
2fc102
-                external_mapping = false;
2fc102
-                name = r->trusted_dom_sid;
2fc102
-                sid = r->trusted_dom_sid;
2fc102
-            } else {
2fc102
-                DEBUG(SSSDBG_MINOR_FAILURE, ("Cannot determine range type, " \
2fc102
-                                             "skipping id ange [%s].\n",
2fc102
-                                             r->name));
2fc102
-                continue;
2fc102
-            }
2fc102
-        } else {
2fc102
-            if (strcmp(r->range_type, IPA_RANGE_LOCAL) == 0) {
2fc102
-                rid = 0;
2fc102
-                external_mapping = true;
2fc102
-                name = idmap_ctx->id_ctx->be->domain->name;
2fc102
-                sid = NULL;
2fc102
-            } else if (strcmp(r->range_type, IPA_RANGE_AD_TRUST_POSIX) == 0) {
2fc102
-                rid = 0;
2fc102
-                external_mapping = true;
2fc102
-                name = r->trusted_dom_sid;
2fc102
-                sid = r->trusted_dom_sid;
2fc102
-            } else if (strcmp(r->range_type, IPA_RANGE_AD_TRUST) == 0) {
2fc102
-                rid = r->base_rid;
2fc102
-                external_mapping = false;
2fc102
-                name = r->trusted_dom_sid;
2fc102
-                sid = r->trusted_dom_sid;
2fc102
-            } else {
2fc102
-                DEBUG(SSSDBG_MINOR_FAILURE, ("Range type [%s] not supported, " \
2fc102
-                                             "skipping id range [%s].\n",
2fc102
-                                             r->range_type, r->name));
2fc102
-                continue;
2fc102
-            }
2fc102
+        ret = get_idmap_data_from_range(range_list[c],
2fc102
+                                        idmap_ctx->id_ctx->be->domain->name,
2fc102
+                                        &name, &sid, &rid, &range,
2fc102
+                                        &external_mapping);
2fc102
+        if (ret != EOK) {
2fc102
+            DEBUG(SSSDBG_OP_FAILURE, ("get_idmap_data_from_range failed for " \
2fc102
+                                      "id range [%s], skipping.\n",
2fc102
+                                      range_list[c]->name));
2fc102
+            continue;
2fc102
         }
2fc102
 
2fc102
-        range.min = r->base_id;
2fc102
-        range.max = r->base_id + r->id_range_size -1;
2fc102
         err = sss_idmap_add_domain_ex(idmap_ctx->map, name, sid, &range,
2fc102
-                                      r->name, rid, external_mapping);
2fc102
-        if (err != IDMAP_SUCCESS && err != IDMAP_COLLISION) {
2fc102
-            DEBUG(SSSDBG_CRIT_FAILURE, ("Could not add range [%s] to ID map\n",
2fc102
-                                        r->name));
2fc102
-            ret = EIO;
2fc102
+                                      range_list[c]->name, rid,
2fc102
+                                      external_mapping);
2fc102
+        if (err != IDMAP_SUCCESS) {
2fc102
+            if (!allow_collisions || err != IDMAP_COLLISION) {
2fc102
+                DEBUG(SSSDBG_CRIT_FAILURE, ("Could not add range [%s] to ID map\n",
2fc102
+                                            range_list[c]->name));
2fc102
+                ret = EIO;
2fc102
+                goto done;
2fc102
+            }
2fc102
+        }
2fc102
+    }
2fc102
+
2fc102
+    if (dom_name != NULL || dom_sid_str != NULL) {
2fc102
+        ret = ipa_idmap_check_posix_child(idmap_ctx, dom_name, dom_sid_str,
2fc102
+                                          range_count, range_list);
2fc102
+        if (ret != EOK) {
2fc102
+            DEBUG(SSSDBG_OP_FAILURE, ("ipa_idmap_check_posix_child failed.\n"));
2fc102
             goto done;
2fc102
         }
2fc102
     }
2fc102
 
2fc102
-    ret = ipa_idmap_check_posix_child(idmap_ctx, dom_name, dom_sid_str,
2fc102
-                                      range_count, range_list);
2fc102
-    if (ret != EOK) {
2fc102
-        DEBUG(SSSDBG_OP_FAILURE, ("ipa_idmap_check_posix_child failed.\n"));
2fc102
-        goto done;
2fc102
-    }
2fc102
-
2fc102
     ret = EOK;
2fc102
 
2fc102
 done:
2fc102
@@ -263,6 +286,14 @@ done:
2fc102
     return ret;
2fc102
 }
2fc102
 
2fc102
+errno_t ipa_idmap_find_new_domain(struct sdap_idmap_ctx *idmap_ctx,
2fc102
+                                  const char *dom_name,
2fc102
+                                  const char *dom_sid_str)
2fc102
+{
2fc102
+    return ipa_idmap_get_ranges_from_sysdb(idmap_ctx, dom_name, dom_sid_str,
2fc102
+                                           true);
2fc102
+}
2fc102
+
2fc102
 errno_t ipa_idmap_init(TALLOC_CTX *mem_ctx,
2fc102
                        struct sdap_id_ctx *id_ctx,
2fc102
                        struct sdap_idmap_ctx **_idmap_ctx)
2fc102
@@ -270,17 +301,7 @@ errno_t ipa_idmap_init(TALLOC_CTX *mem_ctx,
2fc102
     errno_t ret;
2fc102
     TALLOC_CTX *tmp_ctx;
2fc102
     enum idmap_error_code err;
2fc102
-    size_t c;
2fc102
     struct sdap_idmap_ctx *idmap_ctx = NULL;
2fc102
-    struct sysdb_ctx *sysdb = id_ctx->be->domain->sysdb;
2fc102
-    size_t range_count;
2fc102
-    struct range_info **range_list;
2fc102
-    struct range_info *r;
2fc102
-    struct sss_idmap_range range;
2fc102
-    uint32_t rid;
2fc102
-    bool external_mapping;
2fc102
-    char *name;
2fc102
-    char *sid;
2fc102
 
2fc102
     tmp_ctx = talloc_new(NULL);
2fc102
     if (!tmp_ctx) return ENOMEM;
2fc102
@@ -309,82 +330,13 @@ errno_t ipa_idmap_init(TALLOC_CTX *mem_ctx,
2fc102
         goto done;
2fc102
     }
2fc102
 
2fc102
-
2fc102
-    /* Read in any existing mappings from the cache */
2fc102
-    ret = sysdb_get_ranges(tmp_ctx, sysdb, &range_count, &range_list);
2fc102
-    if (ret != EOK && ret != ENOENT) {
2fc102
-        DEBUG(SSSDBG_FATAL_FAILURE,
2fc102
-              ("Could not read ranges from the cache: [%s]\n",
2fc102
-               strerror(ret)));
2fc102
+    ret = ipa_idmap_get_ranges_from_sysdb(idmap_ctx, NULL, NULL, false);
2fc102
+    if (ret != EOK) {
2fc102
+        DEBUG(SSSDBG_OP_FAILURE,
2fc102
+              ("ipa_idmap_get_ranges_from_sysdb failed.\n"));
2fc102
         goto done;
2fc102
     }
2fc102
 
2fc102
-    DEBUG(SSSDBG_CONF_SETTINGS,
2fc102
-          ("Initializing [%zu] domains for ID-mapping\n", range_count));
2fc102
-
2fc102
-    for (c = 0; c < range_count; c++) {
2fc102
-
2fc102
-        r = range_list[c];
2fc102
-
2fc102
-        if (r->range_type == NULL) {
2fc102
-            /* Older IPA servers might not have the range_type attribute, but
2fc102
-             * only support local ranges and trusts with algorithmic mapping. */
2fc102
-
2fc102
-            if (r->trusted_dom_sid == NULL && r->secondary_base_rid != 0) {
2fc102
-                /* local IPA domain */
2fc102
-                rid = 0;
2fc102
-                external_mapping = true;
2fc102
-                sid = NULL;
2fc102
-                name = id_ctx->be->domain->name;
2fc102
-            } else if (r->trusted_dom_sid != NULL
2fc102
-                    && r->secondary_base_rid == 0) {
2fc102
-                /* trusted domain */
2fc102
-                rid = r->base_rid;
2fc102
-                external_mapping = false;
2fc102
-                sid = r->trusted_dom_sid;
2fc102
-                name = sid;
2fc102
-            } else {
2fc102
-                DEBUG(SSSDBG_MINOR_FAILURE, ("Cannot determine range type, " \
2fc102
-                                             "skipping id ange [%s].\n",
2fc102
-                                             r->name));
2fc102
-                continue;
2fc102
-            }
2fc102
-        } else {
2fc102
-            if (strcmp(r->range_type, IPA_RANGE_LOCAL) == 0) {
2fc102
-                rid = 0;
2fc102
-                external_mapping = true;
2fc102
-                sid = NULL;
2fc102
-                name = id_ctx->be->domain->name;
2fc102
-            } else if (strcmp(r->range_type, IPA_RANGE_AD_TRUST_POSIX) == 0) {
2fc102
-                rid = 0;
2fc102
-                external_mapping = true;
2fc102
-                sid = r->trusted_dom_sid;
2fc102
-                name = sid;
2fc102
-            } else if (strcmp(r->range_type, IPA_RANGE_AD_TRUST) == 0) {
2fc102
-                rid = r->base_rid;
2fc102
-                external_mapping = false;
2fc102
-                sid = r->trusted_dom_sid;
2fc102
-                name = sid;
2fc102
-            } else {
2fc102
-                DEBUG(SSSDBG_MINOR_FAILURE, ("Range type [%s] not supported, " \
2fc102
-                                             "skipping id range [%s].\n",
2fc102
-                                             r->range_type, r->name));
2fc102
-                continue;
2fc102
-            }
2fc102
-        }
2fc102
-
2fc102
-        range.min = r->base_id;
2fc102
-        range.max = r->base_id + r->id_range_size -1;
2fc102
-        err = sss_idmap_add_domain_ex(idmap_ctx->map, name, sid, &range,
2fc102
-                                      r->name, rid, external_mapping);
2fc102
-        if (err != IDMAP_SUCCESS) {
2fc102
-            DEBUG(SSSDBG_CRIT_FAILURE, ("Could not add range [%s] to ID map\n",
2fc102
-                                        r->name));
2fc102
-            ret = EIO;
2fc102
-            goto done;
2fc102
-        }
2fc102
-    }
2fc102
-
2fc102
     *_idmap_ctx = talloc_steal(mem_ctx, idmap_ctx);
2fc102
     ret = EOK;
2fc102
 
2fc102
diff --git a/src/tests/cmocka/test_ipa_idmap.c b/src/tests/cmocka/test_ipa_idmap.c
2fc102
new file mode 100644
2fc102
index 0000000000000000000000000000000000000000..2fb2cde2f9a7f1172fb69b268d19b559ff9d2f32
2fc102
--- /dev/null
2fc102
+++ b/src/tests/cmocka/test_ipa_idmap.c
2fc102
@@ -0,0 +1,249 @@
2fc102
+/*
2fc102
+    Authors:
2fc102
+        Sumit Bose <sbose@redhat.com>
2fc102
+
2fc102
+    Copyright (C) 2014 Red Hat
2fc102
+
2fc102
+    SSSD tests: Unit tests for id-mapping in the IPA provider
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 <popt.h>
2fc102
+
2fc102
+#include "tests/cmocka/common_mock.h"
2fc102
+#include "lib/idmap/sss_idmap.h"
2fc102
+#include "providers/ipa/ipa_common.h"
2fc102
+#include "providers/ldap/sdap_idmap.h"
2fc102
+
2fc102
+#define RANGE_NAME discard_const("range1")
2fc102
+#define DOMAIN_SID discard_const("S-1-5-21-2-3-4")
2fc102
+#define DOMAIN_NAME discard_const("dom.test")
2fc102
+#define BASE_RID 111
2fc102
+#define SECONDARY_BASE_RID 11223344
2fc102
+#define BASE_ID 123456
2fc102
+#define RANGE_SIZE 222222
2fc102
+#define RANGE_MAX (BASE_ID + RANGE_SIZE - 1)
2fc102
+
2fc102
+void test_get_idmap_data_from_range(void **state)
2fc102
+{
2fc102
+    char *dom_name;
2fc102
+    char *sid;
2fc102
+    uint32_t rid;
2fc102
+    struct sss_idmap_range range;
2fc102
+    bool external_mapping;
2fc102
+    size_t c;
2fc102
+    errno_t ret;
2fc102
+
2fc102
+    struct test_data {
2fc102
+        struct range_info r;
2fc102
+        errno_t exp_ret;
2fc102
+        char *exp_dom_name;
2fc102
+        char *exp_sid;
2fc102
+        uint32_t exp_rid;
2fc102
+        struct sss_idmap_range exp_range;
2fc102
+        bool exp_external_mapping;
2fc102
+    } d[] = {
2fc102
+        /* working IPA_RANGE_LOCAL range */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, SECONDARY_BASE_RID,
2fc102
+          NULL, discard_const(IPA_RANGE_LOCAL)},
2fc102
+         EOK, DOMAIN_NAME, NULL, 0, {BASE_ID, RANGE_MAX}, true},
2fc102
+        /* working old-style IPA_RANGE_LOCAL range without range type */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, SECONDARY_BASE_RID,
2fc102
+          NULL, NULL},
2fc102
+         EOK, DOMAIN_NAME, NULL, 0, {BASE_ID, RANGE_MAX}, true},
2fc102
+        /* old-style IPA_RANGE_LOCAL without SID and secondary base rid */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, NULL, NULL},
2fc102
+         EINVAL, NULL, NULL, 0, {0, 0}, false},
2fc102
+        /* old-style range with SID and secondary base rid */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, SECONDARY_BASE_RID,
2fc102
+          DOMAIN_SID, NULL},
2fc102
+         EINVAL, NULL, NULL, 0, {0, 0}, false},
2fc102
+        /* working IPA_RANGE_AD_TRUST range */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, DOMAIN_SID,
2fc102
+          discard_const(IPA_RANGE_AD_TRUST)},
2fc102
+         EOK, DOMAIN_SID, DOMAIN_SID, BASE_RID, {BASE_ID, RANGE_MAX}, false},
2fc102
+        /* working old-style IPA_RANGE_AD_TRUST range without range type */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, DOMAIN_SID, NULL},
2fc102
+         EOK, DOMAIN_SID, DOMAIN_SID, BASE_RID, {BASE_ID, RANGE_MAX}, false},
2fc102
+        /* working IPA_RANGE_AD_TRUST_POSIX range */
2fc102
+        {{RANGE_NAME, BASE_ID, RANGE_SIZE, BASE_RID, 0, DOMAIN_SID,
2fc102
+          discard_const(IPA_RANGE_AD_TRUST_POSIX)},
2fc102
+         EOK, DOMAIN_SID, DOMAIN_SID, 0, {BASE_ID, RANGE_MAX}, true},
2fc102
+        {{0}, 0, NULL, NULL, 0, {0, 0}, false}
2fc102
+    };
2fc102
+
2fc102
+    for (c = 0; d[c].exp_dom_name != NULL; c++) {
2fc102
+        ret = get_idmap_data_from_range(&d[c].r, DOMAIN_NAME, &dom_name, &sid,
2fc102
+                                        &rid, &range, &external_mapping);
2fc102
+        assert_int_equal(ret, d[c].exp_ret);
2fc102
+        assert_string_equal(dom_name, d[c].exp_dom_name);
2fc102
+        if (d[c].exp_sid == NULL) {
2fc102
+            assert_null(sid);
2fc102
+        } else {
2fc102
+            assert_string_equal(sid, d[c].exp_sid);
2fc102
+        }
2fc102
+        assert_int_equal(rid, d[c].exp_rid);
2fc102
+        assert_int_equal(range.min, d[c].exp_range.min);
2fc102
+        assert_int_equal(range.max, d[c].exp_range.max);
2fc102
+        assert_true(external_mapping == d[c].exp_external_mapping);
2fc102
+    }
2fc102
+}
2fc102
+
2fc102
+errno_t __wrap_sysdb_get_ranges(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
2fc102
+                                size_t *range_count,
2fc102
+                                struct range_info ***range_list)
2fc102
+{
2fc102
+
2fc102
+    *range_count = sss_mock_type(size_t);
2fc102
+    *range_list = talloc_steal(mem_ctx,
2fc102
+                               sss_mock_ptr_type(struct range_info **));
2fc102
+    return EOK;
2fc102
+}
2fc102
+
2fc102
+struct test_ctx {
2fc102
+    struct sdap_idmap_ctx *idmap_ctx;
2fc102
+    struct sdap_id_ctx *sdap_id_ctx;
2fc102
+};
2fc102
+
2fc102
+static struct range_info **get_range_list(TALLOC_CTX *mem_ctx)
2fc102
+{
2fc102
+    struct range_info **range_list;
2fc102
+
2fc102
+    range_list = talloc_array(mem_ctx, struct range_info *, 2);
2fc102
+    assert_non_null(range_list);
2fc102
+
2fc102
+    range_list[0] = talloc_zero(range_list, struct range_info);
2fc102
+    assert_non_null(range_list[0]);
2fc102
+
2fc102
+    range_list[0]->name = talloc_strdup(range_list[0], RANGE_NAME);
2fc102
+    assert_non_null( range_list[0]->name);
2fc102
+    range_list[0]->base_id = BASE_ID;
2fc102
+    range_list[0]->id_range_size = RANGE_SIZE;
2fc102
+    range_list[0]->base_rid = BASE_RID;
2fc102
+    range_list[0]->secondary_base_rid = 0;
2fc102
+    range_list[0]->trusted_dom_sid = talloc_strdup(range_list[0], DOMAIN_SID);
2fc102
+    assert_non_null(range_list[0]->trusted_dom_sid);
2fc102
+    range_list[0]->range_type = talloc_strdup(range_list[0],
2fc102
+                                              IPA_RANGE_AD_TRUST);
2fc102
+    assert_non_null(range_list[0]->range_type);
2fc102
+
2fc102
+    return range_list;
2fc102
+}
2fc102
+
2fc102
+void setup_idmap_ctx(void **state)
2fc102
+{
2fc102
+    int ret;
2fc102
+    struct test_ctx *test_ctx;
2fc102
+
2fc102
+    assert_true(leak_check_setup());
2fc102
+
2fc102
+    test_ctx = talloc_zero(global_talloc_context, struct test_ctx);
2fc102
+    assert_non_null(test_ctx);
2fc102
+
2fc102
+    test_ctx->sdap_id_ctx = talloc_zero(test_ctx,
2fc102
+                                        struct sdap_id_ctx);
2fc102
+    assert_non_null(test_ctx->sdap_id_ctx);
2fc102
+
2fc102
+    test_ctx->sdap_id_ctx->be = talloc_zero(test_ctx->sdap_id_ctx,
2fc102
+                                            struct be_ctx);
2fc102
+    assert_non_null(test_ctx->sdap_id_ctx->be);
2fc102
+
2fc102
+    test_ctx->sdap_id_ctx->be->domain = talloc_zero(test_ctx->sdap_id_ctx->be,
2fc102
+                                                    struct sss_domain_info);
2fc102
+    assert_non_null(test_ctx->sdap_id_ctx->be->domain);
2fc102
+
2fc102
+    test_ctx->sdap_id_ctx->be->domain->name =
2fc102
+                  talloc_strdup(test_ctx->sdap_id_ctx->be->domain, DOMAIN_NAME);
2fc102
+    assert_non_null(test_ctx->sdap_id_ctx->be->domain->name);
2fc102
+
2fc102
+    will_return(__wrap_sysdb_get_ranges, 1);
2fc102
+    will_return(__wrap_sysdb_get_ranges, get_range_list(global_talloc_context));
2fc102
+
2fc102
+    ret = ipa_idmap_init(test_ctx, test_ctx->sdap_id_ctx,
2fc102
+                         &test_ctx->idmap_ctx);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    check_leaks_push(test_ctx);
2fc102
+    *state = test_ctx;
2fc102
+}
2fc102
+
2fc102
+void teardown_idmap_ctx(void **state)
2fc102
+{
2fc102
+    struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
2fc102
+
2fc102
+    assert_non_null(test_ctx);
2fc102
+
2fc102
+    assert_true(check_leaks_pop(test_ctx) == true);
2fc102
+
2fc102
+    talloc_free(test_ctx);
2fc102
+    assert_true(leak_check_teardown());
2fc102
+}
2fc102
+
2fc102
+void test_ipa_idmap_get_ranges_from_sysdb(void **state)
2fc102
+{
2fc102
+    int ret;
2fc102
+    struct test_ctx *test_ctx = talloc_get_type(*state, struct test_ctx);
2fc102
+    assert_non_null(test_ctx);
2fc102
+
2fc102
+    will_return(__wrap_sysdb_get_ranges, 1);
2fc102
+    will_return(__wrap_sysdb_get_ranges, get_range_list(test_ctx->idmap_ctx));
2fc102
+    ret = ipa_idmap_get_ranges_from_sysdb(test_ctx->idmap_ctx,
2fc102
+                                          DOMAIN_NAME, DOMAIN_SID, true);
2fc102
+    assert_int_equal(ret, EOK);
2fc102
+
2fc102
+    will_return(__wrap_sysdb_get_ranges, 1);
2fc102
+    will_return(__wrap_sysdb_get_ranges, get_range_list(global_talloc_context));
2fc102
+    ret = ipa_idmap_get_ranges_from_sysdb(test_ctx->idmap_ctx,
2fc102
+                                          DOMAIN_NAME, DOMAIN_SID, false);
2fc102
+    assert_int_equal(ret, EIO);
2fc102
+}
2fc102
+
2fc102
+int main(int argc, const char *argv[])
2fc102
+{
2fc102
+    poptContext pc;
2fc102
+    int opt;
2fc102
+    struct poptOption long_options[] = {
2fc102
+        POPT_AUTOHELP
2fc102
+        SSSD_DEBUG_OPTS
2fc102
+        POPT_TABLEEND
2fc102
+    };
2fc102
+
2fc102
+    const UnitTest tests[] = {
2fc102
+        unit_test(test_get_idmap_data_from_range),
2fc102
+        unit_test_setup_teardown(test_ipa_idmap_get_ranges_from_sysdb,
2fc102
+                                 setup_idmap_ctx, teardown_idmap_ctx),
2fc102
+    };
2fc102
+
2fc102
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
2fc102
+    debug_level = SSSDBG_INVALID;
2fc102
+
2fc102
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
2fc102
+    while((opt = poptGetNextOpt(pc)) != -1) {
2fc102
+        switch(opt) {
2fc102
+        default:
2fc102
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
2fc102
+                    poptBadOption(pc, 0), poptStrerror(opt));
2fc102
+            poptPrintUsage(pc, stderr, 0);
2fc102
+            return 1;
2fc102
+        }
2fc102
+    }
2fc102
+    poptFreeContext(pc);
2fc102
+
2fc102
+    DEBUG_INIT(debug_level);
2fc102
+
2fc102
+    tests_set_cwd();
2fc102
+
2fc102
+    return run_tests(tests);
2fc102
+}
2fc102
-- 
2fc102
1.8.5.3
2fc102