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