OpenDNSSEC-enforcer  1.3.4
/build/buildd/opendnssec-1.3.4/enforcer/test/cunit/test_routines_cunit.c
Go to the documentation of this file.
00001 /*
00002  * $Id: test_routines_cunit.c 4643 2011-03-24 14:10:24Z sion $
00003  *
00004  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00021  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00023  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 
00029 /*+
00030  * test_routines_cunit.c
00031  *
00032  * Description:
00033  *      This module contains some shells around the CUnit routines.
00034  *
00035  *      The module is not included in libcommon.a, so avoiding the need to
00036  *      include libcunit.a into any code using the library; it must be included
00037  *      separately in the link command line.
00038 -*/
00039 
00040 #include <string.h>
00041 #include <strings.h>
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <unistd.h>
00045 
00046 #include "CUnit/Automated.h"
00047 #include "CUnit/Basic.h"
00048 #include "CUnit/Console.h"
00049 
00050 #include "test_routines.h"
00051 
00052 /*+
00053  * TcuInitialize - Initialize CUnit Wrapper
00054  *
00055  * Description:
00056  *      Initializes the CUnit registry.  If the initialization fails, the
00057  *      program terminates.
00058  *
00059  *      This should be called after TestInitialize().
00060  *
00061  * Arguments:
00062  *      None.
00063  */
00064 
00065 void TcuInitialize(void)
00066 {
00067     if (CU_initialize_registry() != CUE_SUCCESS) {
00068         fprintf(stderr, "Failed to initialize the CUnit registry.\n");
00069         exit(1);
00070     }
00071     return;
00072 }
00073 
00074 
00075 
00076 /*
00077  * Tcu - Execute Tests
00078  *
00079  * Description:
00080  *      Executes the tests and cleans up the registry afterwards.
00081  *
00082  * Arguments:
00083  *      None.
00084  */
00085 
00086 void TcuExecute(void)
00087 {
00088     if (TestGetAutomatic()) {
00089         if (TestGetFilename()) {
00090             CU_set_output_filename(TestGetFilename());
00091         }
00092         CU_automated_run_tests();
00093     }
00094     else if (TestGetBasic()) {
00095         CU_basic_set_mode(CU_BRM_VERBOSE);
00096         (void) CU_basic_run_tests();
00097     }
00098     else if (TestGetConsole()) {
00099         CU_console_run_tests();
00100     }
00101     else if (TestGetList()) {
00102         if (TestGetFilename()) {
00103             CU_set_output_filename(TestGetFilename());
00104         }
00105         (void) CU_list_tests_to_file();
00106     }
00107 
00108         if (CU_get_number_of_tests_failed()) {
00109                 return;
00110         }
00111 
00112     /* Clean up the registry */
00113 
00114     CU_cleanup_registry();
00115 }
00116 
00117 
00118     
00119 /*
00120  * TcuCreateSuite - Create Suite of Tests
00121  *
00122  * Description:
00123  *      Creates a suite of tests.  This handles the common actions in all test
00124  *      suite creation routines.
00125  *
00126  * Arguments:
00127  *      const char* title (input)
00128  *          Title for the suite.
00129  *
00130  *      int (*init)() (input)
00131  *          Pointer to the initialization routine.  This may be NULL if there is
00132  *          no initialization routine for the suite.
00133  *
00134  *      int (*teardown)() (input)
00135  *          Pointer to the teardown routine.  This may be NULL if there is no
00136  *          teardown routine for the suite.
00137  *
00138  *      struct test_testdef* tests (input)
00139  *          Pointer to an array of test definitions structures defining the
00140  *          tests. This array should end with a pair of NULLs.
00141  *
00142  * Returns:
00143  *      int
00144  *          0   => Success
00145  *          <>0 => CUnit error code
00146  */
00147 
00148 int TcuCreateSuite(const char* title, int (*init)(), int (*teardown)(),
00149     struct test_testdef* tests)
00150 {
00151     int i;                  /* Loop counter */
00152         CU_pSuite pSuite;               /* Pointer to the test suite */
00153 
00154     /* Create the suite */
00155 
00156     pSuite = CU_add_suite(title, init, teardown);
00157     if (NULL == pSuite) {
00158         CU_cleanup_registry();
00159         return CU_get_error();
00160     }
00161 
00162     /* Add the tests to the suite */
00163 
00164     i = 0;
00165     while (tests[i].title) {
00166         if (CU_add_test(pSuite, tests[i].title, tests[i].function) == NULL) {
00167             CU_cleanup_registry();
00168             return CU_get_error();
00169         }
00170         ++i;
00171     }
00172 
00173     return 0;
00174 }