OpenDNSSEC-enforcer  1.4.1
test_routines_cunit.c
Go to the documentation of this file.
1 /*
2  * $Id: test_routines_cunit.c 4643 2011-03-24 14:10:24Z sion $
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_cunit.c
31  *
32  * Description:
33  * This module contains some shells around the CUnit routines.
34  *
35  * The module is not included in libcommon.a, so avoiding the need to
36  * include libcunit.a into any code using the library; it must be included
37  * separately in the link command line.
38 -*/
39 
40 #include <string.h>
41 #include <strings.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 
46 #include "CUnit/Automated.h"
47 #include "CUnit/Basic.h"
48 #include "CUnit/Console.h"
49 
50 #include "test_routines.h"
51 
52 /*+
53  * TcuInitialize - Initialize CUnit Wrapper
54  *
55  * Description:
56  * Initializes the CUnit registry. If the initialization fails, the
57  * program terminates.
58  *
59  * This should be called after TestInitialize().
60  *
61  * Arguments:
62  * None.
63  */
64 
65 void TcuInitialize(void)
66 {
67  if (CU_initialize_registry() != CUE_SUCCESS) {
68  fprintf(stderr, "Failed to initialize the CUnit registry.\n");
69  exit(1);
70  }
71  return;
72 }
73 
74 
75 
76 /*
77  * Tcu - Execute Tests
78  *
79  * Description:
80  * Executes the tests and cleans up the registry afterwards.
81  *
82  * Arguments:
83  * None.
84  */
85 
86 void TcuExecute(void)
87 {
88  if (TestGetAutomatic()) {
89  if (TestGetFilename()) {
90  CU_set_output_filename(TestGetFilename());
91  }
92  CU_automated_run_tests();
93  }
94  else if (TestGetBasic()) {
95  CU_basic_set_mode(CU_BRM_VERBOSE);
96  (void) CU_basic_run_tests();
97  }
98  else if (TestGetConsole()) {
99  CU_console_run_tests();
100  }
101  else if (TestGetList()) {
102  if (TestGetFilename()) {
103  CU_set_output_filename(TestGetFilename());
104  }
105  (void) CU_list_tests_to_file();
106  }
107 
108  if (CU_get_number_of_tests_failed()) {
109  return;
110  }
111 
112  /* Clean up the registry */
113 
114  CU_cleanup_registry();
115 }
116 
117 
118 
119 /*
120  * TcuCreateSuite - Create Suite of Tests
121  *
122  * Description:
123  * Creates a suite of tests. This handles the common actions in all test
124  * suite creation routines.
125  *
126  * Arguments:
127  * const char* title (input)
128  * Title for the suite.
129  *
130  * int (*init)() (input)
131  * Pointer to the initialization routine. This may be NULL if there is
132  * no initialization routine for the suite.
133  *
134  * int (*teardown)() (input)
135  * Pointer to the teardown routine. This may be NULL if there is no
136  * teardown routine for the suite.
137  *
138  * struct test_testdef* tests (input)
139  * Pointer to an array of test definitions structures defining the
140  * tests. This array should end with a pair of NULLs.
141  *
142  * Returns:
143  * int
144  * 0 => Success
145  * <>0 => CUnit error code
146  */
147 
148 int TcuCreateSuite(const char* title, int (*init)(), int (*teardown)(),
149  struct test_testdef* tests)
150 {
151  int i; /* Loop counter */
152  CU_pSuite pSuite; /* Pointer to the test suite */
153 
154  /* Create the suite */
155 
156  pSuite = CU_add_suite(title, init, teardown);
157  if (NULL == pSuite) {
158  CU_cleanup_registry();
159  return CU_get_error();
160  }
161 
162  /* Add the tests to the suite */
163 
164  i = 0;
165  while (tests[i].title) {
166  if (CU_add_test(pSuite, tests[i].title, tests[i].function) == NULL) {
167  CU_cleanup_registry();
168  return CU_get_error();
169  }
170  ++i;
171  }
172 
173  return 0;
174 }