From da33a9d7742d45975bd97d33dd8ad04a843e7656 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Tue, 12 Feb 2013 13:18:00 +0100 Subject: Add utility functions for tests that use sysdb or tevent. There was shared code for several unit tests that connected to their own sysdb instance. This patch adds common code to run a generic request to completion or connect to a test sysdb. --- src/tests/common.h | 40 ++++++++++++ src/tests/common_dom.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tests/common_tev.c | 60 ++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 src/tests/common_dom.c create mode 100644 src/tests/common_tev.c (limited to 'src/tests') diff --git a/src/tests/common.h b/src/tests/common.h index f13181b7..b7eee46e 100644 --- a/src/tests/common.h +++ b/src/tests/common.h @@ -55,4 +55,44 @@ errno_t compare_sdap_attr_maps(struct sdap_attr_map *map1, size_t size1, struct sdap_attr_map *map2); +/* A common test structure for tests that require a domain to be set up. */ +struct sss_test_ctx { + struct sysdb_ctx *sysdb; + struct confdb_ctx *confdb; + struct tevent_context *ev; + struct sss_domain_info *dom; + + bool done; + int error; +}; + +struct sss_test_conf_param { + const char *key; + const char *value; +}; + +struct sss_test_ctx * +create_dom_test_ctx(TALLOC_CTX *mem_ctx, + const char *tests_path, + const char *confdb_path, + const char *sysdb_path, + const char *domain_name, + const char *id_provider, + struct sss_test_conf_param *params); + +void test_dom_suite_setup(const char *tests_path); + +void test_dom_suite_cleanup(const char *tests_path, + const char *confdb_path, + const char *sysdb_path); + +struct tevent_req * +test_request_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, errno_t err); + +#define test_req_succeed_send(mem_ctx, ev) test_request_send(mem_ctx, ev, 0) + +errno_t test_request_recv(struct tevent_req *req); + +int test_ev_loop(struct sss_test_ctx *tctx); + #endif /* !__TESTS_COMMON_H__ */ diff --git a/src/tests/common_dom.c b/src/tests/common_dom.c new file mode 100644 index 00000000..f8356210 --- /dev/null +++ b/src/tests/common_dom.c @@ -0,0 +1,167 @@ +/* + Authors: + Jakub Hrozek + + Copyright (C) 2013 Red Hat + + SSSD tests: Common utilities for tests that exercise domains + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include + +#include "tests/common.h" + +struct sss_test_ctx * +create_dom_test_ctx(TALLOC_CTX *mem_ctx, + const char *tests_path, + const char *confdb_path, + const char *sysdb_path, + const char *domain_name, + const char *id_provider, + struct sss_test_conf_param *params) +{ + struct sss_test_ctx *test_ctx; + char *conf_db; + size_t i; + const char *val[2]; + val[1] = NULL; + errno_t ret; + char *dompath; + + test_ctx = talloc_zero(mem_ctx, struct sss_test_ctx); + if (test_ctx == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_zero failed\n")); + goto fail; + } + + /* Create an event context */ + test_ctx->ev = tevent_context_init(test_ctx); + if (test_ctx->ev == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_context_init failed\n")); + goto fail; + } + + conf_db = talloc_asprintf(test_ctx, "%s/%s", tests_path, confdb_path); + if (conf_db == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_asprintf failed\n")); + goto fail; + } + + /* Connect to the conf db */ + ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, ("confdb_init failed: %d\n", ret)); + goto fail; + } + + val[0] = domain_name; + ret = confdb_add_param(test_ctx->confdb, true, + "config/sssd", "domains", val); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, ("cannot add domain: %d\n", ret)); + goto fail; + } + + dompath = talloc_asprintf(test_ctx, "config/domain/%s", domain_name); + if (dompath == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_asprintf failed\n")); + goto fail; + } + + val[0] = id_provider; + ret = confdb_add_param(test_ctx->confdb, true, + dompath, "id_provider", val); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, ("cannot add id_provider: %d\n", ret)); + goto fail; + } + + if (params) { + for (i=0; params[i].key; i++) { + val[0] = params[i].key; + ret = confdb_add_param(test_ctx->confdb, true, + dompath, params[i].value, val); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("cannot add parameter %s: %d\n", params[i].key, ret)); + goto fail; + } + } + } + + ret = sssd_domain_init(test_ctx, test_ctx->confdb, domain_name, + tests_path, &test_ctx->dom); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, ("cannot add id_provider: %d\n", ret)); + goto fail; + } + test_ctx->sysdb = test_ctx->dom->sysdb; + + return test_ctx; + +fail: + talloc_free(test_ctx); + return NULL; +} + +void test_dom_suite_setup(const char *tests_path) +{ + errno_t ret; + + /* Create tests directory if it doesn't exist */ + /* (relative to current dir) */ + ret = mkdir(tests_path, 0775); + if (ret != 0 && errno != EEXIST) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("Could not create test directory\n")); + } +} + +void test_dom_suite_cleanup(const char *tests_path, + const char *confdb_path, + const char *sysdb_path) +{ + errno_t ret; + char *conf_db; + char *sys_db; + + conf_db = talloc_asprintf(NULL, "%s/%s", tests_path, confdb_path); + sys_db = talloc_asprintf(NULL, "%s/%s", tests_path, sysdb_path); + if (!conf_db || !sys_db) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("Could not construct db paths\n")); + } + + errno = 0; + ret = unlink(conf_db); + if (ret != 0 && errno != ENOENT) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("Could not delete the test config ldb file (%d) (%s)\n", + errno, strerror(errno))); + } + + errno = 0; + ret = unlink(sys_db); + if (ret != 0 && errno != ENOENT) { + DEBUG(SSSDBG_CRIT_FAILURE, + ("Could not delete the test ldb file (%d) (%s)\n", + errno, strerror(errno))); + } + + talloc_free(conf_db); + talloc_free(sys_db); +} diff --git a/src/tests/common_tev.c b/src/tests/common_tev.c new file mode 100644 index 00000000..83ba9fd7 --- /dev/null +++ b/src/tests/common_tev.c @@ -0,0 +1,60 @@ +/* + Authors: + Jakub Hrozek + + Copyright (C) 2013 Red Hat + + SSSD tests: Common utilities for tests that exercise domains + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include + +#include "tests/common.h" + +struct tevent_req * +test_request_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, errno_t err) +{ + struct tevent_req *req; + int state; + + req = tevent_req_create(mem_ctx, &state, int); + if (!req) return NULL; + + if (err == EOK) { + tevent_req_done(req); + } else { + tevent_req_error(req, err); + } + tevent_req_post(req, ev); + return req; +} + +errno_t test_request_recv(struct tevent_req *req) +{ + TEVENT_REQ_RETURN_ON_ERROR(req); + + return EOK; +} + +int test_ev_loop(struct sss_test_ctx *tctx) +{ + while (!tctx->done) + tevent_loop_once(tctx->ev); + + return tctx->error; +} -- cgit