dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0006-sysdb-add-sysdb_attrs_copy.patch

bb7cd1
From cae55d342a5f5c5ac22ac913b9251c2112b22c42 Mon Sep 17 00:00:00 2001
bb7cd1
From: Sumit Bose <sbose@redhat.com>
bb7cd1
Date: Mon, 21 Sep 2015 12:32:48 +0200
bb7cd1
Subject: [PATCH 06/15] sysdb: add sysdb_attrs_copy()
bb7cd1
MIME-Version: 1.0
bb7cd1
Content-Type: text/plain; charset=UTF-8
bb7cd1
Content-Transfer-Encoding: 8bit
bb7cd1
bb7cd1
Related to https://pagure.io/SSSD/sssd/issue/3050
bb7cd1
bb7cd1
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
bb7cd1
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
bb7cd1
---
bb7cd1
 src/db/sysdb.c          | 24 ++++++++++++++
bb7cd1
 src/db/sysdb.h          |  1 +
bb7cd1
 src/tests/sysdb-tests.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++
bb7cd1
 3 files changed, 112 insertions(+)
bb7cd1
bb7cd1
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
bb7cd1
index 5160e3df3810a113d4ec1371350e51a074aaa146..98b7afbfab5141fa9b63a4aab31c620545b3c1f2 100644
bb7cd1
--- a/src/db/sysdb.c
bb7cd1
+++ b/src/db/sysdb.c
bb7cd1
@@ -752,6 +752,30 @@ done:
bb7cd1
     return ret;
bb7cd1
 }
bb7cd1
 
