[01579ad] | 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 | #ifndef PCUT_NO_LONG_JUMP
|
---|
| 36 | #include <setjmp.h>
|
---|
| 37 | #endif
|
---|
| 38 |
|
---|
| 39 | #ifndef PCUT_NO_LONG_JUMP
|
---|
| 40 | /** Long-jump buffer. */
|
---|
| 41 | static jmp_buf start_test_jump;
|
---|
| 42 | #endif
|
---|
| 43 |
|
---|
| 44 | /** Whether to run a tear-down function on a failure.
|
---|
| 45 | *
|
---|
| 46 | * Used to determine whether we are already in a tear-down context.
|
---|
| 47 | */
|
---|
| 48 | static int execute_teardown_on_failure;
|
---|
| 49 |
|
---|
| 50 | /** Whether to report test result at all.
|
---|
| 51 | *
|
---|
| 52 | * Used to determine whether we are the forked or the parent process.
|
---|
| 53 | */
|
---|
| 54 | static int report_test_result;
|
---|
| 55 |
|
---|
| 56 | /** Whether to print test error.
|
---|
| 57 | *
|
---|
| 58 | * Used to determine whether we are the forked or the parent process.
|
---|
| 59 | */
|
---|
| 60 | static int print_test_error;
|
---|
| 61 |
|
---|
| 62 | /** Whether leaving a test means a process exit. */
|
---|
| 63 | static int leave_means_exit;
|
---|
| 64 |
|
---|
| 65 | /** Pointer to currently running test. */
|
---|
| 66 | static pcut_item_t *current_test = NULL;
|
---|
| 67 |
|
---|
| 68 | /** Pointer to current test suite. */
|
---|
| 69 | static pcut_item_t *current_suite = NULL;
|
---|
| 70 |
|
---|
| 71 | /** A NULL-like suite. */
|
---|
[9b20126] | 72 | static pcut_item_t default_suite;
|
---|
| 73 | static int default_suite_initialized = 0;
|
---|
| 74 |
|
---|
[1433ecda] | 75 | static void init_default_suite_when_needed()
|
---|
| 76 | {
|
---|
[9eb1ff5] | 77 | if (default_suite_initialized) {
|
---|
[9b20126] | 78 | return;
|
---|
[9eb1ff5] | 79 | }
|
---|
[9b20126] | 80 | default_suite.id = -1;
|
---|
| 81 | default_suite.kind = PCUT_KIND_TESTSUITE;
|
---|
| 82 | default_suite.previous = NULL;
|
---|
| 83 | default_suite.next = NULL;
|
---|
| 84 | default_suite.name = "Default";
|
---|
| 85 | default_suite.setup_func = NULL;
|
---|
| 86 | default_suite.teardown_func = NULL;
|
---|
| 87 | }
|
---|
[01579ad] | 88 |
|
---|
| 89 | /** Find the suite given test belongs to.
|
---|
| 90 | *
|
---|
| 91 | * @param it The test.
|
---|
| 92 | * @return Always a valid test suite item.
|
---|
| 93 | */
|
---|
[1433ecda] | 94 | static pcut_item_t *pcut_find_parent_suite(pcut_item_t *it)
|
---|
| 95 | {
|
---|
[01579ad] | 96 | while (it != NULL) {
|
---|
[9eb1ff5] | 97 | if (it->kind == PCUT_KIND_TESTSUITE) {
|
---|
[01579ad] | 98 | return it;
|
---|
[9eb1ff5] | 99 | }
|
---|
[01579ad] | 100 | it = it->previous;
|
---|
| 101 | }
|
---|
[9b20126] | 102 | init_default_suite_when_needed();
|
---|
[01579ad] | 103 | return &default_suite;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Run a set-up (tear-down) function.
|
---|
| 107 | *
|
---|
| 108 | * @param func Function to run (can be NULL).
|
---|
| 109 | */
|
---|
[1433ecda] | 110 | static void run_setup_teardown(pcut_setup_func_t func)
|
---|
| 111 | {
|
---|
[9eb1ff5] | 112 | if (func != NULL) {
|
---|
[01579ad] | 113 | func();
|
---|
[9eb1ff5] | 114 | }
|
---|
[01579ad] | 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 | */
|
---|
[1433ecda] | 124 | static void leave_test(int outcome)
|
---|
| 125 | {
|
---|
[9b20126] | 126 | PCUT_DEBUG("leave_test(outcome=%d), will_exit=%s", outcome,
|
---|
[1433ecda] | 127 | leave_means_exit ? "yes" : "no");
|
---|
[9eb1ff5] | 128 | if (leave_means_exit) {
|
---|
[01579ad] | 129 | exit(outcome);
|
---|
[9eb1ff5] | 130 | }
|
---|
| 131 |
|
---|
[01579ad] | 132 | #ifndef PCUT_NO_LONG_JUMP
|
---|
| 133 | longjmp(start_test_jump, 1);
|
---|
| 134 | #endif
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Process a failed assertion.
|
---|
| 138 | *
|
---|
| 139 | * @warning This function calls leave_test() and typically will not
|
---|
| 140 | * return.
|
---|
| 141 | *
|
---|
| 142 | * @param message Message describing the failure.
|
---|
| 143 | */
|
---|
[1433ecda] | 144 | void pcut_failed_assertion(const char *message)
|
---|
| 145 | {
|
---|
[01579ad] | 146 | static const char *prev_message = NULL;
|
---|
| 147 | /*
|
---|
| 148 | * The assertion failed. We need to abort the current test,
|
---|
| 149 | * inform the user and perform some clean-up. That could
|
---|
| 150 | * include running the tear-down routine.
|
---|
| 151 | */
|
---|
[9eb1ff5] | 152 | if (print_test_error) {
|
---|
[01579ad] | 153 | pcut_print_fail_message(message);
|
---|
[9eb1ff5] | 154 | }
|
---|
| 155 |
|
---|
[01579ad] | 156 | if (execute_teardown_on_failure) {
|
---|
| 157 | execute_teardown_on_failure = 0;
|
---|
| 158 | prev_message = message;
|
---|
[9b20126] | 159 | run_setup_teardown(current_suite->teardown_func);
|
---|
[9eb1ff5] | 160 |
|
---|
[01579ad] | 161 | /* Tear-down was okay. */
|
---|
| 162 | if (report_test_result) {
|
---|
[9eb1ff5] | 163 | pcut_report_test_done(current_test, PCUT_OUTCOME_FAIL,
|
---|
[1433ecda] | 164 | message, NULL, NULL);
|
---|
[01579ad] | 165 | }
|
---|
| 166 | } else {
|
---|
| 167 | if (report_test_result) {
|
---|
[9eb1ff5] | 168 | pcut_report_test_done(current_test, PCUT_OUTCOME_FAIL,
|
---|
[1433ecda] | 169 | prev_message, message, NULL);
|
---|
[01579ad] | 170 | }
|
---|
| 171 | }
|
---|
[9eb1ff5] | 172 |
|
---|
[01579ad] | 173 | prev_message = NULL;
|
---|
[9eb1ff5] | 174 |
|
---|
| 175 | leave_test(PCUT_OUTCOME_FAIL); /* No return. */
|
---|
[01579ad] | 176 | }
|
---|
| 177 |
|
---|
| 178 | /** Run a test.
|
---|
| 179 | *
|
---|
| 180 | * @param test Test to execute.
|
---|
| 181 | * @return Error status (zero means success).
|
---|
| 182 | */
|
---|
[1433ecda] | 183 | static int run_test(pcut_item_t *test)
|
---|
| 184 | {
|
---|
[01579ad] | 185 | /*
|
---|
| 186 | * Set here as the returning point in case of test failure.
|
---|
| 187 | * If we get here, it means something failed during the
|
---|
| 188 | * test execution.
|
---|
| 189 | */
|
---|
| 190 | #ifndef PCUT_NO_LONG_JUMP
|
---|
| 191 | int test_finished = setjmp(start_test_jump);
|
---|
[9eb1ff5] | 192 | if (test_finished) {
|
---|
| 193 | return PCUT_OUTCOME_FAIL;
|
---|
| 194 | }
|
---|
[01579ad] | 195 | #endif
|
---|
[9eb1ff5] | 196 |
|
---|
| 197 | if (report_test_result) {
|
---|
[01579ad] | 198 | pcut_report_test_start(test);
|
---|
[9eb1ff5] | 199 | }
|
---|
| 200 |
|
---|
[01579ad] | 201 | current_suite = pcut_find_parent_suite(test);
|
---|
| 202 | current_test = test;
|
---|
[9eb1ff5] | 203 |
|
---|
[9b20126] | 204 | pcut_hook_before_test(test);
|
---|
[9eb1ff5] | 205 |
|
---|
[01579ad] | 206 | /*
|
---|
| 207 | * If anything goes wrong, execute the tear-down function
|
---|
| 208 | * as well.
|
---|
| 209 | */
|
---|
| 210 | execute_teardown_on_failure = 1;
|
---|
[9eb1ff5] | 211 |
|
---|
[01579ad] | 212 | /*
|
---|
| 213 | * Run the set-up function.
|
---|
| 214 | */
|
---|
[9b20126] | 215 | run_setup_teardown(current_suite->setup_func);
|
---|
[9eb1ff5] | 216 |
|
---|
[01579ad] | 217 | /*
|
---|
| 218 | * The setup function was performed, it is time to run
|
---|
| 219 | * the actual test.
|
---|
| 220 | */
|
---|
[9b20126] | 221 | test->test_func();
|
---|
[9eb1ff5] | 222 |
|
---|
[01579ad] | 223 | /*
|
---|
| 224 | * Finally, run the tear-down function. We need to clear
|
---|
| 225 | * the flag to prevent endless loop.
|
---|
| 226 | */
|
---|
| 227 | execute_teardown_on_failure = 0;
|
---|
[9b20126] | 228 | run_setup_teardown(current_suite->teardown_func);
|
---|
[9eb1ff5] | 229 |
|
---|
[01579ad] | 230 | /*
|
---|
| 231 | * If we got here, it means everything went well with
|
---|
| 232 | * this test.
|
---|
| 233 | */
|
---|
[9eb1ff5] | 234 | if (report_test_result) {
|
---|
| 235 | pcut_report_test_done(current_test, PCUT_OUTCOME_PASS,
|
---|
[1433ecda] | 236 | NULL, NULL, NULL);
|
---|
[9eb1ff5] | 237 | }
|
---|
| 238 |
|
---|
| 239 | return PCUT_OUTCOME_PASS;
|
---|
[01579ad] | 240 | }
|
---|
| 241 |
|
---|
| 242 | /** Run a test in a forked mode.
|
---|
| 243 | *
|
---|
| 244 | * Forked mode means that the caller of the test is already a new
|
---|
| 245 | * process running this test only.
|
---|
| 246 | *
|
---|
| 247 | * @param test Test to execute.
|
---|
| 248 | * @return Error status (zero means success).
|
---|
| 249 | */
|
---|
[1433ecda] | 250 | int pcut_run_test_forked(pcut_item_t *test)
|
---|
| 251 | {
|
---|
[9eb1ff5] | 252 | int rc;
|
---|
| 253 |
|
---|
[01579ad] | 254 | report_test_result = 0;
|
---|
| 255 | print_test_error = 1;
|
---|
| 256 | leave_means_exit = 1;
|
---|
[9eb1ff5] | 257 |
|
---|
| 258 | rc = run_test(test);
|
---|
| 259 |
|
---|
[01579ad] | 260 | current_test = NULL;
|
---|
| 261 | current_suite = NULL;
|
---|
[9eb1ff5] | 262 |
|
---|
[01579ad] | 263 | return rc;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /** Run a test in a single mode.
|
---|
| 267 | *
|
---|
| 268 | * Single mode means that the test is called in the context of the
|
---|
| 269 | * parent process, that is no new process is forked.
|
---|
| 270 | *
|
---|
| 271 | * @param test Test to execute.
|
---|
| 272 | * @return Error status (zero means success).
|
---|
| 273 | */
|
---|
[1433ecda] | 274 | int pcut_run_test_single(pcut_item_t *test)
|
---|
| 275 | {
|
---|
[9eb1ff5] | 276 | int rc;
|
---|
| 277 |
|
---|
[01579ad] | 278 | report_test_result = 1;
|
---|
| 279 | print_test_error = 0;
|
---|
| 280 | leave_means_exit = 0;
|
---|
[9eb1ff5] | 281 |
|
---|
| 282 | rc = run_test(test);
|
---|
| 283 |
|
---|
[01579ad] | 284 | current_test = NULL;
|
---|
| 285 | current_suite = NULL;
|
---|
[9eb1ff5] | 286 |
|
---|
[01579ad] | 287 | return rc;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[134ac5d] | 290 | /** Tells time-out length for a given test.
|
---|
| 291 | *
|
---|
| 292 | * @param test Test for which the time-out is questioned.
|
---|
| 293 | * @return Timeout in seconds.
|
---|
| 294 | */
|
---|
[1433ecda] | 295 | int pcut_get_test_timeout(pcut_item_t *test)
|
---|
| 296 | {
|
---|
[134ac5d] | 297 | int timeout = PCUT_DEFAULT_TEST_TIMEOUT;
|
---|
[9b20126] | 298 | pcut_extra_t *extras = test->extras;
|
---|
[9eb1ff5] | 299 |
|
---|
| 300 |
|
---|
[134ac5d] | 301 | while (extras->type != PCUT_EXTRA_LAST) {
|
---|
[9eb1ff5] | 302 | if (extras->type == PCUT_EXTRA_TIMEOUT) {
|
---|
[134ac5d] | 303 | timeout = extras->timeout;
|
---|
[9eb1ff5] | 304 | }
|
---|
[134ac5d] | 305 | extras++;
|
---|
| 306 | }
|
---|
[9eb1ff5] | 307 |
|
---|
[134ac5d] | 308 | return timeout;
|
---|
| 309 | }
|
---|