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_port.h"
23*2d543d20SAndroid Build Coastguard Worker
24*2d543d20SAndroid Build Coastguard Worker #define PORT_COUNT 3
25*2d543d20SAndroid Build Coastguard Worker
26*2d543d20SAndroid Build Coastguard Worker #define PORT1_LOW 80
27*2d543d20SAndroid Build Coastguard Worker #define PORT1_HIGH 80
28*2d543d20SAndroid Build Coastguard Worker #define PORT1_PROTO SEPOL_PROTO_TCP
29*2d543d20SAndroid Build Coastguard Worker
30*2d543d20SAndroid Build Coastguard Worker #define PORT2_LOW 1
31*2d543d20SAndroid Build Coastguard Worker #define PORT2_HIGH 1023
32*2d543d20SAndroid Build Coastguard Worker #define PORT2_PROTO SEPOL_PROTO_UDP
33*2d543d20SAndroid Build Coastguard Worker
34*2d543d20SAndroid Build Coastguard Worker #define PORT3_LOW 12345
35*2d543d20SAndroid Build Coastguard Worker #define PORT3_HIGH 12345
36*2d543d20SAndroid Build Coastguard Worker #define PORT3_PROTO SEPOL_PROTO_TCP
37*2d543d20SAndroid Build Coastguard Worker
38*2d543d20SAndroid Build Coastguard Worker /* port_record.h */
39*2d543d20SAndroid Build Coastguard Worker static void test_port_compare(void);
40*2d543d20SAndroid Build Coastguard Worker static void test_port_compare2(void);
41*2d543d20SAndroid Build Coastguard Worker static void test_port_key_create(void);
42*2d543d20SAndroid Build Coastguard Worker static void test_port_key_extract(void);
43*2d543d20SAndroid Build Coastguard Worker static void test_port_get_set_proto(void);
44*2d543d20SAndroid Build Coastguard Worker static void test_port_get_proto_str(void);
45*2d543d20SAndroid Build Coastguard Worker static void test_port_get_set_port(void);
46*2d543d20SAndroid Build Coastguard Worker static void test_port_get_set_con(void);
47*2d543d20SAndroid Build Coastguard Worker static void test_port_create(void);
48*2d543d20SAndroid Build Coastguard Worker static void test_port_clone(void);
49*2d543d20SAndroid Build Coastguard Worker
50*2d543d20SAndroid Build Coastguard Worker /* ports_policy.h */
51*2d543d20SAndroid Build Coastguard Worker static void test_port_query(void);
52*2d543d20SAndroid Build Coastguard Worker static void test_port_exists(void);
53*2d543d20SAndroid Build Coastguard Worker static void test_port_count(void);
54*2d543d20SAndroid Build Coastguard Worker static void test_port_iterate(void);
55*2d543d20SAndroid Build Coastguard Worker static void test_port_list(void);
56*2d543d20SAndroid Build Coastguard Worker
57*2d543d20SAndroid Build Coastguard Worker /* ports_local.h */
58*2d543d20SAndroid Build Coastguard Worker static void test_port_modify_del_local(void);
59*2d543d20SAndroid Build Coastguard Worker static void test_port_query_local(void);
60*2d543d20SAndroid Build Coastguard Worker static void test_port_exists_local(void);
61*2d543d20SAndroid Build Coastguard Worker static void test_port_count_local(void);
62*2d543d20SAndroid Build Coastguard Worker static void test_port_iterate_local(void);
63*2d543d20SAndroid Build Coastguard Worker static void test_port_list_local(void);
64*2d543d20SAndroid Build Coastguard Worker
65*2d543d20SAndroid Build Coastguard Worker /* internal */
66*2d543d20SAndroid Build Coastguard Worker static void test_port_validate_local(void);
67*2d543d20SAndroid Build Coastguard Worker
68*2d543d20SAndroid Build Coastguard Worker extern semanage_handle_t *sh;
69*2d543d20SAndroid Build Coastguard Worker
port_test_init(void)70*2d543d20SAndroid Build Coastguard Worker int port_test_init(void)
71*2d543d20SAndroid Build Coastguard Worker {
72*2d543d20SAndroid Build Coastguard Worker if (create_test_store() < 0) {
73*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "Could not create test store\n");
74*2d543d20SAndroid Build Coastguard Worker return 1;
75*2d543d20SAndroid Build Coastguard Worker }
76*2d543d20SAndroid Build Coastguard Worker
77*2d543d20SAndroid Build Coastguard Worker if (write_test_policy_from_file("test_port.policy") < 0) {
78*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "Could not write test policy\n");
79*2d543d20SAndroid Build Coastguard Worker return 1;
80*2d543d20SAndroid Build Coastguard Worker }
81*2d543d20SAndroid Build Coastguard Worker
82*2d543d20SAndroid Build Coastguard Worker return 0;
83*2d543d20SAndroid Build Coastguard Worker }
84*2d543d20SAndroid Build Coastguard Worker
port_test_cleanup(void)85*2d543d20SAndroid Build Coastguard Worker int port_test_cleanup(void)
86*2d543d20SAndroid Build Coastguard Worker {
87*2d543d20SAndroid Build Coastguard Worker if (destroy_test_store() < 0) {
88*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "Could not destroy test store\n");
89*2d543d20SAndroid Build Coastguard Worker return 1;
90*2d543d20SAndroid Build Coastguard Worker }
91*2d543d20SAndroid Build Coastguard Worker
92*2d543d20SAndroid Build Coastguard Worker return 0;
93*2d543d20SAndroid Build Coastguard Worker }
94*2d543d20SAndroid Build Coastguard Worker
port_add_tests(CU_pSuite suite)95*2d543d20SAndroid Build Coastguard Worker int port_add_tests(CU_pSuite suite)
96*2d543d20SAndroid Build Coastguard Worker {
97*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_compare", test_port_compare);
98*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_compare2", test_port_compare2);
99*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_key_create", test_port_key_create);
100*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_key_extract", test_port_key_extract);
101*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_get_set_proto", test_port_get_set_proto);
102*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_get_proto_str", test_port_get_proto_str);
103*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_get_set_port", test_port_get_set_port);
104*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_get_set_con", test_port_get_set_con);
105*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_create", test_port_create);
106*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_clone", test_port_clone);
107*2d543d20SAndroid Build Coastguard Worker
108*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_query", test_port_query);
109*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_exists", test_port_exists);
110*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_count", test_port_count);
111*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_iterate", test_port_iterate);
112*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_list", test_port_list);
113*2d543d20SAndroid Build Coastguard Worker
114*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_modify_del_local", test_port_modify_del_local);
115*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_query_local", test_port_query_local);
116*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_exists_local", test_port_exists_local);
117*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_count_local", test_port_count_local);
118*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_iterate_local", test_port_iterate_local);
119*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_list_local", test_port_list_local);
120*2d543d20SAndroid Build Coastguard Worker
121*2d543d20SAndroid Build Coastguard Worker CU_add_test(suite, "port_validate_local", test_port_validate_local);
122*2d543d20SAndroid Build Coastguard Worker
123*2d543d20SAndroid Build Coastguard Worker return 0;
124*2d543d20SAndroid Build Coastguard Worker }
125*2d543d20SAndroid Build Coastguard Worker
126*2d543d20SAndroid Build Coastguard Worker /* Helpers */
127*2d543d20SAndroid Build Coastguard Worker
get_port_nth(int idx)128*2d543d20SAndroid Build Coastguard Worker static semanage_port_t *get_port_nth(int idx)
129*2d543d20SAndroid Build Coastguard Worker {
130*2d543d20SAndroid Build Coastguard Worker int res;
131*2d543d20SAndroid Build Coastguard Worker semanage_port_t **records;
132*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port;
133*2d543d20SAndroid Build Coastguard Worker unsigned int count;
134*2d543d20SAndroid Build Coastguard Worker
135*2d543d20SAndroid Build Coastguard Worker if (idx == I_NULL)
136*2d543d20SAndroid Build Coastguard Worker return NULL;
137*2d543d20SAndroid Build Coastguard Worker
138*2d543d20SAndroid Build Coastguard Worker res = semanage_port_list(sh, &records, &count);
139*2d543d20SAndroid Build Coastguard Worker
140*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(res >= 0);
141*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(count >= (unsigned int) idx + 1);
142*2d543d20SAndroid Build Coastguard Worker
143*2d543d20SAndroid Build Coastguard Worker port = records[idx];
144*2d543d20SAndroid Build Coastguard Worker
145*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
146*2d543d20SAndroid Build Coastguard Worker if (i != (unsigned int) idx)
147*2d543d20SAndroid Build Coastguard Worker semanage_port_free(records[i]);
148*2d543d20SAndroid Build Coastguard Worker
149*2d543d20SAndroid Build Coastguard Worker free(records);
150*2d543d20SAndroid Build Coastguard Worker
151*2d543d20SAndroid Build Coastguard Worker return port;
152*2d543d20SAndroid Build Coastguard Worker }
153*2d543d20SAndroid Build Coastguard Worker
get_port_key_nth(int idx)154*2d543d20SAndroid Build Coastguard Worker static semanage_port_key_t *get_port_key_nth(int idx)
155*2d543d20SAndroid Build Coastguard Worker {
156*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key;
157*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port;
158*2d543d20SAndroid Build Coastguard Worker int res;
159*2d543d20SAndroid Build Coastguard Worker
160*2d543d20SAndroid Build Coastguard Worker if (idx == I_NULL)
161*2d543d20SAndroid Build Coastguard Worker return NULL;
162*2d543d20SAndroid Build Coastguard Worker
163*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(idx);
164*2d543d20SAndroid Build Coastguard Worker
165*2d543d20SAndroid Build Coastguard Worker res = semanage_port_key_extract(sh, port, &key);
166*2d543d20SAndroid Build Coastguard Worker
167*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(res >= 0);
168*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(key);
169*2d543d20SAndroid Build Coastguard Worker
170*2d543d20SAndroid Build Coastguard Worker /* cleanup */
171*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
172*2d543d20SAndroid Build Coastguard Worker
173*2d543d20SAndroid Build Coastguard Worker return key;
174*2d543d20SAndroid Build Coastguard Worker }
175*2d543d20SAndroid Build Coastguard Worker
add_local_port(int port_idx)176*2d543d20SAndroid Build Coastguard Worker static void add_local_port(int port_idx)
177*2d543d20SAndroid Build Coastguard Worker {
178*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port;
179*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
180*2d543d20SAndroid Build Coastguard Worker
181*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(port_idx != I_NULL);
182*2d543d20SAndroid Build Coastguard Worker
183*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(port_idx);
184*2d543d20SAndroid Build Coastguard Worker
185*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_port_key_extract(sh, port, &key) >= 0);
186*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(key);
187*2d543d20SAndroid Build Coastguard Worker
188*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_port_modify_local(sh, key, port) >= 0);
189*2d543d20SAndroid Build Coastguard Worker
190*2d543d20SAndroid Build Coastguard Worker /* cleanup */
191*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
192*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
193*2d543d20SAndroid Build Coastguard Worker }
194*2d543d20SAndroid Build Coastguard Worker
delete_local_port(int port_idx)195*2d543d20SAndroid Build Coastguard Worker static void delete_local_port(int port_idx)
196*2d543d20SAndroid Build Coastguard Worker {
197*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
198*2d543d20SAndroid Build Coastguard Worker
199*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(port_idx != I_NULL);
200*2d543d20SAndroid Build Coastguard Worker
201*2d543d20SAndroid Build Coastguard Worker key = get_port_key_nth(port_idx);
202*2d543d20SAndroid Build Coastguard Worker
203*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_FATAL(semanage_port_del_local(sh, key) >= 0);
204*2d543d20SAndroid Build Coastguard Worker
205*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
206*2d543d20SAndroid Build Coastguard Worker }
207*2d543d20SAndroid Build Coastguard Worker
208*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_compare */
helper_port_compare(int idx1,int idx2)209*2d543d20SAndroid Build Coastguard Worker static void helper_port_compare(int idx1, int idx2)
210*2d543d20SAndroid Build Coastguard Worker {
211*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
212*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
213*2d543d20SAndroid Build Coastguard Worker int res = 42;
214*2d543d20SAndroid Build Coastguard Worker
215*2d543d20SAndroid Build Coastguard Worker /* setup */
216*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
217*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(idx1);
218*2d543d20SAndroid Build Coastguard Worker key = get_port_key_nth(idx2);
219*2d543d20SAndroid Build Coastguard Worker
220*2d543d20SAndroid Build Coastguard Worker /* test */
221*2d543d20SAndroid Build Coastguard Worker res = semanage_port_compare(port, key);
222*2d543d20SAndroid Build Coastguard Worker
223*2d543d20SAndroid Build Coastguard Worker if (idx1 == idx2) {
224*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(res == 0);
225*2d543d20SAndroid Build Coastguard Worker } else {
226*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(res != 0);
227*2d543d20SAndroid Build Coastguard Worker }
228*2d543d20SAndroid Build Coastguard Worker
229*2d543d20SAndroid Build Coastguard Worker /* cleanup */
230*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
231*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
232*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
233*2d543d20SAndroid Build Coastguard Worker }
234*2d543d20SAndroid Build Coastguard Worker
test_port_compare(void)235*2d543d20SAndroid Build Coastguard Worker static void test_port_compare(void)
236*2d543d20SAndroid Build Coastguard Worker {
237*2d543d20SAndroid Build Coastguard Worker helper_port_compare(I_FIRST, I_FIRST);
238*2d543d20SAndroid Build Coastguard Worker helper_port_compare(I_FIRST, I_SECOND);
239*2d543d20SAndroid Build Coastguard Worker helper_port_compare(I_SECOND, I_FIRST);
240*2d543d20SAndroid Build Coastguard Worker helper_port_compare(I_SECOND, I_SECOND);
241*2d543d20SAndroid Build Coastguard Worker }
242*2d543d20SAndroid Build Coastguard Worker
243*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_compare2 */
helper_port_compare2(int idx1,int idx2)244*2d543d20SAndroid Build Coastguard Worker static void helper_port_compare2(int idx1, int idx2)
245*2d543d20SAndroid Build Coastguard Worker {
246*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port1 = NULL;
247*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port2 = NULL;
248*2d543d20SAndroid Build Coastguard Worker int res = 42;
249*2d543d20SAndroid Build Coastguard Worker
250*2d543d20SAndroid Build Coastguard Worker /* setup */
251*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
252*2d543d20SAndroid Build Coastguard Worker
253*2d543d20SAndroid Build Coastguard Worker port1 = get_port_nth(idx1);
254*2d543d20SAndroid Build Coastguard Worker port2 = get_port_nth(idx2);
255*2d543d20SAndroid Build Coastguard Worker
256*2d543d20SAndroid Build Coastguard Worker /* test */
257*2d543d20SAndroid Build Coastguard Worker res = semanage_port_compare2(port1, port2);
258*2d543d20SAndroid Build Coastguard Worker
259*2d543d20SAndroid Build Coastguard Worker if (idx1 == idx2) {
260*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(res == 0);
261*2d543d20SAndroid Build Coastguard Worker } else {
262*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(res != 0);
263*2d543d20SAndroid Build Coastguard Worker }
264*2d543d20SAndroid Build Coastguard Worker
265*2d543d20SAndroid Build Coastguard Worker /* cleanup */
266*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port1);
267*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port2);
268*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
269*2d543d20SAndroid Build Coastguard Worker }
270*2d543d20SAndroid Build Coastguard Worker
test_port_compare2(void)271*2d543d20SAndroid Build Coastguard Worker static void test_port_compare2(void)
272*2d543d20SAndroid Build Coastguard Worker {
273*2d543d20SAndroid Build Coastguard Worker helper_port_compare2(I_FIRST, I_FIRST);
274*2d543d20SAndroid Build Coastguard Worker helper_port_compare2(I_FIRST, I_SECOND);
275*2d543d20SAndroid Build Coastguard Worker helper_port_compare2(I_SECOND, I_FIRST);
276*2d543d20SAndroid Build Coastguard Worker helper_port_compare2(I_SECOND, I_SECOND);
277*2d543d20SAndroid Build Coastguard Worker }
278*2d543d20SAndroid Build Coastguard Worker
279*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_create */
test_port_key_create(void)280*2d543d20SAndroid Build Coastguard Worker static void test_port_key_create(void)
281*2d543d20SAndroid Build Coastguard Worker {
282*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
283*2d543d20SAndroid Build Coastguard Worker
284*2d543d20SAndroid Build Coastguard Worker /* setup */
285*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
286*2d543d20SAndroid Build Coastguard Worker
287*2d543d20SAndroid Build Coastguard Worker /* test */
288*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_key_create(sh, 1000, 1200, 0, &key) >= 0);
289*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL(key);
290*2d543d20SAndroid Build Coastguard Worker
291*2d543d20SAndroid Build Coastguard Worker /* cleanup */
292*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
293*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
294*2d543d20SAndroid Build Coastguard Worker }
295*2d543d20SAndroid Build Coastguard Worker
296*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_extract */
test_port_key_extract(void)297*2d543d20SAndroid Build Coastguard Worker static void test_port_key_extract(void)
298*2d543d20SAndroid Build Coastguard Worker {
299*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
300*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
301*2d543d20SAndroid Build Coastguard Worker
302*2d543d20SAndroid Build Coastguard Worker /* setup */
303*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
304*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(I_FIRST);
305*2d543d20SAndroid Build Coastguard Worker
306*2d543d20SAndroid Build Coastguard Worker /* test */
307*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_key_extract(sh, port, &key) >= 0);
308*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL(key);
309*2d543d20SAndroid Build Coastguard Worker
310*2d543d20SAndroid Build Coastguard Worker /* cleanup */
311*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
312*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
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_port_get_proto, semanage_port_set_proto */
helper_port_get_set_proto(int idx)317*2d543d20SAndroid Build Coastguard Worker static void helper_port_get_set_proto(int idx)
318*2d543d20SAndroid Build Coastguard Worker {
319*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = 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 port = get_port_nth(idx);
324*2d543d20SAndroid Build Coastguard Worker
325*2d543d20SAndroid Build Coastguard Worker /* test */
326*2d543d20SAndroid Build Coastguard Worker semanage_port_set_proto(port, 0);
327*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_proto(port) == 0);
328*2d543d20SAndroid Build Coastguard Worker semanage_port_set_proto(port, 1);
329*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_proto(port) == 1);
330*2d543d20SAndroid Build Coastguard Worker
331*2d543d20SAndroid Build Coastguard Worker /* cleanup */
332*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
333*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
334*2d543d20SAndroid Build Coastguard Worker }
335*2d543d20SAndroid Build Coastguard Worker
test_port_get_set_proto(void)336*2d543d20SAndroid Build Coastguard Worker static void test_port_get_set_proto(void)
337*2d543d20SAndroid Build Coastguard Worker {
338*2d543d20SAndroid Build Coastguard Worker helper_port_get_set_proto(I_FIRST);
339*2d543d20SAndroid Build Coastguard Worker helper_port_get_set_proto(I_SECOND);
340*2d543d20SAndroid Build Coastguard Worker }
341*2d543d20SAndroid Build Coastguard Worker
342*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_get_proto_str */
test_port_get_proto_str(void)343*2d543d20SAndroid Build Coastguard Worker static void test_port_get_proto_str(void)
344*2d543d20SAndroid Build Coastguard Worker {
345*2d543d20SAndroid Build Coastguard Worker const char *str = NULL;
346*2d543d20SAndroid Build Coastguard Worker
347*2d543d20SAndroid Build Coastguard Worker str = semanage_port_get_proto_str(-1);
348*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(str, "???");
349*2d543d20SAndroid Build Coastguard Worker
350*2d543d20SAndroid Build Coastguard Worker str = semanage_port_get_proto_str(0);
351*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(str, "udp");
352*2d543d20SAndroid Build Coastguard Worker
353*2d543d20SAndroid Build Coastguard Worker str = semanage_port_get_proto_str(1);
354*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(str, "tcp");
355*2d543d20SAndroid Build Coastguard Worker
356*2d543d20SAndroid Build Coastguard Worker str = semanage_port_get_proto_str(2);
357*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(str, "dccp");
358*2d543d20SAndroid Build Coastguard Worker
359*2d543d20SAndroid Build Coastguard Worker str = semanage_port_get_proto_str(3);
360*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(str, "sctp");
361*2d543d20SAndroid Build Coastguard Worker
362*2d543d20SAndroid Build Coastguard Worker str = semanage_port_get_proto_str(4);
363*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_STRING_EQUAL(str, "???");
364*2d543d20SAndroid Build Coastguard Worker }
365*2d543d20SAndroid Build Coastguard Worker
366*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_get_low, semanage_port_get_high, */
367*2d543d20SAndroid Build Coastguard Worker /* semanage_port_set_port, semanage_port_set_range */
test_port_get_set_port(void)368*2d543d20SAndroid Build Coastguard Worker static void test_port_get_set_port(void)
369*2d543d20SAndroid Build Coastguard Worker {
370*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
371*2d543d20SAndroid Build Coastguard Worker
372*2d543d20SAndroid Build Coastguard Worker /* setup */
373*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
374*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(I_FIRST);
375*2d543d20SAndroid Build Coastguard Worker
376*2d543d20SAndroid Build Coastguard Worker /* test */
377*2d543d20SAndroid Build Coastguard Worker semanage_port_set_port(port, 1000);
378*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_low(port) == 1000);
379*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_high(port) == 1000);
380*2d543d20SAndroid Build Coastguard Worker
381*2d543d20SAndroid Build Coastguard Worker semanage_port_set_range(port, 1000, 1200);
382*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_low(port) == 1000);
383*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_high(port) == 1200);
384*2d543d20SAndroid Build Coastguard Worker
385*2d543d20SAndroid Build Coastguard Worker /* cleanup */
386*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
387*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
388*2d543d20SAndroid Build Coastguard Worker }
389*2d543d20SAndroid Build Coastguard Worker
390*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_get_con, semanage_port_set_con */
test_port_get_set_con(void)391*2d543d20SAndroid Build Coastguard Worker static void test_port_get_set_con(void)
392*2d543d20SAndroid Build Coastguard Worker {
393*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
394*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port_tmp = NULL;
395*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con1 = NULL;
396*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con2 = NULL;
397*2d543d20SAndroid Build Coastguard Worker
398*2d543d20SAndroid Build Coastguard Worker /* setup */
399*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
400*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(I_FIRST);
401*2d543d20SAndroid Build Coastguard Worker port_tmp = get_port_nth(I_SECOND);
402*2d543d20SAndroid Build Coastguard Worker con1 = semanage_port_get_con(port_tmp);
403*2d543d20SAndroid Build Coastguard Worker
404*2d543d20SAndroid Build Coastguard Worker /* test */
405*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_set_con(sh, port, con1) >= 0);
406*2d543d20SAndroid Build Coastguard Worker con2 = semanage_port_get_con(port);
407*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_CONTEXT_EQUAL(con1, con2);
408*2d543d20SAndroid Build Coastguard Worker
409*2d543d20SAndroid Build Coastguard Worker /* cleanup */
410*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
411*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port_tmp);
412*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
413*2d543d20SAndroid Build Coastguard Worker }
414*2d543d20SAndroid Build Coastguard Worker
415*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_create */
test_port_create(void)416*2d543d20SAndroid Build Coastguard Worker static void test_port_create(void)
417*2d543d20SAndroid Build Coastguard Worker {
418*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
419*2d543d20SAndroid Build Coastguard Worker
420*2d543d20SAndroid Build Coastguard Worker /* setup */
421*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
422*2d543d20SAndroid Build Coastguard Worker
423*2d543d20SAndroid Build Coastguard Worker /* test */
424*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_create(sh, &port) >= 0);
425*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_low(port) == 0);
426*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_high(port) == 0);
427*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_con(port) == NULL);
428*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_proto(port) == 0);
429*2d543d20SAndroid Build Coastguard Worker
430*2d543d20SAndroid Build Coastguard Worker /* cleanup */
431*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
432*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
433*2d543d20SAndroid Build Coastguard Worker }
434*2d543d20SAndroid Build Coastguard Worker
435*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_clone */
test_port_clone(void)436*2d543d20SAndroid Build Coastguard Worker static void test_port_clone(void)
437*2d543d20SAndroid Build Coastguard Worker {
438*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
439*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port_clone = NULL;
440*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
441*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con2 = NULL;
442*2d543d20SAndroid Build Coastguard Worker
443*2d543d20SAndroid Build Coastguard Worker /* setup */
444*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
445*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_create(sh, &port) >= 0);
446*2d543d20SAndroid Build Coastguard Worker semanage_port_set_range(port, 1000, 1200);
447*2d543d20SAndroid Build Coastguard Worker semanage_port_set_proto(port, 1);
448*2d543d20SAndroid Build Coastguard Worker semanage_context_from_string(sh, "user_u:role_r:type_t:s0", &con);
449*2d543d20SAndroid Build Coastguard Worker semanage_port_set_con(sh, port, con);
450*2d543d20SAndroid Build Coastguard Worker
451*2d543d20SAndroid Build Coastguard Worker /* test */
452*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_clone(sh, port, &port_clone) >= 0);
453*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_low(port_clone) == 1000);
454*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_high(port_clone) == 1200);
455*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_proto(port_clone) == 1);
456*2d543d20SAndroid Build Coastguard Worker
457*2d543d20SAndroid Build Coastguard Worker con2 = semanage_port_get_con(port_clone);
458*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_CONTEXT_EQUAL(con, con2);
459*2d543d20SAndroid Build Coastguard Worker
460*2d543d20SAndroid Build Coastguard Worker /* cleanup */
461*2d543d20SAndroid Build Coastguard Worker semanage_context_free(con);
462*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
463*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port_clone);
464*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
465*2d543d20SAndroid Build Coastguard Worker }
466*2d543d20SAndroid Build Coastguard Worker
467*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_query */
test_port_query(void)468*2d543d20SAndroid Build Coastguard Worker static void test_port_query(void)
469*2d543d20SAndroid Build Coastguard Worker {
470*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
471*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port_exp = NULL;
472*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
473*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
474*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con_exp = NULL;
475*2d543d20SAndroid Build Coastguard Worker
476*2d543d20SAndroid Build Coastguard Worker /* setup */
477*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
478*2d543d20SAndroid Build Coastguard Worker key = get_port_key_nth(I_FIRST);
479*2d543d20SAndroid Build Coastguard Worker port_exp = get_port_nth(I_FIRST);
480*2d543d20SAndroid Build Coastguard Worker
481*2d543d20SAndroid Build Coastguard Worker /* test */
482*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_query(sh, key, &port) >= 0);
483*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_low(port) ==
484*2d543d20SAndroid Build Coastguard Worker semanage_port_get_low(port_exp));
485*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_high(port) ==
486*2d543d20SAndroid Build Coastguard Worker semanage_port_get_high(port_exp));
487*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_proto(port) ==
488*2d543d20SAndroid Build Coastguard Worker semanage_port_get_proto(port_exp));
489*2d543d20SAndroid Build Coastguard Worker
490*2d543d20SAndroid Build Coastguard Worker con = semanage_port_get_con(port);
491*2d543d20SAndroid Build Coastguard Worker con_exp = semanage_port_get_con(port_exp);
492*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_CONTEXT_EQUAL(con, con_exp);
493*2d543d20SAndroid Build Coastguard Worker
494*2d543d20SAndroid Build Coastguard Worker /* cleanup */
495*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
496*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
497*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port_exp);
498*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
499*2d543d20SAndroid Build Coastguard Worker }
500*2d543d20SAndroid Build Coastguard Worker
501*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_exists */
test_port_exists(void)502*2d543d20SAndroid Build Coastguard Worker static void test_port_exists(void)
503*2d543d20SAndroid Build Coastguard Worker {
504*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key1 = NULL;
505*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key2 = NULL;
506*2d543d20SAndroid Build Coastguard Worker int resp = 42;
507*2d543d20SAndroid Build Coastguard Worker
508*2d543d20SAndroid Build Coastguard Worker /* setup */
509*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
510*2d543d20SAndroid Build Coastguard Worker key1 = get_port_key_nth(I_FIRST);
511*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_key_create(sh, 123, 456, 0, &key2) >= 0);
512*2d543d20SAndroid Build Coastguard Worker
513*2d543d20SAndroid Build Coastguard Worker /* test */
514*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_exists(sh, key1, &resp) >= 0);
515*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(resp);
516*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_exists(sh, key2, &resp) >= 0);
517*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(!resp);
518*2d543d20SAndroid Build Coastguard Worker
519*2d543d20SAndroid Build Coastguard Worker /* cleanup */
520*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key1);
521*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key2);
522*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
523*2d543d20SAndroid Build Coastguard Worker }
524*2d543d20SAndroid Build Coastguard Worker
525*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_count */
test_port_count(void)526*2d543d20SAndroid Build Coastguard Worker static void test_port_count(void)
527*2d543d20SAndroid Build Coastguard Worker {
528*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
529*2d543d20SAndroid Build Coastguard Worker
530*2d543d20SAndroid Build Coastguard Worker /* setup */
531*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
532*2d543d20SAndroid Build Coastguard Worker
533*2d543d20SAndroid Build Coastguard Worker /* test */
534*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_count(sh, &count) >= 0);
535*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == PORT_COUNT);
536*2d543d20SAndroid Build Coastguard Worker
537*2d543d20SAndroid Build Coastguard Worker /* cleanup */
538*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
539*2d543d20SAndroid Build Coastguard Worker }
540*2d543d20SAndroid Build Coastguard Worker
541*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_iterate */
542*2d543d20SAndroid Build Coastguard Worker unsigned int counter_port_iterate = 0;
543*2d543d20SAndroid Build Coastguard Worker
handler_port_iterate(const semanage_port_t * record,void * varg)544*2d543d20SAndroid Build Coastguard Worker static int handler_port_iterate(const semanage_port_t *record, void *varg)
545*2d543d20SAndroid Build Coastguard Worker {
546*2d543d20SAndroid Build Coastguard Worker counter_port_iterate++;
547*2d543d20SAndroid Build Coastguard Worker return 0;
548*2d543d20SAndroid Build Coastguard Worker }
549*2d543d20SAndroid Build Coastguard Worker
test_port_iterate(void)550*2d543d20SAndroid Build Coastguard Worker static void test_port_iterate(void)
551*2d543d20SAndroid Build Coastguard Worker {
552*2d543d20SAndroid Build Coastguard Worker /* setup */
553*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
554*2d543d20SAndroid Build Coastguard Worker
555*2d543d20SAndroid Build Coastguard Worker /* test */
556*2d543d20SAndroid Build Coastguard Worker semanage_port_iterate(sh, handler_port_iterate, NULL);
557*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(counter_port_iterate == PORT_COUNT);
558*2d543d20SAndroid Build Coastguard Worker
559*2d543d20SAndroid Build Coastguard Worker /* cleanup */
560*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
561*2d543d20SAndroid Build Coastguard Worker }
562*2d543d20SAndroid Build Coastguard Worker
563*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_list */
test_port_list(void)564*2d543d20SAndroid Build Coastguard Worker static void test_port_list(void)
565*2d543d20SAndroid Build Coastguard Worker {
566*2d543d20SAndroid Build Coastguard Worker semanage_port_t **records = NULL;
567*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
568*2d543d20SAndroid Build Coastguard Worker
569*2d543d20SAndroid Build Coastguard Worker /* setup */
570*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_CONNECT);
571*2d543d20SAndroid Build Coastguard Worker
572*2d543d20SAndroid Build Coastguard Worker /* test */
573*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_list(sh, &records, &count) >= 0);
574*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == PORT_COUNT);
575*2d543d20SAndroid Build Coastguard Worker
576*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
577*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL(records[i]);
578*2d543d20SAndroid Build Coastguard Worker
579*2d543d20SAndroid Build Coastguard Worker /* cleanup */
580*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
581*2d543d20SAndroid Build Coastguard Worker semanage_port_free(records[i]);
582*2d543d20SAndroid Build Coastguard Worker
583*2d543d20SAndroid Build Coastguard Worker free(records);
584*2d543d20SAndroid Build Coastguard Worker
585*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_CONNECT);
586*2d543d20SAndroid Build Coastguard Worker }
587*2d543d20SAndroid Build Coastguard Worker
588*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_modify_local, semanage_port_del_local */
test_port_modify_del_local(void)589*2d543d20SAndroid Build Coastguard Worker static void test_port_modify_del_local(void)
590*2d543d20SAndroid Build Coastguard Worker {
591*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port;
592*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port_local;
593*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
594*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
595*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con_local = NULL;
596*2d543d20SAndroid Build Coastguard Worker
597*2d543d20SAndroid Build Coastguard Worker /* setup */
598*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
599*2d543d20SAndroid Build Coastguard Worker port = get_port_nth(I_FIRST);
600*2d543d20SAndroid Build Coastguard Worker semanage_context_from_string(sh, "user_u:role_r:type_t:s0", &con);
601*2d543d20SAndroid Build Coastguard Worker semanage_port_set_con(sh, port, con);
602*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_key_extract(sh, port, &key) >= 0);
603*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL(key);
604*2d543d20SAndroid Build Coastguard Worker
605*2d543d20SAndroid Build Coastguard Worker /* test */
606*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_modify_local(sh, key, port) >= 0);
607*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_query_local(sh, key, &port_local) >= 0);
608*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL_FATAL(port_local);
609*2d543d20SAndroid Build Coastguard Worker
610*2d543d20SAndroid Build Coastguard Worker con_local = semanage_port_get_con(port_local);
611*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_CONTEXT_EQUAL(con, con_local);
612*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port_local);
613*2d543d20SAndroid Build Coastguard Worker
614*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_del_local(sh, key) >= 0);
615*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_query_local(sh, key, &port_local) < 0);
616*2d543d20SAndroid Build Coastguard Worker
617*2d543d20SAndroid Build Coastguard Worker /* cleanup */
618*2d543d20SAndroid Build Coastguard Worker semanage_context_free(con);
619*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
620*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
621*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
622*2d543d20SAndroid Build Coastguard Worker }
623*2d543d20SAndroid Build Coastguard Worker
624*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_query_local */
test_port_query_local(void)625*2d543d20SAndroid Build Coastguard Worker static void test_port_query_local(void)
626*2d543d20SAndroid Build Coastguard Worker {
627*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port = NULL;
628*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port_exp = NULL;
629*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
630*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con = NULL;
631*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con_exp = NULL;
632*2d543d20SAndroid Build Coastguard Worker
633*2d543d20SAndroid Build Coastguard Worker /* setup */
634*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
635*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
636*2d543d20SAndroid Build Coastguard Worker key = get_port_key_nth(I_FIRST);
637*2d543d20SAndroid Build Coastguard Worker port_exp = get_port_nth(I_FIRST);
638*2d543d20SAndroid Build Coastguard Worker
639*2d543d20SAndroid Build Coastguard Worker /* test */
640*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_query_local(sh, key, &port) >= 0);
641*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_low(port) ==
642*2d543d20SAndroid Build Coastguard Worker semanage_port_get_low(port_exp));
643*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_high(port) ==
644*2d543d20SAndroid Build Coastguard Worker semanage_port_get_high(port_exp));
645*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_get_proto(port) ==
646*2d543d20SAndroid Build Coastguard Worker semanage_port_get_proto(port_exp));
647*2d543d20SAndroid Build Coastguard Worker
648*2d543d20SAndroid Build Coastguard Worker con = semanage_port_get_con(port);
649*2d543d20SAndroid Build Coastguard Worker con_exp = semanage_port_get_con(port_exp);
650*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_CONTEXT_EQUAL(con, con_exp);
651*2d543d20SAndroid Build Coastguard Worker
652*2d543d20SAndroid Build Coastguard Worker /* cleanup */
653*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
654*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
655*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port);
656*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port_exp);
657*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
658*2d543d20SAndroid Build Coastguard Worker }
659*2d543d20SAndroid Build Coastguard Worker
660*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_exists_local */
test_port_exists_local(void)661*2d543d20SAndroid Build Coastguard Worker static void test_port_exists_local(void)
662*2d543d20SAndroid Build Coastguard Worker {
663*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key1 = NULL;
664*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key2 = NULL;
665*2d543d20SAndroid Build Coastguard Worker int resp = 42;
666*2d543d20SAndroid Build Coastguard Worker
667*2d543d20SAndroid Build Coastguard Worker /* setup */
668*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
669*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
670*2d543d20SAndroid Build Coastguard Worker key1 = get_port_key_nth(I_FIRST);
671*2d543d20SAndroid Build Coastguard Worker key2 = get_port_key_nth(I_SECOND);
672*2d543d20SAndroid Build Coastguard Worker
673*2d543d20SAndroid Build Coastguard Worker /* test */
674*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_exists_local(sh, key1, &resp) >= 0);
675*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(resp);
676*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_exists_local(sh, key2, &resp) >= 0);
677*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(!resp);
678*2d543d20SAndroid Build Coastguard Worker
679*2d543d20SAndroid Build Coastguard Worker /* cleanup */
680*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
681*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key1);
682*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key2);
683*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
684*2d543d20SAndroid Build Coastguard Worker }
685*2d543d20SAndroid Build Coastguard Worker
686*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_count_local */
test_port_count_local(void)687*2d543d20SAndroid Build Coastguard Worker static void test_port_count_local(void)
688*2d543d20SAndroid Build Coastguard Worker {
689*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
690*2d543d20SAndroid Build Coastguard Worker
691*2d543d20SAndroid Build Coastguard Worker /* setup */
692*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
693*2d543d20SAndroid Build Coastguard Worker
694*2d543d20SAndroid Build Coastguard Worker /* test */
695*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);
696*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 0);
697*2d543d20SAndroid Build Coastguard Worker
698*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
699*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);
700*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 1);
701*2d543d20SAndroid Build Coastguard Worker
702*2d543d20SAndroid Build Coastguard Worker add_local_port(I_SECOND);
703*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);
704*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 2);
705*2d543d20SAndroid Build Coastguard Worker
706*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_SECOND);
707*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);
708*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 1);
709*2d543d20SAndroid Build Coastguard Worker
710*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
711*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_count_local(sh, &count) >= 0);
712*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 0);
713*2d543d20SAndroid Build Coastguard Worker
714*2d543d20SAndroid Build Coastguard Worker /* cleanup */
715*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
716*2d543d20SAndroid Build Coastguard Worker }
717*2d543d20SAndroid Build Coastguard Worker
718*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_iterate_local */
719*2d543d20SAndroid Build Coastguard Worker unsigned int counter_port_iterate_local = 0;
720*2d543d20SAndroid Build Coastguard Worker
handler_port_iterate_local(const semanage_port_t * record,void * varg)721*2d543d20SAndroid Build Coastguard Worker static int handler_port_iterate_local(const semanage_port_t *record, void *varg)
722*2d543d20SAndroid Build Coastguard Worker {
723*2d543d20SAndroid Build Coastguard Worker counter_port_iterate_local++;
724*2d543d20SAndroid Build Coastguard Worker return 0;
725*2d543d20SAndroid Build Coastguard Worker }
726*2d543d20SAndroid Build Coastguard Worker
test_port_iterate_local(void)727*2d543d20SAndroid Build Coastguard Worker static void test_port_iterate_local(void)
728*2d543d20SAndroid Build Coastguard Worker {
729*2d543d20SAndroid Build Coastguard Worker /* setup */
730*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
731*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
732*2d543d20SAndroid Build Coastguard Worker add_local_port(I_SECOND);
733*2d543d20SAndroid Build Coastguard Worker add_local_port(I_THIRD);
734*2d543d20SAndroid Build Coastguard Worker
735*2d543d20SAndroid Build Coastguard Worker /* test */
736*2d543d20SAndroid Build Coastguard Worker semanage_port_iterate_local(sh, handler_port_iterate_local, NULL);
737*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(counter_port_iterate_local == 3);
738*2d543d20SAndroid Build Coastguard Worker
739*2d543d20SAndroid Build Coastguard Worker /* cleanup */
740*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
741*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_SECOND);
742*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_THIRD);
743*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
744*2d543d20SAndroid Build Coastguard Worker }
745*2d543d20SAndroid Build Coastguard Worker
746*2d543d20SAndroid Build Coastguard Worker /* Function semanage_port_list_local */
test_port_list_local(void)747*2d543d20SAndroid Build Coastguard Worker static void test_port_list_local(void)
748*2d543d20SAndroid Build Coastguard Worker {
749*2d543d20SAndroid Build Coastguard Worker semanage_port_t **records = NULL;
750*2d543d20SAndroid Build Coastguard Worker unsigned int count = 42;
751*2d543d20SAndroid Build Coastguard Worker
752*2d543d20SAndroid Build Coastguard Worker /* setup */
753*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
754*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
755*2d543d20SAndroid Build Coastguard Worker add_local_port(I_SECOND);
756*2d543d20SAndroid Build Coastguard Worker add_local_port(I_THIRD);
757*2d543d20SAndroid Build Coastguard Worker
758*2d543d20SAndroid Build Coastguard Worker /* test */
759*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_list_local(sh, &records, &count) >= 0);
760*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(count == 3);
761*2d543d20SAndroid Build Coastguard Worker
762*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
763*2d543d20SAndroid Build Coastguard Worker CU_ASSERT_PTR_NOT_NULL(records[i]);
764*2d543d20SAndroid Build Coastguard Worker
765*2d543d20SAndroid Build Coastguard Worker /* cleanup */
766*2d543d20SAndroid Build Coastguard Worker for (unsigned int i = 0; i < count; i++)
767*2d543d20SAndroid Build Coastguard Worker semanage_port_free(records[i]);
768*2d543d20SAndroid Build Coastguard Worker
769*2d543d20SAndroid Build Coastguard Worker free(records);
770*2d543d20SAndroid Build Coastguard Worker
771*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
772*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_SECOND);
773*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_THIRD);
774*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
775*2d543d20SAndroid Build Coastguard Worker }
776*2d543d20SAndroid Build Coastguard Worker
777*2d543d20SAndroid Build Coastguard Worker /* Internal function semanage_port_validate_local */
helper_port_validate_local_noport(void)778*2d543d20SAndroid Build Coastguard Worker static void helper_port_validate_local_noport(void)
779*2d543d20SAndroid Build Coastguard Worker {
780*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key = NULL;
781*2d543d20SAndroid Build Coastguard Worker int resp = 42;
782*2d543d20SAndroid Build Coastguard Worker
783*2d543d20SAndroid Build Coastguard Worker /* setup */
784*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
785*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
786*2d543d20SAndroid Build Coastguard Worker helper_commit();
787*2d543d20SAndroid Build Coastguard Worker key = get_port_key_nth(I_FIRST);
788*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_exists_local(sh, key, &resp) >= 0);
789*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(resp);
790*2d543d20SAndroid Build Coastguard Worker
791*2d543d20SAndroid Build Coastguard Worker /* test */
792*2d543d20SAndroid Build Coastguard Worker helper_begin_transaction();
793*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
794*2d543d20SAndroid Build Coastguard Worker helper_commit();
795*2d543d20SAndroid Build Coastguard Worker
796*2d543d20SAndroid Build Coastguard Worker /* cleanup */
797*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key);
798*2d543d20SAndroid Build Coastguard Worker helper_begin_transaction();
799*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
800*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
801*2d543d20SAndroid Build Coastguard Worker }
802*2d543d20SAndroid Build Coastguard Worker
helper_port_validate_local_oneport(void)803*2d543d20SAndroid Build Coastguard Worker static void helper_port_validate_local_oneport(void)
804*2d543d20SAndroid Build Coastguard Worker {
805*2d543d20SAndroid Build Coastguard Worker /* setup */
806*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
807*2d543d20SAndroid Build Coastguard Worker add_local_port(I_FIRST);
808*2d543d20SAndroid Build Coastguard Worker
809*2d543d20SAndroid Build Coastguard Worker /* test */
810*2d543d20SAndroid Build Coastguard Worker helper_commit();
811*2d543d20SAndroid Build Coastguard Worker
812*2d543d20SAndroid Build Coastguard Worker /* cleanup */
813*2d543d20SAndroid Build Coastguard Worker helper_begin_transaction();
814*2d543d20SAndroid Build Coastguard Worker delete_local_port(I_FIRST);
815*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
816*2d543d20SAndroid Build Coastguard Worker }
817*2d543d20SAndroid Build Coastguard Worker
helper_port_validate_local_twoports(void)818*2d543d20SAndroid Build Coastguard Worker static void helper_port_validate_local_twoports(void)
819*2d543d20SAndroid Build Coastguard Worker {
820*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key1 = NULL;
821*2d543d20SAndroid Build Coastguard Worker semanage_port_key_t *key2 = NULL;
822*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port1 = NULL;
823*2d543d20SAndroid Build Coastguard Worker semanage_port_t *port2 = NULL;
824*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con1 = NULL;
825*2d543d20SAndroid Build Coastguard Worker semanage_context_t *con2 = NULL;
826*2d543d20SAndroid Build Coastguard Worker
827*2d543d20SAndroid Build Coastguard Worker /* setup */
828*2d543d20SAndroid Build Coastguard Worker setup_handle(SH_TRANS);
829*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_key_create(sh, 101, 200, 0, &key1) >= 0);
830*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_key_create(sh, 201, 300, 0, &key2) >= 0);
831*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_create(sh, &port1) >= 0);
832*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_create(sh, &port2) >= 0);
833*2d543d20SAndroid Build Coastguard Worker
834*2d543d20SAndroid Build Coastguard Worker semanage_port_set_range(port1, 101, 200);
835*2d543d20SAndroid Build Coastguard Worker semanage_port_set_range(port2, 201, 300);
836*2d543d20SAndroid Build Coastguard Worker semanage_port_set_proto(port1, 0);
837*2d543d20SAndroid Build Coastguard Worker semanage_port_set_proto(port2, 0);
838*2d543d20SAndroid Build Coastguard Worker
839*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_context_from_string(sh,
840*2d543d20SAndroid Build Coastguard Worker "system_u:object_r:user_home_t:s0", &con1) >= 0);
841*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_context_from_string(sh,
842*2d543d20SAndroid Build Coastguard Worker "system_u:object_r:user_tmp_t:s0", &con2) >= 0);
843*2d543d20SAndroid Build Coastguard Worker
844*2d543d20SAndroid Build Coastguard Worker semanage_port_set_con(sh, port1, con1);
845*2d543d20SAndroid Build Coastguard Worker semanage_port_set_con(sh, port2, con2);
846*2d543d20SAndroid Build Coastguard Worker
847*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_modify_local(sh, key1, port1) >= 0);
848*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_modify_local(sh, key2, port2) >= 0);
849*2d543d20SAndroid Build Coastguard Worker
850*2d543d20SAndroid Build Coastguard Worker /* test */
851*2d543d20SAndroid Build Coastguard Worker helper_commit();
852*2d543d20SAndroid Build Coastguard Worker
853*2d543d20SAndroid Build Coastguard Worker /* cleanup */
854*2d543d20SAndroid Build Coastguard Worker helper_begin_transaction();
855*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_del_local(sh, key1) >= 0);
856*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(semanage_port_del_local(sh, key2) >= 0);
857*2d543d20SAndroid Build Coastguard Worker semanage_context_free(con2);
858*2d543d20SAndroid Build Coastguard Worker semanage_context_free(con1);
859*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key1);
860*2d543d20SAndroid Build Coastguard Worker semanage_port_key_free(key2);
861*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port1);
862*2d543d20SAndroid Build Coastguard Worker semanage_port_free(port2);
863*2d543d20SAndroid Build Coastguard Worker cleanup_handle(SH_TRANS);
864*2d543d20SAndroid Build Coastguard Worker }
865*2d543d20SAndroid Build Coastguard Worker
test_port_validate_local(void)866*2d543d20SAndroid Build Coastguard Worker static void test_port_validate_local(void)
867*2d543d20SAndroid Build Coastguard Worker {
868*2d543d20SAndroid Build Coastguard Worker helper_port_validate_local_noport();
869*2d543d20SAndroid Build Coastguard Worker helper_port_validate_local_oneport();
870*2d543d20SAndroid Build Coastguard Worker helper_port_validate_local_twoports();
871*2d543d20SAndroid Build Coastguard Worker }
872