bb7cd1
+errno_t sysdb_attrs_copy(struct sysdb_attrs *src, struct sysdb_attrs *dst)
bb7cd1
+{
bb7cd1
+    int ret;
bb7cd1
+    size_t c;
bb7cd1
+    size_t d;
bb7cd1
+
bb7cd1
+    if (src == NULL || dst == NULL) {
bb7cd1
+        return EINVAL;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    for (c = 0; c < src->num; c++) {
bb7cd1
+        for (d = 0; d < src->a[c].num_values; d++) {
bb7cd1
+            ret = sysdb_attrs_add_val_safe(dst, src->a[c].name,
bb7cd1
+                                           &src->a[c].values[d]);
bb7cd1
+            if (ret != EOK) {
bb7cd1
+                DEBUG(SSSDBG_OP_FAILURE, "sysdb_attrs_add_val failed.\n");
bb7cd1
+                return ret;
bb7cd1
+            }
bb7cd1
+        }
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    return EOK;
bb7cd1
+}
bb7cd1
+
bb7cd1
 int sysdb_attrs_users_from_str_list(struct sysdb_attrs *attrs,
bb7cd1
                                     const char *attr_name,
bb7cd1
                                     const char *domain,
bb7cd1
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
bb7cd1
index 83d0d794c737c094d1fd52e7cc7f2113b5d9a7a0..c677957bb639e40db2f985205160612094302e78 100644
bb7cd1
--- a/src/db/sysdb.h
bb7cd1
+++ b/src/db/sysdb.h
bb7cd1
@@ -352,6 +352,7 @@ int sysdb_attrs_add_lc_name_alias_safe(struct sysdb_attrs *attrs,
bb7cd1
 int sysdb_attrs_copy_values(struct sysdb_attrs *src,
bb7cd1
                             struct sysdb_attrs *dst,
bb7cd1
                             const char *name);
bb7cd1
+errno_t sysdb_attrs_copy(struct sysdb_attrs *src, struct sysdb_attrs *dst);
bb7cd1
 int sysdb_attrs_get_el(struct sysdb_attrs *attrs, const char *name,
bb7cd1
                        struct ldb_message_element **el);
bb7cd1
 int sysdb_attrs_get_el_ext(struct sysdb_attrs *attrs, const char *name,
bb7cd1
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
bb7cd1
index 013b01a9a68d9de87d796d3aff41d98cef8cccc3..c343c734a27a335303974b6866a5d9e88d4c307e 100644
bb7cd1
--- a/src/tests/sysdb-tests.c
bb7cd1
+++ b/src/tests/sysdb-tests.c
bb7cd1
@@ -4997,6 +4997,92 @@ START_TEST(test_sysdb_attrs_add_string_safe)
bb7cd1
 }
bb7cd1
 END_TEST
bb7cd1
 
bb7cd1
+START_TEST(test_sysdb_attrs_copy)
bb7cd1
+{
bb7cd1
+    int ret;
bb7cd1
+    struct sysdb_attrs *src;
bb7cd1
+    struct sysdb_attrs *dst;
bb7cd1
+    TALLOC_CTX *tmp_ctx;
bb7cd1
+    const char *val;
bb7cd1
+    const char **array;
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(NULL, NULL);
bb7cd1
+    fail_unless(ret == EINVAL, "Wrong return code");
bb7cd1
+
bb7cd1
+    tmp_ctx = talloc_new(NULL);
bb7cd1
+    fail_unless(tmp_ctx != NULL, "talloc_new failed");
bb7cd1
+
bb7cd1
+    src = sysdb_new_attrs(tmp_ctx);
bb7cd1
+    fail_unless(src != NULL, "sysdb_new_attrs failed");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(src, NULL);
bb7cd1
+    fail_unless(ret == EINVAL, "Wrong return code");
bb7cd1
+
bb7cd1
+    dst = sysdb_new_attrs(tmp_ctx);
bb7cd1
+    fail_unless(dst != NULL, "sysdb_new_attrs failed");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(NULL, dst);
bb7cd1
+    fail_unless(ret == EINVAL, "Wrong return code");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(src, dst);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_copy failed");
bb7cd1
+    fail_unless(dst->num == 0, "Wrong number of elements");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_add_string(src, TEST_ATTR_NAME, TEST_ATTR_VALUE);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(src, dst);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_copy failed");
bb7cd1
+    fail_unless(dst->num == 1, "Wrong number of elements");
bb7cd1
+    ret = sysdb_attrs_get_string(dst, TEST_ATTR_NAME, &val;;
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_get_string failed.\n");
bb7cd1
+    fail_unless(strcmp(val, TEST_ATTR_VALUE) == 0, "Wrong attribute value.");
bb7cd1
+
bb7cd1
+    /* Make sure the same entry is not copied twice */
bb7cd1
+    ret = sysdb_attrs_copy(src, dst);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_copy failed");
bb7cd1
+    fail_unless(dst->num == 1, "Wrong number of elements");
bb7cd1
+    ret = sysdb_attrs_get_string(dst, TEST_ATTR_NAME, &val;;
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_get_string failed.\n");
bb7cd1
+    fail_unless(strcmp(val, TEST_ATTR_VALUE) == 0, "Wrong attribute value.");
bb7cd1
+
bb7cd1
+    /* Add new value to existing attribute */
bb7cd1
+    ret = sysdb_attrs_add_string(src, TEST_ATTR_NAME, TEST_ATTR_VALUE"_2nd");
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(src, dst);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_copy failed");
bb7cd1
+    fail_unless(dst->num == 1, "Wrong number of elements");
bb7cd1
+    ret = sysdb_attrs_get_string_array(dst, TEST_ATTR_NAME, tmp_ctx, &array);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_get_string_array failed.\n");
bb7cd1
+    fail_unless(strcmp(array[0], TEST_ATTR_VALUE) == 0,
bb7cd1
+                       "Wrong attribute value.");
bb7cd1
+    fail_unless(strcmp(array[1], TEST_ATTR_VALUE"_2nd") == 0,
bb7cd1
+                       "Wrong attribute value.");
bb7cd1
+    fail_unless(array[2] == NULL, "Wrong number of values.");
bb7cd1
+
bb7cd1
+    /* Add new attribute */
bb7cd1
+    ret = sysdb_attrs_add_string(src, TEST_ATTR_NAME"_2nd", TEST_ATTR_VALUE);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_add_val failed.");
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_copy(src, dst);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_copy failed");
bb7cd1
+    fail_unless(dst->num == 2, "Wrong number of elements");
bb7cd1
+    ret = sysdb_attrs_get_string_array(dst, TEST_ATTR_NAME, tmp_ctx, &array);
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_get_string_array failed.\n");
bb7cd1
+    fail_unless(strcmp(array[0], TEST_ATTR_VALUE) == 0,
bb7cd1
+                       "Wrong attribute value.");
bb7cd1
+    fail_unless(strcmp(array[1], TEST_ATTR_VALUE"_2nd") == 0,
bb7cd1
+                       "Wrong attribute value.");
bb7cd1
+    fail_unless(array[2] == NULL, "Wrong number of values.");
bb7cd1
+    ret = sysdb_attrs_get_string(dst, TEST_ATTR_NAME"_2nd", &val;;
bb7cd1
+    fail_unless(ret == EOK, "sysdb_attrs_get_string failed.\n");
bb7cd1
+    fail_unless(strcmp(val, TEST_ATTR_VALUE) == 0, "Wrong attribute value.");
bb7cd1
+
bb7cd1
+    talloc_free(tmp_ctx);
bb7cd1
+}
bb7cd1
+END_TEST
bb7cd1
+
bb7cd1
 START_TEST (test_sysdb_search_return_ENOENT)
bb7cd1
 {
bb7cd1
     struct sysdb_test_ctx *test_ctx;
bb7cd1
@@ -6995,6 +7081,7 @@ Suite *create_sysdb_suite(void)
bb7cd1
     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_val);
bb7cd1
     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_val_safe);
bb7cd1
     tcase_add_test(tc_sysdb, test_sysdb_attrs_add_string_safe);
bb7cd1
+    tcase_add_test(tc_sysdb, test_sysdb_attrs_copy);
bb7cd1
 
bb7cd1
 /* ===== Test search return empty result ===== */
bb7cd1
     tcase_add_test(tc_sysdb, test_sysdb_search_return_ENOENT);
bb7cd1
-- 
bb7cd1
2.9.3
bb7cd1