From e36ec25b898ecba4e28b244683c57a4372731042 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Thu, 16 Sep 2010 14:05:35 -0400 Subject: Fix sysdb_group_dn_name --- src/db/sysdb.c | 9 ++++++++- src/tests/sysdb-tests.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/db/sysdb.c b/src/db/sysdb.c index 4e1243c08..5f002d844 100644 --- a/src/db/sysdb.c +++ b/src/db/sysdb.c @@ -56,6 +56,7 @@ errno_t sysdb_group_dn_name(struct sysdb_ctx *ctx, void *memctx, const char *_dn, char **_name) { struct ldb_dn *dn; + const struct ldb_val *val; *_name = NULL; dn = ldb_dn_new_fmt(memctx, ctx->ldb, "%s", _dn); @@ -63,7 +64,13 @@ errno_t sysdb_group_dn_name(struct sysdb_ctx *ctx, void *memctx, return ENOMEM; } - *_name = talloc_strdup(memctx, ldb_dn_get_rdn_name(dn)); + val = ldb_dn_get_rdn_val(dn); + if (val == NULL) { + talloc_zfree(dn); + return EINVAL; + } + + *_name = talloc_strndup(memctx, (char *) val->data, val->length); if (!*_name) { talloc_zfree(dn); return ENOMEM; diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c index b320afdd0..98d9a20d4 100644 --- a/src/tests/sysdb-tests.c +++ b/src/tests/sysdb-tests.c @@ -2158,6 +2158,38 @@ START_TEST (test_sysdb_update_members) } END_TEST +START_TEST (test_sysdb_group_dn_name) +{ + struct sysdb_test_ctx *test_ctx; + int ret; + struct ldb_dn *group_dn; + const char *groupname; + char *parsed; + + /* Setup */ + ret = setup_sysdb_tests(&test_ctx); + if (ret != EOK) { + fail("Could not set up the test"); + return; + } + + groupname = talloc_asprintf(test_ctx, "testgroup%d", _i); + group_dn = sysdb_group_dn(test_ctx->sysdb, test_ctx, "LOCAL", groupname); + if (!group_dn || !groupname) { + fail("Out of memory"); + return; + } + + ret = sysdb_group_dn_name(test_ctx->sysdb, test_ctx, + ldb_dn_get_linearized(group_dn), &parsed); + fail_if(ret != EOK, "Cannot get the group name from DN"); + + fail_if(strcmp(groupname, parsed) != 0, + "Names don't match (got %s)", parsed); + talloc_free(test_ctx); +} +END_TEST + Suite *create_sysdb_suite(void) { Suite *s = suite_create("sysdb"); @@ -2176,6 +2208,9 @@ Suite *create_sysdb_suite(void) /* Verify the groups were added */ tcase_add_loop_test(tc_sysdb, test_sysdb_getgrnam, 28000, 28010); + /* sysdb_group_dn_name returns the name of the group in question */ + tcase_add_loop_test(tc_sysdb, test_sysdb_group_dn_name, 28000, 28010); + /* sysdb_store_user allows setting attributes for existing users */ tcase_add_loop_test(tc_sysdb, test_sysdb_store_user_existing, 27000, 27010); -- cgit