From 329165182d0decee35f3837c1d2ad899f99e9950 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Tue, 28 Jan 2014 14:51:58 +0100
Subject: [PATCH 70/71] DB: Add sss_ldb_el_to_string_list
---
src/db/sysdb.c | 41 ++++++++++++++++++++++++++---------------
src/db/sysdb.h | 2 ++
src/tests/sysdb-tests.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 77 insertions(+), 15 deletions(-)
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index 0e07ed60858298a1ac85d06146ccb98c5899a705..592cadc1a322ee8504c407a508e34e7eb9465a15 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -466,35 +466,46 @@ errno_t sysdb_attrs_get_bool(struct sysdb_attrs *attrs, const char *name,
return EOK;
}
-int sysdb_attrs_get_string_array(struct sysdb_attrs *attrs, const char *name,
- TALLOC_CTX *mem_ctx, const char ***string)
+const char **sss_ldb_el_to_string_list(TALLOC_CTX *mem_ctx,
+ struct ldb_message_element *el)
{
- struct ldb_message_element *el;
- int ret;
unsigned int u;
const char **a;
- ret = sysdb_attrs_get_el_ext(attrs, name, false, &el);
- if (ret) {
- return ret;
- }
-
- a = talloc_array(mem_ctx, const char *, el->num_values + 1);
+ a = talloc_zero_array(mem_ctx, const char *, el->num_values + 1);
if (a == NULL) {
- return ENOMEM;
+ return NULL;
}
- memset(a, 0, sizeof(const char *) * (el->num_values + 1));
-
- for(u = 0; u < el->num_values; u++) {
+ for (u = 0; u < el->num_values; u++) {
a[u] = talloc_strndup(a, (const char *)el->values[u].data,
el->values[u].length);
if (a[u] == NULL) {
talloc_free(a);
- return ENOMEM;
+ return NULL;
}
}
+ return a;
+}
+
+int sysdb_attrs_get_string_array(struct sysdb_attrs *attrs, const char *name,
+ TALLOC_CTX *mem_ctx, const char ***string)
+{
+ struct ldb_message_element *el;
+ int ret;
+ const char **a;
+
+ ret = sysdb_attrs_get_el_ext(attrs, name, false, &el);
+ if (ret) {
+ return ret;
+ }
+
+ a = sss_ldb_el_to_string_list(mem_ctx, el);
+ if (a == NULL) {
+ return ENOMEM;
+ }
+
*string = a;
return EOK;
}
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 9677294b22e47f5169d7631673beec2dbc6117ad..5bd2c50ebff1ca1f4bb11d12e40512d8f460acbb 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -288,6 +288,8 @@ int sysdb_attrs_steal_string(struct sysdb_attrs *attrs,
const char *name, char *str);
int sysdb_attrs_get_string(struct sysdb_attrs *attrs, const char *name,
const char **string);
+const char **sss_ldb_el_to_string_list(TALLOC_CTX *mem_ctx,
+ struct ldb_message_element *el);
int sysdb_attrs_get_string_array(struct sysdb_attrs *attrs, const char *name,
TALLOC_CTX *mem_ctx, const char ***string);
errno_t sysdb_attrs_get_bool(struct sysdb_attrs *attrs, const char *name,
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 63ffac82e15849e5f6534462ce7c58b183412acc..69b09c7d4580595854330062e0f549eab1f8cf22 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -4449,6 +4449,52 @@ START_TEST(test_sysdb_attrs_add_lc_name_alias)
}
END_TEST
+START_TEST(test_sysdb_attrs_get_string_array)
+{
+ int ret;
+ struct sysdb_attrs *attrs;
+ const char **list;
+ const char *attrname = "test_attr";
+ TALLOC_CTX *tmp_ctx;
+ struct ldb_message_element *el = NULL;
+
+ tmp_ctx = talloc_new(NULL);
+ fail_unless(tmp_ctx != NULL, "talloc_new failed");
+
+ attrs = sysdb_new_attrs(NULL);
+ fail_unless(attrs != NULL, "sysdb_new_attrs failed");
+
+ ret = sysdb_attrs_add_string(attrs, attrname, "val1");
+ fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
+ ret = sysdb_attrs_add_string(attrs, attrname, "val2");
+ fail_unless(ret == EOK, "sysdb_attrs_add_string failed");
+
+ ret = sysdb_attrs_get_el_ext(attrs, attrname, false, &el);
+ fail_unless(ret == EOK, "sysdb_attrs_get_el_ext failed");
+
+ list = sss_ldb_el_to_string_list(tmp_ctx, el);
+ fail_if(list == NULL, ("sss_ldb_el_to_string_list failed\n"));
+
+ ck_assert_str_eq(list[0], "val1");
+ ck_assert_str_eq(list[1], "val2");
+ fail_unless(list[2] == NULL, "Expected terminated list");
+
+ talloc_free(list);
+
+ ret = sysdb_attrs_get_string_array(attrs, attrname, tmp_ctx, &list);
+ fail_unless(ret == EOK, "sysdb_attrs_get_string_array failed");
+
+ /* This test relies on values keeping the same order. It is the case
+ * with LDB, but if we ever switch from LDB, we need to amend the test
+ */
+ ck_assert_str_eq(list[0], "val1");
+ ck_assert_str_eq(list[1], "val2");
+ fail_unless(list[2] == NULL, "Expected terminated list");
+
+ talloc_free(tmp_ctx);
+}
+END_TEST
+
START_TEST(test_sysdb_has_enumerated)
{
errno_t ret;
@@ -5217,6 +5263,9 @@ Suite *create_sysdb_suite(void)
tcase_add_test(tc_sysdb, test_sysdb_attrs_add_lc_name_alias);
+/* ===== UTIL TESTS ===== */
+ tcase_add_test(tc_sysdb, test_sysdb_attrs_get_string_array);
+
/* Add all test cases to the test suite */
suite_add_tcase(s, tc_sysdb);
--
1.8.4.2