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