summaryrefslogtreecommitdiffstats
path: root/src/tests/cmocka/test_sysdb_views.c
blob: 7238467c3c9bf91fd0d48204dd7217d4d273e965 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
    SSSD

    sysdb_views - Tests for view and override related sysdb calls

    Authors:
        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2014 Red Hat

    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 <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <popt.h>

#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;
    struct tevent_context *ev;
    struct sss_domain_info *domain;
};

static int _setup_sysdb_tests(struct sysdb_test_ctx **ctx, bool enumerate)
{
    struct sysdb_test_ctx *test_ctx;
    char *conf_db;
    int ret;

    const char *val[2];
    val[1] = NULL;

    /* Create tests directory if it doesn't exist */
    /* (relative to current dir) */
    ret = mkdir(TESTS_PATH, 0775);
    assert_true(ret == 0 || errno == EEXIST);

    test_ctx = talloc_zero(global_talloc_context, struct sysdb_test_ctx);
    assert_non_null(test_ctx);

    /* Create an event context
     * It will not be used except in confdb_init and sysdb_init
     */
    test_ctx->ev = tevent_context_init(test_ctx);
    assert_non_null(test_ctx->ev);

    conf_db = talloc_asprintf(test_ctx, "%s/%s", TESTS_PATH, TEST_CONF_FILE);
    assert_non_null(conf_db);
    DEBUG(SSSDBG_MINOR_FAILURE, "CONFDB: %s\n", conf_db);

    /* Connect to the conf db */
    ret = confdb_init(test_ctx, &test_ctx->confdb, conf_db);
    assert_int_equal(ret, EOK);

    val[0] = "LOCAL";
    ret = confdb_add_param(test_ctx->confdb, true,
                           "config/sssd", "domains", val);
    assert_int_equal(ret, EOK);

    val[0] = "local";
    ret = confdb_add_param(test_ctx->confdb, true,
                           "config/domain/LOCAL", "id_provider", val);
    assert_int_equal(ret, EOK);

    val[0] = enumerate ? "TRUE" : "FALSE";
    ret = confdb_add_param(test_ctx->confdb, true,
                           "config/domain/LOCAL", "enumerate", val);
    assert_int_equal(ret, EOK);

    val[0] = "TRUE";
    ret = confdb_add_param(test_ctx->confdb, true,
                           "config/domain/LOCAL", "cache_credentials", val);
    assert_int_equal(ret, EOK);

    ret = sssd_domain_init(test_ctx, test_ctx->confdb, "local",
                           TESTS_PATH, &test_ctx->domain);
    assert_int_equal(ret, EOK);

    test_ctx->sysdb = test_ctx->domain->sysdb;

    *ctx = test_ctx;
    return EOK;
}

#define setup_sysdb_tests(ctx) _setup_sysdb_tests((ctx), false)

static void test_sysdb_setup(void **state)
{
    int ret;
    struct sysdb_test_ctx *test_ctx;

    assert_true(leak_check_setup());

    ret = setup_sysdb_tests(&test_ctx);
    assert_int_equal(ret, EOK);

    *state = (void *) test_ctx;
}

static void test_sysdb_teardown(void **state)
{
    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
                                                         struct sysdb_test_ctx);

    talloc_free(test_ctx);
    assert_true(leak_check_teardown());
}

void test_sysdb_add_overrides_to_object(void **state)
{
    int ret;
    struct ldb_message *orig;
    struct ldb_message *override;
    struct ldb_message_element *el;
    char *tmp_str;
    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
                                                         struct sysdb_test_ctx);

    orig = ldb_msg_new(test_ctx);
    assert_non_null(orig);

    tmp_str = talloc_strdup(orig,  "ORIGNAME");
    ret = ldb_msg_add_string(orig, SYSDB_NAME, tmp_str);
    assert_int_equal(ret, EOK);

    tmp_str = talloc_strdup(orig,  "ORIGGECOS");
    ret = ldb_msg_add_string(orig, SYSDB_GECOS, tmp_str);
    assert_int_equal(ret, EOK);

    override = ldb_msg_new(test_ctx);
    assert_non_null(override);

    tmp_str = talloc_strdup(override, "OVERRIDENAME");
    ret = ldb_msg_add_string(override, SYSDB_NAME, tmp_str);
    assert_int_equal(ret, EOK);

    tmp_str = talloc_strdup(override, "OVERRIDEGECOS");
    ret = ldb_msg_add_string(override, SYSDB_GECOS, tmp_str);
    assert_int_equal(ret, EOK);

    tmp_str = talloc_strdup(override, "OVERRIDEKEY1");
    ret = ldb_msg_add_string(override, SYSDB_SSH_PUBKEY, tmp_str);
    assert_int_equal(ret, EOK);

    tmp_str = talloc_strdup(override, "OVERRIDEKEY2");
    ret = ldb_msg_add_string(override, SYSDB_SSH_PUBKEY, tmp_str);
    assert_int_equal(ret, EOK);


    ret = sysdb_add_overrides_to_object(test_ctx->domain, orig, override, NULL);
    assert_int_equal(ret, EOK);

    assert_string_equal(ldb_msg_find_attr_as_string(orig, SYSDB_NAME, NULL),
                        "ORIGNAME");
    assert_string_equal(ldb_msg_find_attr_as_string(orig, SYSDB_GECOS, NULL),
                        "ORIGGECOS");
    assert_string_equal(ldb_msg_find_attr_as_string(orig,
                                                    OVERRIDE_PREFIX SYSDB_NAME,
                                                    NULL),
                        "OVERRIDENAME");
    assert_string_equal(ldb_msg_find_attr_as_string(orig,
                                                    OVERRIDE_PREFIX SYSDB_GECOS,
                                                    NULL),
                        "OVERRIDEGECOS");

    el = ldb_msg_find_element(orig, OVERRIDE_PREFIX SYSDB_SSH_PUBKEY);
    assert_non_null(el);
    assert_int_equal(el->num_values, 2);
    assert_int_equal(ldb_val_string_cmp(&el->values[0], "OVERRIDEKEY1"), 0);
    assert_int_equal(ldb_val_string_cmp(&el->values[1], "OVERRIDEKEY2"), 0);
}

