summaryrefslogtreecommitdiff
path: root/src/tests/pac_responder-tests.c
blob: 81cee3c1f959dadf2ad9f0c9075e547c6319ce33 (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
/*
    SSSD - Test for PAC reponder functions

    Authors:
        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2012 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 <check.h>

#include <stdbool.h>
#include <util/data_blob.h>
#include <gen_ndr/security.h>

#include "tests/common.h"
#include "responder/pac/pacsrv.h"

struct dom_sid test_smb_sid = {1, 5, {0, 0, 0, 0, 0, 5},
                               {21, 2127521184, 1604012920, 1887927527, 1123,
                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
const uint32_t test_id = 1200123;

struct dom_sid test_smb_sid_2nd = {1, 5, {0, 0, 0, 0, 0, 5},
                               {21, 2127521184, 1604012920, 1887927527, 201456,
                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
const uint32_t test_id_2nd = 1200456;

struct local_mapping_ranges test_map = {{1200000, 1399999},
                                        {1000, 200999},
                                        {201000, 400999}};


START_TEST(pac_test_local_sid_to_id)
{
    int ret;
    uint32_t id;

    ret = local_sid_to_id(&test_map, &test_smb_sid, &id);
    fail_unless(ret == EOK,
                "Failed to convert local sid to id.");
    fail_unless(id == test_id, "Wrong id returne, expected [%d], got [%d].",
                               test_id, id);
}
END_TEST

START_TEST(pac_test_seondary_local_sid_to_id)
{
    int ret;
    uint32_t id;

    ret = local_sid_to_id(&test_map, &test_smb_sid_2nd, &id);
    fail_unless(ret == EOK,
                "Failed to convert local sid to id.");
    fail_unless(id == test_id_2nd, "Wrong id returne, expected [%d], got [%d].",
                               test_id_2nd, id);
}
END_TEST

START_TEST(pac_test_get_gids_to_add_and_remove)
{
    TALLOC_CTX *mem_ctx;
    int ret;
    size_t c;
    size_t add_gid_count = 0;
    struct pac_grp *add_gids = NULL;
    size_t del_gid_count = 0;
    struct grp_info **del_gids = NULL;

    struct pac_grp pac_grp_2 = {2, NULL};
    struct pac_grp pac_grp_3 = {3, NULL};

    struct pac_grp gid_list_2[] = {pac_grp_2};
    struct pac_grp gid_list_3[] = {pac_grp_3};
    struct pac_grp gid_list_23[] = {pac_grp_2, pac_grp_3};

    struct grp_info grp_info_1 = {1, NULL, NULL};
    struct grp_info grp_info_2 = {2, NULL, NULL};
    struct grp_info  grp_list_1[] = {grp_info_1};
    struct grp_info  grp_list_12[] = {grp_info_1, grp_info_2};

    struct a_and_r_data {
        size_t cur_gid_count;
        struct grp_info *cur_gids;
        size_t gid_count;
        struct pac_grp *gids;
        int exp_ret;
        size_t exp_add_gid_count;
        struct pac_grp *exp_add_gids;
        size_t exp_del_gid_count;
        struct grp_info *exp_del_gids;
    } a_and_r_data[] = {
            {1, grp_list_1, 1, gid_list_2, EOK, 1, gid_list_2, 1, grp_list_1},
            {1, grp_list_1, 0, NULL, EOK, 0, NULL, 1, grp_list_1},
            {0, NULL, 1, gid_list_2, EOK, 1, gid_list_2, 0, NULL},
            {2, grp_list_12, 1, gid_list_2, EOK,  0, NULL, 1, grp_list_1},
            {2, grp_list_12, 2, gid_list_23, EOK, 1, gid_list_3, 1, grp_list_1},
            {0, NULL, 0, NULL, 0, 0, NULL, 0, NULL}
    };

    mem_ctx = talloc_new(NULL);
    fail_unless(mem_ctx != NULL, "talloc_new failed.");

    ret = diff_gid_lists(mem_ctx, 0, NULL, 0, NULL,
                         &add_gid_count, &add_gids,
                         &del_gid_count, &del_gids);
    fail_unless(ret == EOK, "get_gids_to_add_and_remove failed with empty " \
                            "groups.");

    ret = diff_gid_lists(mem_ctx, 1, NULL, 0, NULL,
                         &add_gid_count, &add_gids,
                         &del_gid_count, &del_gids);
    fail_unless(ret == EINVAL, "get_gids_to_add_and_remove failed with " \
                               "invalid current groups.");

    ret = diff_gid_lists(mem_ctx, 0, NULL, 1, NULL,
                         &add_gid_count, &add_gids,
                         &del_gid_count, &del_gids);
    fail_unless(ret == EINVAL, "get_gids_to_add_and_remove failed with " \
                               "invalid new groups.");

    for (c = 0; a_and_r_data[c].cur_gids != NULL ||
                a_and_r_data[c].gids != NULL; c++) {
        ret = diff_gid_lists(mem_ctx,
                             a_and_r_data[c].cur_gid_count,
                             a_and_r_data[c].cur_gids,
                             a_and_r_data[c].gid_count,
                             a_and_r_data[c].gids,
                             &add_gid_count, &add_gids,
                             &del_gid_count, &del_gids);
        fail_unless(ret == a_and_r_data[c].exp_ret,
                    "Unexpected return value for test data #%d, " \
                    "expected [%d], got [%d]",
                    c, a_and_r_data[c].exp_ret, ret);
        fail_unless(add_gid_count ==  a_and_r_data[c].exp_add_gid_count,
                    "Unexpected numer of groups to add for test data #%d, " \
                    "expected [%d], got [%d]",
                    c, a_and_r_data[c].exp_add_gid_count, add_gid_count);
        fail_unless(del_gid_count ==  a_and_r_data[c].exp_del_gid_count,
                    "Unexpected numer of groups to delete for test data #%d, " \
                    "expected [%d], got [%d]",
                    c, a_and_r_data[c].exp_del_gid_count, del_gid_count);

        /* The lists might be returned in any order, to make tests simple we
         * only look at lists with 1 element. TODO: add code to compare lists
         * with more than 1 member. */
        if (add_gid_count == 1) {
            fail_unless(add_gids[0].gid ==  a_and_r_data[c].exp_add_gids[0].gid,
                        "Unexpected gid to add for test data #%d, " \
                        "expected [%d], got [%d]",
                        c, a_and_r_data[c].exp_add_gids[0].gid, add_gids[0].gid);
        }

        if (del_gid_count == 1) {
            fail_unless(del_gids[0]->gid == a_and_r_data[c].exp_del_gids[0].gid,
                        "Unexpected gid to delete for test data #%d, " \
                        "expected [%d], got [%d]",
                        c, a_and_r_data[c].exp_del_gids[0].gid,
                        del_gids[0]->gid);
        }
    }

    talloc_free(mem_ctx);
}
END_TEST

