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