diff options
author | Sumit Bose <sbose@redhat.com> | 2009-11-04 12:36:25 +0100 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2009-11-04 10:51:08 -0500 |
commit | 980851d1848562405d9047b499f7faf489d9031a (patch) | |
tree | 576a4975003dde455cde293bc8339d2dda6145da | |
parent | 0efb1696699304475847ba8c190b42000b639463 (diff) | |
download | sssd-980851d1848562405d9047b499f7faf489d9031a.tar.gz sssd-980851d1848562405d9047b499f7faf489d9031a.tar.bz2 sssd-980851d1848562405d9047b499f7faf489d9031a.zip |
Add sysdb_attrs_replace_name to sysdb API.
-rw-r--r-- | server/db/sysdb.c | 32 | ||||
-rw-r--r-- | server/db/sysdb.h | 3 | ||||
-rw-r--r-- | server/tests/sysdb-tests.c | 45 |
3 files changed, 80 insertions, 0 deletions
diff --git a/server/db/sysdb.c b/server/db/sysdb.c index a2ac3b22..ae32ef4b 100644 --- a/server/db/sysdb.c +++ b/server/db/sysdb.c @@ -1429,3 +1429,35 @@ int compare_ldb_dn_comp_num(const void *m1, const void *m2) return ldb_dn_get_comp_num(msg2->dn) - ldb_dn_get_comp_num(msg1->dn); } +int sysdb_attrs_replace_name(struct sysdb_attrs *attrs, const char *oldname, + const char *newname) +{ + struct ldb_message_element *e = NULL; + int i; + const char *dummy; + + if (attrs == NULL || oldname == NULL || newname == NULL) return EINVAL; + + for (i = 0; i < attrs->num; i++) { + if (strcasecmp(oldname, attrs->a[i].name) == 0) { + e = &(attrs->a[i]); + } + if (strcasecmp(newname, attrs->a[i].name) == 0) { + DEBUG(3, ("New attribute name [%s] already exists.\n", newname)); + return EEXIST; + } + } + + if (e != NULL) { + dummy = talloc_strdup(attrs, newname); + if (dummy == NULL) { + DEBUG(1, ("talloc_strdup failed.\n")); + return ENOMEM; + } + + talloc_free(discard_const(e->name)); + e->name = dummy; + } + + return EOK; +} diff --git a/server/db/sysdb.h b/server/db/sysdb.h index 642cc30a..0b474221 100644 --- a/server/db/sysdb.h +++ b/server/db/sysdb.h @@ -174,6 +174,9 @@ int sysdb_attrs_get_el(struct sysdb_attrs *attrs, const char *name, int sysdb_attrs_steal_string(struct sysdb_attrs *attrs, const char *name, char *str); +int sysdb_attrs_replace_name(struct sysdb_attrs *attrs, const char *oldname, + const char *newname); + /* convert an ldb error into an errno error */ int sysdb_error_to_errno(int ldberr); diff --git a/server/tests/sysdb-tests.c b/server/tests/sysdb-tests.c index ed61e279..d08a1552 100644 --- a/server/tests/sysdb-tests.c +++ b/server/tests/sysdb-tests.c @@ -2310,6 +2310,49 @@ START_TEST (test_sysdb_delete_recursive) } END_TEST +START_TEST (test_sysdb_attrs_replace_name) +{ + struct sysdb_attrs *attrs; + struct ldb_message_element *el; + int ret; + + attrs = sysdb_new_attrs(NULL); + fail_unless(attrs != NULL, "sysdb_new_attrs failed"); + + ret = sysdb_attrs_add_string(attrs, "foo", "bar"); + fail_unless(ret == EOK, "sysdb_attrs_add_string failed"); + + ret = sysdb_attrs_add_string(attrs, "fool", "bool"); + fail_unless(ret == EOK, "sysdb_attrs_add_string failed"); + + ret = sysdb_attrs_add_string(attrs, "foot", "boot"); + fail_unless(ret == EOK, "sysdb_attrs_add_string failed"); + + ret = sysdb_attrs_replace_name(attrs, "foo", "foot"); + fail_unless(ret == EEXIST, + "sysdb_attrs_replace overwrites existing attribute"); + + ret = sysdb_attrs_replace_name(attrs, "foo", "oof"); + fail_unless(ret == EOK, "sysdb_attrs_replace failed"); + + ret = sysdb_attrs_get_el(attrs, "foo", &el); + fail_unless(ret == EOK, "sysdb_attrs_get_el failed"); + fail_unless(el->num_values == 0, "Attribute foo is not empty."); + + ret = sysdb_attrs_get_el(attrs, "oof", &el); + fail_unless(ret == EOK, "sysdb_attrs_get_el failed"); + fail_unless(el->num_values == 1, + "Wrong number of values for attribute oof, " + "expected [1] got [%d].", el->num_values); + fail_unless(strncmp("bar", (char *) el->values[0].data, + el->values[0].length) == 0, + "Wrong value, expected [bar] got [%.*s]", el->values[0].length, + el->values[0].data); + + talloc_free(attrs); +} +END_TEST + Suite *create_sysdb_suite(void) { Suite *s = suite_create("sysdb"); @@ -2404,6 +2447,8 @@ Suite *create_sysdb_suite(void) /* test recursive delete */ tcase_add_test(tc_sysdb, test_sysdb_delete_recursive); + tcase_add_test(tc_sysdb, test_sysdb_attrs_replace_name); + /* Add all test cases to the test suite */ suite_add_tcase(s, tc_sysdb); |