xref: /aosp_15_r20/external/selinux/libsemanage/tests/libsemanage-tests.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 /* Authors: Christopher Ashworth <[email protected]>
2  *          Caleb Case <[email protected]>
3  *          Chad Sellers <[email protected]>
4  *
5  * Copyright (C) 2006 Tresys Technology, LLC
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2.1 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "test_semanage_store.h"
23 #include "test_utilities.h"
24 #include "test_handle.h"
25 #include "test_bool.h"
26 #include "test_fcontext.h"
27 #include "test_iface.h"
28 #include "test_ibendport.h"
29 #include "test_node.h"
30 #include "test_port.h"
31 #include "test_user.h"
32 #include "test_other.h"
33 
34 #include <CUnit/Basic.h>
35 #include <CUnit/Console.h>
36 #include <CUnit/TestDB.h>
37 
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <getopt.h>
41 #include <stdlib.h>
42 
43 #define DECLARE_SUITE(name) \
44 	do { \
45 		suite = CU_add_suite(#name, name##_test_init, name##_test_cleanup); \
46 		if (NULL == suite) { \
47 			CU_cleanup_registry(); \
48 			return CU_get_error(); \
49 		} \
50 		if (name##_add_tests(suite)) { \
51 			CU_cleanup_registry(); \
52 			return CU_get_error(); \
53 		} \
54 	} while (0)
55 
usage(char * progname)56 static void usage(char *progname)
57 {
58 	printf("usage:  %s [options]\n", progname);
59 	printf("options:\n");
60 	printf("\t-v, --verbose\t\t\tverbose output\n");
61 	printf("\t-i, --interactive\t\tinteractive console\n");
62 }
63 
do_tests(int interactive,int verbose)64 static bool do_tests(int interactive, int verbose)
65 {
66 	CU_pSuite suite = NULL;
67 	unsigned int num_failures;
68 
69 	/* Initialize the CUnit test registry. */
70 	if (CUE_SUCCESS != CU_initialize_registry())
71 		return CU_get_error();
72 
73 	DECLARE_SUITE(semanage_store);
74 	DECLARE_SUITE(semanage_utilities);
75 	DECLARE_SUITE(handle);
76 	DECLARE_SUITE(boolean);
77 	DECLARE_SUITE(fcontext);
78 	DECLARE_SUITE(iface);
79 	DECLARE_SUITE(ibendport);
80 	DECLARE_SUITE(node);
81 	DECLARE_SUITE(port);
82 	DECLARE_SUITE(user);
83 	DECLARE_SUITE(other);
84 
85 	if (verbose)
86 		CU_basic_set_mode(CU_BRM_VERBOSE);
87 	else
88 		CU_basic_set_mode(CU_BRM_NORMAL);
89 
90 	if (interactive)
91 		CU_console_run_tests();
92 	else
93 		CU_basic_run_tests();
94 	num_failures = CU_get_number_of_tests_failed();
95 	CU_cleanup_registry();
96 	return CU_get_error() == CUE_SUCCESS && num_failures == 0;
97 
98 }
99 
100 /* The main function for setting up and running the libsemanage unit tests.
101  * Returns a CUE_SUCCESS on success, or a CUnit error code on failure.
102  */
main(int argc,char ** argv)103 int main(int argc, char **argv)
104 {
105 	int i, verbose = 1, interactive = 0;
106 
107 	struct option opts[] = {
108 		{"verbose", 0, NULL, 'v'},
109 		{"interactive", 0, NULL, 'i'},
110 		{NULL, 0, NULL, 0}
111 	};
112 
113 	while ((i = getopt_long(argc, argv, "vi", opts, NULL)) != -1) {
114 		switch (i) {
115 		case 'v':
116 			verbose = 1;
117 			break;
118 		case 'i':
119 			interactive = 1;
120 			break;
121 		case 'h':
122 		default:{
123 				usage(argv[0]);
124 				exit(1);
125 			}
126 		}
127 	}
128 
129 	if (!do_tests(interactive, verbose))
130 		return -1;
131 
132 	return 0;
133 }
134