| 1 |
/* |
| 2 |
* Check: a unit test framework for C |
| 3 |
* Copyright (C) 2001, 2002 Arien Malec |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 18 |
* MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "libcompat/libcompat.h" |
| 22 |
|
| 23 |
#include <sys/types.h> |
| 24 |
#include <time.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <signal.h> |
| 30 |
#include <setjmp.h> |
| 31 |
|
| 32 |
#include <glib.h> |
| 33 |
|
| 34 |
#include "internal-check.h" |
| 35 |
#include "check_error.h" |
| 36 |
#include "check_list.h" |
| 37 |
#include "check_impl.h" |
| 38 |
#include "check_msg.h" |
| 39 |
#include "check_log.h" |
| 40 |
|
| 41 |
enum rinfo |
| 42 |
{ |
| 43 |
CK_R_SIG, |
| 44 |
CK_R_PASS, |
| 45 |
CK_R_EXIT, |
| 46 |
CK_R_FAIL_TEST, |
| 47 |
CK_R_FAIL_FIXTURE |
| 48 |
}; |
| 49 |
|
| 50 |
enum tf_type |
| 51 |
{ |
| 52 |
CK_FORK_TEST, |
| 53 |
CK_NOFORK_TEST, |
| 54 |
CK_NOFORK_FIXTURE |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
/* all functions are defined in the same order they are declared. |
| 59 |
functions that depend on forking are gathered all together. |
| 60 |
non-static functions are at the end of the file. */ |
| 61 |
static void srunner_run_init (SRunner * sr, enum print_output print_mode); |
| 62 |
static void srunner_run_end (SRunner * sr, enum print_output print_mode); |
| 63 |
static void srunner_iterate_suites (SRunner * sr, |
| 64 |
const char *sname, const char *tcname, |
| 65 |
const char *include_tags, |
| 66 |
const char *exclude_tags, enum print_output print_mode); |
| 67 |
static void srunner_iterate_tcase_tfuns (SRunner * sr, TCase * tc); |
| 68 |
static void srunner_add_failure (SRunner * sr, TestResult * tf); |
| 69 |
static TestResult *srunner_run_setup (List * func_list, |
| 70 |
enum fork_status fork_usage, const char *test_name, const char *setup_name); |
| 71 |
static int srunner_run_unchecked_setup (SRunner * sr, TCase * tc); |
| 72 |
static TestResult *tcase_run_checked_setup (SRunner * sr, TCase * tc); |
| 73 |
static void srunner_run_teardown (List * fixture_list, |
| 74 |
enum fork_status fork_usage); |
| 75 |
static void srunner_run_unchecked_teardown (SRunner * sr, TCase * tc); |
| 76 |
static void tcase_run_checked_teardown (TCase * tc); |
| 77 |
static void srunner_run_tcase (SRunner * sr, TCase * tc); |
| 78 |
static TestResult *tcase_run_tfun_nofork (SRunner * sr, TCase * tc, TF * tf, |
| 79 |
int i); |
| 80 |
static TestResult *receive_result_info_nofork (const char *tcname, |
| 81 |
const char *tname, int iter, int duration); |
| 82 |
static void set_nofork_info (TestResult * tr); |
| 83 |
static char *pass_msg (void); |
| 84 |
|
| 85 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 86 |
static TestResult *tcase_run_tfun_fork (SRunner * sr, TCase * tc, TF * tf, |
| 87 |
int i); |
| 88 |
static TestResult *receive_result_info_fork (const char *tcname, |
| 89 |
const char *tname, int iter, |
| 90 |
int status, int expected_signal, signed char allowed_exit_value); |
| 91 |
static void set_fork_info (TestResult * tr, int status, int expected_signal, |
| 92 |
signed char allowed_exit_value); |
| 93 |
static char *signal_msg (int sig); |
| 94 |
static char *signal_error_msg (int signal_received, int signal_expected); |
| 95 |
static char *exit_msg (int exitstatus); |
| 96 |
static int waserror (int status, int expected_signal); |
| 97 |
|
| 98 |
static int alarm_received; |
| 99 |
static pid_t group_pid; |
| 100 |
static struct sigaction sigint_old_action; |
| 101 |
static struct sigaction sigterm_old_action; |
| 102 |
|
| 103 |
static void CK_ATTRIBUTE_UNUSED |
| 104 |
sig_handler (int sig_nr) |
| 105 |
{ |
| 106 |
switch (sig_nr) { |
| 107 |
case SIGALRM: |
| 108 |
alarm_received = 1; |
| 109 |
killpg (group_pid, SIGABRT); |
| 110 |
break; |
| 111 |
case SIGTERM: |
| 112 |
case SIGINT: |
| 113 |
{ |
| 114 |
pid_t own_group_pid; |
| 115 |
int child_sig = SIGTERM; |
| 116 |
|
| 117 |
if (sig_nr == SIGINT) { |
| 118 |
child_sig = SIGKILL; |
| 119 |
sigaction (SIGINT, &sigint_old_action, NULL); |
| 120 |
} else { |
| 121 |
sigaction (SIGTERM, &sigterm_old_action, NULL); |
| 122 |
} |
| 123 |
|
| 124 |
killpg (group_pid, child_sig); |
| 125 |
|
| 126 |
/* POSIX says that calling killpg(0) |
| 127 |
* does not necessarily mean to call it on the callers |
| 128 |
* group pid! */ |
| 129 |
own_group_pid = getpgrp (); |
| 130 |
killpg (own_group_pid, sig_nr); |
| 131 |
break; |
| 132 |
} |
| 133 |
default: |
| 134 |
eprintf ("Unhandled signal: %d", __FILE__, __LINE__, sig_nr); |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
#endif /* HAVE_FORK */ |
| 139 |
|
| 140 |
#define MSG_LEN 100 |
| 141 |
|
| 142 |
static void |
| 143 |
srunner_run_init (SRunner * sr, enum print_output print_mode) |
| 144 |
{ |
| 145 |
set_fork_status (srunner_fork_status (sr)); |
| 146 |
setup_messaging (); |
| 147 |
srunner_init_logging (sr, print_mode); |
| 148 |
log_srunner_start (sr); |
| 149 |
} |
| 150 |
|
| 151 |
static void |
| 152 |
srunner_run_end (SRunner * sr, enum print_output CK_ATTRIBUTE_UNUSED print_mode) |
| 153 |
{ |
| 154 |
log_srunner_end (sr); |
| 155 |
srunner_end_logging (sr); |
| 156 |
teardown_messaging (); |
| 157 |
set_fork_status (CK_FORK); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
srunner_iterate_suites (SRunner * sr, |
| 162 |
const char *sname, const char *tcname, |
| 163 |
const char *include_tags, |
| 164 |
const char *exclude_tags, enum print_output CK_ATTRIBUTE_UNUSED print_mode) |
| 165 |
{ |
| 166 |
List *include_tag_lst; |
| 167 |
List *exclude_tag_lst; |
| 168 |
List *slst; |
| 169 |
List *tcl; |
| 170 |
TCase *tc; |
| 171 |
|
| 172 |
slst = sr->slst; |
| 173 |
|
| 174 |
include_tag_lst = tag_string_to_list (include_tags); |
| 175 |
exclude_tag_lst = tag_string_to_list (exclude_tags); |
| 176 |
|
| 177 |
for (check_list_front (slst); !check_list_at_end (slst); |
| 178 |
check_list_advance (slst)) { |
| 179 |
Suite *s = (Suite *) check_list_val (slst); |
| 180 |
|
| 181 |
if (((sname != NULL) && (strcmp (sname, s->name) != 0)) |
| 182 |
|| ((tcname != NULL) && (!suite_tcase (s, tcname)))) |
| 183 |
continue; |
| 184 |
|
| 185 |
log_suite_start (sr, s); |
| 186 |
|
| 187 |
tcl = s->tclst; |
| 188 |
|
| 189 |
for (check_list_front (tcl); !check_list_at_end (tcl); |
| 190 |
check_list_advance (tcl)) { |
| 191 |
tc = (TCase *) check_list_val (tcl); |
| 192 |
|
| 193 |
if ((tcname != NULL) && (strcmp (tcname, tc->name) != 0)) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
if (include_tags != NULL) { |
| 197 |
if (!tcase_matching_tag (tc, include_tag_lst)) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
} |
| 201 |
if (exclude_tags != NULL) { |
| 202 |
if (tcase_matching_tag (tc, exclude_tag_lst)) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
srunner_run_tcase (sr, tc); |
| 208 |
} |
| 209 |
|
| 210 |
log_suite_end (sr, s); |
| 211 |
} |
| 212 |
|
| 213 |
check_list_apply (include_tag_lst, free); |
| 214 |
check_list_apply (exclude_tag_lst, free); |
| 215 |
check_list_free (include_tag_lst); |
| 216 |
check_list_free (exclude_tag_lst); |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
srunner_iterate_tcase_tfuns (SRunner * sr, TCase * tc) |
| 221 |
{ |
| 222 |
List *tfl; |
| 223 |
TF *tfun; |
| 224 |
TestResult *tr = NULL; |
| 225 |
|
| 226 |
tfl = tc->tflst; |
| 227 |
|
| 228 |
for (check_list_front (tfl); !check_list_at_end (tfl); |
| 229 |
check_list_advance (tfl)) { |
| 230 |
int i; |
| 231 |
|
| 232 |
tfun = (TF *) check_list_val (tfl); |
| 233 |
|
| 234 |
for (i = tfun->loop_start; i < tfun->loop_end; i++) { |
| 235 |
log_test_start (sr, tc, tfun); |
| 236 |
switch (srunner_fork_status (sr)) { |
| 237 |
case CK_FORK: |
| 238 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 239 |
tr = tcase_run_tfun_fork (sr, tc, tfun, i); |
| 240 |
#else /* HAVE_FORK */ |
| 241 |
eprintf ("This version does not support fork", __FILE__, __LINE__); |
| 242 |
#endif /* HAVE_FORK */ |
| 243 |
break; |
| 244 |
case CK_NOFORK: |
| 245 |
tr = tcase_run_tfun_nofork (sr, tc, tfun, i); |
| 246 |
break; |
| 247 |
case CK_FORK_GETENV: |
| 248 |
default: |
| 249 |
eprintf ("Bad fork status in SRunner", __FILE__, __LINE__); |
| 250 |
} |
| 251 |
|
| 252 |
if (NULL != tr) { |
| 253 |
srunner_add_failure (sr, tr); |
| 254 |
log_test_end (sr, tr); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
srunner_add_failure (SRunner * sr, TestResult * tr) |
| 262 |
{ |
| 263 |
check_list_add_end (sr->resultlst, tr); |
| 264 |
sr->stats->n_checked++; /* count checks during setup, test, and teardown */ |
| 265 |
if (tr->rtype == CK_FAILURE) |
| 266 |
sr->stats->n_failed++; |
| 267 |
else if (tr->rtype == CK_ERROR) |
| 268 |
sr->stats->n_errors++; |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
static TestResult * |
| 273 |
srunner_run_setup (List * fixture_list, enum fork_status fork_usage, |
| 274 |
const char *test_name, const char *setup_name) |
| 275 |
{ |
| 276 |
TestResult *tr = NULL; |
| 277 |
Fixture *setup_fixture; |
| 278 |
|
| 279 |
if (fork_usage == CK_FORK) { |
| 280 |
send_ctx_info (CK_CTX_SETUP); |
| 281 |
} |
| 282 |
|
| 283 |
for (check_list_front (fixture_list); !check_list_at_end (fixture_list); |
| 284 |
check_list_advance (fixture_list)) { |
| 285 |
setup_fixture = (Fixture *) check_list_val (fixture_list); |
| 286 |
|
| 287 |
if (fork_usage == CK_NOFORK) { |
| 288 |
send_ctx_info (CK_CTX_SETUP); |
| 289 |
|
| 290 |
if (0 == setjmp (error_jmp_buffer)) { |
| 291 |
setup_fixture->fun (); |
| 292 |
} |
| 293 |
|
| 294 |
/* Stop the setup and return the failure in nofork mode. */ |
| 295 |
tr = receive_result_info_nofork (test_name, setup_name, 0, -1); |
| 296 |
if (tr->rtype != CK_PASS) { |
| 297 |
break; |
| 298 |
} |
| 299 |
|
| 300 |
free (tr->file); |
| 301 |
free (tr->msg); |
| 302 |
free (tr); |
| 303 |
tr = NULL; |
| 304 |
} else { |
| 305 |
setup_fixture->fun (); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
return tr; |
| 310 |
} |
| 311 |
|
| 312 |
static int |
| 313 |
srunner_run_unchecked_setup (SRunner * sr, TCase * tc) |
| 314 |
{ |
| 315 |
TestResult *tr = NULL; |
| 316 |
int rval = 1; |
| 317 |
|
| 318 |
set_fork_status (CK_NOFORK); |
| 319 |
tr = srunner_run_setup (tc->unch_sflst, CK_NOFORK, tc->name, |
| 320 |
"unchecked_setup"); |
| 321 |
set_fork_status (srunner_fork_status (sr)); |
| 322 |
|
| 323 |
if (tr != NULL && tr->rtype != CK_PASS) { |
| 324 |
srunner_add_failure (sr, tr); |
| 325 |
rval = 0; |
| 326 |
} |
| 327 |
|
| 328 |
return rval; |
| 329 |
} |
| 330 |
|
| 331 |
static TestResult * |
| 332 |
tcase_run_checked_setup (SRunner * sr, TCase * tc) |
| 333 |
{ |
| 334 |
TestResult *tr = srunner_run_setup (tc->ch_sflst, srunner_fork_status (sr), |
| 335 |
tc->name, "checked_setup"); |
| 336 |
|
| 337 |
return tr; |
| 338 |
} |
| 339 |
|
| 340 |
static void |
| 341 |
srunner_run_teardown (List * fixture_list, enum fork_status fork_usage) |
| 342 |
{ |
| 343 |
Fixture *fixture; |
| 344 |
|
| 345 |
for (check_list_front (fixture_list); !check_list_at_end (fixture_list); |
| 346 |
check_list_advance (fixture_list)) { |
| 347 |
fixture = (Fixture *) check_list_val (fixture_list); |
| 348 |
send_ctx_info (CK_CTX_TEARDOWN); |
| 349 |
|
| 350 |
if (fork_usage == CK_NOFORK) { |
| 351 |
if (0 == setjmp (error_jmp_buffer)) { |
| 352 |
fixture->fun (); |
| 353 |
} else { |
| 354 |
/* Abort the remaining teardowns */ |
| 355 |
break; |
| 356 |
} |
| 357 |
} else { |
| 358 |
fixture->fun (); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
static void |
| 364 |
srunner_run_unchecked_teardown (SRunner * sr, TCase * tc) |
| 365 |
{ |
| 366 |
srunner_run_teardown (tc->unch_tflst, srunner_fork_status (sr)); |
| 367 |
} |
| 368 |
|
| 369 |
static void |
| 370 |
tcase_run_checked_teardown (TCase * tc) |
| 371 |
{ |
| 372 |
srunner_run_teardown (tc->ch_tflst, CK_NOFORK); |
| 373 |
} |
| 374 |
|
| 375 |
static void |
| 376 |
srunner_run_tcase (SRunner * sr, TCase * tc) |
| 377 |
{ |
| 378 |
if (srunner_run_unchecked_setup (sr, tc)) { |
| 379 |
srunner_iterate_tcase_tfuns (sr, tc); |
| 380 |
srunner_run_unchecked_teardown (sr, tc); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
static TestResult * |
| 385 |
tcase_run_tfun_nofork (SRunner * sr, TCase * tc, TF * tfun, int i) |
| 386 |
{ |
| 387 |
TestResult *tr; |
| 388 |
struct timespec ts_start = { 0, 0 }, ts_end = { |
| 389 |
0, 0 |
| 390 |
}; |
| 391 |
|
| 392 |
tr = tcase_run_checked_setup (sr, tc); |
| 393 |
if (tr == NULL) { |
| 394 |
clock_gettime (check_get_clockid (), &ts_start); |
| 395 |
if (0 == setjmp (error_jmp_buffer)) { |
| 396 |
tfun->fn (i); |
| 397 |
} |
| 398 |
clock_gettime (check_get_clockid (), &ts_end); |
| 399 |
tcase_run_checked_teardown (tc); |
| 400 |
return receive_result_info_nofork (tc->name, tfun->name, i, |
| 401 |
DIFF_IN_USEC (ts_start, ts_end)); |
| 402 |
} |
| 403 |
|
| 404 |
return tr; |
| 405 |
} |
| 406 |
|
| 407 |
static TestResult * |
| 408 |
receive_result_info_nofork (const char *tcname, |
| 409 |
const char *tname, int iter, int duration) |
| 410 |
{ |
| 411 |
TestResult *tr; |
| 412 |
|
| 413 |
tr = receive_test_result (0); |
| 414 |
if (tr == NULL) { |
| 415 |
eprintf ("Failed to receive test result", __FILE__, __LINE__); |
| 416 |
} else { |
| 417 |
tr->tcname = tcname; |
| 418 |
tr->tname = tname; |
| 419 |
tr->iter = iter; |
| 420 |
tr->duration = duration; |
| 421 |
set_nofork_info (tr); |
| 422 |
} |
| 423 |
|
| 424 |
return tr; |
| 425 |
} |
| 426 |
|
| 427 |
static void |
| 428 |
set_nofork_info (TestResult * tr) |
| 429 |
{ |
| 430 |
if (tr->msg == NULL) { |
| 431 |
tr->rtype = CK_PASS; |
| 432 |
tr->msg = pass_msg (); |
| 433 |
} else { |
| 434 |
tr->rtype = CK_FAILURE; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
static char * |
| 439 |
pass_msg (void) |
| 440 |
{ |
| 441 |
return strdup ("Passed"); |
| 442 |
} |
| 443 |
|
| 444 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 445 |
static TestResult * |
| 446 |
tcase_run_tfun_fork (SRunner * sr, TCase * tc, TF * tfun, int i) |
| 447 |
{ |
| 448 |
pid_t pid_w; |
| 449 |
pid_t pid; |
| 450 |
int status = 0; |
| 451 |
struct timespec ts_start = { 0, 0 }, ts_end = { |
| 452 |
0, 0 |
| 453 |
}; |
| 454 |
|
| 455 |
timer_t timerid; |
| 456 |
struct itimerspec timer_spec; |
| 457 |
TestResult *tr; |
| 458 |
|
| 459 |
|
| 460 |
pid = fork (); |
| 461 |
if (pid == -1) |
| 462 |
eprintf ("Error in call to fork:", __FILE__, __LINE__ - 2); |
| 463 |
if (pid == 0) { |
| 464 |
setpgid (0, 0); |
| 465 |
group_pid = getpgrp (); |
| 466 |
tr = tcase_run_checked_setup (sr, tc); |
| 467 |
free (tr); |
| 468 |
clock_gettime (check_get_clockid (), &ts_start); |
| 469 |
tfun->fn (i); |
| 470 |
clock_gettime (check_get_clockid (), &ts_end); |
| 471 |
tcase_run_checked_teardown (tc); |
| 472 |
send_duration_info (DIFF_IN_USEC (ts_start, ts_end)); |
| 473 |
g_thread_pool_stop_unused_threads (); |
| 474 |
exit (EXIT_SUCCESS); |
| 475 |
} else { |
| 476 |
group_pid = pid; |
| 477 |
} |
| 478 |
|
| 479 |
alarm_received = 0; |
| 480 |
|
| 481 |
if (timer_create (check_get_clockid (), |
| 482 |
NULL /* fire SIGALRM if timer expires */ , |
| 483 |
&timerid) == 0) { |
| 484 |
/* Set the timer to fire once */ |
| 485 |
timer_spec.it_value = tc->timeout; |
| 486 |
timer_spec.it_interval.tv_sec = 0; |
| 487 |
timer_spec.it_interval.tv_nsec = 0; |
| 488 |
if (timer_settime (timerid, 0, &timer_spec, NULL) == 0) { |
| 489 |
do { |
| 490 |
pid_w = waitpid (pid, &status, 0); |
| 491 |
} |
| 492 |
while (pid_w == -1); |
| 493 |
} else { |
| 494 |
eprintf ("Error in call to timer_settime:", __FILE__, __LINE__); |
| 495 |
} |
| 496 |
|
| 497 |
/* If the timer has not fired, disable it */ |
| 498 |
timer_delete (timerid); |
| 499 |
} else { |
| 500 |
eprintf ("Error in call to timer_create:", __FILE__, __LINE__); |
| 501 |
} |
| 502 |
|
| 503 |
killpg (pid, SIGKILL); /* Kill remaining processes. */ |
| 504 |
|
| 505 |
return receive_result_info_fork (tc->name, tfun->name, i, status, |
| 506 |
tfun->signal, tfun->allowed_exit_value); |
| 507 |
} |
| 508 |
|
| 509 |
static TestResult * |
| 510 |
receive_result_info_fork (const char *tcname, |
| 511 |
const char *tname, |
| 512 |
int iter, int status, int expected_signal, signed char allowed_exit_value) |
| 513 |
{ |
| 514 |
TestResult *tr; |
| 515 |
|
| 516 |
tr = receive_test_result (waserror (status, expected_signal)); |
| 517 |
if (tr == NULL) { |
| 518 |
eprintf ("Failed to receive test result", __FILE__, __LINE__); |
| 519 |
} else { |
| 520 |
tr->tcname = tcname; |
| 521 |
tr->tname = tname; |
| 522 |
tr->iter = iter; |
| 523 |
set_fork_info (tr, status, expected_signal, allowed_exit_value); |
| 524 |
} |
| 525 |
|
| 526 |
return tr; |
| 527 |
} |
| 528 |
|
| 529 |
static void |
| 530 |
set_fork_info (TestResult * tr, int status, int signal_expected, |
| 531 |
signed char allowed_exit_value) |
| 532 |
{ |
| 533 |
int was_sig = WIFSIGNALED (status); |
| 534 |
int was_exit = WIFEXITED (status); |
| 535 |
signed char exit_status = WEXITSTATUS (status); |
| 536 |
int signal_received = WTERMSIG (status); |
| 537 |
|
| 538 |
if (was_sig) { |
| 539 |
if (signal_expected == signal_received) { |
| 540 |
if (alarm_received) { |
| 541 |
/* Got alarm instead of signal */ |
| 542 |
tr->rtype = CK_ERROR; |
| 543 |
if (tr->msg != NULL) { |
| 544 |
free (tr->msg); |
| 545 |
} |
| 546 |
tr->msg = signal_error_msg (signal_received, signal_expected); |
| 547 |
} else { |
| 548 |
tr->rtype = CK_PASS; |
| 549 |
if (tr->msg != NULL) { |
| 550 |
free (tr->msg); |
| 551 |
} |
| 552 |
tr->msg = pass_msg (); |
| 553 |
} |
| 554 |
} else if (signal_expected != 0) { |
| 555 |
/* signal received, but not the expected one */ |
| 556 |
tr->rtype = CK_ERROR; |
| 557 |
if (tr->msg != NULL) { |
| 558 |
free (tr->msg); |
| 559 |
} |
| 560 |
tr->msg = signal_error_msg (signal_received, signal_expected); |
| 561 |
} else { |
| 562 |
/* signal received and none expected */ |
| 563 |
tr->rtype = CK_ERROR; |
| 564 |
if (tr->msg != NULL) { |
| 565 |
free (tr->msg); |
| 566 |
} |
| 567 |
tr->msg = signal_msg (signal_received); |
| 568 |
} |
| 569 |
} else if (signal_expected == 0) { |
| 570 |
if (was_exit && exit_status == allowed_exit_value) { |
| 571 |
tr->rtype = CK_PASS; |
| 572 |
if (tr->msg != NULL) { |
| 573 |
free (tr->msg); |
| 574 |
} |
| 575 |
tr->msg = pass_msg (); |
| 576 |
} else if (was_exit && exit_status != allowed_exit_value) { |
| 577 |
if (tr->msg == NULL) { /* early exit */ |
| 578 |
tr->rtype = CK_ERROR; |
| 579 |
tr->msg = exit_msg (exit_status); |
| 580 |
} else { |
| 581 |
tr->rtype = CK_FAILURE; |
| 582 |
} |
| 583 |
} |
| 584 |
} else { /* a signal was expected and none raised */ |
| 585 |
if (was_exit) { |
| 586 |
if (tr->msg != NULL) { |
| 587 |
free (tr->msg); |
| 588 |
} |
| 589 |
tr->msg = exit_msg (exit_status); |
| 590 |
tr->rtype = CK_FAILURE; /* normal exit status */ |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
static char * |
| 596 |
signal_msg (int signal) |
| 597 |
{ |
| 598 |
char *msg = (char *) emalloc (MSG_LEN); /* free'd by caller */ |
| 599 |
|
| 600 |
if (alarm_received) { |
| 601 |
snprintf (msg, MSG_LEN, "Test timeout expired"); |
| 602 |
} else { |
| 603 |
snprintf (msg, MSG_LEN, "Received signal %d (%s)", |
| 604 |
signal, strsignal (signal)); |
| 605 |
} |
| 606 |
return msg; |
| 607 |
} |
| 608 |
|
| 609 |
static char * |
| 610 |
signal_error_msg (int signal_received, int signal_expected) |
| 611 |
{ |
| 612 |
char *sig_r_str; |
| 613 |
char *sig_e_str; |
| 614 |
char *msg = (char *) emalloc (MSG_LEN); /* free'd by caller */ |
| 615 |
|
| 616 |
sig_r_str = strdup (strsignal (signal_received)); |
| 617 |
sig_e_str = strdup (strsignal (signal_expected)); |
| 618 |
if (alarm_received) { |
| 619 |
snprintf (msg, MSG_LEN, |
| 620 |
"Test timeout expired, expected signal %d (%s)", |
| 621 |
signal_expected, sig_e_str); |
| 622 |
} else { |
| 623 |
snprintf (msg, MSG_LEN, "Received signal %d (%s), expected %d (%s)", |
| 624 |
signal_received, sig_r_str, signal_expected, sig_e_str); |
| 625 |
} |
| 626 |
free (sig_r_str); |
| 627 |
free (sig_e_str); |
| 628 |
return msg; |
| 629 |
} |
| 630 |
|
| 631 |
static char * |
| 632 |
exit_msg (int exitval) |
| 633 |
{ |
| 634 |
char *msg = (char *) emalloc (MSG_LEN); /* free'd by caller */ |
| 635 |
|
| 636 |
snprintf (msg, MSG_LEN, "Early exit with return value %d", exitval); |
| 637 |
return msg; |
| 638 |
} |
| 639 |
|
| 640 |
static int |
| 641 |
waserror (int status, int signal_expected) |
| 642 |
{ |
| 643 |
int was_sig = WIFSIGNALED (status); |
| 644 |
int was_exit = WIFEXITED (status); |
| 645 |
int exit_status = WEXITSTATUS (status); |
| 646 |
int signal_received = WTERMSIG (status); |
| 647 |
|
| 648 |
return ((was_sig && (signal_received != signal_expected)) || |
| 649 |
(was_exit && exit_status != 0)); |
| 650 |
} |
| 651 |
#endif /* HAVE_FORK */ |
| 652 |
|
| 653 |
enum fork_status |
| 654 |
srunner_fork_status (SRunner * sr) |
| 655 |
{ |
| 656 |
if (sr->fstat == CK_FORK_GETENV) { |
| 657 |
char *env = getenv ("CK_FORK"); |
| 658 |
|
| 659 |
if (env == NULL) |
| 660 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 661 |
return CK_FORK; |
| 662 |
#else |
| 663 |
return CK_NOFORK; |
| 664 |
#endif |
| 665 |
if (strcmp (env, "no") == 0) |
| 666 |
return CK_NOFORK; |
| 667 |
else { |
| 668 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 669 |
return CK_FORK; |
| 670 |
#else /* HAVE_FORK */ |
| 671 |
eprintf ("This version does not support fork", __FILE__, __LINE__); |
| 672 |
/* Ignoring, as Check is not compiled with fork support. */ |
| 673 |
return CK_NOFORK; |
| 674 |
#endif /* HAVE_FORK */ |
| 675 |
} |
| 676 |
} else |
| 677 |
return sr->fstat; |
| 678 |
} |
| 679 |
|
| 680 |
void |
| 681 |
srunner_set_fork_status (SRunner * sr, enum fork_status fstat) |
| 682 |
{ |
| 683 |
#if !defined(HAVE_FORK) || HAVE_FORK==0 |
| 684 |
/* If fork() is unavailable, do not allow a fork mode to be set */ |
| 685 |
if (fstat != CK_NOFORK) { |
| 686 |
eprintf ("This version does not support fork", __FILE__, __LINE__); |
| 687 |
/* Overriding, as Check is not compiled with fork support. */ |
| 688 |
fstat = CK_NOFORK; |
| 689 |
} |
| 690 |
#endif /* ! HAVE_FORK */ |
| 691 |
sr->fstat = fstat; |
| 692 |
} |
| 693 |
|
| 694 |
void |
| 695 |
srunner_run_all (SRunner * sr, enum print_output print_mode) |
| 696 |
{ |
| 697 |
srunner_run (sr, NULL, /* All test suites. */ |
| 698 |
NULL, /* All test cases. */ |
| 699 |
print_mode); |
| 700 |
} |
| 701 |
|
| 702 |
void |
| 703 |
srunner_run_tagged (SRunner * sr, const char *sname, const char *tcname, |
| 704 |
const char *include_tags, const char *exclude_tags, |
| 705 |
enum print_output print_mode) |
| 706 |
{ |
| 707 |
#if defined(HAVE_SIGACTION) && defined(HAVE_FORK) |
| 708 |
static struct sigaction sigalarm_old_action; |
| 709 |
static struct sigaction sigalarm_new_action; |
| 710 |
static struct sigaction sigint_new_action; |
| 711 |
static struct sigaction sigterm_new_action; |
| 712 |
#endif /* HAVE_SIGACTION && HAVE_FORK */ |
| 713 |
|
| 714 |
/* Get the selected test suite and test case from the |
| 715 |
environment. */ |
| 716 |
if (!tcname) |
| 717 |
tcname = getenv ("CK_RUN_CASE"); |
| 718 |
if (!sname) |
| 719 |
sname = getenv ("CK_RUN_SUITE"); |
| 720 |
if (!include_tags) |
| 721 |
include_tags = getenv ("CK_INCLUDE_TAGS"); |
| 722 |
if (!exclude_tags) |
| 723 |
exclude_tags = getenv ("CK_EXCLUDE_TAGS"); |
| 724 |
|
| 725 |
if (sr == NULL) |
| 726 |
return; |
| 727 |
if (print_mode >= CK_LAST) { |
| 728 |
eprintf ("Bad print_mode argument to srunner_run_all: %d", |
| 729 |
__FILE__, __LINE__, print_mode); |
| 730 |
} |
| 731 |
#if defined(HAVE_SIGACTION) && defined(HAVE_FORK) |
| 732 |
memset (&sigalarm_new_action, 0, sizeof (sigalarm_new_action)); |
| 733 |
sigalarm_new_action.sa_handler = sig_handler; |
| 734 |
sigaction (SIGALRM, &sigalarm_new_action, &sigalarm_old_action); |
| 735 |
|
| 736 |
memset (&sigint_new_action, 0, sizeof (sigint_new_action)); |
| 737 |
sigint_new_action.sa_handler = sig_handler; |
| 738 |
sigaction (SIGINT, &sigint_new_action, &sigint_old_action); |
| 739 |
|
| 740 |
memset (&sigterm_new_action, 0, sizeof (sigterm_new_action)); |
| 741 |
sigterm_new_action.sa_handler = sig_handler; |
| 742 |
sigaction (SIGTERM, &sigterm_new_action, &sigterm_old_action); |
| 743 |
#endif /* HAVE_SIGACTION && HAVE_FORK */ |
| 744 |
srunner_run_init (sr, print_mode); |
| 745 |
srunner_iterate_suites (sr, sname, tcname, include_tags, exclude_tags, |
| 746 |
print_mode); |
| 747 |
srunner_run_end (sr, print_mode); |
| 748 |
#if defined(HAVE_SIGACTION) && defined(HAVE_FORK) |
| 749 |
sigaction (SIGALRM, &sigalarm_old_action, NULL); |
| 750 |
sigaction (SIGINT, &sigint_old_action, NULL); |
| 751 |
sigaction (SIGTERM, &sigterm_old_action, NULL); |
| 752 |
#endif /* HAVE_SIGACTION && HAVE_FORK */ |
| 753 |
} |
| 754 |
|
| 755 |
void |
| 756 |
srunner_run (SRunner * sr, const char *sname, const char *tcname, |
| 757 |
enum print_output print_mode) |
| 758 |
{ |
| 759 |
srunner_run_tagged (sr, sname, tcname, NULL, NULL, print_mode); |
| 760 |
} |
| 761 |
|
| 762 |
pid_t |
| 763 |
check_fork (void) |
| 764 |
{ |
| 765 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 766 |
pid_t pid = fork (); |
| 767 |
|
| 768 |
/* Set the process to a process group to be able to kill it easily. */ |
| 769 |
if (pid >= 0) { |
| 770 |
setpgid (pid, group_pid); |
| 771 |
} |
| 772 |
return pid; |
| 773 |
#else /* HAVE_FORK */ |
| 774 |
eprintf ("This version does not support fork", __FILE__, __LINE__); |
| 775 |
return 0; |
| 776 |
#endif /* HAVE_FORK */ |
| 777 |
} |
| 778 |
|
| 779 |
void |
| 780 |
check_waitpid_and_exit (pid_t pid CK_ATTRIBUTE_UNUSED) |
| 781 |
{ |
| 782 |
#if defined(HAVE_FORK) && HAVE_FORK==1 |
| 783 |
pid_t pid_w; |
| 784 |
int status; |
| 785 |
|
| 786 |
if (pid > 0) { |
| 787 |
do { |
| 788 |
pid_w = waitpid (pid, &status, 0); |
| 789 |
} |
| 790 |
while (pid_w == -1); |
| 791 |
if (waserror (status, 0)) { |
| 792 |
g_thread_pool_stop_unused_threads (); |
| 793 |
exit (EXIT_FAILURE); |
| 794 |
} |
| 795 |
} |
| 796 |
g_thread_pool_stop_unused_threads (); |
| 797 |
exit (EXIT_SUCCESS); |
| 798 |
#else /* HAVE_FORK */ |
| 799 |
eprintf ("This version does not support fork", __FILE__, __LINE__); |
| 800 |
/* Ignoring, as Check is not compiled with fork support. */ |
| 801 |
exit (EXIT_FAILURE); |
| 802 |
#endif /* HAVE_FORK */ |
| 803 |
} |