Blame tests/libsemanage-tests.c

Packit 366192
/* Authors: Christopher Ashworth <cashworth@tresys.com>
Packit 366192
 *          Caleb Case <ccase@tresys.com>
Packit 366192
 *          Chad Sellers <csellers@tresys.com>
Packit 366192
 *
Packit 366192
 * Copyright (C) 2006 Tresys Technology, LLC
Packit 366192
 *
Packit 366192
 *  This library is free software; you can redistribute it and/or
Packit 366192
 *  modify it under the terms of the GNU Lesser General Public
Packit 366192
 *  License as published by the Free Software Foundation; either
Packit 366192
 *  version 2.1 of the License, or (at your option) any later version.
Packit 366192
 *
Packit 366192
 *  This library is distributed in the hope that it will be useful,
Packit 366192
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 366192
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 366192
 *  Lesser General Public License for more details.
Packit 366192
 *
Packit 366192
 *  You should have received a copy of the GNU Lesser General Public
Packit 366192
 *  License along with this library; if not, write to the Free Software
Packit 366192
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 366192
 */
Packit 366192
Packit 366192
#include "test_semanage_store.h"
Packit 366192
#include "test_utilities.h"
Packit 366192
Packit 366192
#include <CUnit/Basic.h>
Packit 366192
#include <CUnit/Console.h>
Packit 366192
#include <CUnit/TestDB.h>
Packit 366192
Packit 366192
#include <stdbool.h>
Packit 366192
#include <stdio.h>
Packit 366192
#include <getopt.h>
Packit 366192
#include <stdlib.h>
Packit 366192
Packit 366192
#define DECLARE_SUITE(name) \
Packit 366192
	suite = CU_add_suite(#name, name##_test_init, name##_test_cleanup); \
Packit 366192
	if (NULL == suite) { \
Packit 366192
		CU_cleanup_registry(); \
Packit 366192
		return CU_get_error(); } \
Packit 366192
	if (name##_add_tests(suite)) { \
Packit 366192
		CU_cleanup_registry(); \
Packit 366192
		return CU_get_error(); }
Packit 366192
Packit 366192
static void usage(char *progname)
Packit 366192
{
Packit 366192
	printf("usage:  %s [options]\n", progname);
Packit 366192
	printf("options:\n");
Packit 366192
	printf("\t-v, --verbose\t\t\tverbose output\n");
Packit 366192
	printf("\t-i, --interactive\t\tinteractive console\n");
Packit 366192
}
Packit 366192
Packit 366192
static bool do_tests(int interactive, int verbose)
Packit 366192
{
Packit 366192
	CU_pSuite suite = NULL;
Packit 366192
	unsigned int num_failures;
Packit 366192
Packit 366192
	/* Initialize the CUnit test registry. */
Packit 366192
	if (CUE_SUCCESS != CU_initialize_registry())
Packit 366192
		return CU_get_error();
Packit 366192
Packit 366192
	DECLARE_SUITE(semanage_store);
Packit 366192
	DECLARE_SUITE(semanage_utilities);
Packit 366192
Packit 366192
	if (verbose)
Packit 366192
		CU_basic_set_mode(CU_BRM_VERBOSE);
Packit 366192
	else
Packit 366192
		CU_basic_set_mode(CU_BRM_NORMAL);
Packit 366192
Packit 366192
	if (interactive)
Packit 366192
		CU_console_run_tests();
Packit 366192
	else
Packit 366192
		CU_basic_run_tests();
Packit 366192
	num_failures = CU_get_number_of_tests_failed();
Packit 366192
	CU_cleanup_registry();
Packit 366192
	return CU_get_error() == CUE_SUCCESS && num_failures == 0;
Packit 366192
Packit 366192
}
Packit 366192
Packit 366192
/* The main function for setting up and running the libsemanage unit tests.
Packit 366192
 * Returns a CUE_SUCCESS on success, or a CUnit error code on failure.
Packit 366192
 */
Packit 366192
int main(int argc, char **argv)
Packit 366192
{
Packit 366192
	int i, verbose = 1, interactive = 0;
Packit 366192
Packit 366192
	struct option opts[] = {
Packit 366192
		{"verbose", 0, NULL, 'v'},
Packit 366192
		{"interactive", 0, NULL, 'i'},
Packit 366192
		{NULL, 0, NULL, 0}
Packit 366192
	};
Packit 366192
Packit 366192
	while ((i = getopt_long(argc, argv, "vi", opts, NULL)) != -1) {
Packit 366192
		switch (i) {
Packit 366192
		case 'v':
Packit 366192
			verbose = 1;
Packit 366192
			break;
Packit 366192
		case 'i':
Packit 366192
			interactive = 1;
Packit 366192
			break;
Packit 366192
		case 'h':
Packit 366192
		default:{
Packit 366192
				usage(argv[0]);
Packit 366192
				exit(1);
Packit 366192
			}
Packit 366192
		}
Packit 366192
	}
Packit 366192
Packit 366192
	if (!do_tests(interactive, verbose))
Packit 366192
		return -1;
Packit 366192
Packit 366192
	return 0;
Packit 366192
}