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