1 /* Authors: Christopher Ashworth <cashworth@tresys.com>
2 * Caleb Case <ccase@tresys.com>
3 * Chad Sellers <csellers@tresys.com>
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
25 #include <CUnit/Basic.h>
26 #include <CUnit/Console.h>
27 #include <CUnit/TestDB.h>
28
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <stdlib.h>
33
34 #define DECLARE_SUITE(name) \
35 suite = CU_add_suite(#name, name##_test_init, name##_test_cleanup); \
36 if (NULL == suite) { \
37 CU_cleanup_registry(); \
38 return CU_get_error(); } \
39 if (name##_add_tests(suite)) { \
40 CU_cleanup_registry(); \
41 return CU_get_error(); }
42
usage(char * progname)43 static void usage(char *progname)
44 {
45 printf("usage: %s [options]\n", progname);
46 printf("options:\n");
47 printf("\t-v, --verbose\t\t\tverbose output\n");
48 printf("\t-i, --interactive\t\tinteractive console\n");
49 }
50
do_tests(int interactive,int verbose)51 static bool do_tests(int interactive, int verbose)
52 {
53 CU_pSuite suite = NULL;
54 unsigned int num_failures;
55
56 /* Initialize the CUnit test registry. */
57 if (CUE_SUCCESS != CU_initialize_registry())
58 return CU_get_error();
59
60 DECLARE_SUITE(semanage_store);
61 DECLARE_SUITE(semanage_utilities);
62
63 if (verbose)
64 CU_basic_set_mode(CU_BRM_VERBOSE);
65 else
66 CU_basic_set_mode(CU_BRM_NORMAL);
67
68 if (interactive)
69 CU_console_run_tests();
70 else
71 CU_basic_run_tests();
72 num_failures = CU_get_number_of_tests_failed();
73 CU_cleanup_registry();
74 return CU_get_error() == CUE_SUCCESS && num_failures == 0;
75
76 }
77
78 /* The main function for setting up and running the libsemanage unit tests.
79 * Returns a CUE_SUCCESS on success, or a CUnit error code on failure.
80 */
main(int argc,char ** argv)81 int main(int argc, char **argv)
82 {
83 int i, verbose = 1, interactive = 0;
84
85 struct option opts[] = {
86 {"verbose", 0, NULL, 'v'},
87 {"interactive", 0, NULL, 'i'},
88 {NULL, 0, NULL, 0}
89 };
90
91 while ((i = getopt_long(argc, argv, "vi", opts, NULL)) != -1) {
92 switch (i) {
93 case 'v':
94 verbose = 1;
95 break;
96 case 'i':
97 interactive = 1;
98 break;
99 case 'h':
100 default:{
101 usage(argv[0]);
102 exit(1);
103 }
104 }
105 }
106
107 if (!do_tests(interactive, verbose))
108 return -1;
109
110 return 0;
111 }
112