summaryrefslogtreecommitdiffstats
path: root/src/tests/common_dom.c
blob: bc69c5a093e17fc56e42ccedd998ab6434d85003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
    Authors:
        Jakub Hrozek <jhrozek@redhat.com>

    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 <http://www.gnu.org/licenses/>.
*/

#include <talloc.h>
#include <errno.h>

#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 *domain_name,
                    const char *id_provider,
                    struct sss_test_conf_param *params)
{
    struct sss_test_ctx *test_ctx;
    size_t i;
    const char *val[2];
    val[1] = NULL;
    errno_t ret;
    char *dompath;

    test_ctx = create_ev_test_ctx(mem_ctx);
    if (test_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero failed\n");
        goto fail;
    }

    test_ctx->confdb_path = talloc_asprintf(test_ctx, "%s/%s",
                                            tests_path, confdb_path);
    if (test_ctx->confdb_path == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed\n");
        goto fail;
    }

    test_ctx->conf_dom_path = talloc_asprintf(test_ctx,
                                              CONFDB_DOMAIN_PATH_TMPL,
                                              domain_name);
    if (test_ctx->conf_dom_path == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf failed\n");
        goto fail;
    }

    /* Connect to the conf db */
    ret = confdb_init(test_ctx, &test_ctx->confdb, test_ctx->confdb_path);
    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].value;
            ret = confdb_add_param(test_ctx->confdb, true,
                                   dompath, params[i].key,
                                   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;

    /* Init with an AD-style regex to be able to test flat name */
    ret = sss_names_init_from_args(test_ctx,
                                   "(((?P<domain>[^\\\\]+)\\\\(?P<name>.+$))|" \
                                   "((?P<name>[^@]+)@(?P<domain>.+$))|" \
                                   "(^(?P<name>[^@\\\\]+)$))",
                                   "%1$s@%2$s", &test_ctx->nctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE, "cannot create names context\n");
        goto fail;
    }
    test_ctx->dom->names = test_ctx->nctx;

    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;
    TALLOC_CTX *tmp_ctx;

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new failed\n");
        return;
    }

    if (confdb_path != NULL) {
        conf_db = talloc_asprintf(tmp_ctx, "%s/%s", tests_path, confdb_path);
        if (!conf_db) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                "Could not construct conf_db path\n");
            goto done;
        }

        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));
        }
    }

    if (sysdb_path != NULL) {
        sys_db = talloc_asprintf(tmp_ctx, "%s/%s", tests_path, sysdb_path);
        if (!sys_db) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                "Could not construct sys_db path\n");
            goto done;
        }

        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));
        }
    }

    errno = 0;
    ret = rmdir(tests_path);
    if (ret != 0 && errno != ENOENT) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Could not delete the test dir (%d) (%s)\n",
               errno, strerror(errno));
    }

done:
    talloc_free(tmp_ctx);
}