summaryrefslogtreecommitdiffstats
path: root/src/tests/cmocka/test_sdap_async.c
blob: c232e938f27fe9bece5536d24a7bdf1cb1130e48 (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
/*
    Copyright (C) 2015 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 <talloc.h>
#include <tevent.h>
#include <errno.h>
#include <popt.h>

#include "tests/cmocka/common_mock.h"
#include "tests/cmocka/common_mock_sdap.h"
#include "providers/ldap/sdap_async.h"
#include "providers/ldap/ldap_opts.h"
#include "providers/ad/ad_opts.h"
#include "util/crypto/sss_crypto.h"

/* Mock parsing search base without overlinking the test */
errno_t sdap_parse_search_base(TALLOC_CTX *mem_ctx,
                               struct dp_option *opts, int class,
                               struct sdap_search_base ***_search_bases)
{
    return EOK;
}

int ldap_get_options(TALLOC_CTX *memctx,
                     struct sss_domain_info *dom,
                     struct confdb_ctx *cdb,
                     const char *conf_path,
                     struct sdap_options **_opts)
{
    return 0;
}

struct posix_test_state {
    struct sss_test_ctx *tctx;

    struct sdap_options *opts;
    struct sdap_handle *sh;
    struct sdap_search_base **search_bases;
};

static int posix_test_setup(void **state)
{

    struct posix_test_state *test_state;
    errno_t ret;

    assert_true(leak_check_setup());

    test_state = talloc_zero(global_talloc_context,
                           struct posix_test_state);
    assert_non_null(test_state);

    test_state->tctx = create_ev_test_ctx(test_state);
    assert_non_null(test_state->tctx);

    test_state->opts = mock_sdap_options(test_state,
                                         ad_2008r2_user_map,
                                         ad_2008r2_group_map,
                                         default_basic_opts);
    assert_non_null(test_state->opts);

    test_state->search_bases = talloc_array(test_state,
                                            struct sdap_search_base *, 2);
    assert_non_null(test_state->search_bases);
    test_state->search_bases[1] = NULL;

    ret = sdap_create_search_base(test_state,
                                  "cn=example,cn=com",
                                  LDAP_SCOPE_SUBTREE,
                                  NULL,
                                  &test_state->search_bases[0]);
    assert_int_equal(ret, EOK);
    test_state->sh = mock_sdap_handle(test_state);
    assert_non_null(test_state->sh);

    *state = test_state;
    return 0;
}

static int posix_test_teardown(void **state)
{
    struct posix_test_state *test_state = talloc_get_type_abort(*state,
                                               struct posix_test_state);
    assert_true(check_leaks_pop(test_state) == true);
    talloc_free(test_state);
    assert_true(leak_check_teardown());
    return 0;
}

static void sdap_posix_check_done(struct tevent_req *req);

static void test_posix_attrs(void **state)
{
    struct posix_test_state *test_ctx =
        talloc_get_type(*state, struct posix_test_state);
    struct tevent_req *req;
    errno_t ret;

    req = sdap_posix_check_send(test_ctx,
                                test_ctx->tctx->ev,
                                test_ctx->opts,
                                test_ctx->sh,
                                test_ctx->search_bases,
                                5);
    assert_non_null(req);

    tevent_req_set_callback(req, sdap_posix_check_done, test_ctx);

    ret = test_ev_loop(test_ctx->tctx);
    assert_int_equal(ret, ERR_OK);
}

static void sdap_posix_check_done(struct tevent_req *req)
{
    struct posix_test_state *test_ctx = \
        tevent_req_callback_data(req, struct posix_test_state);
    errno_t ret;

    ret = sdap_posix_check_recv(req, NULL);
    talloc_zfree(req);
    assert_int_equal(ret, ERR_OK);

    test_ctx->tctx->done = true;
}

int main(int argc, const char *argv[])
{
    poptContext pc;
    int opt;
    struct poptOption long_options[] = {
        POPT_AUTOHELP
        SSSD_DEBUG_OPTS
        POPT_TABLEEND
    };

    const struct CMUnitTest tests[] = {
        cmocka_unit_test_setup_teardown(test_posix_attrs,
                                        posix_test_setup,
                                        posix_test_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);

    /* Even though normally the tests should clean up after themselves
     * they might not after a failed run. Remove the old db to be sure */
    tests_set_cwd();

    return cmocka_run_group_tests(tests, NULL, NULL);
}