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_bool.h"
23 
24 #define BOOL_COUNT 3
25 #define BOOL1_NAME "first_bool"
26 #define BOOL1_VALUE 1
27 #define BOOL2_NAME "second_bool"
28 #define BOOL2_VALUE 0
29 #define BOOL3_NAME "third_bool"
30 #define BOOL3_VALUE 0
31 #define BOOL_NONEXISTENT "asdf"
32 
33 /* boolean_record.h */
34 void test_bool_key_create(void);
35 void test_bool_key_extract(void);
36 void test_bool_compare(void);
37 void test_bool_compare2(void);
38 void test_bool_get_set_name(void);
39 void test_bool_get_set_value(void);
40 void test_bool_create(void);
41 void test_bool_clone(void);
42 
43 /* booleans_policy.h */
44 void test_bool_query(void);
45 void test_bool_exists(void);
46 void test_bool_count(void);
47 void test_bool_iterate(void);
48 void test_bool_list(void);
49 
50 /* booleans_local.h */
51 void test_bool_modify_del_local(void);
52 void test_bool_query_local(void);
53 void test_bool_exists_local(void);
54 void test_bool_count_local(void);
55 void test_bool_iterate_local(void);
56 void test_bool_list_local(void);
57 
58 extern semanage_handle_t *sh;
59 
bool_test_init(void)60 int bool_test_init(void)
61 {
62 	if (create_test_store() < 0) {
63 		fprintf(stderr, "Could not create test store\n");
64 		return 1;
65 	}
66 
67 	if (write_test_policy_from_file("test_bool.policy") < 0) {
68 		fprintf(stderr, "Could not write test policy\n");
69 		return 1;
70 	}
71 
72 	return 0;
73 }
74 
bool_test_cleanup(void)75 int bool_test_cleanup(void)
76 {
77 	if (destroy_test_store() < 0) {
78 		fprintf(stderr, "Could not destroy test store\n");
79 		return 1;
80 	}
81 
82 	return 0;
83 }
84 
bool_add_tests(CU_pSuite suite)85 int bool_add_tests(CU_pSuite suite)
86 {
87 	CU_add_test(suite, "bool_key_create", test_bool_key_create);
88 	CU_add_test(suite, "bool_key_extract", test_bool_key_extract);
89 	CU_add_test(suite, "bool_compare", test_bool_compare);
90 	CU_add_test(suite, "bool_compare2", test_bool_compare2);
91 	CU_add_test(suite, "bool_get_set_name", test_bool_get_set_name);
92 	CU_add_test(suite, "bool_get_set_value", test_bool_get_set_value);
93 	CU_add_test(suite, "bool_create", test_bool_create);
94 	CU_add_test(suite, "bool_clone", test_bool_clone);
95 
96 	CU_add_test(suite, "bool_query", test_bool_query);
97 	CU_add_test(suite, "bool_exists", test_bool_exists);
98 	CU_add_test(suite, "bool_count", test_bool_count);
99 	CU_add_test(suite, "bool_iterate", test_bool_iterate);
100 	CU_add_test(suite, "bool_list", test_bool_list);
101 
102 	CU_add_test(suite, "bool_modify_del_local", test_bool_modify_del_local);
103 	CU_add_test(suite, "bool_query_local", test_bool_query_local);
104 	CU_add_test(suite, "bool_exists_local", test_bool_exists_local);
105 	CU_add_test(suite, "bool_count_local", test_bool_count_local);
106 	CU_add_test(suite, "bool_iterate_local", test_bool_iterate_local);
107 	CU_add_test(suite, "bool_list_local", test_bool_list_local);
108 
109 	return 0;
110 }
111 
112 /* Helpers */
113 
get_bool_nth(int idx)114 semanage_bool_t *get_bool_nth(int idx)
115 {
116 	int res;
117 	semanage_bool_t **records;
118 	semanage_bool_t *boolean;
119 	unsigned int count;
120 
121 	if (idx == I_NULL)
122 		return NULL;
123 
124 	res = semanage_bool_list(sh, &records, &count);
125 
126 	CU_ASSERT_FATAL(res >= 0);
127 	CU_ASSERT_FATAL(count >= (unsigned int) idx + 1);
128 
129 	boolean = records[idx];
130 
131 	for (unsigned int i = 0; i < count; i++)
132 		if (i != (unsigned int) idx)
133 			semanage_bool_free(records[i]);
134 
135 	return boolean;
136 }
137 
get_bool_new(void)138 semanage_bool_t *get_bool_new(void)
139 {
140 	int res;
141 	semanage_bool_t *boolean;
142 
143 	res = semanage_bool_create(sh, &boolean);
144 
145 	CU_ASSERT_FATAL(res >= 0);
146 
147 	return boolean;
148 }
149 
get_bool_key_nth(int idx)150 semanage_bool_key_t *get_bool_key_nth(int idx)
151 {
152 	semanage_bool_key_t *key;
153 	semanage_bool_t *boolean;
154 	int res;
155 
156 	if (idx == I_NULL)
157 		return NULL;
158 
159 	boolean = get_bool_nth(idx);
160 
161 	res = semanage_bool_key_extract(sh, boolean, &key);
162 
163 	CU_ASSERT_FATAL(res >= 0);
164 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
165 
166 	return key;
167 }
168 
get_bool_key_from_str(const char * str)169 semanage_bool_key_t *get_bool_key_from_str(const char *str)
170 {
171 	semanage_bool_key_t *key;
172 	int res;
173 
174 	if (str == NULL)
175 		return NULL;
176 
177 	res = semanage_bool_key_create(sh, str, &key);
178 
179 	CU_ASSERT_FATAL(res >= 0);
180 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
181 
182 	return key;
183 }
184 
add_local_bool(const char * name)185 void add_local_bool(const char *name)
186 {
187 	semanage_bool_t *boolean;
188 	semanage_bool_key_t *key = NULL;
189 
190 	CU_ASSERT_PTR_NOT_NULL_FATAL(name);
191 
192 	CU_ASSERT_FATAL(semanage_bool_key_create(sh, name, &key) >= 0);
193 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
194 
195 	CU_ASSERT_FATAL(semanage_bool_query(sh, key, &boolean) >= 0);
196 	CU_ASSERT_PTR_NOT_NULL_FATAL(boolean);
197 
198 	CU_ASSERT_FATAL(semanage_bool_modify_local(sh, key, boolean) >= 0);
199 }
200 
delete_local_bool(const char * name)201 void delete_local_bool(const char *name)
202 {
203 	semanage_bool_key_t *key = NULL;
204 
205 	CU_ASSERT_PTR_NOT_NULL_FATAL(name);
206 
207 	CU_ASSERT_FATAL(semanage_bool_key_create(sh, name, &key) >= 0);
208 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
209 
210 	CU_ASSERT_FATAL(semanage_bool_del_local(sh, key) >= 0);
211 }
212 
213 /* Function bool_key_create */
214 
helper_bool_key_create(level_t level)215 void helper_bool_key_create(level_t level)
216 {
217 	semanage_bool_key_t *key = NULL;
218 
219 	setup_handle(level);
220 
221 	CU_ASSERT(semanage_bool_key_create(sh, "", &key) >= 0);
222 	CU_ASSERT_PTR_NOT_NULL(key);
223 
224 	semanage_bool_key_free(key);
225 
226 	key = NULL;
227 
228 	CU_ASSERT(semanage_bool_key_create(sh, "testbool", &key) >= 0);
229 	CU_ASSERT_PTR_NOT_NULL(key);
230 
231 	semanage_bool_key_free(key);
232 
233 	cleanup_handle(level);
234 }
235 
test_bool_key_create(void)236 void test_bool_key_create(void)
237 {
238 	helper_bool_key_create(SH_CONNECT);
239 	helper_bool_key_create(SH_TRANS);
240 }
241 
242 /* Function bool_key_extract */
243 #define SK_NULL 1
244 #define SK_NEW 2
245 #define SK_INDEX 3
246 #define SK_KEY_NULL 4
helper_bool_key_extract(level_t level,int mode)247 void helper_bool_key_extract(level_t level, int mode)
248 {
249 	semanage_bool_t *boolean = NULL;
250 	semanage_bool_key_t *key = NULL;
251 	int res;
252 
253 	setup_handle(level);
254 
255 	switch (mode) {
256 	case SK_NULL:
257 		boolean = NULL;
258 		break;
259 	case SK_NEW:
260 		boolean = get_bool_new();
261 		break;
262 	case SK_INDEX:
263 		boolean = get_bool_nth(0);
264 		break;
265 	case SK_KEY_NULL:
266 		boolean = get_bool_nth(0);
267 		break;
268 	default:
269 		CU_FAIL_FATAL("Invalid mode\n");
270 	}
271 
272 	if (mode == SK_KEY_NULL)
273 		res = semanage_bool_key_extract(sh, boolean, NULL);
274 	else
275 		res = semanage_bool_key_extract(sh, boolean, &key);
276 
277 	CU_ASSERT(res >= 0);
278 
279 	res = semanage_bool_compare(boolean, key);
280 
281 	CU_ASSERT(res == 0);
282 
283 	semanage_bool_key_free(key);
284 	semanage_bool_free(boolean);
285 
286 	cleanup_handle(level);
287 }
288 
test_bool_key_extract(void)289 void test_bool_key_extract(void)
290 {
291 	helper_bool_key_extract(SH_CONNECT, SK_INDEX);
292 	helper_bool_key_extract(SH_TRANS, SK_INDEX);
293 }
294 #undef SK_NULL
295 #undef SK_NEW
296 #undef SK_INDEX
297 #undef SK_KEY_NULL
298 
299 /* Function bool_compare */
helper_bool_compare(level_t level,int bool_idx1,int bool_idx2)300 void helper_bool_compare(level_t level, int bool_idx1, int bool_idx2)
301 {
302 	semanage_bool_t *boolean;
303 	semanage_bool_key_t *key;
304 	int res;
305 
306 	setup_handle(level);
307 
308 	boolean = get_bool_nth(bool_idx1);
309 	key = get_bool_key_nth(bool_idx2);
310 
311 	res = semanage_bool_compare(boolean, key);
312 
313 	if (bool_idx1 == bool_idx2) {
314 		CU_ASSERT(res == 0);
315 	} else {
316 		CU_ASSERT(res != 0);
317 	}
318 
319 	semanage_bool_free(boolean);
320 	semanage_bool_key_free(key);
321 	cleanup_handle(level);
322 }
323 
test_bool_compare(void)324 void test_bool_compare(void)
325 {
326 	helper_bool_compare(SH_CONNECT, I_FIRST,  I_FIRST);
327 	helper_bool_compare(SH_CONNECT, I_FIRST,  I_SECOND);
328 	helper_bool_compare(SH_CONNECT, I_SECOND, I_FIRST);
329 	helper_bool_compare(SH_CONNECT, I_SECOND, I_SECOND);
330 
331 	helper_bool_compare(SH_TRANS, I_FIRST,  I_FIRST);
332 	helper_bool_compare(SH_TRANS, I_FIRST,  I_SECOND);
333 	helper_bool_compare(SH_TRANS, I_SECOND, I_FIRST);
334 	helper_bool_compare(SH_TRANS, I_SECOND, I_SECOND);
335 }
336 
337 /* Function bool_compare2 */
helper_bool_compare2(level_t level,int bool_idx1,int bool_idx2)338 void helper_bool_compare2(level_t level, int bool_idx1, int bool_idx2)
339 {
340 	semanage_bool_t *bool1;
341 	semanage_bool_t *bool2;
342 	int res;
343 
344 	setup_handle(level);
345 
346 	bool1 = get_bool_nth(bool_idx1);
347 	bool2 = get_bool_nth(bool_idx2);
348 
349 	res = semanage_bool_compare2(bool1, bool2);
350 
351 	if (bool_idx1 == bool_idx2) {
352 		CU_ASSERT(res == 0);
353 	} else {
354 		CU_ASSERT(res != 0);
355 	}
356 
357 	semanage_bool_free(bool1);
358 	semanage_bool_free(bool2);
359 	cleanup_handle(level);
360 }
361 
test_bool_compare2(void)362 void test_bool_compare2(void)
363 {
364 	helper_bool_compare2(SH_CONNECT, I_FIRST,  I_FIRST);
365 	helper_bool_compare2(SH_CONNECT, I_FIRST,  I_SECOND);
366 	helper_bool_compare2(SH_CONNECT, I_SECOND, I_FIRST);
367 	helper_bool_compare2(SH_CONNECT, I_SECOND, I_SECOND);
368 
369 	helper_bool_compare2(SH_TRANS, I_FIRST,  I_FIRST);
370 	helper_bool_compare2(SH_TRANS, I_FIRST,  I_SECOND);
371 	helper_bool_compare2(SH_TRANS, I_SECOND, I_FIRST);
372 	helper_bool_compare2(SH_TRANS, I_SECOND, I_SECOND);
373 }
374 
375 /* Function bool_get_name, bool_set_name */
helper_bool_get_set_name(level_t level,int bool_idx,const char * name)376 void helper_bool_get_set_name(level_t level, int bool_idx, const char *name)
377 {
378 	semanage_bool_t *boolean;
379 	const char *new_name = NULL;
380 
381 	setup_handle(level);
382 
383 	boolean = get_bool_nth(bool_idx);
384 
385 	CU_ASSERT(semanage_bool_set_name(sh, boolean, name) >= 0);
386 
387 	new_name = semanage_bool_get_name(boolean);
388 
389 	CU_ASSERT_PTR_NOT_NULL(new_name);
390 	/* Use assert to silence the clang analyzer */
391 	assert(new_name);
392 	CU_ASSERT_STRING_EQUAL(new_name, name);
393 
394 	semanage_bool_free(boolean);
395 	cleanup_handle(level);
396 }
397 
test_bool_get_set_name(void)398 void test_bool_get_set_name(void)
399 {
400 	helper_bool_get_set_name(SH_CONNECT, I_FIRST, "testbool");
401 	helper_bool_get_set_name(SH_CONNECT, I_FIRST, "");
402 	helper_bool_get_set_name(SH_CONNECT, I_SECOND, "testbool");
403 	helper_bool_get_set_name(SH_CONNECT, I_SECOND, "");
404 
405 	helper_bool_get_set_name(SH_TRANS, I_FIRST, "testbool");
406 	helper_bool_get_set_name(SH_TRANS, I_FIRST, "");
407 	helper_bool_get_set_name(SH_TRANS, I_SECOND, "testbool");
408 	helper_bool_get_set_name(SH_TRANS, I_SECOND, "");
409 }
410 
411 /* Function bool_get_value, bool_set_value */
helper_bool_get_set_value(int bool_idx,int val)412 void helper_bool_get_set_value(int bool_idx, int val)
413 {
414 	semanage_bool_t *boolean;
415 	int new_val = 0;
416 
417 	setup_handle(SH_CONNECT);
418 	boolean = get_bool_nth(bool_idx);
419 	cleanup_handle(SH_CONNECT);
420 
421 	semanage_bool_set_value(boolean, val);
422 
423 	new_val = semanage_bool_get_value(boolean);
424 
425 	CU_ASSERT(new_val == val);
426 
427 	semanage_bool_free(boolean);
428 }
429 
test_bool_get_set_value(void)430 void test_bool_get_set_value(void)
431 {
432 	helper_bool_get_set_value(I_FIRST, 1);
433 	helper_bool_get_set_value(I_FIRST, 0);
434 	helper_bool_get_set_value(I_SECOND, 1);
435 	helper_bool_get_set_value(I_SECOND, 0);
436 }
437 
438 /* Function bool_create */
helper_bool_create(level_t level)439 void helper_bool_create(level_t level)
440 {
441 	semanage_bool_t *boolean;
442 
443 	setup_handle(level);
444 
445 	CU_ASSERT(semanage_bool_create(sh, &boolean) >= 0);
446 
447 	CU_ASSERT_PTR_NULL(semanage_bool_get_name(boolean));
448 	CU_ASSERT(semanage_bool_get_value(boolean) == 0);
449 
450 	cleanup_handle(level);
451 }
452 
test_bool_create(void)453 void test_bool_create(void)
454 {
455 	helper_bool_create(SH_HANDLE);
456 	helper_bool_create(SH_CONNECT);
457 	helper_bool_create(SH_TRANS);
458 }
459 
460 /* Function bool_clone */
helper_bool_clone(level_t level,int bool_idx)461 void helper_bool_clone(level_t level, int bool_idx)
462 {
463 	semanage_bool_t *boolean;
464 	semanage_bool_t *boolean_clone;
465 	const char *str;
466 	const char *str_clone;
467 	int val;
468 	int val_clone;
469 
470 	setup_handle(level);
471 
472 	boolean = get_bool_nth(bool_idx);
473 
474 	CU_ASSERT(semanage_bool_clone(sh, boolean, &boolean_clone) >= 0);
475 
476 	str = semanage_bool_get_name(boolean);
477 	str_clone = semanage_bool_get_name(boolean_clone);
478 
479 	CU_ASSERT_STRING_EQUAL(str, str_clone);
480 
481 	val = semanage_bool_get_value(boolean);
482 	val_clone = semanage_bool_get_value(boolean_clone);
483 
484 	CU_ASSERT_EQUAL(val, val_clone);
485 
486 	cleanup_handle(level);
487 }
488 
test_bool_clone(void)489 void test_bool_clone(void)
490 {
491 	helper_bool_clone(SH_CONNECT, I_FIRST);
492 	helper_bool_clone(SH_CONNECT, I_SECOND);
493 
494 	helper_bool_clone(SH_TRANS, I_FIRST);
495 	helper_bool_clone(SH_TRANS, I_SECOND);
496 }
497 
498 /* Function bool_query */
helper_bool_query(level_t level,const char * bool_str,int exp_res)499 void helper_bool_query(level_t level, const char *bool_str, int exp_res)
500 {
501 	semanage_bool_key_t *key;
502 	semanage_bool_t *resp = (void *) 42;
503 
504 	setup_handle(level);
505 
506 	key = get_bool_key_from_str(bool_str);
507 
508 	CU_ASSERT(semanage_bool_query(sh, key, &resp) >= 0);
509 
510 	if (exp_res >= 0) {
511 		const char *name = semanage_bool_get_name(resp);
512 		CU_ASSERT_STRING_EQUAL(name, bool_str);
513 	} else {
514 		CU_ASSERT_PTR_NULL(resp);
515 	}
516 
517 	cleanup_handle(level);
518 }
519 
test_bool_query(void)520 void test_bool_query(void)
521 {
522 	helper_bool_query(SH_CONNECT, BOOL1_NAME,  1);
523 	helper_bool_query(SH_CONNECT, BOOL2_NAME, 1);
524 	helper_bool_query(SH_CONNECT, BOOL_NONEXISTENT, -1);
525 
526 	helper_bool_query(SH_TRANS, BOOL1_NAME,  1);
527 	helper_bool_query(SH_TRANS, BOOL2_NAME, 1);
528 	helper_bool_query(SH_TRANS, BOOL_NONEXISTENT, -1);
529 }
530 
531 /* Function bool_exists */
helper_bool_exists(level_t level,const char * bool_str,int exp_resp)532 void helper_bool_exists(level_t level, const char *bool_str, int exp_resp)
533 {
534 	semanage_bool_key_t *key;
535 	int resp;
536 
537 	setup_handle(level);
538 
539 	key = get_bool_key_from_str(bool_str);
540 
541 	CU_ASSERT(semanage_bool_exists(sh, key, &resp) >= 0);
542 	CU_ASSERT(resp == exp_resp);
543 
544 	semanage_bool_key_free(key);
545 
546 	cleanup_handle(level);
547 }
548 
test_bool_exists(void)549 void test_bool_exists(void)
550 {
551 	helper_bool_exists(SH_CONNECT, BOOL1_NAME,  1);
552 	helper_bool_exists(SH_CONNECT, BOOL2_NAME, 1);
553 	helper_bool_exists(SH_CONNECT, BOOL_NONEXISTENT, 0);
554 
555 	helper_bool_exists(SH_TRANS, BOOL1_NAME,  1);
556 	helper_bool_exists(SH_TRANS, BOOL2_NAME, 1);
557 	helper_bool_exists(SH_TRANS, BOOL_NONEXISTENT, 0);
558 }
559 
560 /* Function bool_count */
test_bool_count(void)561 void test_bool_count(void)
562 {
563 	unsigned int resp;
564 
565 	/* handle */
566 	setup_handle(SH_HANDLE);
567 	CU_ASSERT(semanage_bool_count(sh, &resp) < 0);
568 	CU_ASSERT(semanage_bool_count(sh, NULL) < 0);
569 	cleanup_handle(SH_HANDLE);
570 
571 	/* connect */
572 	resp = 0;
573 	setup_handle(SH_CONNECT);
574 	CU_ASSERT(semanage_bool_count(sh, &resp) >= 0);
575 	CU_ASSERT(resp == BOOL_COUNT);
576 	cleanup_handle(SH_CONNECT);
577 
578 	/* trans */
579 	resp = 0;
580 	setup_handle(SH_TRANS);
581 	CU_ASSERT(semanage_bool_count(sh, &resp) >= 0);
582 	CU_ASSERT(resp == BOOL_COUNT);
583 	cleanup_handle(SH_TRANS);
584 }
585 
586 /* Function bool_iterate */
587 unsigned int counter_bool_iterate = 0;
588 
handler_bool_iterate(const semanage_bool_t * record,void * varg)589 int handler_bool_iterate(const semanage_bool_t *record, void *varg)
590 {
591 	counter_bool_iterate++;
592 	return 0;
593 }
594 
helper_bool_iterate_invalid(void)595 void helper_bool_iterate_invalid(void)
596 {
597 	setup_handle(SH_HANDLE);
598 	CU_ASSERT(semanage_bool_iterate(sh, &handler_bool_iterate, NULL) < 0);
599 	CU_ASSERT(semanage_bool_iterate(sh, NULL, NULL) < 0);
600 	cleanup_handle(SH_HANDLE);
601 }
602 
helper_bool_iterate(level_t level)603 void helper_bool_iterate(level_t level)
604 {
605 	setup_handle(level);
606 	counter_bool_iterate = 0;
607 	CU_ASSERT(semanage_bool_iterate(sh, &handler_bool_iterate, NULL) >= 0);
608 	CU_ASSERT(counter_bool_iterate == BOOL_COUNT);
609 	cleanup_handle(level);
610 }
611 
test_bool_iterate(void)612 void test_bool_iterate(void)
613 {
614 	helper_bool_iterate_invalid();
615 	helper_bool_iterate(SH_CONNECT);
616 	helper_bool_iterate(SH_TRANS);
617 }
618 
619 /* Function bool_list */
helper_bool_list_invalid(void)620 void helper_bool_list_invalid(void)
621 {
622 	semanage_bool_t **records;
623 	unsigned int count;
624 
625 	setup_handle(SH_HANDLE);
626 
627 	CU_ASSERT(semanage_bool_list(sh, &records, &count) < 0);
628 	CU_ASSERT(semanage_bool_list(sh, NULL, &count) < 0);
629 	CU_ASSERT(semanage_bool_list(sh, &records, NULL) < 0);
630 
631 	cleanup_handle(SH_HANDLE);
632 }
633 
helper_bool_list(level_t level)634 void helper_bool_list(level_t level)
635 {
636 	semanage_bool_t **records;
637 	unsigned int count;
638 
639 	setup_handle(level);
640 
641 	CU_ASSERT(semanage_bool_list(sh, &records, &count) >= 0);
642 	CU_ASSERT(count == BOOL_COUNT);
643 
644 	for (unsigned int i = 0; i < count; i++)
645 		CU_ASSERT_PTR_NOT_NULL(records[i]);
646 
647 	for (unsigned int i = 0; i < count; i++)
648 		semanage_bool_free(records[i]);
649 
650 	cleanup_handle(level);
651 }
652 
test_bool_list(void)653 void test_bool_list(void)
654 {
655 	helper_bool_list_invalid();
656 	helper_bool_list(SH_CONNECT);
657 	helper_bool_list(SH_TRANS);
658 }
659 
660 /* Function bool_modify_local, bool_del_local */
helper_bool_modify_del_local(level_t level,const char * name,int old_val,int exp_res)661 void helper_bool_modify_del_local(level_t level, const char *name,
662 				  int old_val, int exp_res)
663 {
664 	semanage_bool_t *boolean;
665 	semanage_bool_t *boolean_local;
666 	semanage_bool_key_t *key = NULL;
667 	int res;
668 	int new_val;
669 
670 	/* setup */
671 	setup_handle(level);
672 
673 	CU_ASSERT(semanage_bool_key_create(sh, name, &key) >= 0);
674 	CU_ASSERT_PTR_NOT_NULL(key);
675 
676 	CU_ASSERT(semanage_bool_query(sh, key, &boolean) >= 0);
677 	CU_ASSERT_PTR_NOT_NULL(boolean);
678 
679 	new_val = !old_val;
680 	semanage_bool_set_value(boolean, new_val);
681 
682 	/* test */
683 	res = semanage_bool_modify_local(sh, key, boolean);
684 
685 	if (exp_res < 0) {
686 		CU_ASSERT(res < 0);
687 	} else {
688 		CU_ASSERT(res >= 0);
689 
690 		/* write changes to file */
691 		if (level == SH_TRANS) {
692 			helper_commit();
693 			helper_begin_transaction();
694 		}
695 
696 		CU_ASSERT(semanage_bool_query_local(sh, key,
697 					            &boolean_local) >= 0);
698 		CU_ASSERT(semanage_bool_compare2(boolean_local, boolean) == 0);
699 		CU_ASSERT(semanage_bool_del_local(sh, key) >= 0);
700 		CU_ASSERT(semanage_bool_query_local(sh, key,
701 						    &boolean_local) < 0);
702 	}
703 
704 	/* cleanup */
705 	semanage_bool_key_free(key);
706 	semanage_bool_free(boolean);
707 
708 	cleanup_handle(level);
709 }
710 
test_bool_modify_del_local(void)711 void test_bool_modify_del_local(void)
712 {
713 	helper_bool_modify_del_local(SH_CONNECT, BOOL1_NAME, BOOL1_VALUE, -1);
714 	helper_bool_modify_del_local(SH_CONNECT, BOOL2_NAME, BOOL2_VALUE, -1);
715 	helper_bool_modify_del_local(SH_TRANS, BOOL1_NAME, BOOL1_VALUE, 1);
716 	helper_bool_modify_del_local(SH_TRANS, BOOL2_NAME, BOOL2_VALUE, 1);
717 }
718 
719 /* Function bool_query_local */
test_bool_query_local(void)720 void test_bool_query_local(void)
721 {
722 	semanage_bool_key_t *key = NULL;
723 	semanage_bool_t *resp = NULL;
724 
725 	/* connect */
726 	setup_handle(SH_CONNECT);
727 	CU_ASSERT(semanage_bool_key_create(sh, BOOL1_NAME, &key) >= 0);
728 	CU_ASSERT_PTR_NOT_NULL(key);
729 
730 	CU_ASSERT(semanage_bool_query_local(sh, key, &resp) < 0);
731 	CU_ASSERT_PTR_NULL(resp);
732 
733 	cleanup_handle(SH_CONNECT);
734 
735 	/* transaction */
736 	setup_handle(SH_TRANS);
737 	CU_ASSERT(semanage_bool_key_create(sh, BOOL1_NAME, &key) >= 0);
738 	CU_ASSERT_PTR_NOT_NULL(key);
739 
740 	CU_ASSERT(semanage_bool_query_local(sh, key, &resp) < 0);
741 	CU_ASSERT_PTR_NULL(resp);
742 
743 	add_local_bool(BOOL1_NAME);
744 	CU_ASSERT(semanage_bool_query_local(sh, key, &resp) >= 0);
745 	CU_ASSERT_PTR_NOT_NULL(resp);
746 
747 	semanage_bool_key_free(key);
748 	CU_ASSERT(semanage_bool_key_create(sh, BOOL2_NAME, &key) >= 0);
749 	CU_ASSERT_PTR_NOT_NULL(key);
750 
751 	add_local_bool(BOOL2_NAME);
752 	CU_ASSERT(semanage_bool_query_local(sh, key, &resp) >= 0);
753 	CU_ASSERT_PTR_NOT_NULL(resp);
754 
755 	/* cleanup */
756 	delete_local_bool(BOOL1_NAME);
757 	delete_local_bool(BOOL2_NAME);
758 	cleanup_handle(SH_TRANS);
759 }
760 
761 /* Function bool_exists_local */
test_bool_exists_local(void)762 void test_bool_exists_local(void)
763 {
764 	int resp = -1;
765 	semanage_bool_key_t *key;
766 
767 	/* setup */
768 	setup_handle(SH_TRANS);
769 	CU_ASSERT(semanage_bool_key_create(sh, BOOL1_NAME, &key) >= 0);
770 	CU_ASSERT_PTR_NOT_NULL(key);
771 
772 	/* test */
773 	CU_ASSERT(semanage_bool_exists_local(sh, key, &resp) >= 0);
774 	CU_ASSERT(resp == 0);
775 
776 	add_local_bool(BOOL1_NAME);
777 	resp = -1;
778 	CU_ASSERT(semanage_bool_exists_local(sh, key, &resp) >= 0);
779 	CU_ASSERT(resp == 1);
780 
781 	delete_local_bool(BOOL1_NAME);
782 	resp = -1;
783 	CU_ASSERT(semanage_bool_exists_local(sh, key, &resp) >= 0);
784 	CU_ASSERT(resp == 0);
785 
786 	/* cleanup */
787 	cleanup_handle(SH_TRANS);
788 }
789 
790 /* Function bool_count_local */
test_bool_count_local(void)791 void test_bool_count_local(void)
792 {
793 	unsigned int resp;
794 	unsigned int init_count;
795 
796 	/* handle */
797 	setup_handle(SH_HANDLE);
798 	CU_ASSERT(semanage_bool_count_local(sh, &resp) < 0);
799 	cleanup_handle(SH_HANDLE);
800 
801 	/* connect */
802 	setup_handle(SH_CONNECT);
803 	CU_ASSERT(semanage_bool_count_local(sh, &resp) >= 0);
804 	cleanup_handle(SH_CONNECT);
805 
806 	/* transaction */
807 	setup_handle(SH_TRANS);
808 
809 	CU_ASSERT(semanage_bool_count_local(sh, &resp) >= 0);
810 	init_count = resp;
811 
812 	add_local_bool(BOOL1_NAME);
813 	CU_ASSERT(semanage_bool_count_local(sh, &resp) >= 0);
814 	CU_ASSERT(resp == init_count + 1);
815 
816 	add_local_bool(BOOL2_NAME);
817 	CU_ASSERT(semanage_bool_count_local(sh, &resp) >= 0);
818 	CU_ASSERT(resp == init_count + 2);
819 
820 	delete_local_bool(BOOL2_NAME);
821 	CU_ASSERT(semanage_bool_count_local(sh, &resp) >= 0);
822 	CU_ASSERT(resp == init_count + 1);
823 
824 	delete_local_bool(BOOL1_NAME);
825 	CU_ASSERT(semanage_bool_count_local(sh, &resp) >= 0);
826 	CU_ASSERT(resp == init_count);
827 
828 	/* cleanup */
829 	cleanup_handle(SH_TRANS);
830 }
831 
832 /* Function bool_iterate_local */
833 unsigned int counter_bool_iterate_local = 0;
834 
handler_bool_iterate_local(const semanage_bool_t * record,void * varg)835 int handler_bool_iterate_local(const semanage_bool_t *record, void *varg)
836 {
837 	counter_bool_iterate_local++;
838 	return 0;
839 }
840 
test_bool_iterate_local(void)841 void test_bool_iterate_local(void)
842 {
843 	unsigned int init_count;
844 
845 	/* handle */
846 	setup_handle(SH_HANDLE);
847 	CU_ASSERT(semanage_bool_iterate_local(sh, &handler_bool_iterate_local,
848 					      NULL) < 0);
849 	cleanup_handle(SH_HANDLE);
850 
851 	/* connect */
852 	setup_handle(SH_CONNECT);
853 
854 	counter_bool_iterate_local = 0;
855 	CU_ASSERT(semanage_bool_iterate_local(sh, &handler_bool_iterate_local,
856 					      NULL) >= 0);
857 	init_count = counter_bool_iterate_local;
858 
859 	cleanup_handle(SH_CONNECT);
860 
861 	/* transaction */
862 	setup_handle(SH_TRANS);
863 
864 	counter_bool_iterate_local = 0;
865 	CU_ASSERT(semanage_bool_iterate_local(sh, &handler_bool_iterate_local,
866 					      NULL) >= 0);
867 	CU_ASSERT(counter_bool_iterate_local == init_count);
868 
869 	add_local_bool(BOOL1_NAME);
870 	counter_bool_iterate_local = 0;
871 	CU_ASSERT(semanage_bool_iterate_local(sh, &handler_bool_iterate_local,
872 					      NULL) >= 0);
873 	CU_ASSERT(counter_bool_iterate_local == init_count + 1);
874 
875 	add_local_bool(BOOL2_NAME);
876 	counter_bool_iterate_local = 0;
877 	CU_ASSERT(semanage_bool_iterate_local(sh, &handler_bool_iterate_local,
878 					      NULL) >= 0);
879 	CU_ASSERT(counter_bool_iterate_local == init_count + 2);
880 
881 	/* cleanup */
882 	delete_local_bool(BOOL1_NAME);
883 	delete_local_bool(BOOL2_NAME);
884 	cleanup_handle(SH_TRANS);
885 }
886 
887 /* Function bool_list_local */
test_bool_list_local(void)888 void test_bool_list_local(void)
889 {
890 	semanage_bool_t **records;
891 	unsigned int count;
892 	unsigned int init_count;
893 
894 	/* handle */
895 	setup_handle(SH_HANDLE);
896 
897 	CU_ASSERT(semanage_bool_list_local(sh, &records, &count) < 0);
898 	CU_ASSERT(semanage_bool_list_local(sh, NULL, &count) < 0);
899 	CU_ASSERT(semanage_bool_list_local(sh, &records, NULL) < 0);
900 
901 	cleanup_handle(SH_HANDLE);
902 
903 	/* connect */
904 	setup_handle(SH_CONNECT);
905 
906 	CU_ASSERT(semanage_bool_list_local(sh, &records, &count) >= 0);
907 	init_count = count;
908 
909 	cleanup_handle(SH_CONNECT);
910 
911 	/* transaction */
912 	setup_handle(SH_TRANS);
913 
914 	CU_ASSERT(semanage_bool_list_local(sh, &records, &count) >= 0);
915 	CU_ASSERT(count == init_count);
916 
917 	add_local_bool(BOOL1_NAME);
918 	CU_ASSERT(semanage_bool_list_local(sh, &records, &count) >= 0);
919 	CU_ASSERT(count == init_count + 1);
920 	CU_ASSERT_PTR_NOT_NULL(records[0]);
921 
922 	add_local_bool(BOOL2_NAME);
923 	CU_ASSERT(semanage_bool_list_local(sh, &records, &count) >= 0);
924 	CU_ASSERT(count == init_count + 2);
925 	CU_ASSERT_PTR_NOT_NULL(records[0]);
926 	CU_ASSERT_PTR_NOT_NULL(records[1]);
927 
928 	/* cleanup */
929 	delete_local_bool(BOOL1_NAME);
930 	delete_local_bool(BOOL2_NAME);
931 	cleanup_handle(SH_TRANS);
932 }
933