1 | /*
|
---|
2 | * Copyright (c) 2012-2013 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file
|
---|
30 | *
|
---|
31 | * Test execution routines.
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include "internal.h"
|
---|
35 |
|
---|
36 | #ifndef PCUT_NO_LONG_JUMP
|
---|
37 | #pragma warning(push, 0)
|
---|
38 | #include <setjmp.h>
|
---|
39 | #pragma warning(pop)
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #ifndef PCUT_NO_LONG_JUMP
|
---|
43 | /** Long-jump buffer. */
|
---|
44 | static jmp_buf start_test_jump;
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /** Whether to run a tear-down function on a failure.
|
---|
48 | *
|
---|
49 | * Used to determine whether we are already in a tear-down context.
|
---|
50 | */
|
---|
51 | static int execute_teardown_on_failure;
|
---|
52 |
|
---|
53 | /** Whether to report test result at all.
|
---|
54 | *
|
---|
55 | * Used to determine whether we are the forked or the parent process.
|
---|
56 | */
|
---|
57 | static int report_test_result;
|
---|
58 |
|
---|
59 | /** Whether to print test error.
|
---|
60 | *
|
---|
61 | * Used to determine whether we are the forked or the parent process.
|
---|
62 | */
|
---|
63 | static int print_test_error;
|
---|
64 |
|
---|
65 | /** Whether leaving a test means a process exit. */
|
---|
66 | static int leave_means_exit;
|
---|
67 |
|
---|
68 | /** Pointer to currently running test. */
|
---|
69 | static pcut_item_t *current_test = NULL;
|
---|
70 |
|
---|
71 | /** Pointer to current test suite. */
|
---|
72 | static pcut_item_t *current_suite = NULL;
|
---|
73 |
|
---|
74 | /** A NULL-like suite. */
|
---|
75 | static pcut_item_t default_suite;
|
---|
76 | static int default_suite_initialized = 0;
|
---|
77 |
|
---|
78 | static void init_default_suite_when_needed() {
|
---|
79 | if (default_suite_initialized) {
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | default_suite.id = -1;
|
---|
83 | default_suite.kind = PCUT_KIND_TESTSUITE;
|
---|
84 | default_suite.previous = NULL;
|
---|
85 | default_suite.next = NULL;
|
---|
86 | default_suite.name = "Default";
|
---|
87 | default_suite.setup_func = NULL;
|
---|
88 | default_suite.teardown_func = NULL;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Find the suite given test belongs to.
|
---|
92 | *
|
---|
93 | * @param it The test.
|
---|
94 | * @return Always a valid test suite item.
|
---|
95 | */
|
---|
96 | static pcut_item_t *pcut_find_parent_suite(pcut_item_t *it) {
|
---|
97 | while (it != NULL) {
|
---|
98 | if (it->kind == PCUT_KIND_TESTSUITE) {
|
---|
99 | return it;
|
---|
100 | }
|
---|
101 | it = it->previous;
|
---|
102 | }
|
---|
103 | init_default_suite_when_needed();
|
---|
104 | return &default_suite;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Run a set-up (tear-down) function.
|
---|
108 | *
|
---|
109 | * @param func Function to run (can be NULL).
|
---|
110 | */
|
---|
111 | static void run_setup_teardown(pcut_setup_func_t func) {
|
---|
112 | if (func != NULL) {
|
---|
113 | func();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /** Terminate current test with given outcome.
|
---|
118 | *
|
---|
119 | * @warning This function may execute a long jump or terminate
|
---|
120 | * current process.
|
---|
121 | *
|
---|
122 | * @param outcome Outcome of the current test.
|
---|
123 | */
|
---|
124 | static void leave_test(int outcome) {
|
---|
125 | PCUT_DEBUG("leave_test(outcome=%d), will_exit=%s", outcome,
|
---|
126 | leave_means_exit ? "yes" : "no");
|
---|
127 | if (leave_means_exit) {
|
---|
128 | exit(outcome);
|
---|
129 | }
|
---|
130 |
|
---|
131 | #ifndef PCUT_NO_LONG_JUMP
|
---|
132 | longjmp(start_test_jump, 1);
|
---|
133 | #endif
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Process a failed assertion.
|
---|
137 | *
|
---|
138 | * @warning This function calls leave_test() and typically will not
|
---|
139 | * return.
|
---|
140 | *
|
---|
141 | * @param message Message describing the failure.
|
---|
142 | */
|
---|
143 | void pcut_failed_assertion(const char *message) {
|
---|
144 | static const char *prev_message = NULL;
|
---|
145 | /*
|
---|
146 | * The assertion failed. We need to abort the current test,
|
---|
147 | * inform the user and perform some clean-up. That could
|
---|
148 | * include running the tear-down routine.
|
---|
149 | */
|
---|
150 | if (print_test_error) {
|
---|
151 | pcut_print_fail_message(message);
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (execute_teardown_on_failure) {
|
---|
155 | execute_teardown_on_failure = 0;
|
---|
156 | prev_message = message;
|
---|
157 | run_setup_teardown(current_suite->teardown_func);
|
---|
158 |
|
---|
159 | /* Tear-down was okay. */
|
---|
160 | if (report_test_result) {
|
---|
161 | pcut_report_test_done(current_test, PCUT_OUTCOME_FAIL,
|
---|
162 | message, NULL, NULL);
|
---|
163 | }
|
---|
164 | } else {
|
---|
165 | if (report_test_result) {
|
---|
166 | pcut_report_test_done(current_test, PCUT_OUTCOME_FAIL,
|
---|
167 | prev_message, message, NULL);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | prev_message = NULL;
|
---|
172 |
|
---|
173 | leave_test(PCUT_OUTCOME_FAIL); /* No return. */
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** Run a test.
|
---|
177 | *
|
---|
178 | * @param test Test to execute.
|
---|
179 | * @return Error status (zero means success).
|
---|
180 | */
|
---|
181 | static int run_test(pcut_item_t *test) {
|
---|
182 | /*
|
---|
183 | * Set here as the returning point in case of test failure.
|
---|
184 | * If we get here, it means something failed during the
|
---|
185 | * test execution.
|
---|
186 | */
|
---|
187 | #ifndef PCUT_NO_LONG_JUMP
|
---|
188 | int test_finished = setjmp(start_test_jump);
|
---|
189 | if (test_finished) {
|
---|
190 | return PCUT_OUTCOME_FAIL;
|
---|
191 | }
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | if (report_test_result) {
|
---|
195 | pcut_report_test_start(test);
|
---|
196 | }
|
---|
197 |
|
---|
198 | current_suite = pcut_find_parent_suite(test);
|
---|
199 | current_test = test;
|
---|
200 |
|
---|
201 | pcut_hook_before_test(test);
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * If anything goes wrong, execute the tear-down function
|
---|
205 | * as well.
|
---|
206 | */
|
---|
207 | execute_teardown_on_failure = 1;
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Run the set-up function.
|
---|
211 | */
|
---|
212 | run_setup_teardown(current_suite->setup_func);
|
---|
213 |
|
---|
214 | /*
|
---|
215 | * The setup function was performed, it is time to run
|
---|
216 | * the actual test.
|
---|
217 | */
|
---|
218 | test->test_func();
|
---|
219 |
|
---|
220 | /*
|
---|
221 | * Finally, run the tear-down function. We need to clear
|
---|
222 | * the flag to prevent endless loop.
|
---|
223 | */
|
---|
224 | execute_teardown_on_failure = 0;
|
---|
225 | run_setup_teardown(current_suite->teardown_func);
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * If we got here, it means everything went well with
|
---|
229 | * this test.
|
---|
230 | */
|
---|
231 | if (report_test_result) {
|
---|
232 | pcut_report_test_done(current_test, PCUT_OUTCOME_PASS,
|
---|
233 | NULL, NULL, NULL);
|
---|
234 | }
|
---|
235 |
|
---|
236 | return PCUT_OUTCOME_PASS;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Run a test in a forked mode.
|
---|
240 | *
|
---|
241 | * Forked mode means that the caller of the test is already a new
|
---|
242 | * process running this test only.
|
---|
243 | *
|
---|
244 | * @param test Test to execute.
|
---|
245 | * @return Error status (zero means success).
|
---|
246 | */
|
---|
247 | int pcut_run_test_forked(pcut_item_t *test) {
|
---|
248 | int rc;
|
---|
249 |
|
---|
250 | report_test_result = 0;
|
---|
251 | print_test_error = 1;
|
---|
252 | leave_means_exit = 1;
|
---|
253 |
|
---|
254 | rc = run_test(test);
|
---|
255 |
|
---|
256 | current_test = NULL;
|
---|
257 | current_suite = NULL;
|
---|
258 |
|
---|
259 | return rc;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /** Run a test in a single mode.
|
---|
263 | *
|
---|
264 | * Single mode means that the test is called in the context of the
|
---|
265 | * parent process, that is no new process is forked.
|
---|
266 | *
|
---|
267 | * @param test Test to execute.
|
---|
268 | * @return Error status (zero means success).
|
---|
269 | */
|
---|
270 | int pcut_run_test_single(pcut_item_t *test) {
|
---|
271 | int rc;
|
---|
272 |
|
---|
273 | report_test_result = 1;
|
---|
274 | print_test_error = 0;
|
---|
275 | leave_means_exit = 0;
|
---|
276 |
|
---|
277 | rc = run_test(test);
|
---|
278 |
|
---|
279 | current_test = NULL;
|
---|
280 | current_suite = NULL;
|
---|
281 |
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /** Tells time-out length for a given test.
|
---|
286 | *
|
---|
287 | * @param test Test for which the time-out is questioned.
|
---|
288 | * @return Timeout in seconds.
|
---|
289 | */
|
---|
290 | int pcut_get_test_timeout(pcut_item_t *test) {
|
---|
291 | int timeout = PCUT_DEFAULT_TEST_TIMEOUT;
|
---|
292 | pcut_extra_t *extras = test->extras;
|
---|
293 |
|
---|
294 |
|
---|
295 | while (extras->type != PCUT_EXTRA_LAST) {
|
---|
296 | if (extras->type == PCUT_EXTRA_TIMEOUT) {
|
---|
297 | timeout = extras->timeout;
|
---|
298 | }
|
---|
299 | extras++;
|
---|
300 | }
|
---|
301 |
|
---|
302 | return timeout;
|
---|
303 | }
|
---|