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