diff options
author | Pavel Reichl <preichl@redhat.com> | 2014-11-04 08:52:54 +0000 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2014-12-02 22:41:07 +0100 |
commit | c9eaf8c1e02c155b7ca7ffb2b1edade8a23ce1ff (patch) | |
tree | 357b3c9afb511d30ad1d4d34bfee040f3da8c076 /src | |
parent | b6db8fe1d18bffd600899c8e02f4cea83d70e447 (diff) | |
download | sssd-c9eaf8c1e02c155b7ca7ffb2b1edade8a23ce1ff.tar.gz sssd-c9eaf8c1e02c155b7ca7ffb2b1edade8a23ce1ff.tar.xz sssd-c9eaf8c1e02c155b7ca7ffb2b1edade8a23ce1ff.zip |
SYSDB: sysdb_get_bool() return ENOENT & unit tests
sysdb_get_bool() return ENOENT if no result is found.
Unit test for sysdb_get_bool() & sysdb_set_bool() was added.
This patch also fixes ldap_setup_enumeration() to handle ENOENT returned by
sysdb_has_enumerated().
Resolves:
https://fedorahosted.org/sssd/ticket/1991
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/db/sysdb.c | 9 | ||||
-rw-r--r-- | src/providers/ldap/ldap_id_enum.c | 6 | ||||
-rw-r--r-- | src/tests/sysdb-tests.c | 62 |
3 files changed, 71 insertions, 6 deletions
diff --git a/src/db/sysdb.c b/src/db/sysdb.c index 1f02585e7..61a224001 100644 --- a/src/db/sysdb.c +++ b/src/db/sysdb.c @@ -1508,6 +1508,7 @@ errno_t sysdb_get_bool(struct sysdb_ctx *sysdb, errno_t ret; int lret; const char *attrs[2] = {attr_name, NULL}; + struct ldb_message_element *el; tmp_ctx = talloc_new(NULL); if (tmp_ctx == NULL) { @@ -1530,7 +1531,7 @@ errno_t sysdb_get_bool(struct sysdb_ctx *sysdb, * to contain this attribute. */ *value = false; - ret = EOK; + ret = ENOENT; goto done; } else if (res->count != 1) { DEBUG(SSSDBG_CRIT_FAILURE, @@ -1539,6 +1540,12 @@ errno_t sysdb_get_bool(struct sysdb_ctx *sysdb, goto done; } + el = ldb_msg_find_element(res->msgs[0], attr_name); + if (el == NULL || el->num_values == 0) { + ret = ENOENT; + goto done; + } + *value = ldb_msg_find_attr_as_bool(res->msgs[0], attr_name, false); ret = EOK; diff --git a/src/providers/ldap/ldap_id_enum.c b/src/providers/ldap/ldap_id_enum.c index 9ffa3e5d9..13d2a6254 100644 --- a/src/providers/ldap/ldap_id_enum.c +++ b/src/providers/ldap/ldap_id_enum.c @@ -41,7 +41,11 @@ errno_t ldap_setup_enumeration(struct be_ctx *be_ctx, struct ldap_enum_ctx *ectx; ret = sysdb_has_enumerated(sdom->dom, &has_enumerated); - if (ret != EOK) { + if (ret == ENOENT) { + /* default value */ + has_enumerated = false; + ret = EOK; + } else if (ret != EOK) { return ret; } diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c index 26835d9eb..b55901a30 100644 --- a/src/tests/sysdb-tests.c +++ b/src/tests/sysdb-tests.c @@ -3474,6 +3474,58 @@ START_TEST (test_sysdb_memberof_user_cleanup) } END_TEST +START_TEST (test_sysdb_set_get_bool) +{ + struct sysdb_test_ctx *test_ctx; + struct ldb_dn *dn, *ne_dn; + bool value; + int ret; + const char *attr_val = "BOOL_VALUE"; + + /* Setup */ + ret = setup_sysdb_tests(&test_ctx); + if (ret != EOK) { + fail("Could not set up the test"); + return; + } + + dn = sysdb_domain_dn(test_ctx, test_ctx->domain); + fail_unless(dn != NULL); + + /* attribute is not created yet */ + ret = sysdb_get_bool(test_ctx->sysdb, dn, attr_val, + &value); + fail_unless(ret == ENOENT, + "sysdb_get_bool returned %d:[%s], but ENOENT is expected", + ret, sss_strerror(ret)); + + /* add attribute */ + ret = sysdb_set_bool(test_ctx->sysdb, dn, test_ctx->domain->name, + attr_val, true); + fail_unless(ret == EOK); + + /* successfully obtain attribute */ + ret = sysdb_get_bool(test_ctx->sysdb, dn, attr_val, + &value); + fail_unless(ret == EOK, "sysdb_get_bool failed %d:[%s]", + ret, sss_strerror(ret)); + fail_unless(value == true); + + /* use non-existing DN */ + ne_dn = ldb_dn_new_fmt(test_ctx, test_ctx->sysdb->ldb, SYSDB_DOM_BASE, + "non-existing domain"); + fail_unless(ne_dn != NULL); + ret = sysdb_get_bool(test_ctx->sysdb, ne_dn, attr_val, + &value); + fail_unless(ret == ENOENT, + "sysdb_get_bool returned %d:[%s], but ENOENT is expected", + ret, sss_strerror(ret)); + + /* free ctx */ + talloc_free(test_ctx); +} +END_TEST + START_TEST (test_sysdb_attrs_to_list) { struct sysdb_attrs *attrs_list[3]; @@ -4907,10 +4959,9 @@ START_TEST(test_sysdb_has_enumerated) fail_if(ret != EOK, "Could not set up the test"); ret = sysdb_has_enumerated(test_ctx->domain, &enumerated); - fail_if(ret != EOK, "Error [%d][%s] checking enumeration", - ret, strerror(ret)); - - fail_if(enumerated, "Enumeration should default to false"); + fail_if(ret != ENOENT, + "Error [%d][%s] checking enumeration ENOENT is expected", + ret, strerror(ret)); ret = sysdb_set_enumerated(test_ctx->domain, true); fail_if(ret != EOK, "Error [%d][%s] setting enumeration", @@ -6197,6 +6248,9 @@ Suite *create_sysdb_suite(void) /* ===== Test search return empty result ===== */ tcase_add_test(tc_sysdb, test_sysdb_search_return_ENOENT); +/* ===== Misc ===== */ + tcase_add_test(tc_sysdb, test_sysdb_set_get_bool); + /* Add all test cases to the test suite */ suite_add_tcase(s, tc_sysdb); |