Line data Source code
1 : /*
2 : SSSD - Test for routine to check to access to responder sockets
3 :
4 : Authors:
5 : Sumit Bose <sbose@redhat.com>
6 :
7 : Copyright (C) 2012 Red Hat
8 :
9 : This program is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : This program is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with this program. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include <popt.h>
24 : #include <check.h>
25 : #include <string.h>
26 :
27 : #include "tests/common.h"
28 : #include "responder/common/responder.h"
29 :
30 0 : struct cli_protocol_version *register_cli_protocol_version(void)
31 : {
32 : static struct cli_protocol_version responder_test_cli_protocol_version[] = {
33 : {0, NULL, NULL}
34 : };
35 :
36 0 : return responder_test_cli_protocol_version;
37 : }
38 :
39 : struct s2a_data {
40 : const char *inp;
41 : int exp_ret;
42 : size_t exp_count;
43 : uid_t *exp_uids;
44 : };
45 :
46 : struct s2a_data s2a_data[] = {
47 : {"1,2,3", 0, 3, (uid_t []){1, 2, 3}},
48 : {"1,2,3, 4,5 , 6 , 7 ", 0, 7, (uid_t []){1, 2, 3, 4, 5, 6, 7}},
49 : {"1", 0, 1, (uid_t []){1}},
50 : {"1, +2,3", 0, 3, (uid_t []){1, 2, 3}},
51 : {"1, -2,3", ERANGE, 0, NULL},
52 : {"1, 2ab, 3, 4", EINVAL, 0, NULL},
53 : {"1,", EINVAL, 0, NULL},
54 : {"", EINVAL, 0, NULL},
55 : {"1, 2, 4294967295", 0, 3, (uid_t []){1, 2, 4294967295U}},
56 : {"1, 2, 4294967296", ERANGE, 0, NULL},
57 : {"1, 2, root, 4, 5", 0, 5, (uid_t []){1, 2, 0, 4, 5}},
58 : {NULL, EINVAL, 0, NULL},
59 : {NULL, -1, 0, NULL}
60 : };
61 :
62 1 : START_TEST(resp_str_to_array_test)
63 : {
64 : int ret;
65 : size_t uid_count;
66 1 : uid_t *uids = NULL;
67 : size_t c;
68 : size_t d;
69 :
70 13 : for (c = 0; s2a_data[c].exp_ret != -1; c++) {
71 12 : ret = csv_string_to_uid_array(global_talloc_context, s2a_data[c].inp,
72 : true, &uid_count, &uids);
73 12 : fail_unless(ret == s2a_data[c].exp_ret,
74 : "csv_string_to_uid_array failed [%d][%s].", ret,
75 : strerror(ret));
76 12 : if (ret == 0) {
77 6 : fail_unless(uid_count == s2a_data[c].exp_count,
78 : "Wrong number of values, expected [%d], got [%d].",
79 : s2a_data[c].exp_count, uid_count);
80 28 : for (d = 0; d < s2a_data[c].exp_count; d++) {
81 22 : fail_unless(uids[d] == s2a_data[c].exp_uids[d],
82 : "Wrong value, expected [%d], got [%d].\n",
83 : s2a_data[c].exp_uids[d], uids[d]);
84 : }
85 : }
86 :
87 12 : talloc_free(uids);
88 12 : uids = NULL;
89 : }
90 :
91 : }
92 1 : END_TEST
93 :
94 : struct uid_check_data {
95 : uid_t uid;
96 : size_t allowed_uids_count;
97 : uid_t *allowed_uids;
98 : int exp_ret;
99 : };
100 :
101 : struct uid_check_data uid_check_data[] = {
102 : {1, 3, (uid_t []){1, 2, 3}, 0},
103 : {2, 3, (uid_t []){1, 2, 3}, 0},
104 : {3, 3, (uid_t []){1, 2, 3}, 0},
105 : {4, 3, (uid_t []){1, 2, 3}, EACCES},
106 : {4, 0, NULL, EINVAL},
107 : {0, 0, NULL, -1}
108 : };
109 :
110 1 : START_TEST(check_allowed_uids_test)
111 : {
112 : int ret;
113 : size_t c;
114 :
115 6 : for (c = 0; uid_check_data[c].exp_ret != -1; c++) {
116 5 : ret = check_allowed_uids(uid_check_data[c].uid,
117 : uid_check_data[c].allowed_uids_count,
118 : uid_check_data[c].allowed_uids);
119 5 : fail_unless(ret == uid_check_data[c].exp_ret,
120 : "check_allowed_uids failed [%d][%s].", ret, strerror(ret));
121 : }
122 : }
123 1 : END_TEST
124 :
125 1 : Suite *responder_test_suite(void)
126 : {
127 1 : Suite *s = suite_create ("Responder socket access");
128 :
129 1 : TCase *tc_utils = tcase_create("Utility test");
130 :
131 1 : tcase_add_test(tc_utils, resp_str_to_array_test);
132 1 : tcase_add_test(tc_utils, check_allowed_uids_test);
133 :
134 1 : suite_add_tcase(s, tc_utils);
135 :
136 1 : return s;
137 : }
138 :
139 1 : int main(int argc, const char *argv[])
140 : {
141 : int opt;
142 : int number_failed;
143 : poptContext pc;
144 :
145 6 : struct poptOption long_options[] = {
146 : POPT_AUTOHELP
147 5 : SSSD_MAIN_OPTS
148 : POPT_TABLEEND
149 : };
150 :
151 : /* Set debug level to invalid value so we can deside if -d 0 was used. */
152 1 : debug_level = SSSDBG_INVALID;
153 :
154 1 : pc = poptGetContext(argv[0], argc, argv, long_options, 0);
155 1 : while((opt = poptGetNextOpt(pc)) != -1) {
156 : switch(opt) {
157 : default:
158 0 : fprintf(stderr, "\nInvalid option %s: %s\n\n",
159 : poptBadOption(pc, 0), poptStrerror(opt));
160 0 : poptPrintUsage(pc, stderr, 0);
161 0 : return 1;
162 : }
163 : }
164 1 : poptFreeContext(pc);
165 :
166 1 : DEBUG_CLI_INIT(debug_level);
167 1 : tests_set_cwd();
168 :
169 1 : Suite *s = responder_test_suite();
170 1 : SRunner *sr = srunner_create(s);
171 :
172 : /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
173 1 : srunner_run_all(sr, CK_ENV);
174 1 : number_failed = srunner_ntests_failed (sr);
175 1 : srunner_free (sr);
176 :
177 1 : return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
178 : }
|