[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. */
|
---|
| 72 | static pcut_item_t default_suite = {
|
---|
| 73 | .kind = PCUT_KIND_TESTSUITE,
|
---|
| 74 | .id = -1,
|
---|
| 75 | .previous = NULL,
|
---|
| 76 | .next = NULL,
|
---|
| 77 | .suite = {
|
---|
| 78 | .name = "Default",
|
---|
| 79 | .setup = NULL,
|
---|
| 80 | .teardown = NULL
|
---|
| 81 | }
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | /** Find the suite given test belongs to.
|
---|
| 85 | *
|
---|
| 86 | * @param it The test.
|
---|
| 87 | * @return Always a valid test suite item.
|
---|
| 88 | */
|
---|
| 89 | static pcut_item_t *pcut_find_parent_suite(pcut_item_t *it) {
|
---|
| 90 | while (it != NULL) {
|
---|
| 91 | if (it->kind == PCUT_KIND_TESTSUITE) {
|
---|
| 92 | return it;
|
---|
| 93 | }
|
---|
| 94 | it = it->previous;
|
---|
| 95 | }
|
---|
| 96 | return &default_suite;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /** Run a set-up (tear-down) function.
|
---|
| 100 | *
|
---|
| 101 | * @param func Function to run (can be NULL).
|
---|
| 102 | */
|
---|
| 103 | static void run_setup_teardown(pcut_setup_func_t func) {
|
---|
| 104 | if (func != NULL) {
|
---|
| 105 | func();
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Terminate current test with given outcome.
|
---|
| 110 | *
|
---|
| 111 | * @warning This function may execute a long jump or terminate
|
---|
| 112 | * current process.
|
---|
| 113 | *
|
---|
| 114 | * @param outcome Outcome of the current test.
|
---|
| 115 | */
|
---|
| 116 | static void leave_test(int outcome) {
|
---|
| 117 | if (leave_means_exit) {
|
---|
| 118 | exit(outcome);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | #ifndef PCUT_NO_LONG_JUMP
|
---|
| 122 | longjmp(start_test_jump, 1);
|
---|
| 123 | #endif
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Process a failed assertion.
|
---|
| 127 | *
|
---|
| 128 | * @warning This function calls leave_test() and typically will not
|
---|
| 129 | * return.
|
---|
| 130 | *
|
---|
| 131 | * @param message Message describing the failure.
|
---|
| 132 | */
|
---|
| 133 | void pcut_failed_assertion(const char *message) {
|
---|
| 134 | static const char *prev_message = NULL;
|
---|
| 135 | /*
|
---|
| 136 | * The assertion failed. We need to abort the current test,
|
---|
| 137 | * inform the user and perform some clean-up. That could
|
---|
| 138 | * include running the tear-down routine.
|
---|
| 139 | */
|
---|
| 140 | if (print_test_error) {
|
---|
| 141 | pcut_print_fail_message(message);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | if (execute_teardown_on_failure) {
|
---|
| 145 | execute_teardown_on_failure = 0;
|
---|
| 146 | prev_message = message;
|
---|
| 147 | run_setup_teardown(current_suite->suite.teardown);
|
---|
| 148 |
|
---|
| 149 | /* Tear-down was okay. */
|
---|
| 150 | if (report_test_result) {
|
---|
| 151 | pcut_report_test_done(current_test, TEST_OUTCOME_FAIL,
|
---|
| 152 | message, NULL, NULL);
|
---|
| 153 | }
|
---|
| 154 | } else {
|
---|
| 155 | if (report_test_result) {
|
---|
| 156 | pcut_report_test_done(current_test, TEST_OUTCOME_FAIL,
|
---|
| 157 | prev_message, message, NULL);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | prev_message = NULL;
|
---|
| 162 |
|
---|
| 163 | leave_test(TEST_OUTCOME_FAIL); /* No return. */
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /** Run a test.
|
---|
| 167 | *
|
---|
| 168 | * @param test Test to execute.
|
---|
| 169 | * @return Error status (zero means success).
|
---|
| 170 | */
|
---|
| 171 | static int run_test(pcut_item_t *test) {
|
---|
| 172 | /*
|
---|
| 173 | * Set here as the returning point in case of test failure.
|
---|
| 174 | * If we get here, it means something failed during the
|
---|
| 175 | * test execution.
|
---|
| 176 | */
|
---|
| 177 | #ifndef PCUT_NO_LONG_JUMP
|
---|
| 178 | int test_finished = setjmp(start_test_jump);
|
---|
| 179 | if (test_finished) {
|
---|
| 180 | return 1;
|
---|
| 181 | }
|
---|
| 182 | #endif
|
---|
| 183 |
|
---|
| 184 | if (report_test_result) {
|
---|
| 185 | pcut_report_test_start(test);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | current_suite = pcut_find_parent_suite(test);
|
---|
| 189 | current_test = test;
|
---|
| 190 |
|
---|
| 191 | /*
|
---|
| 192 | * If anything goes wrong, execute the tear-down function
|
---|
| 193 | * as well.
|
---|
| 194 | */
|
---|
| 195 | execute_teardown_on_failure = 1;
|
---|
| 196 |
|
---|
| 197 | /*
|
---|
| 198 | * Run the set-up function.
|
---|
| 199 | */
|
---|
| 200 | run_setup_teardown(current_suite->suite.setup);
|
---|
| 201 |
|
---|
| 202 | /*
|
---|
| 203 | * The setup function was performed, it is time to run
|
---|
| 204 | * the actual test.
|
---|
| 205 | */
|
---|
| 206 | test->test.func();
|
---|
| 207 |
|
---|
| 208 | /*
|
---|
| 209 | * Finally, run the tear-down function. We need to clear
|
---|
| 210 | * the flag to prevent endless loop.
|
---|
| 211 | */
|
---|
| 212 | execute_teardown_on_failure = 0;
|
---|
| 213 | run_setup_teardown(current_suite->suite.teardown);
|
---|
| 214 |
|
---|
| 215 | /*
|
---|
| 216 | * If we got here, it means everything went well with
|
---|
| 217 | * this test.
|
---|
| 218 | */
|
---|
| 219 | if (report_test_result) {
|
---|
| 220 | pcut_report_test_done(current_test, TEST_OUTCOME_PASS,
|
---|
| 221 | NULL, NULL, NULL);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | return 0;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /** Run a test in a forked mode.
|
---|
| 228 | *
|
---|
| 229 | * Forked mode means that the caller of the test is already a new
|
---|
| 230 | * process running this test only.
|
---|
| 231 | *
|
---|
| 232 | * @param test Test to execute.
|
---|
| 233 | * @return Error status (zero means success).
|
---|
| 234 | */
|
---|
| 235 | int pcut_run_test_forked(pcut_item_t *test) {
|
---|
| 236 | report_test_result = 0;
|
---|
| 237 | print_test_error = 1;
|
---|
| 238 | leave_means_exit = 1;
|
---|
| 239 |
|
---|
| 240 | int rc = run_test(test);
|
---|
| 241 |
|
---|
| 242 | current_test = NULL;
|
---|
| 243 | current_suite = NULL;
|
---|
| 244 |
|
---|
| 245 | return rc;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /** Run a test in a single mode.
|
---|
| 249 | *
|
---|
| 250 | * Single mode means that the test is called in the context of the
|
---|
| 251 | * parent process, that is no new process is forked.
|
---|
| 252 | *
|
---|
| 253 | * @param test Test to execute.
|
---|
| 254 | * @return Error status (zero means success).
|
---|
| 255 | */
|
---|
| 256 | int pcut_run_test_single(pcut_item_t *test) {
|
---|
| 257 | report_test_result = 1;
|
---|
| 258 | print_test_error = 0;
|
---|
| 259 | leave_means_exit = 0;
|
---|
| 260 |
|
---|
| 261 | int rc = run_test(test);
|
---|
| 262 |
|
---|
| 263 | current_test = NULL;
|
---|
| 264 | current_suite = NULL;
|
---|
| 265 |
|
---|
| 266 | return rc;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[134ac5d] | 269 | /** Tells time-out length for a given test.
|
---|
| 270 | *
|
---|
| 271 | * @param test Test for which the time-out is questioned.
|
---|
| 272 | * @return Timeout in seconds.
|
---|
| 273 | */
|
---|
| 274 | int pcut_get_test_timeout(pcut_item_t *test) {
|
---|
| 275 | PCUT_UNUSED(test);
|
---|
| 276 |
|
---|
| 277 | int timeout = PCUT_DEFAULT_TEST_TIMEOUT;
|
---|
| 278 |
|
---|
| 279 | pcut_extra_t *extras = test->test.extras;
|
---|
| 280 | while (extras->type != PCUT_EXTRA_LAST) {
|
---|
| 281 | if (extras->type == PCUT_EXTRA_TIMEOUT) {
|
---|
| 282 | timeout = extras->timeout;
|
---|
| 283 | }
|
---|
| 284 | extras++;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | return timeout;
|
---|
| 288 | }
|
---|