da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek/*
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek Authors:
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek Copyright (C) 2013 Red Hat
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek SSSD tests: Common utilities for tests that exercise domains
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek This program is free software; you can redistribute it and/or modify
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek it under the terms of the GNU General Public License as published by
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek (at your option) any later version.
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek This program is distributed in the hope that it will be useful,
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek GNU General Public License for more details.
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek You should have received a copy of the GNU General Public License
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek*/
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include <talloc.h>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include <errno.h>
af820c9fc6aa1768e2e6b0df78fb489dbb1b28d0Pavel Březina#include <ldb_module.h>
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek/* Including private header makes sure we can initialize test domains. */
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek#include "db/sysdb_private.h"
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek#include "tests/common.h"
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinastatic errno_t
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinamock_confdb(TALLOC_CTX *mem_ctx,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *tests_path,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *cdb_file,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct confdb_ctx **_cdb)
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek{
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina TALLOC_CTX *tmp_ctx = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct confdb_ctx *cdb = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina char *cdb_path = NULL;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek errno_t ret;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina tmp_ctx = talloc_new(NULL);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (tmp_ctx == NULL) {
c02b8482375837b57cb618ed56d4bede0e006d9dPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina return ENOMEM;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina cdb_path = talloc_asprintf(tmp_ctx, "%s/%s", tests_path, cdb_file);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (cdb_path == NULL) {
919cfb623e9b725eaa654c6e1b513d1305057314Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed\n");
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = ENOMEM;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
919cfb623e9b725eaa654c6e1b513d1305057314Jakub Hrozek }
919cfb623e9b725eaa654c6e1b513d1305057314Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* connect to the confdb */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = confdb_init(tmp_ctx, &cdb, cdb_path);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (ret != EOK) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov DEBUG(SSSDBG_CRIT_FAILURE, "confdb_init failed: %d\n", ret);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina *_cdb = talloc_steal(mem_ctx, cdb);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = EOK;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinadone:
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina talloc_free(tmp_ctx);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina return ret;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina}
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinastatic errno_t
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinamock_confdb_domain(TALLOC_CTX *mem_ctx,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct confdb_ctx *cdb,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *db_path,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *name,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *id_provider,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct sss_test_conf_param *params,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina char **_cdb_path)
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina{
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina TALLOC_CTX *tmp_ctx = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *val[2] = {NULL, NULL};
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina char *cdb_path = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina char **array = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina char *list = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina bool exists = false;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina errno_t ret;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina int i;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina tmp_ctx = talloc_new(NULL);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (tmp_ctx == NULL) {
c02b8482375837b57cb618ed56d4bede0e006d9dPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina return ENOMEM;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* get current domain list */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = confdb_get_string(cdb, tmp_ctx, "config/sssd", "domains",
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina NULL, &list);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* check if the domain is already in */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (list != NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = split_on_separator(tmp_ctx, list, ',', true, true, &array, NULL);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina for (i = 0; array[i] != NULL; i++) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (strcmp(array[i], name) == 0) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina exists = true;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina break;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* add domain to the list of enabled domains */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (!exists) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (list == NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina list = talloc_strdup(tmp_ctx, name);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina } else {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina list = talloc_asprintf_append(list, ", %s", name);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (list == NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = ENOMEM;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina val[0] = list;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = confdb_add_param(cdb, true, "config/sssd", "domains", val);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to change domain list [%d]: %s\n",
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret, sss_strerror(ret));
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* create domain section */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina cdb_path = talloc_asprintf(tmp_ctx, CONFDB_DOMAIN_PATH_TMPL, name);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (cdb_path == NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = ENOMEM;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek val[0] = id_provider;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = confdb_add_param(cdb, true, cdb_path, "id_provider", val);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add id_provider [%d]: %s\n",
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret, sss_strerror(ret));
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (params != NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina for (i = 0; params[i].key != NULL; i++) {
8164714c5c1ca06ed0435db281e0dbe1db80850cJakub Hrozek val[0] = params[i].value;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = confdb_add_param(cdb, true, cdb_path, params[i].key, val);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add parameter %s [%d]: "
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina "%s\n", params[i].key, ret, sss_strerror(ret));
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (_cdb_path != NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina *_cdb_path = talloc_steal(mem_ctx, cdb_path);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = EOK;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinadone:
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina talloc_free(tmp_ctx);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina return ret;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina}
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozekvoid reset_ldb_errstrings(struct sss_domain_info *dom)
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek{
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek ldb_reset_err_string(sysdb_ctx_get_ldb(dom->sysdb));
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek if (dom->sysdb->ldb_ts) {
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek ldb_reset_err_string(dom->sysdb->ldb_ts);
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek }
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek}
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinastatic errno_t
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinamock_domain(TALLOC_CTX *mem_ctx,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct confdb_ctx *cdb,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *db_path,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina const char *name,
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct sss_domain_info **_domain)
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina{
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina struct sss_domain_info *domain = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina errno_t ret;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* initialize sysdb */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = sssd_domain_init(mem_ctx, cdb, name, db_path, &domain);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "sssd_domain_init() of %s failed "
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina "[%d]: %s\n", name, ret, sss_strerror(ret));
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek }
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek reset_ldb_errstrings(domain);
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina /* init with an AD-style regex to be able to test flat name */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = sss_names_init_from_args(domain,
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek "(((?P<domain>[^\\\\]+)\\\\(?P<name>.+$))|" \
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek "((?P<name>[^@]+)@(?P<domain>.+$))|" \
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek "(^(?P<name>[^@\\\\]+)$))",
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina "%1$s@%2$s", &domain->names);
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek if (ret != EOK) {
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "cannot create names context\n");
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto done;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (_domain != NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina *_domain = domain;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret = EOK;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinadone:
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina talloc_free(domain);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina return ret;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina}
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březinastruct sss_test_ctx *
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březinacreate_multidom_test_ctx(TALLOC_CTX *mem_ctx,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *tests_path,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *cdb_file,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char **domains,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *id_provider,
f1e3364a72eb75673d10cf8c97ba8f1d7a385405Petr Čech struct sss_test_conf_param **params)
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina{
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina struct sss_domain_info *domain = NULL;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina struct sss_test_ctx *test_ctx = NULL;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina char *cdb_path = NULL;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina errno_t ret;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina int i;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina test_ctx = create_ev_test_ctx(mem_ctx);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (test_ctx == NULL) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "create_ev_test_ctx() failed\n");
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina goto fail;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina ret = mock_confdb(test_ctx, tests_path, cdb_file, &test_ctx->confdb);
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina if (ret != EOK) {
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to initialize confdb [%d]: %s\n",
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina ret, sss_strerror(ret));
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek goto fail;
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina /* create confdb objects for the domains */
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina for (i = 0; domains[i] != NULL; i++) {
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina ret = mock_confdb_domain(test_ctx, test_ctx->confdb, tests_path,
f1e3364a72eb75673d10cf8c97ba8f1d7a385405Petr Čech domains[i], id_provider, params != NULL ? params[i] : NULL,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina (cdb_path == NULL ? &cdb_path : NULL));
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina if (ret != EOK) {
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to initialize confdb domain "
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina "[%d]: %s\n", ret, sss_strerror(ret));
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina goto fail;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina /* initialize domain list and sysdb of the domains */
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina for (i = 0; domains[i] != NULL; i++) {
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina ret = mock_domain(test_ctx, test_ctx->confdb, tests_path, domains[i],
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina (domain == NULL ? &domain : NULL));
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina if (ret != EOK) {
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to add new domain [%d]: %s\n",
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina ret, sss_strerror(ret));
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina goto fail;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina }
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina /* the first domain we obtained is already head of the complete list */
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina test_ctx->dom = domain;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina /* set data from the first domain */
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina test_ctx->sysdb = test_ctx->dom->sysdb;
dbb990fb29e7178a3cce53474e48ce69ab3a97a0Pavel Březina test_ctx->nctx = test_ctx->dom->names;
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina test_ctx->conf_dom_path = cdb_path;
e4b4b669e0c1ef5ec3be04768edf2565a7bac5a1Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek return test_ctx;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozekfail:
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek talloc_free(test_ctx);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek return NULL;
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek}
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březinastruct sss_test_ctx *
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březinacreate_dom_test_ctx(TALLOC_CTX *mem_ctx,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *tests_path,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *confdb_path,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *domain_name,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *id_provider,
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina struct sss_test_conf_param *params)
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina{
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina const char *domains[] = {domain_name, NULL};
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina return create_multidom_test_ctx(mem_ctx, tests_path, confdb_path, domains,
f1e3364a72eb75673d10cf8c97ba8f1d7a385405Petr Čech id_provider, &params);
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina}
acc1c0c07fc76bc05d91c0bd2f172cd638ff3546Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březinavoid test_multidom_suite_cleanup(const char *tests_path,
629a188ec71155911301fddc36e360831045d2c6Pavel Březina const char *cdb_file,
629a188ec71155911301fddc36e360831045d2c6Pavel Březina const char **domains)
629a188ec71155911301fddc36e360831045d2c6Pavel Březina{
629a188ec71155911301fddc36e360831045d2c6Pavel Březina TALLOC_CTX *tmp_ctx = NULL;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina char *cdb_path = NULL;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina char *sysdb_path = NULL;
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek char *sysdb_ts_path = NULL;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina errno_t ret;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina int i;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březina tmp_ctx = talloc_new(NULL);
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (tmp_ctx == NULL) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
629a188ec71155911301fddc36e360831045d2c6Pavel Březina return;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (cdb_file != NULL) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina cdb_path = talloc_asprintf(tmp_ctx, "%s/%s", tests_path, cdb_file);
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (cdb_path == NULL) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not contruct cdb path\n");
629a188ec71155911301fddc36e360831045d2c6Pavel Březina goto done;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březina errno = 0;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret = unlink(cdb_path);
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (ret != 0 && errno != ENOENT) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret = errno;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not delete the test config "
629a188ec71155911301fddc36e360831045d2c6Pavel Březina "ldb file [%d]: (%s)\n", ret, sss_strerror(ret));
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (domains != NULL) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina for (i = 0; domains[i] != NULL; i++) {
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina if (strcmp(domains[i], LOCAL_SYSDB_FILE) == 0) {
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina /* local domain */
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek ret = sysdb_get_db_file(tmp_ctx, "local", domains[i], tests_path,
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek &sysdb_path, &sysdb_ts_path);
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek if (ret != EOK) {
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek goto done;
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek }
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina } else {
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek /* The mocked database doesn't really care about its provider type, just
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek * distinguishes between a local and non-local databases
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek */
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek ret = sysdb_get_db_file(tmp_ctx, "fake_nonlocal", domains[i], tests_path,
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek &sysdb_path, &sysdb_ts_path);
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek if (ret != EOK) {
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek goto done;
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek }
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (sysdb_path == NULL) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not construct sysdb path\n");
629a188ec71155911301fddc36e360831045d2c6Pavel Březina goto done;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březina errno = 0;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret = unlink(sysdb_path);
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (ret != 0 && errno != ENOENT) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret = errno;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not delete the test domain "
629a188ec71155911301fddc36e360831045d2c6Pavel Březina "ldb file [%d]: (%s)\n", ret, sss_strerror(ret));
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek if (sysdb_ts_path) {
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek errno = 0;
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek ret = unlink(sysdb_ts_path);
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek if (ret != 0 && errno != ENOENT) {
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek ret = errno;
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, "Could not delete the test domain "
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek "ldb timestamp file [%d]: (%s)\n", ret, sss_strerror(ret));
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek }
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek }
f983b400bf4f6fb14a2174d6f58071e06e9ec832Jakub Hrozek
629a188ec71155911301fddc36e360831045d2c6Pavel Březina talloc_zfree(sysdb_path);
dd285415d7a8d8376207960cfa3e977524c3b98cJakub Hrozek
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březina errno = 0;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret = rmdir(tests_path);
629a188ec71155911301fddc36e360831045d2c6Pavel Březina if (ret != 0 && errno != ENOENT) {
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret = errno;
629a188ec71155911301fddc36e360831045d2c6Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Could not delete the test dir (%d) (%s)\n",
629a188ec71155911301fddc36e360831045d2c6Pavel Březina ret, sss_strerror(ret));
629a188ec71155911301fddc36e360831045d2c6Pavel Březina }
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
629a188ec71155911301fddc36e360831045d2c6Pavel Březinadone:
629a188ec71155911301fddc36e360831045d2c6Pavel Březina talloc_free(tmp_ctx);
629a188ec71155911301fddc36e360831045d2c6Pavel Březina}
629a188ec71155911301fddc36e360831045d2c6Pavel Březina
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozekvoid test_dom_suite_cleanup(const char *tests_path,
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina const char *cdb_file,
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina const char *domain)
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek{
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina const char *domains[] = {domain, NULL};
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek
cb4742876508a08ba90c82466c9dba708e4bf999Pavel Březina test_multidom_suite_cleanup(tests_path, cdb_file, domains);
da33a9d7742d45975bd97d33dd8ad04a843e7656Jakub Hrozek}
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozekstruct sss_domain_info *named_domain(TALLOC_CTX *mem_ctx,
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek const char *name,
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek struct sss_domain_info *parent)
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek{
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek struct sss_domain_info *dom = NULL;
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek dom = talloc_zero(mem_ctx, struct sss_domain_info);
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek if (dom == NULL) {
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek return NULL;
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek }
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek dom->name = talloc_strdup(dom, name);
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek if (dom->name == NULL) {
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek talloc_free(dom);
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek return NULL;
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek }
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek dom->parent = parent;
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek return dom;
cffe3defa3cb5011efc92a7773fe113a1e69774fJakub Hrozek}