selinux/libsemanage/tests/libsemanage-tests.c
Nicolas Iooss e51b233831 libsemanage/tests: make "make test" fail when a CUnit test fails
When modifications to libsemanage functions break the test cases tested
with the CUnit framework, "make test" currently succeeds, even though it
prints an output similar to:

    Suite: semanage_store
      Test: semanage_store_access_check ...passed
      Test: semanage_get_lock ...passed
      Test: semanage_nc_sort ...passed
    Suite: semanage_utilities
      Test: semanage_is_prefix ...passed
      Test: semanage_split_on_space ...FAILED
        1. test_utilities.c:150  - CU_ASSERT_STRING_EQUAL(temp,"baz")
      Test: semanage_split ...passed
      Test: semanage_list ...passed
      Test: semanage_str_count ...passed
      Test: semanage_rtrim ...passed
      Test: semanage_str_replace ...passed
      Test: semanage_findval ...passed
      Test: slurp_file_filter ...passed

Like commit 2489b50a91 ("libsepol: make "make test" fails when a CUnit
test fails") did for libsepol tests, modify the logic of function
do_tests() to return an error value when there has been at least one
failure. This makes "make test" fail as expected.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
2016-12-21 13:40:11 -05:00

112 lines
2.8 KiB
C

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