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