From 68f94d995142fc1aadf278be2f6816566208e189 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Wed, 12 Nov 2014 12:43:23 +0100
Subject: [PATCH 113/115] sysdb: add sysdb_delete_view_tree()
---
src/db/sysdb.h | 2 +
src/db/sysdb_views.c | 34 +++++++++++++++++
src/tests/cmocka/test_sysdb_views.c | 74 +++++++++++++++++++++++++++++++++++++
3 files changed, 110 insertions(+)
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 7a51117f439dc54ded3433c230d2d262a4d660dc..a3ffa7b759e8aa16fc8a60cb01c6f0cb49278cc0 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -444,6 +444,8 @@ errno_t sysdb_update_view_name(struct sysdb_ctx *sysdb, const char *view_name);
errno_t sysdb_get_view_name(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb,
char **view_name);
+errno_t sysdb_delete_view_tree(struct sysdb_ctx *sysdb, const char *view_name);
+
errno_t sysdb_apply_default_override(struct sss_domain_info *domain,
struct sysdb_attrs *override_attrs,
struct ldb_dn *obj_dn);
diff --git a/src/db/sysdb_views.c b/src/db/sysdb_views.c
index 27b58701fe0f9a4f545df5e4bfb884c04517d0d3..e17321455ad2ac4e4f17094f83c482d95bb00d8b 100644
--- a/src/db/sysdb_views.c
+++ b/src/db/sysdb_views.c
@@ -180,6 +180,40 @@ done:
return ret;
}
+errno_t sysdb_delete_view_tree(struct sysdb_ctx *sysdb, const char *view_name)
+{
+ struct ldb_dn *dn;
+ TALLOC_CTX *tmp_ctx;
+ int ret;
+
+ tmp_ctx = talloc_new(NULL);
+ if (tmp_ctx == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
+ return ENOMEM;
+ }
+
+ dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_TMPL_VIEW_SEARCH_BASE,
+ view_name);
+ if (dn == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE, "ldb_dn_new_fmt failed.\n");
+ ret = EIO;
+ goto done;
+ }
+
+ ret = sysdb_delete_recursive(sysdb, dn, true);
+ if (ret != EOK) {
+ DEBUG(SSSDBG_OP_FAILURE, "sysdb_delete_recursive failed.\n");
+ goto done;
+ }
+
+ ret = EOK;
+
+done:
+ talloc_free(tmp_ctx);
+
+ return ret;
+}
+
static errno_t
add_name_and_aliases_for_name_override(struct sss_domain_info *domain,
struct sysdb_attrs *attrs,
diff --git a/src/tests/cmocka/test_sysdb_views.c b/src/tests/cmocka/test_sysdb_views.c
index 0dc51443b406673f131cc69be4d781f7c49e538c..6be28e624d102060121f9afa91b282f7e8620676 100644
--- a/src/tests/cmocka/test_sysdb_views.c
+++ b/src/tests/cmocka/test_sysdb_views.c
@@ -30,10 +30,22 @@
#include "tests/cmocka/common_mock.h"
#include "providers/ipa/ipa_id.h"
+#include "db/sysdb_private.h" /* for sysdb->ldb member */
#define TESTS_PATH "tests_sysdb_views"
#define TEST_CONF_FILE "tests_conf.ldb"
+#define TEST_ANCHOR_PREFIX ":ANCHOR:"
+#define TEST_VIEW_NAME "test view"
+#define TEST_VIEW_CONTAINER "cn=" TEST_VIEW_NAME ",cn=views,cn=sysdb"
+#define TEST_USER_NAME "test_user"
+#define TEST_USER_UID 1234
+#define TEST_USER_GID 5678
+#define TEST_USER_GECOS "Gecos field"
+#define TEST_USER_HOMEDIR "/home/home"
+#define TEST_USER_SHELL "/bin/shell"
+#define TEST_USER_SID "S-1-2-3-4"
+
struct sysdb_test_ctx {
struct sysdb_ctx *sysdb;
struct confdb_ctx *confdb;
@@ -219,6 +231,66 @@ void test_split_ipa_anchor(void **state)
assert_string_equal(uuid, "def");
}
+void test_sysdb_delete_view_tree(void **state)
+{
+ int ret;
+ struct ldb_message *msg;
+ struct ldb_message **msgs = NULL;
+ struct sysdb_attrs *attrs;
+ size_t count;
+ struct ldb_dn *views_dn;
+
+ struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
+ struct sysdb_test_ctx);
+
+ test_ctx->domain->mpg = false;
+
+ ret = sysdb_update_view_name(test_ctx->domain->sysdb, TEST_VIEW_NAME);
+ assert_int_equal(ret, EOK);
+
+ ret = sysdb_store_user(test_ctx->domain, TEST_USER_NAME, NULL,
+ TEST_USER_UID, TEST_USER_GID, TEST_USER_GECOS,
+ TEST_USER_HOMEDIR, TEST_USER_SHELL, NULL, NULL, NULL,
+ 0,0);
+ assert_int_equal(ret, EOK);
+
+ ret = sysdb_search_user_by_name(test_ctx, test_ctx->domain, TEST_USER_NAME,
+ NULL, &msg);
+ assert_int_equal(ret, EOK);
+ assert_non_null(msg);
+
+ attrs = sysdb_new_attrs(test_ctx);
+ assert_non_null(attrs);
+
+ ret = sysdb_attrs_add_string(attrs, SYSDB_OVERRIDE_ANCHOR_UUID,
+ TEST_ANCHOR_PREFIX TEST_USER_SID);
+ assert_int_equal(ret, EOK);
+
+ ret = sysdb_store_override(test_ctx->domain, TEST_VIEW_NAME,
+ SYSDB_MEMBER_USER, attrs, msg->dn);
+ assert_int_equal(ret, EOK);
+
+ views_dn = ldb_dn_new(test_ctx, test_ctx->domain->sysdb->ldb,
+ SYSDB_TMPL_VIEW_BASE);
+ assert_non_null(views_dn);
+
+ ret = sysdb_search_entry(test_ctx, test_ctx->domain->sysdb, views_dn,
+ LDB_SCOPE_SUBTREE, NULL, NULL, &count, &msgs);
+ assert_int_equal(ret, EOK);
+ assert_true(count > 1);
+ assert_non_null(msgs);
+
+ ret = sysdb_delete_view_tree(test_ctx->domain->sysdb, TEST_VIEW_NAME);
+ assert_int_equal(ret, EOK);
+
+ ret = sysdb_search_entry(test_ctx, test_ctx->domain->sysdb, views_dn,
+ LDB_SCOPE_SUBTREE, NULL, NULL, &count, &msgs);
+ assert_int_equal(ret, EOK);
+ assert_int_equal(count, 1);
+ assert_true(ldb_dn_compare(views_dn, msgs[0]->dn) == 0);
+
+}
+
int main(int argc, const char *argv[])
{
int rv;
@@ -238,6 +310,8 @@ int main(int argc, const char *argv[])
test_sysdb_setup, test_sysdb_teardown),
unit_test_setup_teardown(test_split_ipa_anchor,
test_sysdb_setup, test_sysdb_teardown),
+ unit_test_setup_teardown(test_sysdb_delete_view_tree,
+ test_sysdb_setup, test_sysdb_teardown),
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */
--
1.9.3