1 /*
2 * Authors: Jan Zarsky <jzarsky@redhat.com>
3 *
4 * Copyright (C) 2019 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "utilities.h"
22 #include "test_handle.h"
23
24 static void test_handle_create(void);
25 static void test_connect(void);
26 static void test_disconnect(void);
27 static void test_transaction(void);
28 static void test_commit(void);
29 static void test_is_connected(void);
30 static void test_access_check(void);
31 static void test_is_managed(void);
32 static void test_mls_enabled(void);
33 static void test_msg_set_callback(void);
34 static void test_root(void);
35 static void test_select_store(void);
36
37 extern semanage_handle_t *sh;
38
handle_test_init(void)39 int handle_test_init(void)
40 {
41 if (create_test_store() < 0) {
42 fprintf(stderr, "Could not create test store\n");
43 return 1;
44 }
45
46 if (write_test_policy_from_file("test_handle.policy") < 0) {
47 fprintf(stderr, "Could not write test policy\n");
48 return 1;
49 }
50
51 return 0;
52 }
53
handle_test_cleanup(void)54 int handle_test_cleanup(void)
55 {
56 if (destroy_test_store() < 0) {
57 fprintf(stderr, "Could not destroy test store\n");
58 return 1;
59 }
60
61 return 0;
62 }
63
handle_add_tests(CU_pSuite suite)64 int handle_add_tests(CU_pSuite suite)
65 {
66 CU_add_test(suite, "test_handle_create", test_handle_create);
67 CU_add_test(suite, "test_connect", test_connect);
68 CU_add_test(suite, "test_disconnect", test_disconnect);
69 CU_add_test(suite, "test_transaction", test_transaction);
70 CU_add_test(suite, "test_commit", test_commit);
71 CU_add_test(suite, "test_is_connected", test_is_connected);
72 CU_add_test(suite, "test_access_check", test_access_check);
73 CU_add_test(suite, "test_is_managed", test_is_managed);
74 CU_add_test(suite, "test_mls_enabled", test_mls_enabled);
75 CU_add_test(suite, "msg_set_callback", test_msg_set_callback);
76 CU_add_test(suite, "test_root", test_root);
77 CU_add_test(suite, "test_select_store", test_select_store);
78
79 return 0;
80 }
81
82 /* Function semanage_handle_create */
test_handle_create(void)83 static void test_handle_create(void)
84 {
85 sh = semanage_handle_create();
86 CU_ASSERT_PTR_NOT_NULL(sh);
87 semanage_handle_destroy(sh);
88 }
89
90 /* Function semanage_connect */
test_connect(void)91 static void test_connect(void)
92 {
93 /* test handle created */
94 setup_handle(SH_HANDLE);
95 CU_ASSERT(semanage_connect(sh) >= 0);
96 CU_ASSERT(semanage_disconnect(sh) >= 0);
97 cleanup_handle(SH_HANDLE);
98
99 /* test invalid store */
100 setup_handle_invalid_store(SH_HANDLE);
101 CU_ASSERT(semanage_connect(sh) < 0);
102 cleanup_handle(SH_HANDLE);
103
104 /* test normal use */
105 setup_handle(SH_HANDLE);
106 CU_ASSERT(semanage_connect(sh) >= 0);
107 CU_ASSERT(semanage_disconnect(sh) >= 0);
108 cleanup_handle(SH_HANDLE);
109 }
110
111 /* Function semanage_disconnect */
test_disconnect(void)112 static void test_disconnect(void)
113 {
114 setup_handle(SH_CONNECT);
115 CU_ASSERT(semanage_disconnect(sh) >= 0);
116 cleanup_handle(SH_HANDLE);
117 }
118
119 /* Function semanage_begin_transaction */
test_transaction(void)120 static void test_transaction(void)
121 {
122 /* test disconnected */
123 setup_handle(SH_CONNECT);
124 helper_disconnect();
125 CU_ASSERT(semanage_begin_transaction(sh) < 0);
126
127 cleanup_handle(SH_HANDLE);
128
129 /* test normal use */
130 setup_handle(SH_CONNECT);
131 CU_ASSERT(semanage_begin_transaction(sh) >= 0);
132 CU_ASSERT(semanage_commit(sh) >= 0);
133
134 cleanup_handle(SH_CONNECT);
135 }
136
137 /* Function semanage_commit */
test_commit(void)138 static void test_commit(void)
139 {
140 /* test without transaction */
141 setup_handle(SH_CONNECT);
142 CU_ASSERT(semanage_commit(sh) < 0);
143
144 /* test with transaction */
145 helper_begin_transaction();
146 CU_ASSERT(semanage_commit(sh) >= 0);
147
148 cleanup_handle(SH_CONNECT);
149 }
150
151 /* Function semanage_is_connected */
test_is_connected(void)152 static void test_is_connected(void)
153 {
154 /* test disconnected */
155 setup_handle(SH_HANDLE);
156 CU_ASSERT(semanage_is_connected(sh) == 0);
157
158 /* test connected */
159 helper_connect();
160 CU_ASSERT(semanage_is_connected(sh) == 1);
161
162 /* test in transaction */
163 helper_begin_transaction();
164 CU_ASSERT(semanage_is_connected(sh) == 1);
165
166 cleanup_handle(SH_TRANS);
167 }
168
169 /* Function semanage_access_check */
test_access_check(void)170 static void test_access_check(void)
171 {
172 int res = 0;
173
174 /* test with handle */
175 setup_handle(SH_HANDLE);
176 res = semanage_access_check(sh);
177 CU_ASSERT(res == 0 || res == SEMANAGE_CAN_READ
178 || res == SEMANAGE_CAN_WRITE);
179 cleanup_handle(SH_HANDLE);
180
181 /* test with invalid store */
182 setup_handle_invalid_store(SH_HANDLE);
183 CU_ASSERT(semanage_access_check(sh) < 0);
184 cleanup_handle(SH_HANDLE);
185
186 /* test connected */
187 setup_handle(SH_CONNECT);
188 res = semanage_access_check(sh);
189 CU_ASSERT(res == 0 || res == SEMANAGE_CAN_READ
190 || res == SEMANAGE_CAN_WRITE);
191 cleanup_handle(SH_CONNECT);
192 }
193
194 /* Function semanage_is_managed */
test_is_managed(void)195 static void test_is_managed(void)
196 {
197 int res = 0;
198
199 /* test with handle */
200 setup_handle(SH_HANDLE);
201 res = semanage_is_managed(sh);
202 CU_ASSERT(res == 0 || res == 1);
203
204 /* test connected */
205 helper_connect();
206 res = semanage_is_managed(sh);
207 CU_ASSERT(res < 0);
208
209 cleanup_handle(SH_CONNECT);
210 }
211
212 /* Function semanage_mls_enabled */
test_mls_enabled(void)213 static void test_mls_enabled(void)
214 {
215 int res = 0;
216
217 /* test with handle */
218 setup_handle(SH_HANDLE);
219 res = semanage_mls_enabled(sh);
220 CU_ASSERT(res == 0 || res == 1);
221 cleanup_handle(SH_HANDLE);
222
223 /* test with invalid store */
224 setup_handle_invalid_store(SH_HANDLE);
225 CU_ASSERT(semanage_mls_enabled(sh) < 0);
226 cleanup_handle(SH_HANDLE);
227
228 /* test connected */
229 setup_handle(SH_CONNECT);
230 res = semanage_mls_enabled(sh);
231 CU_ASSERT(res == 0 || res == 1);
232
233 cleanup_handle(SH_CONNECT);
234 }
235
236 /* Function semanage_set_callback */
237 int msg_set_callback_count = 0;
238
helper_msg_set_callback(void * varg,semanage_handle_t * handle,const char * fmt,...)239 static void helper_msg_set_callback(void *varg, semanage_handle_t *handle,
240 const char *fmt, ...)
241 {
242 msg_set_callback_count++;
243 }
244
test_msg_set_callback(void)245 static void test_msg_set_callback(void)
246 {
247 setup_handle(SH_CONNECT);
248
249 semanage_msg_set_callback(sh, helper_msg_set_callback, NULL);
250
251 /* produce error message */
252 semanage_commit(sh);
253 CU_ASSERT(msg_set_callback_count == 1);
254 semanage_msg_set_callback(sh, NULL, NULL);
255
256 /* produce error message */
257 semanage_commit(sh);
258 CU_ASSERT(msg_set_callback_count == 1);
259
260 cleanup_handle(SH_CONNECT);
261 }
262
263 /* Function semanage_root, semanage_set_root */
helper_root(void)264 static void helper_root(void)
265 {
266 const char *root = NULL;
267
268 CU_ASSERT(semanage_set_root("asdf") >= 0);
269 root = semanage_root();
270 CU_ASSERT_STRING_EQUAL(root, "asdf");
271
272 CU_ASSERT(semanage_set_root("") >= 0);
273 root = semanage_root();
274 CU_ASSERT_STRING_EQUAL(root, "");
275 }
276
test_root(void)277 static void test_root(void)
278 {
279 /* test without handle */
280 setup_handle(SH_NULL);
281 helper_root();
282
283 /* test with handle */
284 helper_handle_create();
285 helper_root();
286
287 /* test connected */
288 helper_connect();
289 helper_root();
290
291 cleanup_handle(SH_CONNECT);
292 }
293
294 /* Function semanage_select_store */
helper_select_store(const char * name,enum semanage_connect_type type,int exp_res)295 static void helper_select_store(const char *name, enum semanage_connect_type type,
296 int exp_res)
297 {
298 setup_handle(SH_HANDLE);
299
300 /* FIXME: the storename parameter of semanage_select_store should be
301 * 'const char *'
302 */
303 semanage_select_store(sh, (char *) name, type);
304
305 int res = semanage_connect(sh);
306
307 if (exp_res < 0) {
308 CU_ASSERT(res < 0);
309 } else {
310 CU_ASSERT(res >= 0);
311 }
312
313 if (res >= 0)
314 cleanup_handle(SH_CONNECT);
315 else
316 cleanup_handle(SH_HANDLE);
317 }
318
test_select_store(void)319 static void test_select_store(void)
320 {
321 helper_select_store("asdf", SEMANAGE_CON_INVALID - 1, -1);
322 helper_select_store("asdf", SEMANAGE_CON_POLSERV_REMOTE + 1, -1);
323 helper_select_store("", SEMANAGE_CON_DIRECT, 0);
324
325 helper_select_store("asdf", SEMANAGE_CON_INVALID, -1);
326 helper_select_store("asdf", SEMANAGE_CON_DIRECT, 0);
327 helper_select_store("asdf", SEMANAGE_CON_POLSERV_LOCAL, -1);
328 helper_select_store("asdf", SEMANAGE_CON_POLSERV_REMOTE, -1);
329 }
330