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