OpenDNSSEC-enforcer  1.4.5
test_routines.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /*+
28  * test_routines.c - Unit Testing Routines
29  *
30  * Description:
31  * These are common routines used in various unit tests.
32  *
33  * The unit testing routines made use of the CUint framework,
34  * available from http://cunit.sourcefourge.net.
35 -*/
36 
37 #include "config.h"
38 
39 #include <assert.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 
46 #include "ksm/memory.h"
47 #include "test_routines.h"
48 
49 static int m_automatic = 0; /* Set 1 for automatic mode */
50 static int m_basic = 0; /* Set 1 for basic mode */
51 static int m_console = 0; /* Set 1 for console mode */
52 static int m_list = 0; /* Set 1 for list mode */
53 static int m_curses= 0; /* Set 1 for for curses mode */
54 static char* m_filename = NULL; /* If a filename is given */
55 
56 
57 
58 /*
59  * TestHelp - Print Help
60  *
61  * Description:
62  * Prints help for the test driver. This just lists the most common
63  * options.
64  *
65  * Arguments:
66  * None.
67  */
68 
69 static void TestHelp(void)
70 {
71  static const char* lines[] = {
72  "The following switches are available:",
73  "",
74  " -a Automatic - run tests in automatic mode. If the -f switch is also",
75  " given, the output is set to a file whose root name is given here.",
76  " Two files are produced, <root>-Listing.xml, listing the tests,",
77  " and <root>-Results.xml listing the contents of the tests. If not",
78  " specified, a default name (CUnitAutomated) is used instead.",
79  " -b Basic - run tests in basic mode. (This is the default.)",
80  " -c Console - run tests using console mode.",
81  " -f file Name of the file for automatic or list mode.",
82  " -h Print this message and exit.",
83  " -l List tests to file.",
84  " -u Curses - run tests using curses interface.",
85  "",
86  " (The options 'a', 'b', 'c', 'l' and 'u' are mutually exclusive.)",
87  NULL
88  };
89  int i;
90 
91  for (i = 0; lines[i]; ++i) {
92  printf("%s\n", lines[i]);
93  }
94 }
95 
96 
97 
98 /*+
99  * TestCommandLine - Process Command Line
100  *
101  * Description:
102  * Parses the command line and sets the flags. (See TestHelp for a list
103  * of supported flags.) If the help flag is encountered, prints the help
104  * and exits.
105  *
106  * Arguments:
107  * int argc, char **argv
108  * Standard command-line arguments.
109 -*/
110 
111 static void TestCommandLine(int argc, char** argv)
112 {
113  int c = 0; /* Option found with getopt() */
114  /* extern char* optarg from getopt(3) */
115  /* extern int optind from getopt(3) */
116  /* extern int optopt from getopt(3) */
117 
118  while ((c = getopt(argc, argv, "abcf:hlu")) != -1) {
119  switch (c) {
120  case 'a':
121  m_automatic = 1;
122  break;
123 
124  case 'b':
125  m_basic = 1;
126  break;
127 
128  case 'c':
129  m_console = 1;
130  break;
131 
132  case 'f':
133  m_filename = optarg;
134  break;
135 
136  case 'h':
137  TestHelp();
138  exit(0);
139 
140  case 'l':
141  m_list = 1;
142  break;
143 
144  case 'u':
145  m_curses = 1;
146  break;
147 
148  default:
149  fprintf(stderr, "Unrecognised switch: -%c\n", optopt);
150  exit(1);
151  }
152  }
153 }
154 
155 
156 /*
157  * TestInitialize - Initialize Tests
158  *
159  * Description:
160  * Processes options and initializes test registry.
161  *
162  * Arguments:
163  * int argc (input)
164  * char **argv (input)
165  * Arguments passed to main().
166  */
167 
168 void TestInitialize(int argc, char** argv)
169 {
170  int sum; /* For checking options given */
171 
172  /* Process command-line options */
173 
174  TestCommandLine(argc, argv);
175 
176  /* Check for conflicting options */
177 
180  if (sum == 0) {
181  m_basic = 1; /* Flag as the default option */
182  }
183  else if (sum > 1) {
184  printf("Conflicting options given\n\n");
185  TestHelp();
186  exit(1);
187  }
188 
189  return;
190 }
191 
192 
193 /*
194  * TestGetXxx - Access Methods
195  *
196  * Description:
197  * Self-explanatory routine to obtain the command-line options.
198  *
199  * Arguments:
200  * None.
201  *
202  * Returns:
203  * Various.
204  */
205 
207 {
208  /* Look for the "-a" flag. */
209 
210  return m_automatic;
211 }
212 
213 int TestGetBasic(void)
214 {
215  return m_basic;
216 }
217 
218 int TestGetConsole(void)
219 {
220  return m_console;
221 }
222 
223 int TestGetList(void)
224 {
225  return m_list;
226 }
227 
228 int TestGetCurses(void)
229 {
230  return m_curses;
231 }
232 
233 
234 
235 /*
236  * TestGetFilename - Get Output Filename
237  *
238  * Description:
239  * Returns a pointer to a string holding the filename specified on the
240  * command line with the "-f filename" extension.
241  *
242  * Arguments:
243  * None.
244  *
245  * Returns:
246  * const char*
247  * Pointer to name of file (excluding leading "f:") or NULL if
248  * not found. This string should not be freed by the caller.
249  */
250 
251 const char* TestGetFilename(void)
252 {
253  return m_filename;
254 }