#define NUM_DOMAINS 10
START_TEST(pac_test_find_domain_by_id)
{
    struct sss_domain_info *dom;
    struct sss_domain_info **domains;
    size_t c;
    char *id;

    dom = find_domain_by_id(NULL, NULL);
    fail_unless(dom == NULL, "Domain returned without any input.");

    dom = find_domain_by_id(NULL, "id");
    fail_unless(dom == NULL, "Domain returned without domain list.");

    domains = talloc_zero_array(global_talloc_context, struct sss_domain_info *,
                                NUM_DOMAINS);
    for (c = 0; c < NUM_DOMAINS; c++) {
        domains[c] = talloc_zero(domains, struct sss_domain_info);
        fail_unless(domains[c] != NULL, "talloc_zero failed.");

        domains[c]->domain_id = talloc_asprintf(domains[c],
                                                "ID-of-domains-%zu", c);
        fail_unless(domains[c]->domain_id != NULL, "talloc_asprintf failed.");
        if (c > 0) {
            domains[c-1]->next = domains[c];
        }
    }

    dom = find_domain_by_id(domains[0], NULL);
    fail_unless(dom == NULL, "Domain returned without search domain.");

    dom = find_domain_by_id(domains[0], "DOES-NOT_EXISTS");
    fail_unless(dom == NULL, "Domain returned with non existing id.");

    for (c = 0; c < NUM_DOMAINS; c++) {
        id = talloc_asprintf(global_talloc_context, "ID-of-domains-%zu", c);
        fail_unless(id != NULL, "talloc_asprintf failed.\n");

        dom = find_domain_by_id(domains[0], id);
        fail_unless(dom == domains[c], "Wrong domain returned for id [%s].",
                                       id);

        talloc_free(id);
    }

    talloc_free(domains);
}
END_TEST

Suite *idmap_test_suite (void)
{
    Suite *s = suite_create ("PAC responder");

    TCase *tc_pac = tcase_create("PAC responder tests");
    tcase_add_checked_fixture(tc_pac,
                              leak_check_setup,
                              leak_check_teardown);

    tcase_add_test(tc_pac, pac_test_local_sid_to_id);
    tcase_add_test(tc_pac, pac_test_seondary_local_sid_to_id);
    tcase_add_test(tc_pac, pac_test_get_gids_to_add_and_remove);
    tcase_add_test(tc_pac, pac_test_find_domain_by_id);

    suite_add_tcase(s, tc_pac);

    return s;
}

int main(int argc, const char *argv[])
{
    int number_failed;

    tests_set_cwd();

    Suite *s = idmap_test_suite();
    SRunner *sr = srunner_create(s);

    /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
    srunner_run_all(sr, CK_ENV);
    number_failed = srunner_ntests_failed (sr);
    srunner_free (sr);

    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}