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