void test_split_ipa_anchor(void **state)
{
    int ret;
    char *dom;
    char *uuid;
    struct sysdb_test_ctx *test_ctx = talloc_get_type_abort(*state,
                                                         struct sysdb_test_ctx);

    ret = split_ipa_anchor(test_ctx, NULL, &dom, &uuid);
    assert_int_equal(ret, EINVAL);

    ret = split_ipa_anchor(test_ctx, "fwfkwjfkw", &dom, &uuid);
    assert_int_equal(ret, ENOMSG);

    ret = split_ipa_anchor(test_ctx, ":IPA:", &dom, &uuid);
    assert_int_equal(ret, EINVAL);

    ret = split_ipa_anchor(test_ctx, ":IPA:abc", &dom, &uuid);
    assert_int_equal(ret, EINVAL);

    ret = split_ipa_anchor(test_ctx, ":IPA:abc:", &dom, &uuid);
    assert_int_equal(ret, EINVAL);

    ret = split_ipa_anchor(test_ctx, ":IPA:abc:def", &dom, &uuid);
    assert_int_equal(ret, EOK);
    assert_string_equal(dom, "abc");
    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);

}

void test_sysdb_invalidate_overrides(void **state)
{
    int ret;
    struct ldb_message *msg;
    struct sysdb_attrs *attrs;
    struct ldb_dn *views_dn;
    const char *user_attrs[] = { SYSDB_NAME,
                                 SYSDB_CACHE_EXPIRE,
                                 SYSDB_OVERRIDE_DN,
                                 NULL};

    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,
                           10,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_delete_view_tree(test_ctx->domain->sysdb, TEST_VIEW_NAME);
    assert_int_equal(ret, EOK);

    ret = sysdb_search_user_by_name(test_ctx, test_ctx->domain, TEST_USER_NAME,
                                    user_attrs, &msg);
    assert_int_equal(ret, EOK);
    assert_non_null(msg);
    assert_true(ldb_msg_find_attr_as_uint64(msg, SYSDB_CACHE_EXPIRE, 0) > 1);
    assert_non_null(ldb_msg_find_attr_as_string(msg, SYSDB_OVERRIDE_DN, NULL));

    ret = sysdb_invalidate_overrides(test_ctx->domain->sysdb);
    assert_int_equal(ret, EOK);

    ret = sysdb_search_user_by_name(test_ctx, test_ctx->domain, TEST_USER_NAME,
                                    user_attrs, &msg);
    assert_int_equal(ret, EOK);
    assert_non_null(msg);
    assert_int_equal(ldb_msg_find_attr_as_uint64(msg, SYSDB_CACHE_EXPIRE, 0),
                     1);
    assert_null(ldb_msg_find_attr_as_string(msg, SYSDB_OVERRIDE_DN, NULL));
}

int main(int argc, const char *argv[])
{
    int rv;
    int no_cleanup = 0;
    poptContext pc;
    int opt;
    struct poptOption long_options[] = {
        POPT_AUTOHELP
        SSSD_DEBUG_OPTS
        {"no-cleanup", 'n', POPT_ARG_NONE, &no_cleanup, 0,
         _("Do not delete the test database after a test run"), NULL },
        POPT_TABLEEND
    };

    const UnitTest tests[] = {
        unit_test_setup_teardown(test_sysdb_add_overrides_to_object,
                                 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),
        unit_test_setup_teardown(test_sysdb_invalidate_overrides,
                                 test_sysdb_setup, test_sysdb_teardown),
    };

    /* Set debug level to invalid value so we can deside if -d 0 was used. */
    debug_level = SSSDBG_INVALID;

    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    while((opt = poptGetNextOpt(pc)) != -1) {
        switch(opt) {
        default:
            fprintf(stderr, "\nInvalid option %s: %s\n\n",
                    poptBadOption(pc, 0), poptStrerror(opt));
            poptPrintUsage(pc, stderr, 0);
            return 1;
        }
    }
    poptFreeContext(pc);

    DEBUG_CLI_INIT(debug_level);

    tests_set_cwd();
    test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, LOCAL_SYSDB_FILE);
    test_dom_suite_setup(TESTS_PATH);
    rv = run_tests(tests);

    if (rv == 0 && no_cleanup == 0) {
        test_dom_suite_cleanup(TESTS_PATH, TEST_CONF_FILE, LOCAL_SYSDB_FILE);
    }
    return rv;
}