summaryrefslogtreecommitdiffstats
path: root/src/tests/responder_socket_access-tests.c
blob: a01a5d12c24aae356a90fd537878b0a0953d89de (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
/*
    SSSD - Test for routine to check to access to responder sockets

    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 <popt.h>
#include <check.h>
#include <string.h>

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

struct cli_protocol_version *register_cli_protocol_version(void)
{
    static struct cli_protocol_version responder_test_cli_protocol_version[] = {
        {0, NULL, NULL}
    };

    return responder_test_cli_protocol_version;
}

struct s2a_data {
    const char *inp;
    int exp_ret;
    size_t exp_count;
    uid_t *exp_uids;
};

struct s2a_data s2a_data[] = {
    {"1,2,3", 0, 3, (uid_t []){1, 2, 3}},
    {"1,2,3, 4,5  , 6 , 7  ", 0, 7, (uid_t []){1, 2, 3, 4, 5, 6, 7}},
    {"1", 0, 1, (uid_t []){1}},
    {"1, +2,3", 0, 3, (uid_t []){1, 2, 3}},
    {"1, -2,3", ERANGE, 0, NULL},
    {"1, 2ab, 3, 4", EINVAL, 0, NULL},
    {"1,", EINVAL, 0, NULL},
    {"", EINVAL, 0, NULL},
    {"1, 2, 4294967295", 0, 3, (uid_t []){1, 2, 4294967295U}},
    {"1, 2, 4294967296", ERANGE, 0, NULL},
    {"1, 2, root, 4, 5", 0, 5, (uid_t []){1, 2, 0, 4, 5}},
    {NULL, EINVAL, 0, NULL},
    {NULL, -1, 0, NULL}
};

START_TEST(resp_str_to_array_test)
{
    int ret;
    size_t uid_count;
    uid_t *uids = NULL;
    size_t c;
    size_t d;

    for (c = 0; s2a_data[c].exp_ret != -1; c++) {
        ret = csv_string_to_uid_array(global_talloc_context, s2a_data[c].inp,
                                      true, &uid_count, &uids);
        fail_unless(ret == s2a_data[c].exp_ret,
                    "csv_string_to_uid_array failed [%d][%s].", ret,
                                                                strerror(ret));
        if (ret == 0) {
            fail_unless(uid_count == s2a_data[c].exp_count,
                        "Wrong number of values, expected [%d], got [%d].",
                        s2a_data[c].exp_count, uid_count);
            for (d = 0; d < s2a_data[c].exp_count; d++) {
                fail_unless(uids[d] == s2a_data[c].exp_uids[d],
                            "Wrong value, expected [%d], got [%d].\n",
                            s2a_data[c].exp_uids[d], uids[d]);
            }
        }

        talloc_free(uids);
        uids = NULL;
    }

}
END_TEST

struct uid_check_data {
    uid_t uid;
    size_t allowed_uids_count;
    uid_t *allowed_uids;
    int exp_ret;
};

struct uid_check_data uid_check_data[] = {
    {1, 3, (uid_t []){1, 2, 3}, 0},
    {2, 3, (uid_t []){1, 2, 3}, 0},
    {3, 3, (uid_t []){1, 2, 3}, 0},
    {4, 3, (uid_t []){1, 2, 3}, EACCES},
    {4, 0, NULL, EINVAL},
    {0, 0, NULL, -1}
};

START_TEST(check_allowed_uids_test)
{
    int ret;
    size_t c;

    for (c = 0; uid_check_data[c].exp_ret == -1; c++) {
        ret = check_allowed_uids(uid_check_data[c].uid,
                                 uid_check_data[c].allowed_uids_count,
                                 uid_check_data[c].allowed_uids);
        fail_unless(ret == uid_check_data[c].exp_ret,
                    "check_allowed_uids failed [%d][%s].", ret, strerror(ret));
    }
}
END_TEST

Suite *responder_test_suite(void)
{
    Suite *s = suite_create ("Responder socket access");

    TCase *tc_utils = tcase_create("Utility test");

    tcase_add_test(tc_utils, resp_str_to_array_test);
    tcase_add_test(tc_utils, check_allowed_uids_test);

    suite_add_tcase(s, tc_utils);

    return s;
}

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

    struct poptOption long_options[] = {
        POPT_AUTOHELP
        SSSD_MAIN_OPTS
        POPT_TABLEEND
    };

    /* 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_INIT(debug_level);
    tests_set_cwd();

    Suite *s = responder_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;
}