summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/db/sysdb.c37
-rw-r--r--src/db/sysdb.h2
-rw-r--r--src/tests/sysdb-tests.c49
3 files changed, 75 insertions, 13 deletions
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index 0e07ed608..592cadc1a 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -466,12 +466,34 @@ errno_t sysdb_attrs_get_bool(struct sysdb_attrs *attrs, const char *name,
return EOK;
}
+const char **sss_ldb_el_to_string_list(TALLOC_CTX *mem_ctx,
+ struct ldb_message_element *el)
+{
+ unsigned int u;
+ const char **a;
+
+ a = talloc_zero_array(mem_ctx, const char *, el->num_values + 1);
+ if (a == NULL) {
+ return NULL;
+ }
+
+ 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 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;
- unsigned int u;
const char **a;
ret = sysdb_attrs_get_el_ext(attrs, name, false, &el);
@@ -479,22 +501,11 @@ int sysdb_attrs_get_string_array(struct sysdb_attrs *attrs, const char *name,
return ret;
}
- a = talloc_array(mem_ctx, const char *, el->num_values + 1);
+ a = sss_ldb_el_to_string_list(mem_ctx, el);
if (a == NULL) {
return ENOMEM;
}
- memset(a, 0, sizeof(const char *) * (el->num_values + 1));
-
- 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;
- }
- }
-
*string = a;
return EOK;
}
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 9677294b2..5bd2c50eb 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 63ffac82e..69b09c7d4 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);