OpenDNSSEC-enforcer  1.3.8
/build/buildd/opendnssec-1.3.8/enforcer/test/cunit/test_routines.c
Go to the documentation of this file.
00001 /*
00002  * $Id: test_routines.c 3811 2010-08-26 15:05:19Z jakob $
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.c - Unit Testing Routines
00031  *
00032  * Description:
00033  *      These are common routines used in various unit tests.
00034  *
00035  *      The unit testing routines made use of the CUint framework,
00036  *      available from http://cunit.sourcefourge.net.
00037 -*/
00038 
00039 #include "config.h"
00040 
00041 #include <assert.h>
00042 #include <string.h>
00043 #include <strings.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 
00048 #include "ksm/memory.h"
00049 #include "test_routines.h"
00050 
00051 static int m_automatic = 0;     /* Set 1 for automatic mode */
00052 static int m_basic = 0;         /* Set 1 for basic mode */
00053 static int m_console = 0;       /* Set 1 for console mode */
00054 static int m_list = 0;          /* Set 1 for list mode */
00055 static int m_curses= 0;         /* Set 1 for for curses mode */
00056 static char* m_filename = NULL; /* If a filename is given */
00057 
00058 
00059 
00060 /*
00061  * TestHelp - Print Help
00062  *
00063  * Description:
00064  *      Prints help for the test driver.  This just lists the most common
00065  *      options.
00066  *
00067  * Arguments:
00068  *      None.
00069  */
00070 
00071 static void TestHelp(void)
00072 {
00073     static const char* lines[] = {
00074         "The following switches are available:",
00075         "",
00076         "    -a        Automatic - run tests in automatic mode. If the -f switch is also",
00077         "              given, the output is set to a file whose root name is given here.",
00078         "              Two files are produced, <root>-Listing.xml, listing the tests,",
00079         "              and <root>-Results.xml listing the contents of the tests.  If not",
00080         "              specified, a default name (CUnitAutomated) is used instead.",
00081         "    -b        Basic - run tests in basic mode. (This is the default.)",
00082         "    -c        Console - run tests using console mode.",
00083         "    -f file   Name of the file for automatic or list mode.",
00084         "    -h        Print this message and exit.",
00085         "    -l        List tests to file.",
00086         "    -u        Curses - run tests using curses interface.",
00087         "",
00088         "    (The options 'a', 'b', 'c', 'l' and 'u' are mutually exclusive.)",
00089         NULL
00090     };
00091     int i;
00092 
00093     for (i = 0; lines[i]; ++i) {
00094         printf("%s\n", lines[i]);
00095     }
00096 }
00097 
00098 
00099 
00100 /*+
00101  * TestCommandLine - Process Command Line
00102  *
00103  * Description:
00104  *      Parses the command line and sets the flags.  (See TestHelp for a list
00105  *      of supported flags.)  If the help flag is encountered, prints the help
00106  *      and exits.
00107  *
00108  * Arguments:
00109  *      int argc, char **argv
00110  *          Standard command-line arguments.
00111 -*/
00112 
00113 static void TestCommandLine(int argc, char** argv)
00114 {
00115     int c = 0;      /* Option found with getopt() */
00116     /* extern char* optarg      from getopt(3) */
00117     /* extern int   optind      from getopt(3) */
00118     /* extern int   optopt      from getopt(3) */
00119 
00120     while ((c = getopt(argc, argv, "abcf:hlu")) != -1) {
00121         switch (c) {
00122         case 'a':
00123             m_automatic = 1;
00124             break;
00125 
00126         case 'b':
00127             m_basic = 1;
00128             break;
00129 
00130         case 'c':
00131             m_console = 1;
00132             break;
00133 
00134         case 'f':
00135             m_filename = optarg;
00136             break;
00137 
00138         case 'h':
00139             TestHelp();
00140             exit(0);
00141 
00142         case 'l':
00143             m_list = 1;
00144             break;
00145 
00146         case 'u':
00147             m_curses = 1;
00148             break;
00149 
00150         default:
00151             fprintf(stderr, "Unrecognised switch: -%c\n", optopt);
00152             exit(1);
00153         }
00154     }
00155 }
00156 
00157 
00158 /*
00159  * TestInitialize - Initialize Tests
00160  *
00161  * Description:
00162  *      Processes options and initializes test registry.
00163  *
00164  * Arguments:
00165  *      int argc (input)
00166  *      char **argv (input)
00167  *          Arguments passed to main().
00168  */
00169 
00170 void TestInitialize(int argc, char** argv)
00171 {
00172     int     sum;        /* For checking options given */
00173 
00174     /* Process command-line options */
00175 
00176     TestCommandLine(argc, argv);
00177 
00178     /* Check for conflicting options */
00179 
00180     sum = TestGetAutomatic() + TestGetBasic() + TestGetConsole() +
00181         TestGetCurses() + TestGetList();
00182     if (sum == 0) {
00183         m_basic = 1;    /* Flag as the default option */
00184     }
00185     else if (sum > 1) {
00186         printf("Conflicting options given\n\n");
00187         TestHelp();
00188         exit(1);
00189     }
00190 
00191     return;
00192 }
00193 
00194 
00195 /*
00196  * TestGetXxx - Access Methods
00197  *
00198  * Description:
00199  *      Self-explanatory routine to obtain the command-line options.
00200  *
00201  * Arguments:
00202  *      None.
00203  *
00204  * Returns:
00205  *      Various.
00206  */
00207 
00208 int TestGetAutomatic(void)
00209 {
00210     /* Look for the "-a" flag. */
00211 
00212     return m_automatic;
00213 }
00214 
00215 int TestGetBasic(void)
00216 {
00217     return m_basic;
00218 }
00219 
00220 int TestGetConsole(void)
00221 {
00222     return m_console;
00223 }
00224 
00225 int TestGetList(void)
00226 {
00227     return m_list;
00228 }
00229 
00230 int TestGetCurses(void)
00231 {
00232     return m_curses;
00233 }
00234 
00235 
00236 
00237 /*
00238  * TestGetFilename - Get Output Filename
00239  *
00240  * Description:
00241  *      Returns a pointer to a string holding the filename specified on the
00242  *      command line with the "-f filename" extension.
00243  *
00244  * Arguments:
00245  *      None.
00246  *
00247  * Returns:
00248  *      const char*
00249  *          Pointer to name of file (excluding leading "f:") or NULL if
00250  *          not found.  This string should not be freed by the caller.
00251  */
00252 
00253 const char* TestGetFilename(void)
00254 {
00255     return m_filename;
00256 }