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_ibendport.h"
23*2d543d20SAndroid Build Coastguard Worker
24*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_COUNT 3
25*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_1_NAME "mlx4_0"
26*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_1_PORT 1
27*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_1_CON "system_u:object_r:first_ibendport_t:s0"
28*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_2_NAME "mlx4_1"
29*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_2_PORT 2
30*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_2_CON "system_u:object_r:second_ibendport_second_t:s0"
31*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_3_NAME "mlx4_1"
32*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_3_PORT 3
33*2d543d20SAndroid Build Coastguard Worker #define IBENDPORT_3_CON "system_u:object_r:third_ibendport_second_t:s0"
34*2d543d20SAndroid Build Coastguard Worker
35*2d543d20SAndroid Build Coastguard Worker /* ibendports_policy.h */
36*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_query(void);
37*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_exists(void);
38*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_count(void);
39*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_iterate(void);
40*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_list(void);
41*2d543d20SAndroid Build Coastguard Worker
42*2d543d20SAndroid Build Coastguard Worker /* ibendports_local.h */
43*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_modify_del_query_local(void);
44*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_exists_local(void);
45*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_count_local(void);
46*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_iterate_local(void);
47*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_list_local(void);
48*2d543d20SAndroid Build Coastguard Worker
49*2d543d20SAndroid Build Coastguard Worker extern semanage_handle_t *sh;
50*2d543d20SAndroid Build Coastguard Worker
ibendport_test_init(void)51*2d543d20SAndroid Build Coastguard Worker int ibendport_test_init(void)
52*2d543d20SAndroid Build Coastguard Worker {
53*2d543d20SAndroid Build Coastguard Worker if (create_test_store() < 0) {
54*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "Could not create test store\n");
55*2d543d20SAndroid Build Coastguard Worker return 1;
56*2d543d20SAndroid Build Coastguard Worker }
57*2d543d20SAndroid Build Coastguard Worker
58*2d543d20SAndroid Build Coastguard Worker if (write_test_policy_from_file("test_ibendport.policy") < 0) {
59*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "Could not write test policy\n");
60*2d543d20SAndroid Build Coastguard Worker return 1;
61*2d543d20SAndroid Build Coastguard Worker }
62*2d543d20SAndroid Build Coastguard Worker
63*2d543d20SAndroid Build Coastguard Worker return 0;
64*2d543d20SAndroid Build Coastguard Worker }
65*2d543d20SAndroid Build Coastguard Worker
ibendport_test_cleanup(void)66*2d543d20SAndroid Build Coastguard Worker int ibendport_test_cleanup(void)
67*2d543d20SAndroid Build Coastguard Worker {
68*2d543d20SAndroid Build Coastguard Worker if (destroy_test_store() < 0) {
69*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "Could not destroy test store\n");
70*2d543d20SAndroid Build Coastguard Worker return 1;
71*2d543d20SAndroid Build Coastguard Worker }
72*2d543d20SAndroid Build Coastguard Worker
73*2d543d20SAndroid Build Coastguard Worker return 0;
74*2d543d20SAndroid Build Coastguard Worker }
75*2d543d20SAndroid Build Coastguard Worker
ibendport_add_tests(CU_pSuite suite)76*2d543d20SAndroid Build Coastguard Worker int ibendport_add_tests(CU_pSuite suite)
77*2d543d20SAndroid Build Coastguard Worker {
78*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_query", test_ibendport_query);
79*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_exists", test_ibendport_exists);
80*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_count", test_ibendport_count);
81*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_iterate", test_ibendport_iterate);
82*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_list", test_ibendport_list);
83*2d543d20SAndroid Build Coastguard Worker
84*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_modify_del_query_local",
85*2d543d20SAndroid Build Coastguard Worker test_ibendport_modify_del_query_local);
86*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_exists_local",
87*2d543d20SAndroid Build Coastguard Worker test_ibendport_exists_local);
88*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_count_local", test_ibendport_count_local);
89*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_iterate_local",
90*2d543d20SAndroid Build Coastguard Worker test_ibendport_iterate_local);
91*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "ibendport_list_local", test_ibendport_list_local);
92*2d543d20SAndroid Build Coastguard Worker
93*2d543d20SAndroid Build Coastguard Worker return 0;
94*2d543d20SAndroid Build Coastguard Worker }
95*2d543d20SAndroid Build Coastguard Worker
96*2d543d20SAndroid Build Coastguard Worker /* Helpers */
97*2d543d20SAndroid Build Coastguard Worker
get_ibendport_nth(int idx)98*2d543d20SAndroid Build Coastguard Worker static semanage_ibendport_t *get_ibendport_nth(int idx)
99*2d543d20SAndroid Build Coastguard Worker {
100*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t **records;
101*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport;
102*2d543d20SAndroid Build Coastguard Worker unsigned int count;
103*2d543d20SAndroid Build Coastguard Worker
104*2d543d20SAndroid Build Coastguard Worker if (idx == I_NULL)
105*2d543d20SAndroid Build Coastguard Worker return NULL;
106*2d543d20SAndroid Build Coastguard Worker
107*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_ibendport_list(sh, &records, &count) >= 0);
108*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(count >= (unsigned int) idx + 1);
109*2d543d20SAndroid Build Coastguard Worker
110*2d543d20SAndroid Build Coastguard Worker ibendport = records[idx];
111*2d543d20SAndroid Build Coastguard Worker
112*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
113*2d543d20SAndroid Build Coastguard Worker if (i != (unsigned int) idx)
114*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(records[i]);
115*2d543d20SAndroid Build Coastguard Worker
116*2d543d20SAndroid Build Coastguard Worker free(records);
117*2d543d20SAndroid Build Coastguard Worker
118*2d543d20SAndroid Build Coastguard Worker return ibendport;
119*2d543d20SAndroid Build Coastguard Worker }
120*2d543d20SAndroid Build Coastguard Worker
get_ibendport_key_nth(int idx)121*2d543d20SAndroid Build Coastguard Worker static semanage_ibendport_key_t *get_ibendport_key_nth(int idx)
122*2d543d20SAndroid Build Coastguard Worker {
123*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key;
124*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport;
125*2d543d20SAndroid Build Coastguard Worker int res;
126*2d543d20SAndroid Build Coastguard Worker
127*2d543d20SAndroid Build Coastguard Worker if (idx == I_NULL)
128*2d543d20SAndroid Build Coastguard Worker return NULL;
129*2d543d20SAndroid Build Coastguard Worker
130*2d543d20SAndroid Build Coastguard Worker ibendport = get_ibendport_nth(idx);
131*2d543d20SAndroid Build Coastguard Worker
132*2d543d20SAndroid Build Coastguard Worker res = semanage_ibendport_key_extract(sh, ibendport, &key);
133*2d543d20SAndroid Build Coastguard Worker
134*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(res >= 0);
135*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(key);
136*2d543d20SAndroid Build Coastguard Worker
137*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(ibendport);
138*2d543d20SAndroid Build Coastguard Worker
139*2d543d20SAndroid Build Coastguard Worker return key;
140*2d543d20SAndroid Build Coastguard Worker }
141*2d543d20SAndroid Build Coastguard Worker
add_local_ibendport(int idx)142*2d543d20SAndroid Build Coastguard Worker static void add_local_ibendport(int idx)
143*2d543d20SAndroid Build Coastguard Worker {
144*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport;
145*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key = NULL;
146*2d543d20SAndroid Build Coastguard Worker
147*2d543d20SAndroid Build Coastguard Worker ibendport = get_ibendport_nth(idx);
148*2d543d20SAndroid Build Coastguard Worker
149*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_ibendport_key_extract(sh, ibendport,
150*2d543d20SAndroid Build Coastguard Worker &key) >= 0);
151*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(key);
152*2d543d20SAndroid Build Coastguard Worker
153*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_ibendport_modify_local(sh, key,
154*2d543d20SAndroid Build Coastguard Worker ibendport) >= 0);
155*2d543d20SAndroid Build Coastguard Worker
156*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key);
157*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(ibendport);
158*2d543d20SAndroid Build Coastguard Worker }
159*2d543d20SAndroid Build Coastguard Worker
delete_local_ibendport(int idx)160*2d543d20SAndroid Build Coastguard Worker static void delete_local_ibendport(int idx)
161*2d543d20SAndroid Build Coastguard Worker {
162*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key = NULL;
163*2d543d20SAndroid Build Coastguard Worker key = get_ibendport_key_nth(idx);
164*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_ibendport_del_local(sh, key) >= 0);
165*2d543d20SAndroid Build Coastguard Worker
166*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key);
167*2d543d20SAndroid Build Coastguard Worker }
168*2d543d20SAndroid Build Coastguard Worker
169*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_query */
test_ibendport_query(void)170*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_query(void)
171*2d543d20SAndroid Build Coastguard Worker {
172*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport = NULL;
173*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport_exp = NULL;
174*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key = NULL;
175*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
176*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con_exp = NULL;
177*2d543d20SAndroid Build Coastguard Worker char *name;
178*2d543d20SAndroid Build Coastguard Worker char *name_exp;
179*2d543d20SAndroid Build Coastguard Worker
180*2d543d20SAndroid Build Coastguard Worker /* setup */
181*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
182*2d543d20SAndroid Build Coastguard Worker key = get_ibendport_key_nth(I_FIRST);
183*2d543d20SAndroid Build Coastguard Worker ibendport_exp = get_ibendport_nth(I_FIRST);
184*2d543d20SAndroid Build Coastguard Worker
185*2d543d20SAndroid Build Coastguard Worker /* test */
186*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_query(sh, key, &ibendport) >= 0);
187*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(ibendport);
188*2d543d20SAndroid Build Coastguard Worker
189*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, ibendport, &name) >= 0);
190*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(name);
191*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, ibendport_exp,
192*2d543d20SAndroid Build Coastguard Worker &name_exp) >= 0);
193*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(name_exp);
194*2d543d20SAndroid Build Coastguard Worker assert(name);
195*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(name, name_exp);
196*2d543d20SAndroid Build Coastguard Worker
197*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_get_port(ibendport) ==
198*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_get_port(ibendport_exp));
199*2d543d20SAndroid Build Coastguard Worker
200*2d543d20SAndroid Build Coastguard Worker con = semanage_ibendport_get_con(ibendport);
201*2d543d20SAndroid Build Coastguard Worker con_exp = semanage_ibendport_get_con(ibendport_exp);
202*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(con);
203*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(con_exp);
204*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_CONTEXT_EQUAL(con, con_exp);
205*2d543d20SAndroid Build Coastguard Worker
206*2d543d20SAndroid Build Coastguard Worker /* cleanup */
207*2d543d20SAndroid Build Coastguard Worker free(name_exp);
208*2d543d20SAndroid Build Coastguard Worker free(name);
209*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key);
210*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(ibendport);
211*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(ibendport_exp);
212*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
213*2d543d20SAndroid Build Coastguard Worker }
214*2d543d20SAndroid Build Coastguard Worker
215*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_exists */
test_ibendport_exists(void)216*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_exists(void)
217*2d543d20SAndroid Build Coastguard Worker {
218*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key1 = NULL;
219*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key2 = NULL;
220*2d543d20SAndroid Build Coastguard Worker int resp = 42;
221*2d543d20SAndroid Build Coastguard Worker
222*2d543d20SAndroid Build Coastguard Worker /* setup */
223*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
224*2d543d20SAndroid Build Coastguard Worker key1 = get_ibendport_key_nth(I_FIRST);
225*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_key_create(sh, "asdf", 1, &key2) >= 0);
226*2d543d20SAndroid Build Coastguard Worker
227*2d543d20SAndroid Build Coastguard Worker /* test */
228*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_exists(sh, key1, &resp) >= 0);
229*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(resp);
230*2d543d20SAndroid Build Coastguard Worker
231*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_exists(sh, key2, &resp) >= 0);
232*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(!resp);
233*2d543d20SAndroid Build Coastguard Worker
234*2d543d20SAndroid Build Coastguard Worker /* cleanup */
235*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key1);
236*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key2);
237*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
238*2d543d20SAndroid Build Coastguard Worker }
239*2d543d20SAndroid Build Coastguard Worker
240*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_count */
test_ibendport_count(void)241*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_count(void)
242*2d543d20SAndroid Build Coastguard Worker {
243*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
244*2d543d20SAndroid Build Coastguard Worker
245*2d543d20SAndroid Build Coastguard Worker /* setup */
246*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
247*2d543d20SAndroid Build Coastguard Worker
248*2d543d20SAndroid Build Coastguard Worker /* test */
249*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_count(sh, &count) >= 0);
250*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == IBENDPORT_COUNT);
251*2d543d20SAndroid Build Coastguard Worker
252*2d543d20SAndroid Build Coastguard Worker /* cleanup */
253*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
254*2d543d20SAndroid Build Coastguard Worker }
255*2d543d20SAndroid Build Coastguard Worker
256*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_iterate */
257*2d543d20SAndroid Build Coastguard Worker unsigned int helper_ibendport_iterate_counter = 0;
258*2d543d20SAndroid Build Coastguard Worker
helper_ibendport_iterate(const semanage_ibendport_t * ibendport,void * fn_arg)259*2d543d20SAndroid Build Coastguard Worker static int helper_ibendport_iterate(const semanage_ibendport_t *ibendport,
260*2d543d20SAndroid Build Coastguard Worker void *fn_arg)
261*2d543d20SAndroid Build Coastguard Worker {
262*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(fn_arg == (void *) 42);
263*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_counter++;
264*2d543d20SAndroid Build Coastguard Worker return 0;
265*2d543d20SAndroid Build Coastguard Worker }
266*2d543d20SAndroid Build Coastguard Worker
helper_ibendport_iterate_error(const semanage_ibendport_t * ibendport,void * fn_arg)267*2d543d20SAndroid Build Coastguard Worker static int helper_ibendport_iterate_error(const semanage_ibendport_t *ibendport,
268*2d543d20SAndroid Build Coastguard Worker void *fn_arg)
269*2d543d20SAndroid Build Coastguard Worker {
270*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(fn_arg == (void *) 42);
271*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_counter++;
272*2d543d20SAndroid Build Coastguard Worker return -1;
273*2d543d20SAndroid Build Coastguard Worker }
274*2d543d20SAndroid Build Coastguard Worker
helper_ibendport_iterate_break(const semanage_ibendport_t * ibendport,void * fn_arg)275*2d543d20SAndroid Build Coastguard Worker static int helper_ibendport_iterate_break(const semanage_ibendport_t *ibendport,
276*2d543d20SAndroid Build Coastguard Worker void *fn_arg)
277*2d543d20SAndroid Build Coastguard Worker {
278*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(fn_arg == (void *) 42);
279*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_counter++;
280*2d543d20SAndroid Build Coastguard Worker return 1;
281*2d543d20SAndroid Build Coastguard Worker }
282*2d543d20SAndroid Build Coastguard Worker
test_ibendport_iterate(void)283*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_iterate(void)
284*2d543d20SAndroid Build Coastguard Worker {
285*2d543d20SAndroid Build Coastguard Worker /* setup */
286*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
287*2d543d20SAndroid Build Coastguard Worker
288*2d543d20SAndroid Build Coastguard Worker /* test */
289*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_counter = 0;
290*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_iterate(sh, helper_ibendport_iterate,
291*2d543d20SAndroid Build Coastguard Worker (void *) 42) >= 0);
292*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(helper_ibendport_iterate_counter == IBENDPORT_COUNT);
293*2d543d20SAndroid Build Coastguard Worker
294*2d543d20SAndroid Build Coastguard Worker /* test function which returns error */
295*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_counter = 0;
296*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_iterate(sh, helper_ibendport_iterate_error,
297*2d543d20SAndroid Build Coastguard Worker (void *) 42) < 0);
298*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(helper_ibendport_iterate_counter == 1);
299*2d543d20SAndroid Build Coastguard Worker
300*2d543d20SAndroid Build Coastguard Worker /* test function which requests break */
301*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_counter = 0;
302*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_iterate(sh, helper_ibendport_iterate_break,
303*2d543d20SAndroid Build Coastguard Worker (void *) 42) >= 0);
304*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(helper_ibendport_iterate_counter == 1);
305*2d543d20SAndroid Build Coastguard Worker
306*2d543d20SAndroid Build Coastguard Worker /* cleanup */
307*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
308*2d543d20SAndroid Build Coastguard Worker }
309*2d543d20SAndroid Build Coastguard Worker
310*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_list */
test_ibendport_list(void)311*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_list(void)
312*2d543d20SAndroid Build Coastguard Worker {
313*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t **records = NULL;
314*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
315*2d543d20SAndroid Build Coastguard Worker char *name = NULL;
316*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
317*2d543d20SAndroid Build Coastguard Worker
318*2d543d20SAndroid Build Coastguard Worker /* setup */
319*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
320*2d543d20SAndroid Build Coastguard Worker
321*2d543d20SAndroid Build Coastguard Worker /* test */
322*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_list(sh, &records, &count) >= 0);
323*2d543d20SAndroid Build Coastguard Worker
324*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(records);
325*2d543d20SAndroid Build Coastguard Worker assert(records);
326*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == IBENDPORT_COUNT);
327*2d543d20SAndroid Build Coastguard Worker
328*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++) {
329*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(records[i]);
330*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, records[i],
331*2d543d20SAndroid Build Coastguard Worker &name) >= 0);
332*2d543d20SAndroid Build Coastguard Worker con = semanage_ibendport_get_con(records[i]);
333*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(con);
334*2d543d20SAndroid Build Coastguard Worker free(name);
335*2d543d20SAndroid Build Coastguard Worker }
336*2d543d20SAndroid Build Coastguard Worker
337*2d543d20SAndroid Build Coastguard Worker /* cleanup */
338*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
339*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(records[i]);
340*2d543d20SAndroid Build Coastguard Worker
341*2d543d20SAndroid Build Coastguard Worker free(records);
342*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
343*2d543d20SAndroid Build Coastguard Worker }
344*2d543d20SAndroid Build Coastguard Worker
345*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_modify_local, semanage_ibendport_del_local,
346*2d543d20SAndroid Build Coastguard Worker * semanage_ibendport_query_local
347*2d543d20SAndroid Build Coastguard Worker */
test_ibendport_modify_del_query_local(void)348*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_modify_del_query_local(void)
349*2d543d20SAndroid Build Coastguard Worker {
350*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport;
351*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t *ibendport_local;
352*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key = NULL;
353*2d543d20SAndroid Build Coastguard Worker
354*2d543d20SAndroid Build Coastguard Worker /* setup */
355*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
356*2d543d20SAndroid Build Coastguard Worker ibendport = get_ibendport_nth(I_FIRST);
357*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_key_extract(sh, ibendport, &key) >= 0);
358*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL(key);
359*2d543d20SAndroid Build Coastguard Worker
360*2d543d20SAndroid Build Coastguard Worker /* test */
361*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_modify_local(sh, key, ibendport) >= 0);
362*2d543d20SAndroid Build Coastguard Worker
363*2d543d20SAndroid Build Coastguard Worker /* write changes to file */
364*2d543d20SAndroid Build Coastguard Worker helper_commit();
365*2d543d20SAndroid Build Coastguard Worker helper_begin_transaction();
366*2d543d20SAndroid Build Coastguard Worker
367*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_query_local(sh, key,
368*2d543d20SAndroid Build Coastguard Worker &ibendport_local) >= 0);
369*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(ibendport_local);
370*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(ibendport_local);
371*2d543d20SAndroid Build Coastguard Worker
372*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_del_local(sh, key) >= 0);
373*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_query_local(sh, key,
374*2d543d20SAndroid Build Coastguard Worker &ibendport_local) < 0);
375*2d543d20SAndroid Build Coastguard Worker
376*2d543d20SAndroid Build Coastguard Worker /* cleanup */
377*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key);
378*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(ibendport);
379*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
380*2d543d20SAndroid Build Coastguard Worker }
381*2d543d20SAndroid Build Coastguard Worker
382*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_exists_local */
test_ibendport_exists_local(void)383*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_exists_local(void)
384*2d543d20SAndroid Build Coastguard Worker {
385*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key1 = NULL;
386*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_t *key2 = NULL;
387*2d543d20SAndroid Build Coastguard Worker int resp = 42;
388*2d543d20SAndroid Build Coastguard Worker
389*2d543d20SAndroid Build Coastguard Worker /* setup */
390*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
391*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_FIRST);
392*2d543d20SAndroid Build Coastguard Worker key1 = get_ibendport_key_nth(I_FIRST);
393*2d543d20SAndroid Build Coastguard Worker key2 = get_ibendport_key_nth(I_SECOND);
394*2d543d20SAndroid Build Coastguard Worker
395*2d543d20SAndroid Build Coastguard Worker /* test */
396*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_exists_local(sh, key1, &resp) >= 0);
397*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(resp);
398*2d543d20SAndroid Build Coastguard Worker
399*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_exists_local(sh, key2, &resp) >= 0);
400*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(!resp);
401*2d543d20SAndroid Build Coastguard Worker
402*2d543d20SAndroid Build Coastguard Worker /* cleanup */
403*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_del_local(sh, key1) >= 0);
404*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key1);
405*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_key_free(key2);
406*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
407*2d543d20SAndroid Build Coastguard Worker }
408*2d543d20SAndroid Build Coastguard Worker
409*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_count_local */
test_ibendport_count_local(void)410*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_count_local(void)
411*2d543d20SAndroid Build Coastguard Worker {
412*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
413*2d543d20SAndroid Build Coastguard Worker
414*2d543d20SAndroid Build Coastguard Worker /* setup */
415*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
416*2d543d20SAndroid Build Coastguard Worker
417*2d543d20SAndroid Build Coastguard Worker /* test */
418*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_count_local(sh, &count) >= 0);
419*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 0);
420*2d543d20SAndroid Build Coastguard Worker
421*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_FIRST);
422*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_count_local(sh, &count) >= 0);
423*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 1);
424*2d543d20SAndroid Build Coastguard Worker
425*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_SECOND);
426*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_count_local(sh, &count) >= 0);
427*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 2);
428*2d543d20SAndroid Build Coastguard Worker
429*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_SECOND);
430*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_count_local(sh, &count) >= 0);
431*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 1);
432*2d543d20SAndroid Build Coastguard Worker
433*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_FIRST);
434*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_count_local(sh, &count) >= 0);
435*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 0);
436*2d543d20SAndroid Build Coastguard Worker
437*2d543d20SAndroid Build Coastguard Worker /* cleanup */
438*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
439*2d543d20SAndroid Build Coastguard Worker }
440*2d543d20SAndroid Build Coastguard Worker
441*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_iterate_local */
442*2d543d20SAndroid Build Coastguard Worker unsigned int helper_ibendport_iterate_local_counter = 0;
443*2d543d20SAndroid Build Coastguard Worker
helper_ibendport_iterate_local(const semanage_ibendport_t * ibendport,void * fn_arg)444*2d543d20SAndroid Build Coastguard Worker static int helper_ibendport_iterate_local(const semanage_ibendport_t *ibendport,
445*2d543d20SAndroid Build Coastguard Worker void *fn_arg)
446*2d543d20SAndroid Build Coastguard Worker {
447*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(fn_arg == (void *) 42);
448*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_counter++;
449*2d543d20SAndroid Build Coastguard Worker return 0;
450*2d543d20SAndroid Build Coastguard Worker }
451*2d543d20SAndroid Build Coastguard Worker
helper_ibendport_iterate_local_error(const semanage_ibendport_t * ibendport,void * fn_arg)452*2d543d20SAndroid Build Coastguard Worker static int helper_ibendport_iterate_local_error(const semanage_ibendport_t *ibendport,
453*2d543d20SAndroid Build Coastguard Worker void *fn_arg)
454*2d543d20SAndroid Build Coastguard Worker {
455*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(fn_arg == (void *) 42);
456*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_counter++;
457*2d543d20SAndroid Build Coastguard Worker return -1;
458*2d543d20SAndroid Build Coastguard Worker }
459*2d543d20SAndroid Build Coastguard Worker
helper_ibendport_iterate_local_break(const semanage_ibendport_t * ibendport,void * fn_arg)460*2d543d20SAndroid Build Coastguard Worker static int helper_ibendport_iterate_local_break(const semanage_ibendport_t *ibendport,
461*2d543d20SAndroid Build Coastguard Worker void *fn_arg)
462*2d543d20SAndroid Build Coastguard Worker {
463*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(fn_arg == (void *) 42);
464*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_counter++;
465*2d543d20SAndroid Build Coastguard Worker return 1;
466*2d543d20SAndroid Build Coastguard Worker }
467*2d543d20SAndroid Build Coastguard Worker
test_ibendport_iterate_local(void)468*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_iterate_local(void)
469*2d543d20SAndroid Build Coastguard Worker {
470*2d543d20SAndroid Build Coastguard Worker /* setup */
471*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
472*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_FIRST);
473*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_SECOND);
474*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_THIRD);
475*2d543d20SAndroid Build Coastguard Worker
476*2d543d20SAndroid Build Coastguard Worker /* test */
477*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_counter = 0;
478*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_iterate_local(sh,
479*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local, (void *) 42) >= 0);
480*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(helper_ibendport_iterate_local_counter == 3);
481*2d543d20SAndroid Build Coastguard Worker
482*2d543d20SAndroid Build Coastguard Worker /* test function which returns error */
483*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_counter = 0;
484*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_iterate_local(sh,
485*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_error, (void *) 42) < 0);
486*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(helper_ibendport_iterate_local_counter == 1);
487*2d543d20SAndroid Build Coastguard Worker
488*2d543d20SAndroid Build Coastguard Worker /* test function which requests break */
489*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_counter = 0;
490*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_iterate_local(sh,
491*2d543d20SAndroid Build Coastguard Worker helper_ibendport_iterate_local_break, (void *) 42) >= 0);
492*2d543d20SAndroid Build Coastguard Worker
493*2d543d20SAndroid Build Coastguard Worker /* cleanup */
494*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_FIRST);
495*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_SECOND);
496*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_THIRD);
497*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
498*2d543d20SAndroid Build Coastguard Worker }
499*2d543d20SAndroid Build Coastguard Worker
500*2d543d20SAndroid Build Coastguard Worker /* Function semanage_ibendport_list_local */
test_ibendport_list_local(void)501*2d543d20SAndroid Build Coastguard Worker static void test_ibendport_list_local(void)
502*2d543d20SAndroid Build Coastguard Worker {
503*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_t **records = NULL;
504*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
505*2d543d20SAndroid Build Coastguard Worker char *name = NULL;
506*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
507*2d543d20SAndroid Build Coastguard Worker
508*2d543d20SAndroid Build Coastguard Worker /* setup */
509*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
510*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_FIRST);
511*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_SECOND);
512*2d543d20SAndroid Build Coastguard Worker add_local_ibendport(I_THIRD);
513*2d543d20SAndroid Build Coastguard Worker
514*2d543d20SAndroid Build Coastguard Worker /* test */
515*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_list_local(sh, &records, &count) >= 0);
516*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(records);
517*2d543d20SAndroid Build Coastguard Worker assert(records);
518*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 3);
519*2d543d20SAndroid Build Coastguard Worker
520*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++) {
521*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(records[i]);
522*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_ibendport_get_ibdev_name(sh, records[i],
523*2d543d20SAndroid Build Coastguard Worker &name) >= 0);
524*2d543d20SAndroid Build Coastguard Worker con = semanage_ibendport_get_con(records[i]);
525*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(con);
526*2d543d20SAndroid Build Coastguard Worker free(name);
527*2d543d20SAndroid Build Coastguard Worker }
528*2d543d20SAndroid Build Coastguard Worker
529*2d543d20SAndroid Build Coastguard Worker /* cleanup */
530*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
531*2d543d20SAndroid Build Coastguard Worker semanage_ibendport_free(records[i]);
532*2d543d20SAndroid Build Coastguard Worker
533*2d543d20SAndroid Build Coastguard Worker free(records);
534*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_FIRST);
535*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_SECOND);
536*2d543d20SAndroid Build Coastguard Worker delete_local_ibendport(I_THIRD);
537*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
538*2d543d20SAndroid Build Coastguard Worker }
539