xref: /aosp_15_r20/external/selinux/libsemanage/tests/test_fcontext.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_fcontext.h"
23*2d543d20SAndroid Build Coastguard Worker 
24*2d543d20SAndroid Build Coastguard Worker char FCONTEXTS[] =
25*2d543d20SAndroid Build Coastguard Worker     "/etc/selinux(/.*) -s system_u:object_r:first_t:s0\n"
26*2d543d20SAndroid Build Coastguard Worker     "/etc/selinux/targeted -- system_u:object_r:second_t:s0\n"
27*2d543d20SAndroid Build Coastguard Worker     "/etc/selinux(/.*) -b system_u:object_r:third_t:s0\n";
28*2d543d20SAndroid Build Coastguard Worker unsigned int FCONTEXTS_LEN = sizeof(FCONTEXTS);
29*2d543d20SAndroid Build Coastguard Worker 
30*2d543d20SAndroid Build Coastguard Worker #define FCONTEXTS_COUNT 3
31*2d543d20SAndroid Build Coastguard Worker 
32*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT1_EXPR "/etc/selinux(/.*)"
33*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT1_TYPE SEMANAGE_FCONTEXT_SOCK
34*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT1_CON "system_u:object_r:first_t:s0"
35*2d543d20SAndroid Build Coastguard Worker 
36*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT2_EXPR "/etc/selinux/targeted"
37*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT2_TYPE SEMANAGE_FCONTEXT_REG
38*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT2_CON "system_u:object_r:second_t:s0"
39*2d543d20SAndroid Build Coastguard Worker 
40*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT3_EXPR "/etc/selinux(/.*)"
41*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT3_TYPE SEMANAGE_FCONTEXT_BLOCK
42*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT3_CON "system_u:object_r:third_t:s0"
43*2d543d20SAndroid Build Coastguard Worker 
44*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT_NONEXISTENT_EXPR "/asdf"
45*2d543d20SAndroid Build Coastguard Worker #define FCONTEXT_NONEXISTENT_TYPE SEMANAGE_FCONTEXT_ALL
46*2d543d20SAndroid Build Coastguard Worker 
47*2d543d20SAndroid Build Coastguard Worker /* fcontext_record.h */
48*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_compare(void);
49*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_compare2(void);
50*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_key_create(void);
51*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_key_extract(void);
52*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_set_expr(void);
53*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_set_type(void);
54*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_type_str(void);
55*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_set_con(void);
56*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_create(void);
57*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_clone(void);
58*2d543d20SAndroid Build Coastguard Worker 
59*2d543d20SAndroid Build Coastguard Worker /* fcontext_policy.h */
60*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_query(void);
61*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_exists(void);
62*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_count(void);
63*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_iterate(void);
64*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_list(void);
65*2d543d20SAndroid Build Coastguard Worker 
66*2d543d20SAndroid Build Coastguard Worker /* fcontext_local.h */
67*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_modify_del_local(void);
68*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_query_local(void);
69*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_exists_local(void);
70*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_count_local(void);
71*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_iterate_local(void);
72*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_list_local(void);
73*2d543d20SAndroid Build Coastguard Worker 
74*2d543d20SAndroid Build Coastguard Worker extern semanage_handle_t *sh;
75*2d543d20SAndroid Build Coastguard Worker 
write_file_contexts(const char * data,unsigned int data_len)76*2d543d20SAndroid Build Coastguard Worker static int write_file_contexts(const char *data, unsigned int data_len)
77*2d543d20SAndroid Build Coastguard Worker {
78*2d543d20SAndroid Build Coastguard Worker 	FILE *fptr = fopen("test-policy/store/active/file_contexts", "w+");
79*2d543d20SAndroid Build Coastguard Worker 
80*2d543d20SAndroid Build Coastguard Worker 	if (!fptr) {
81*2d543d20SAndroid Build Coastguard Worker 		perror("fopen");
82*2d543d20SAndroid Build Coastguard Worker 		return -1;
83*2d543d20SAndroid Build Coastguard Worker 	}
84*2d543d20SAndroid Build Coastguard Worker 
85*2d543d20SAndroid Build Coastguard Worker 	if (fwrite(data, data_len, 1, fptr) != 1) {
86*2d543d20SAndroid Build Coastguard Worker 		perror("fwrite");
87*2d543d20SAndroid Build Coastguard Worker 		fclose(fptr);
88*2d543d20SAndroid Build Coastguard Worker 		return -1;
89*2d543d20SAndroid Build Coastguard Worker 	}
90*2d543d20SAndroid Build Coastguard Worker 
91*2d543d20SAndroid Build Coastguard Worker 	fclose(fptr);
92*2d543d20SAndroid Build Coastguard Worker 
93*2d543d20SAndroid Build Coastguard Worker 	return 0;
94*2d543d20SAndroid Build Coastguard Worker }
95*2d543d20SAndroid Build Coastguard Worker 
fcontext_test_init(void)96*2d543d20SAndroid Build Coastguard Worker int fcontext_test_init(void)
97*2d543d20SAndroid Build Coastguard Worker {
98*2d543d20SAndroid Build Coastguard Worker 	if (create_test_store() < 0) {
99*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "Could not create test store\n");
100*2d543d20SAndroid Build Coastguard Worker 		return 1;
101*2d543d20SAndroid Build Coastguard Worker 	}
102*2d543d20SAndroid Build Coastguard Worker 
103*2d543d20SAndroid Build Coastguard Worker 	if (write_test_policy_from_file("test_fcontext.policy") < 0) {
104*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "Could not write test policy\n");
105*2d543d20SAndroid Build Coastguard Worker 		return 1;
106*2d543d20SAndroid Build Coastguard Worker 	}
107*2d543d20SAndroid Build Coastguard Worker 
108*2d543d20SAndroid Build Coastguard Worker 	if (write_file_contexts(FCONTEXTS, FCONTEXTS_LEN) < 0) {
109*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "Could not write file contexts\n");
110*2d543d20SAndroid Build Coastguard Worker 		return 1;
111*2d543d20SAndroid Build Coastguard Worker 	}
112*2d543d20SAndroid Build Coastguard Worker 
113*2d543d20SAndroid Build Coastguard Worker 	return 0;
114*2d543d20SAndroid Build Coastguard Worker }
115*2d543d20SAndroid Build Coastguard Worker 
fcontext_test_cleanup(void)116*2d543d20SAndroid Build Coastguard Worker int fcontext_test_cleanup(void)
117*2d543d20SAndroid Build Coastguard Worker {
118*2d543d20SAndroid Build Coastguard Worker 	if (destroy_test_store() < 0) {
119*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "Could not destroy test store\n");
120*2d543d20SAndroid Build Coastguard Worker 		return 1;
121*2d543d20SAndroid Build Coastguard Worker 	}
122*2d543d20SAndroid Build Coastguard Worker 
123*2d543d20SAndroid Build Coastguard Worker 	return 0;
124*2d543d20SAndroid Build Coastguard Worker }
125*2d543d20SAndroid Build Coastguard Worker 
fcontext_add_tests(CU_pSuite suite)126*2d543d20SAndroid Build Coastguard Worker int fcontext_add_tests(CU_pSuite suite)
127*2d543d20SAndroid Build Coastguard Worker {
128*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_compare", test_fcontext_compare);
129*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_compare2", test_fcontext_compare2);
130*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_key_create",
131*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_key_create);
132*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_key_extract",
133*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_key_extract);
134*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_get_set_expr",
135*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_get_set_expr);
136*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_get_set_type",
137*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_get_set_type);
138*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_get_type_str",
139*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_get_type_str);
140*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_get_set_con",
141*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_get_set_con);
142*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_create", test_fcontext_create);
143*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_clone", test_fcontext_clone);
144*2d543d20SAndroid Build Coastguard Worker 
145*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_query", test_fcontext_query);
146*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_exists", test_fcontext_exists);
147*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_count", test_fcontext_count);
148*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_iterate", test_fcontext_iterate);
149*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_list", test_fcontext_list);
150*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_modify_del_local",
151*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_modify_del_local);
152*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_query_local",
153*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_query_local);
154*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_exists_local",
155*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_exists_local);
156*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_count_local",
157*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_count_local);
158*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_iterate_local",
159*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_iterate_local);
160*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "test_fcontext_list_local",
161*2d543d20SAndroid Build Coastguard Worker 		    test_fcontext_list_local);
162*2d543d20SAndroid Build Coastguard Worker 
163*2d543d20SAndroid Build Coastguard Worker 	return 0;
164*2d543d20SAndroid Build Coastguard Worker }
165*2d543d20SAndroid Build Coastguard Worker 
166*2d543d20SAndroid Build Coastguard Worker /* Helpers */
167*2d543d20SAndroid Build Coastguard Worker 
get_fcontext_nth(int idx)168*2d543d20SAndroid Build Coastguard Worker static semanage_fcontext_t *get_fcontext_nth(int idx)
169*2d543d20SAndroid Build Coastguard Worker {
170*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t **records;
171*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
172*2d543d20SAndroid Build Coastguard Worker 	unsigned int count;
173*2d543d20SAndroid Build Coastguard Worker 
174*2d543d20SAndroid Build Coastguard Worker 	if (idx == I_NULL)
175*2d543d20SAndroid Build Coastguard Worker 		return NULL;
176*2d543d20SAndroid Build Coastguard Worker 
177*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(semanage_fcontext_list(sh, &records, &count) >= 0);
178*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(count >= (unsigned int) idx + 1);
179*2d543d20SAndroid Build Coastguard Worker 
180*2d543d20SAndroid Build Coastguard Worker 	fcontext = records[idx];
181*2d543d20SAndroid Build Coastguard Worker 
182*2d543d20SAndroid Build Coastguard Worker 	for (unsigned int i = 0; i < count; i++)
183*2d543d20SAndroid Build Coastguard Worker 		if (i != (unsigned int) idx)
184*2d543d20SAndroid Build Coastguard Worker 			semanage_fcontext_free(records[i]);
185*2d543d20SAndroid Build Coastguard Worker 
186*2d543d20SAndroid Build Coastguard Worker 	free(records);
187*2d543d20SAndroid Build Coastguard Worker 
188*2d543d20SAndroid Build Coastguard Worker 	return fcontext;
189*2d543d20SAndroid Build Coastguard Worker }
190*2d543d20SAndroid Build Coastguard Worker 
get_fcontext_key_nth(int idx)191*2d543d20SAndroid Build Coastguard Worker static semanage_fcontext_key_t *get_fcontext_key_nth(int idx)
192*2d543d20SAndroid Build Coastguard Worker {
193*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key;
194*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
195*2d543d20SAndroid Build Coastguard Worker 
196*2d543d20SAndroid Build Coastguard Worker 	if (idx == I_NULL)
197*2d543d20SAndroid Build Coastguard Worker 		return NULL;
198*2d543d20SAndroid Build Coastguard Worker 
199*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(idx);
200*2d543d20SAndroid Build Coastguard Worker 
201*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(semanage_fcontext_key_extract(sh, fcontext, &key) >= 0);
202*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
203*2d543d20SAndroid Build Coastguard Worker 
204*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
205*2d543d20SAndroid Build Coastguard Worker 
206*2d543d20SAndroid Build Coastguard Worker 	return key;
207*2d543d20SAndroid Build Coastguard Worker }
208*2d543d20SAndroid Build Coastguard Worker 
add_local_fcontext(int fcontext_idx)209*2d543d20SAndroid Build Coastguard Worker static void add_local_fcontext(int fcontext_idx)
210*2d543d20SAndroid Build Coastguard Worker {
211*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
212*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key = NULL;
213*2d543d20SAndroid Build Coastguard Worker 
214*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(fcontext_idx != I_NULL);
215*2d543d20SAndroid Build Coastguard Worker 
216*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(fcontext_idx);
217*2d543d20SAndroid Build Coastguard Worker 
218*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(semanage_fcontext_key_extract(sh, fcontext, &key) >= 0);
219*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
220*2d543d20SAndroid Build Coastguard Worker 
221*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(semanage_fcontext_modify_local(sh, key, fcontext) >= 0);
222*2d543d20SAndroid Build Coastguard Worker 
223*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
224*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
225*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
226*2d543d20SAndroid Build Coastguard Worker }
227*2d543d20SAndroid Build Coastguard Worker 
delete_local_fcontext(int fcontext_idx)228*2d543d20SAndroid Build Coastguard Worker static void delete_local_fcontext(int fcontext_idx)
229*2d543d20SAndroid Build Coastguard Worker {
230*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key = NULL;
231*2d543d20SAndroid Build Coastguard Worker 
232*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(fcontext_idx != I_NULL);
233*2d543d20SAndroid Build Coastguard Worker 
234*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_nth(fcontext_idx);
235*2d543d20SAndroid Build Coastguard Worker 
236*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(semanage_fcontext_del_local(sh, key) >= 0);
237*2d543d20SAndroid Build Coastguard Worker 
238*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
239*2d543d20SAndroid Build Coastguard Worker }
240*2d543d20SAndroid Build Coastguard Worker 
get_fcontext_key_from_str(const char * str,int type)241*2d543d20SAndroid Build Coastguard Worker static semanage_fcontext_key_t *get_fcontext_key_from_str(const char *str, int type)
242*2d543d20SAndroid Build Coastguard Worker {
243*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key;
244*2d543d20SAndroid Build Coastguard Worker 	int res;
245*2d543d20SAndroid Build Coastguard Worker 
246*2d543d20SAndroid Build Coastguard Worker 	if (str == NULL)
247*2d543d20SAndroid Build Coastguard Worker 		return NULL;
248*2d543d20SAndroid Build Coastguard Worker 
249*2d543d20SAndroid Build Coastguard Worker 	res = semanage_fcontext_key_create(sh, str, type, &key);
250*2d543d20SAndroid Build Coastguard Worker 
251*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(res >= 0);
252*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL_FATAL(key);
253*2d543d20SAndroid Build Coastguard Worker 
254*2d543d20SAndroid Build Coastguard Worker 	return key;
255*2d543d20SAndroid Build Coastguard Worker }
256*2d543d20SAndroid Build Coastguard Worker 
257*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_compare */
test_fcontext_compare(void)258*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_compare(void)
259*2d543d20SAndroid Build Coastguard Worker {
260*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
261*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key1;
262*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key2;
263*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key3;
264*2d543d20SAndroid Build Coastguard Worker 
265*2d543d20SAndroid Build Coastguard Worker 	/* setup */
266*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
267*2d543d20SAndroid Build Coastguard Worker 
268*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(I_FIRST);
269*2d543d20SAndroid Build Coastguard Worker 
270*2d543d20SAndroid Build Coastguard Worker 	key1 = get_fcontext_key_nth(I_FIRST);
271*2d543d20SAndroid Build Coastguard Worker 	key2 = get_fcontext_key_nth(I_SECOND);
272*2d543d20SAndroid Build Coastguard Worker 	key3 = get_fcontext_key_nth(I_THIRD);
273*2d543d20SAndroid Build Coastguard Worker 
274*2d543d20SAndroid Build Coastguard Worker 	/* test */
275*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_compare(fcontext, key1) == 0);
276*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_compare(fcontext, key2) < 0);
277*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_compare(fcontext, key3) > 0);
278*2d543d20SAndroid Build Coastguard Worker 
279*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
280*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
281*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key1);
282*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key2);
283*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key3);
284*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
285*2d543d20SAndroid Build Coastguard Worker }
286*2d543d20SAndroid Build Coastguard Worker 
287*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_compare2 */
test_fcontext_compare2(void)288*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_compare2(void)
289*2d543d20SAndroid Build Coastguard Worker {
290*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
291*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext1;
292*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext2;
293*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext3;
294*2d543d20SAndroid Build Coastguard Worker 
295*2d543d20SAndroid Build Coastguard Worker 	/* setup */
296*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
297*2d543d20SAndroid Build Coastguard Worker 
298*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(I_FIRST);
299*2d543d20SAndroid Build Coastguard Worker 	fcontext1 = get_fcontext_nth(I_FIRST);
300*2d543d20SAndroid Build Coastguard Worker 	fcontext2 = get_fcontext_nth(I_SECOND);
301*2d543d20SAndroid Build Coastguard Worker 	fcontext3 = get_fcontext_nth(I_THIRD);
302*2d543d20SAndroid Build Coastguard Worker 
303*2d543d20SAndroid Build Coastguard Worker 	/* test */
304*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_compare2(fcontext, fcontext1) == 0);
305*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_compare2(fcontext, fcontext2) < 0);
306*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_compare2(fcontext, fcontext3) > 0);
307*2d543d20SAndroid Build Coastguard Worker 
308*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
309*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
310*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext1);
311*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext2);
312*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext3);
313*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
314*2d543d20SAndroid Build Coastguard Worker }
315*2d543d20SAndroid Build Coastguard Worker 
316*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_key_create */
test_fcontext_key_create(void)317*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_key_create(void)
318*2d543d20SAndroid Build Coastguard Worker {
319*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key = NULL;
320*2d543d20SAndroid Build Coastguard Worker 
321*2d543d20SAndroid Build Coastguard Worker 	/* setup */
322*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
323*2d543d20SAndroid Build Coastguard Worker 
324*2d543d20SAndroid Build Coastguard Worker 	/* test */
325*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_key_create(sh, "", SEMANAGE_FCONTEXT_ALL,
326*2d543d20SAndroid Build Coastguard Worker 					       &key) >= 0);
327*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(key);
328*2d543d20SAndroid Build Coastguard Worker 
329*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
330*2d543d20SAndroid Build Coastguard Worker 
331*2d543d20SAndroid Build Coastguard Worker 	key = NULL;
332*2d543d20SAndroid Build Coastguard Worker 
333*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_key_create(sh, "testfcontext",
334*2d543d20SAndroid Build Coastguard Worker 					     SEMANAGE_FCONTEXT_ALL, &key) >= 0);
335*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(key);
336*2d543d20SAndroid Build Coastguard Worker 
337*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
338*2d543d20SAndroid Build Coastguard Worker 
339*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
340*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
341*2d543d20SAndroid Build Coastguard Worker }
342*2d543d20SAndroid Build Coastguard Worker 
343*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_key_extract */
test_fcontext_key_extract(void)344*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_key_extract(void)
345*2d543d20SAndroid Build Coastguard Worker {
346*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
347*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key;
348*2d543d20SAndroid Build Coastguard Worker 
349*2d543d20SAndroid Build Coastguard Worker 	/* setup */
350*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
351*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(I_FIRST);
352*2d543d20SAndroid Build Coastguard Worker 
353*2d543d20SAndroid Build Coastguard Worker 	/* test */
354*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_key_extract(sh, fcontext, &key) >= 0);
355*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(key);
356*2d543d20SAndroid Build Coastguard Worker 
357*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
358*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
359*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
360*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
361*2d543d20SAndroid Build Coastguard Worker }
362*2d543d20SAndroid Build Coastguard Worker 
363*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_get_expr, semanage_fcontext_set_expr */
test_fcontext_get_set_expr(void)364*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_set_expr(void)
365*2d543d20SAndroid Build Coastguard Worker {
366*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
367*2d543d20SAndroid Build Coastguard Worker 	const char *expr = NULL;
368*2d543d20SAndroid Build Coastguard Worker 	const char *expr_exp = "/asdf";
369*2d543d20SAndroid Build Coastguard Worker 
370*2d543d20SAndroid Build Coastguard Worker 	/* setup */
371*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
372*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(I_FIRST);
373*2d543d20SAndroid Build Coastguard Worker 
374*2d543d20SAndroid Build Coastguard Worker 	/* test */
375*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_set_expr(sh, fcontext, expr_exp) >= 0);
376*2d543d20SAndroid Build Coastguard Worker 	expr = semanage_fcontext_get_expr(fcontext);
377*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(expr);
378*2d543d20SAndroid Build Coastguard Worker 	assert(expr);
379*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(expr, expr_exp);
380*2d543d20SAndroid Build Coastguard Worker 
381*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
382*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
383*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
384*2d543d20SAndroid Build Coastguard Worker }
385*2d543d20SAndroid Build Coastguard Worker 
386*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_get_type, semanage_fcontext_set_type */
test_fcontext_get_set_type(void)387*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_set_type(void)
388*2d543d20SAndroid Build Coastguard Worker {
389*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
390*2d543d20SAndroid Build Coastguard Worker 	int type_exp = SEMANAGE_FCONTEXT_SOCK;
391*2d543d20SAndroid Build Coastguard Worker 	int type;
392*2d543d20SAndroid Build Coastguard Worker 
393*2d543d20SAndroid Build Coastguard Worker 	/* setup */
394*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
395*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(I_FIRST);
396*2d543d20SAndroid Build Coastguard Worker 
397*2d543d20SAndroid Build Coastguard Worker 	/* test */
398*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_set_type(fcontext, type_exp);
399*2d543d20SAndroid Build Coastguard Worker 	type = semanage_fcontext_get_type(fcontext);
400*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(type == type_exp);
401*2d543d20SAndroid Build Coastguard Worker 
402*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
403*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
404*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
405*2d543d20SAndroid Build Coastguard Worker }
406*2d543d20SAndroid Build Coastguard Worker 
407*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_get_type_str */
helper_fcontext_get_type_str(int type,const char * exp_str)408*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_get_type_str(int type, const char *exp_str)
409*2d543d20SAndroid Build Coastguard Worker {
410*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_fcontext_get_type_str(type), exp_str);
411*2d543d20SAndroid Build Coastguard Worker }
412*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_get_type_str(void)413*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_type_str(void)
414*2d543d20SAndroid Build Coastguard Worker {
415*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_ALL, "all files");
416*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_REG, "regular file");
417*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_DIR, "directory");
418*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_CHAR,
419*2d543d20SAndroid Build Coastguard Worker 				     "character device");
420*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_BLOCK, "block device");
421*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_SOCK, "socket");
422*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_LINK, "symbolic link");
423*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_PIPE, "named pipe");
424*2d543d20SAndroid Build Coastguard Worker 
425*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_ALL - 1, "????");
426*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_type_str(SEMANAGE_FCONTEXT_PIPE + 1, "????");
427*2d543d20SAndroid Build Coastguard Worker }
428*2d543d20SAndroid Build Coastguard Worker 
429*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_get_con, semanage_fcontext_set_con */
helper_fcontext_get_set_con(level_t level,int fcontext_idx,const char * con_str)430*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_get_set_con(level_t level, int fcontext_idx,
431*2d543d20SAndroid Build Coastguard Worker 				 const char *con_str)
432*2d543d20SAndroid Build Coastguard Worker {
433*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
434*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *con = NULL;
435*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *new_con = NULL;
436*2d543d20SAndroid Build Coastguard Worker 
437*2d543d20SAndroid Build Coastguard Worker 	/* setup */
438*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
439*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(fcontext_idx);
440*2d543d20SAndroid Build Coastguard Worker 
441*2d543d20SAndroid Build Coastguard Worker 	if (con_str != NULL) {
442*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(semanage_context_from_string(sh, con_str, &con) >= 0);
443*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_PTR_NOT_NULL(con);
444*2d543d20SAndroid Build Coastguard Worker 	} else {
445*2d543d20SAndroid Build Coastguard Worker 		con = NULL;
446*2d543d20SAndroid Build Coastguard Worker 	}
447*2d543d20SAndroid Build Coastguard Worker 
448*2d543d20SAndroid Build Coastguard Worker 	/* test */
449*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_set_con(sh, fcontext, con) >= 0);
450*2d543d20SAndroid Build Coastguard Worker 	new_con = semanage_fcontext_get_con(fcontext);
451*2d543d20SAndroid Build Coastguard Worker 
452*2d543d20SAndroid Build Coastguard Worker 	if (con_str != NULL) {
453*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_CONTEXT_EQUAL(con, new_con);
454*2d543d20SAndroid Build Coastguard Worker 	} else {
455*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_PTR_NULL(new_con);
456*2d543d20SAndroid Build Coastguard Worker 	}
457*2d543d20SAndroid Build Coastguard Worker 
458*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
459*2d543d20SAndroid Build Coastguard Worker 	semanage_context_free(con);
460*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
461*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
462*2d543d20SAndroid Build Coastguard Worker }
463*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_get_set_con(void)464*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_get_set_con(void)
465*2d543d20SAndroid Build Coastguard Worker {
466*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_set_con(SH_CONNECT, I_FIRST, NULL);
467*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_set_con(SH_CONNECT, I_FIRST,
468*2d543d20SAndroid Build Coastguard Worker 				    "user_u:role_r:type_t:s0");
469*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_set_con(SH_CONNECT, I_SECOND,
470*2d543d20SAndroid Build Coastguard Worker 				    "user_u:role_r:type_t:s0");
471*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_set_con(SH_TRANS, I_FIRST, NULL);
472*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_set_con(SH_TRANS, I_FIRST,
473*2d543d20SAndroid Build Coastguard Worker 				    "user_u:role_r:type_t:s0");
474*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_get_set_con(SH_TRANS, I_SECOND,
475*2d543d20SAndroid Build Coastguard Worker 				    "user_u:role_r:type_t:s0");
476*2d543d20SAndroid Build Coastguard Worker }
477*2d543d20SAndroid Build Coastguard Worker 
478*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_create */
helper_fcontext_create(level_t level)479*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_create(level_t level)
480*2d543d20SAndroid Build Coastguard Worker {
481*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
482*2d543d20SAndroid Build Coastguard Worker 
483*2d543d20SAndroid Build Coastguard Worker 	/* setup */
484*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
485*2d543d20SAndroid Build Coastguard Worker 
486*2d543d20SAndroid Build Coastguard Worker 	/* test */
487*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_create(sh, &fcontext) >= 0);
488*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NULL(semanage_fcontext_get_expr(fcontext));
489*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_get_type(fcontext)
490*2d543d20SAndroid Build Coastguard Worker 		  == SEMANAGE_FCONTEXT_ALL);
491*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NULL(semanage_fcontext_get_con(fcontext));
492*2d543d20SAndroid Build Coastguard Worker 
493*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
494*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
495*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
496*2d543d20SAndroid Build Coastguard Worker }
497*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_create(void)498*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_create(void)
499*2d543d20SAndroid Build Coastguard Worker {
500*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_create(SH_NULL);
501*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_create(SH_HANDLE);
502*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_create(SH_CONNECT);
503*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_create(SH_TRANS);
504*2d543d20SAndroid Build Coastguard Worker }
505*2d543d20SAndroid Build Coastguard Worker 
506*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_clone */
helper_fcontext_clone(level_t level,int fcontext_idx)507*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_clone(level_t level, int fcontext_idx)
508*2d543d20SAndroid Build Coastguard Worker {
509*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
510*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext_clone;
511*2d543d20SAndroid Build Coastguard Worker 	const char *expr;
512*2d543d20SAndroid Build Coastguard Worker 	const char *expr_clone;
513*2d543d20SAndroid Build Coastguard Worker 	int type;
514*2d543d20SAndroid Build Coastguard Worker 	int type_clone;
515*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *con;
516*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *con_clone;
517*2d543d20SAndroid Build Coastguard Worker 
518*2d543d20SAndroid Build Coastguard Worker 	/* setup */
519*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
520*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(fcontext_idx);
521*2d543d20SAndroid Build Coastguard Worker 
522*2d543d20SAndroid Build Coastguard Worker 	/* test */
523*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_clone(sh, fcontext, &fcontext_clone) >= 0);
524*2d543d20SAndroid Build Coastguard Worker 
525*2d543d20SAndroid Build Coastguard Worker 	expr = semanage_fcontext_get_expr(fcontext);
526*2d543d20SAndroid Build Coastguard Worker 	expr_clone = semanage_fcontext_get_expr(fcontext_clone);
527*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(expr, expr_clone);
528*2d543d20SAndroid Build Coastguard Worker 
529*2d543d20SAndroid Build Coastguard Worker 	type = semanage_fcontext_get_type(fcontext);
530*2d543d20SAndroid Build Coastguard Worker 	type_clone = semanage_fcontext_get_type(fcontext_clone);
531*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_EQUAL(type, type_clone);
532*2d543d20SAndroid Build Coastguard Worker 
533*2d543d20SAndroid Build Coastguard Worker 	con = semanage_fcontext_get_con(fcontext);
534*2d543d20SAndroid Build Coastguard Worker 	con_clone = semanage_fcontext_get_con(fcontext_clone);
535*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_CONTEXT_EQUAL(con, con_clone);
536*2d543d20SAndroid Build Coastguard Worker 
537*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
538*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
539*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext_clone);
540*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
541*2d543d20SAndroid Build Coastguard Worker }
542*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_clone(void)543*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_clone(void)
544*2d543d20SAndroid Build Coastguard Worker {
545*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_clone(SH_CONNECT, I_FIRST);
546*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_clone(SH_CONNECT, I_SECOND);
547*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_clone(SH_TRANS, I_FIRST);
548*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_clone(SH_TRANS, I_SECOND);
549*2d543d20SAndroid Build Coastguard Worker }
550*2d543d20SAndroid Build Coastguard Worker 
551*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_query */
helper_fcontext_query(level_t level,const char * fcontext_expr,int fcontext_type,int exp_res)552*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_query(level_t level, const char *fcontext_expr,
553*2d543d20SAndroid Build Coastguard Worker 			   int fcontext_type, int exp_res)
554*2d543d20SAndroid Build Coastguard Worker {
555*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key;
556*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *resp = (void *) 42;
557*2d543d20SAndroid Build Coastguard Worker 	int res;
558*2d543d20SAndroid Build Coastguard Worker 
559*2d543d20SAndroid Build Coastguard Worker 	/* setup */
560*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
561*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_from_str(fcontext_expr, fcontext_type);
562*2d543d20SAndroid Build Coastguard Worker 
563*2d543d20SAndroid Build Coastguard Worker 	/* test */
564*2d543d20SAndroid Build Coastguard Worker 	res = semanage_fcontext_query(sh, key, &resp);
565*2d543d20SAndroid Build Coastguard Worker 
566*2d543d20SAndroid Build Coastguard Worker 	if (exp_res >= 0) {
567*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(res >= 0);
568*2d543d20SAndroid Build Coastguard Worker 		const char *expr = semanage_fcontext_get_expr(resp);
569*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_STRING_EQUAL(expr, fcontext_expr);
570*2d543d20SAndroid Build Coastguard Worker 		semanage_fcontext_free(resp);
571*2d543d20SAndroid Build Coastguard Worker 	} else {
572*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(res < 0);
573*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(resp == (void *) 42);
574*2d543d20SAndroid Build Coastguard Worker 	}
575*2d543d20SAndroid Build Coastguard Worker 
576*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
577*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
578*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
579*2d543d20SAndroid Build Coastguard Worker }
580*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_query(void)581*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_query(void)
582*2d543d20SAndroid Build Coastguard Worker {
583*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_CONNECT, FCONTEXT_NONEXISTENT_EXPR,
584*2d543d20SAndroid Build Coastguard Worker 			      FCONTEXT_NONEXISTENT_TYPE, -1);
585*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_CONNECT, FCONTEXT2_EXPR, FCONTEXT1_TYPE, -1);
586*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_CONNECT, FCONTEXT1_EXPR, FCONTEXT1_TYPE, 1);
587*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_CONNECT, FCONTEXT2_EXPR, FCONTEXT2_TYPE, 1);
588*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_TRANS, FCONTEXT_NONEXISTENT_EXPR,
589*2d543d20SAndroid Build Coastguard Worker 			      FCONTEXT_NONEXISTENT_TYPE, -1);
590*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_TRANS, FCONTEXT2_EXPR, FCONTEXT1_TYPE, -1);
591*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_TRANS, FCONTEXT1_EXPR, FCONTEXT1_TYPE, 1);
592*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_query(SH_TRANS, FCONTEXT2_EXPR, FCONTEXT2_TYPE, 1);
593*2d543d20SAndroid Build Coastguard Worker }
594*2d543d20SAndroid Build Coastguard Worker 
595*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_exists */
helper_fcontext_exists(level_t level,const char * fcontext_expr,int fcontext_type,int exp_resp)596*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_exists(level_t level, const char *fcontext_expr,
597*2d543d20SAndroid Build Coastguard Worker 			    int fcontext_type, int exp_resp)
598*2d543d20SAndroid Build Coastguard Worker {
599*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key;
600*2d543d20SAndroid Build Coastguard Worker 	int resp;
601*2d543d20SAndroid Build Coastguard Worker 
602*2d543d20SAndroid Build Coastguard Worker 	/* setup */
603*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
604*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_from_str(fcontext_expr, fcontext_type);
605*2d543d20SAndroid Build Coastguard Worker 
606*2d543d20SAndroid Build Coastguard Worker 	/* test */
607*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_exists(sh, key, &resp) >= 0);
608*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == exp_resp);
609*2d543d20SAndroid Build Coastguard Worker 
610*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
611*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
612*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
613*2d543d20SAndroid Build Coastguard Worker }
614*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_exists(void)615*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_exists(void)
616*2d543d20SAndroid Build Coastguard Worker {
617*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_CONNECT, FCONTEXT_NONEXISTENT_EXPR,
618*2d543d20SAndroid Build Coastguard Worker 			       FCONTEXT_NONEXISTENT_TYPE, 0);
619*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_CONNECT, FCONTEXT2_EXPR, FCONTEXT1_TYPE, 0);
620*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_CONNECT, FCONTEXT1_EXPR, FCONTEXT1_TYPE, 1);
621*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_CONNECT, FCONTEXT2_EXPR, FCONTEXT2_TYPE, 1);
622*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_TRANS, FCONTEXT_NONEXISTENT_EXPR,
623*2d543d20SAndroid Build Coastguard Worker 			       FCONTEXT_NONEXISTENT_TYPE, 0);
624*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_TRANS, FCONTEXT2_EXPR, FCONTEXT1_TYPE, 0);
625*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_TRANS, FCONTEXT1_EXPR, FCONTEXT1_TYPE, 1);
626*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_exists(SH_TRANS, FCONTEXT2_EXPR, FCONTEXT2_TYPE, 1);
627*2d543d20SAndroid Build Coastguard Worker }
628*2d543d20SAndroid Build Coastguard Worker 
629*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_count */
test_fcontext_count(void)630*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_count(void)
631*2d543d20SAndroid Build Coastguard Worker {
632*2d543d20SAndroid Build Coastguard Worker 	unsigned int resp;
633*2d543d20SAndroid Build Coastguard Worker 
634*2d543d20SAndroid Build Coastguard Worker 	/* handle */
635*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_HANDLE);
636*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count(sh, &resp) < 0);
637*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count(sh, NULL) < 0);
638*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_HANDLE);
639*2d543d20SAndroid Build Coastguard Worker 
640*2d543d20SAndroid Build Coastguard Worker 	/* connect */
641*2d543d20SAndroid Build Coastguard Worker 	resp = 0;
642*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
643*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count(sh, &resp) >= 0);
644*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == FCONTEXTS_COUNT);
645*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
646*2d543d20SAndroid Build Coastguard Worker 
647*2d543d20SAndroid Build Coastguard Worker 	/* trans */
648*2d543d20SAndroid Build Coastguard Worker 	resp = 0;
649*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_TRANS);
650*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count(sh, &resp) >= 0);
651*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == FCONTEXTS_COUNT);
652*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_TRANS);
653*2d543d20SAndroid Build Coastguard Worker }
654*2d543d20SAndroid Build Coastguard Worker 
655*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_iterate */
656*2d543d20SAndroid Build Coastguard Worker unsigned int counter_fcontext_iterate = 0;
657*2d543d20SAndroid Build Coastguard Worker 
handler_fcontext_iterate(const semanage_fcontext_t * record,void * varg)658*2d543d20SAndroid Build Coastguard Worker static int handler_fcontext_iterate(const semanage_fcontext_t *record, void *varg)
659*2d543d20SAndroid Build Coastguard Worker {
660*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(record);
661*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate++;
662*2d543d20SAndroid Build Coastguard Worker 	return 0;
663*2d543d20SAndroid Build Coastguard Worker }
664*2d543d20SAndroid Build Coastguard Worker 
helper_fcontext_iterate_invalid(void)665*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_iterate_invalid(void)
666*2d543d20SAndroid Build Coastguard Worker {
667*2d543d20SAndroid Build Coastguard Worker 	/* setup */
668*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_HANDLE);
669*2d543d20SAndroid Build Coastguard Worker 
670*2d543d20SAndroid Build Coastguard Worker 	/* test */
671*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate(sh, &handler_fcontext_iterate,
672*2d543d20SAndroid Build Coastguard Worker 				            NULL) < 0);
673*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate(sh, NULL, NULL) < 0);
674*2d543d20SAndroid Build Coastguard Worker 
675*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
676*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_HANDLE);
677*2d543d20SAndroid Build Coastguard Worker }
678*2d543d20SAndroid Build Coastguard Worker 
helper_fcontext_iterate(level_t level)679*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_iterate(level_t level)
680*2d543d20SAndroid Build Coastguard Worker {
681*2d543d20SAndroid Build Coastguard Worker 	/* setup */
682*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
683*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate = 0;
684*2d543d20SAndroid Build Coastguard Worker 
685*2d543d20SAndroid Build Coastguard Worker 	/* test */
686*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate(sh, &handler_fcontext_iterate,
687*2d543d20SAndroid Build Coastguard Worker 					    NULL) >= 0);
688*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(counter_fcontext_iterate == FCONTEXTS_COUNT);
689*2d543d20SAndroid Build Coastguard Worker 
690*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
691*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
692*2d543d20SAndroid Build Coastguard Worker }
693*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_iterate(void)694*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_iterate(void)
695*2d543d20SAndroid Build Coastguard Worker {
696*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_iterate_invalid();
697*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_iterate(SH_CONNECT);
698*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_iterate(SH_TRANS);
699*2d543d20SAndroid Build Coastguard Worker }
700*2d543d20SAndroid Build Coastguard Worker 
701*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_list */
helper_fcontext_list_invalid(void)702*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_list_invalid(void)
703*2d543d20SAndroid Build Coastguard Worker {
704*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t **records;
705*2d543d20SAndroid Build Coastguard Worker 	unsigned int count;
706*2d543d20SAndroid Build Coastguard Worker 
707*2d543d20SAndroid Build Coastguard Worker 	/* setup */
708*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_HANDLE);
709*2d543d20SAndroid Build Coastguard Worker 
710*2d543d20SAndroid Build Coastguard Worker 	/* test */
711*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list(sh, &records, &count) < 0);
712*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list(sh, NULL, &count) < 0);
713*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list(sh, &records, NULL) < 0);
714*2d543d20SAndroid Build Coastguard Worker 
715*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
716*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_HANDLE);
717*2d543d20SAndroid Build Coastguard Worker }
718*2d543d20SAndroid Build Coastguard Worker 
helper_fcontext_list(level_t level)719*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_list(level_t level)
720*2d543d20SAndroid Build Coastguard Worker {
721*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t **records;
722*2d543d20SAndroid Build Coastguard Worker 	unsigned int count;
723*2d543d20SAndroid Build Coastguard Worker 
724*2d543d20SAndroid Build Coastguard Worker 	/* setup */
725*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
726*2d543d20SAndroid Build Coastguard Worker 
727*2d543d20SAndroid Build Coastguard Worker 	/* test */
728*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list(sh, &records, &count) >= 0);
729*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(count == FCONTEXTS_COUNT);
730*2d543d20SAndroid Build Coastguard Worker 
731*2d543d20SAndroid Build Coastguard Worker 	for (unsigned int i = 0; i < count; i++)
732*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_PTR_NOT_NULL(records[i]);
733*2d543d20SAndroid Build Coastguard Worker 
734*2d543d20SAndroid Build Coastguard Worker 	for (unsigned int i = 0; i < count; i++)
735*2d543d20SAndroid Build Coastguard Worker 		semanage_fcontext_free(records[i]);
736*2d543d20SAndroid Build Coastguard Worker 
737*2d543d20SAndroid Build Coastguard Worker 	free(records);
738*2d543d20SAndroid Build Coastguard Worker 
739*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
740*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
741*2d543d20SAndroid Build Coastguard Worker }
742*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_list(void)743*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_list(void)
744*2d543d20SAndroid Build Coastguard Worker {
745*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_list_invalid();
746*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_list(SH_CONNECT);
747*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_list(SH_TRANS);
748*2d543d20SAndroid Build Coastguard Worker }
749*2d543d20SAndroid Build Coastguard Worker 
750*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_modify_local, semanage_fcontext_del_local */
helper_fcontext_modify_del_local(level_t level,int fcontext_idx,const char * con_str,int exp_res)751*2d543d20SAndroid Build Coastguard Worker static void helper_fcontext_modify_del_local(level_t level, int fcontext_idx,
752*2d543d20SAndroid Build Coastguard Worker 				      const char *con_str, int exp_res)
753*2d543d20SAndroid Build Coastguard Worker {
754*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext;
755*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *fcontext_local = NULL;
756*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key = NULL;
757*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *con = NULL;
758*2d543d20SAndroid Build Coastguard Worker 	int res;
759*2d543d20SAndroid Build Coastguard Worker 
760*2d543d20SAndroid Build Coastguard Worker 	/* setup */
761*2d543d20SAndroid Build Coastguard Worker 	setup_handle(level);
762*2d543d20SAndroid Build Coastguard Worker 	fcontext = get_fcontext_nth(fcontext_idx);
763*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_key_extract(sh, fcontext, &key) >= 0);
764*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(key);
765*2d543d20SAndroid Build Coastguard Worker 
766*2d543d20SAndroid Build Coastguard Worker 	if (con_str != NULL) {
767*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(semanage_context_from_string(sh, con_str, &con) >= 0);
768*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_PTR_NOT_NULL(con);
769*2d543d20SAndroid Build Coastguard Worker 	} else {
770*2d543d20SAndroid Build Coastguard Worker 		con = NULL;
771*2d543d20SAndroid Build Coastguard Worker 	}
772*2d543d20SAndroid Build Coastguard Worker 
773*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_set_con(sh, fcontext, con) >= 0);
774*2d543d20SAndroid Build Coastguard Worker 
775*2d543d20SAndroid Build Coastguard Worker 	/* test */
776*2d543d20SAndroid Build Coastguard Worker 	res = semanage_fcontext_modify_local(sh, key, fcontext);
777*2d543d20SAndroid Build Coastguard Worker 
778*2d543d20SAndroid Build Coastguard Worker 	if (exp_res >= 0) {
779*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(res >= 0);
780*2d543d20SAndroid Build Coastguard Worker 
781*2d543d20SAndroid Build Coastguard Worker 		if (level == SH_TRANS) {
782*2d543d20SAndroid Build Coastguard Worker 			helper_commit();
783*2d543d20SAndroid Build Coastguard Worker 			helper_begin_transaction();
784*2d543d20SAndroid Build Coastguard Worker 		}
785*2d543d20SAndroid Build Coastguard Worker 
786*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(semanage_fcontext_query_local(sh, key,
787*2d543d20SAndroid Build Coastguard Worker 					                &fcontext_local) >= 0);
788*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(semanage_fcontext_compare2(fcontext_local,
789*2d543d20SAndroid Build Coastguard Worker 						     fcontext) == 0);
790*2d543d20SAndroid Build Coastguard Worker 		semanage_fcontext_free(fcontext_local);
791*2d543d20SAndroid Build Coastguard Worker 
792*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(semanage_fcontext_del_local(sh, key) >= 0);
793*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(semanage_fcontext_query_local(sh, key,
794*2d543d20SAndroid Build Coastguard Worker 					                &fcontext_local) < 0);
795*2d543d20SAndroid Build Coastguard Worker 	} else {
796*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(res < 0);
797*2d543d20SAndroid Build Coastguard Worker 	}
798*2d543d20SAndroid Build Coastguard Worker 
799*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
800*2d543d20SAndroid Build Coastguard Worker 	semanage_context_free(con);
801*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
802*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(fcontext);
803*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(level);
804*2d543d20SAndroid Build Coastguard Worker }
805*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_modify_del_local(void)806*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_modify_del_local(void)
807*2d543d20SAndroid Build Coastguard Worker {
808*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_modify_del_local(SH_CONNECT, I_FIRST,
809*2d543d20SAndroid Build Coastguard Worker 					 "system_u:object_r:tmp_t:s0", -1);
810*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_modify_del_local(SH_CONNECT, I_SECOND,
811*2d543d20SAndroid Build Coastguard Worker 					 "system_u:object_r:tmp_t:s0", -1);
812*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_modify_del_local(SH_TRANS, I_FIRST,
813*2d543d20SAndroid Build Coastguard Worker 					 "system_u:object_r:tmp_t:s0", 1);
814*2d543d20SAndroid Build Coastguard Worker 	helper_fcontext_modify_del_local(SH_TRANS, I_SECOND,
815*2d543d20SAndroid Build Coastguard Worker 					 "system_u:object_r:tmp_t:s0", 1);
816*2d543d20SAndroid Build Coastguard Worker }
817*2d543d20SAndroid Build Coastguard Worker 
818*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_query_local */
test_fcontext_query_local(void)819*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_query_local(void)
820*2d543d20SAndroid Build Coastguard Worker {
821*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key = NULL;
822*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t *resp = NULL;
823*2d543d20SAndroid Build Coastguard Worker 
824*2d543d20SAndroid Build Coastguard Worker 	/* connect */
825*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
826*2d543d20SAndroid Build Coastguard Worker 
827*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_nth(I_FIRST);
828*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_query_local(sh, key, &resp) < 0);
829*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NULL(resp);
830*2d543d20SAndroid Build Coastguard Worker 
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 	semanage_fcontext_key_free(key);
837*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_nth(I_FIRST);
838*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_query_local(sh, key, &resp) < 0);
839*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NULL(resp);
840*2d543d20SAndroid Build Coastguard Worker 
841*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_FIRST);
842*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_query_local(sh, key, &resp) >= 0);
843*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(resp);
844*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(resp);
845*2d543d20SAndroid Build Coastguard Worker 	resp = NULL;
846*2d543d20SAndroid Build Coastguard Worker 
847*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
848*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_nth(I_SECOND);
849*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_SECOND);
850*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_query_local(sh, key, &resp) >= 0);
851*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(resp);
852*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(resp);
853*2d543d20SAndroid Build Coastguard Worker 	resp = NULL;
854*2d543d20SAndroid Build Coastguard Worker 
855*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
856*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
857*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_FIRST);
858*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_SECOND);
859*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_TRANS);
860*2d543d20SAndroid Build Coastguard Worker }
861*2d543d20SAndroid Build Coastguard Worker 
862*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_exists_local */
test_fcontext_exists_local(void)863*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_exists_local(void)
864*2d543d20SAndroid Build Coastguard Worker {
865*2d543d20SAndroid Build Coastguard Worker 	int resp = -1;
866*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_t *key;
867*2d543d20SAndroid Build Coastguard Worker 
868*2d543d20SAndroid Build Coastguard Worker 	/* setup */
869*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_TRANS);
870*2d543d20SAndroid Build Coastguard Worker 	key = get_fcontext_key_nth(I_FIRST);
871*2d543d20SAndroid Build Coastguard Worker 
872*2d543d20SAndroid Build Coastguard Worker 	/* test */
873*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_exists_local(sh, key, &resp) >= 0);
874*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 0);
875*2d543d20SAndroid Build Coastguard Worker 
876*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_FIRST);
877*2d543d20SAndroid Build Coastguard Worker 	resp = -1;
878*2d543d20SAndroid Build Coastguard Worker 
879*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_exists_local(sh, key, &resp) >= 0);
880*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 1);
881*2d543d20SAndroid Build Coastguard Worker 
882*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_FIRST);
883*2d543d20SAndroid Build Coastguard Worker 	resp = -1;
884*2d543d20SAndroid Build Coastguard Worker 
885*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_exists_local(sh, key, &resp) >= 0);
886*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 0);
887*2d543d20SAndroid Build Coastguard Worker 
888*2d543d20SAndroid Build Coastguard Worker 	resp = -1;
889*2d543d20SAndroid Build Coastguard Worker 
890*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_exists_local(sh, NULL, &resp) >= 0);
891*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 0);
892*2d543d20SAndroid Build Coastguard Worker 
893*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
894*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_key_free(key);
895*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_TRANS);
896*2d543d20SAndroid Build Coastguard Worker }
897*2d543d20SAndroid Build Coastguard Worker 
898*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_count_local */
test_fcontext_count_local(void)899*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_count_local(void)
900*2d543d20SAndroid Build Coastguard Worker {
901*2d543d20SAndroid Build Coastguard Worker 	unsigned int resp;
902*2d543d20SAndroid Build Coastguard Worker 
903*2d543d20SAndroid Build Coastguard Worker 	/* handle */
904*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_HANDLE);
905*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count_local(sh, &resp) < 0);
906*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_HANDLE);
907*2d543d20SAndroid Build Coastguard Worker 
908*2d543d20SAndroid Build Coastguard Worker 	/* connect */
909*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
910*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count_local(sh, &resp) >= 0);
911*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 0);
912*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
913*2d543d20SAndroid Build Coastguard Worker 
914*2d543d20SAndroid Build Coastguard Worker 	/* transaction */
915*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_TRANS);
916*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count_local(sh, &resp) >= 0);
917*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 0);
918*2d543d20SAndroid Build Coastguard Worker 
919*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_FIRST);
920*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count_local(sh, &resp) >= 0);
921*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 1);
922*2d543d20SAndroid Build Coastguard Worker 
923*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_SECOND);
924*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count_local(sh, &resp) >= 0);
925*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 2);
926*2d543d20SAndroid Build Coastguard Worker 
927*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_SECOND);
928*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_count_local(sh, &resp) >= 0);
929*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(resp == 1);
930*2d543d20SAndroid Build Coastguard Worker 
931*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
932*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_FIRST);
933*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_TRANS);
934*2d543d20SAndroid Build Coastguard Worker }
935*2d543d20SAndroid Build Coastguard Worker 
936*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_iterate_local */
937*2d543d20SAndroid Build Coastguard Worker unsigned int counter_fcontext_iterate_local = 0;
938*2d543d20SAndroid Build Coastguard Worker 
handler_fcontext_iterate_local(const semanage_fcontext_t * record,void * varg)939*2d543d20SAndroid Build Coastguard Worker static int handler_fcontext_iterate_local(const semanage_fcontext_t *record,
940*2d543d20SAndroid Build Coastguard Worker 				   void *varg)
941*2d543d20SAndroid Build Coastguard Worker {
942*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(record);
943*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate_local++;
944*2d543d20SAndroid Build Coastguard Worker 	return 0;
945*2d543d20SAndroid Build Coastguard Worker }
946*2d543d20SAndroid Build Coastguard Worker 
test_fcontext_iterate_local(void)947*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_iterate_local(void)
948*2d543d20SAndroid Build Coastguard Worker {
949*2d543d20SAndroid Build Coastguard Worker 	/* handle */
950*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_HANDLE);
951*2d543d20SAndroid Build Coastguard Worker 
952*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh,
953*2d543d20SAndroid Build Coastguard Worker 				    &handler_fcontext_iterate_local, NULL) < 0);
954*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh, NULL, NULL) < 0);
955*2d543d20SAndroid Build Coastguard Worker 
956*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_HANDLE);
957*2d543d20SAndroid Build Coastguard Worker 
958*2d543d20SAndroid Build Coastguard Worker 	/* connect */
959*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
960*2d543d20SAndroid Build Coastguard Worker 
961*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate_local = 0;
962*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh,
963*2d543d20SAndroid Build Coastguard Worker 				   &handler_fcontext_iterate_local, NULL) >= 0);
964*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(counter_fcontext_iterate_local == 0);
965*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh, NULL, NULL) >= 0);
966*2d543d20SAndroid Build Coastguard Worker 
967*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
968*2d543d20SAndroid Build Coastguard Worker 
969*2d543d20SAndroid Build Coastguard Worker 	/* transaction */
970*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_TRANS);
971*2d543d20SAndroid Build Coastguard Worker 
972*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate_local = 0;
973*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh,
974*2d543d20SAndroid Build Coastguard Worker 				   &handler_fcontext_iterate_local, NULL) >= 0);
975*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(counter_fcontext_iterate_local == 0);
976*2d543d20SAndroid Build Coastguard Worker 
977*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_FIRST);
978*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate_local = 0;
979*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh,
980*2d543d20SAndroid Build Coastguard Worker 				   &handler_fcontext_iterate_local, NULL) >= 0);
981*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(counter_fcontext_iterate_local == 1);
982*2d543d20SAndroid Build Coastguard Worker 
983*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_SECOND);
984*2d543d20SAndroid Build Coastguard Worker 	counter_fcontext_iterate_local = 0;
985*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_iterate_local(sh,
986*2d543d20SAndroid Build Coastguard Worker 				   &handler_fcontext_iterate_local, NULL) >= 0);
987*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(counter_fcontext_iterate_local == 2);
988*2d543d20SAndroid Build Coastguard Worker 
989*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
990*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_FIRST);
991*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_SECOND);
992*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_TRANS);
993*2d543d20SAndroid Build Coastguard Worker }
994*2d543d20SAndroid Build Coastguard Worker 
995*2d543d20SAndroid Build Coastguard Worker /* Function semanage_fcontext_list_local */
test_fcontext_list_local(void)996*2d543d20SAndroid Build Coastguard Worker static void test_fcontext_list_local(void)
997*2d543d20SAndroid Build Coastguard Worker {
998*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_t **records;
999*2d543d20SAndroid Build Coastguard Worker 	unsigned int count;
1000*2d543d20SAndroid Build Coastguard Worker 
1001*2d543d20SAndroid Build Coastguard Worker 	/* handle */
1002*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_HANDLE);
1003*2d543d20SAndroid Build Coastguard Worker 
1004*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, &records, &count) < 0);
1005*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, NULL, &count) < 0);
1006*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, &records, NULL) < 0);
1007*2d543d20SAndroid Build Coastguard Worker 
1008*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_HANDLE);
1009*2d543d20SAndroid Build Coastguard Worker 
1010*2d543d20SAndroid Build Coastguard Worker 	/* connect */
1011*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
1012*2d543d20SAndroid Build Coastguard Worker 
1013*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, &records, &count) >= 0);
1014*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(count == 0);
1015*2d543d20SAndroid Build Coastguard Worker 
1016*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
1017*2d543d20SAndroid Build Coastguard Worker 
1018*2d543d20SAndroid Build Coastguard Worker 	/* transaction */
1019*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_TRANS);
1020*2d543d20SAndroid Build Coastguard Worker 
1021*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, &records, &count) >= 0);
1022*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(count == 0);
1023*2d543d20SAndroid Build Coastguard Worker 
1024*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_FIRST);
1025*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, &records, &count) >= 0);
1026*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(count == 1);
1027*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(records[0]);
1028*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(records[0]);
1029*2d543d20SAndroid Build Coastguard Worker 	free(records);
1030*2d543d20SAndroid Build Coastguard Worker 
1031*2d543d20SAndroid Build Coastguard Worker 	add_local_fcontext(I_SECOND);
1032*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_fcontext_list_local(sh, &records, &count) >= 0);
1033*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(count == 2);
1034*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(records[0]);
1035*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(records[1]);
1036*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(records[0]);
1037*2d543d20SAndroid Build Coastguard Worker 	semanage_fcontext_free(records[1]);
1038*2d543d20SAndroid Build Coastguard Worker 	free(records);
1039*2d543d20SAndroid Build Coastguard Worker 
1040*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
1041*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_FIRST);
1042*2d543d20SAndroid Build Coastguard Worker 	delete_local_fcontext(I_SECOND);
1043*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_TRANS);
1044*2d543d20SAndroid Build Coastguard Worker }
1045