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 "../lib/libcompat.h"
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <math.h>
28
29 #if defined(HAVE_FORK) && HAVE_FORK==1
30 #include <unistd.h>
31 #endif /* HAVE_FORK */
32
33 #include "check.h"
34 #include "check_error.h"
35 #include "check_list.h"
36 #include "check_impl.h"
37 #include "check_msg.h"
38
39 #ifndef DEFAULT_TIMEOUT
40 #define DEFAULT_TIMEOUT 4
41 #endif
42
43 /*
44 * When a process exits either normally, with exit(), or
45 * by an uncaught signal, The lower 0x377 bits are passed
46 * to the parent. Of those, only the lower 8 bits are
47 * returned by the WEXITSTATUS() macro.
48 */
49 #define WEXITSTATUS_MASK 0xFF
50
51 int check_major_version = CHECK_MAJOR_VERSION;
52 int check_minor_version = CHECK_MINOR_VERSION;
53 int check_micro_version = CHECK_MICRO_VERSION;
54
55 const char* current_test_name = NULL;
56
57 static int non_pass(enum test_result);
58 static Fixture *fixture_create(SFun fun, int ischecked);
59 static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
60 int ischecked);
61 static void tr_init(TestResult * tr);
62 static void suite_free(Suite * s);
63 static void tcase_free(TCase * tc);
64
suite_create(const char * name)65 Suite *suite_create(const char *name)
66 {
67 Suite *s;
68
69 s = (Suite *)emalloc(sizeof(Suite)); /* freed in suite_free */
70 if(name == NULL)
71 s->name = "";
72 else
73 s->name = name;
74 s->tclst = check_list_create();
75 return s;
76 }
77
suite_tcase(Suite * s,const char * tcname)78 int suite_tcase(Suite * s, const char *tcname)
79 {
80 List *l;
81
82 if(s == NULL)
83 return 0;
84
85 l = s->tclst;
86 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
87 {
88 TCase *tc = (TCase *)check_list_val(l);
89 if(strcmp(tcname, tc->name) == 0)
90 return 1;
91 }
92
93 return 0;
94 }
95
suite_free(Suite * s)96 static void suite_free(Suite * s)
97 {
98 List *l;
99
100 if(s == NULL)
101 return;
102 l = s->tclst;
103 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
104 {
105 tcase_free((TCase *)check_list_val(l));
106 }
107 check_list_free(s->tclst);
108 free(s);
109 }
110
111
tcase_create(const char * name)112 TCase *tcase_create(const char *name)
113 {
114 char *env;
115 double timeout_sec = DEFAULT_TIMEOUT;
116
117 TCase *tc = (TCase *)emalloc(sizeof(TCase)); /*freed in tcase_free */
118
119 if(name == NULL)
120 tc->name = "";
121 else
122 tc->name = name;
123
124 env = getenv("CK_DEFAULT_TIMEOUT");
125 if(env != NULL)
126 {
127 char *endptr = NULL;
128 double tmp = strtod(env, &endptr);
129
130 if(tmp >= 0 && endptr != env && (*endptr) == '\0')
131 {
132 timeout_sec = tmp;
133 }
134 }
135
136 env = getenv("CK_TIMEOUT_MULTIPLIER");
137 if(env != NULL)
138 {
139 char *endptr = NULL;
140 double tmp = strtod(env, &endptr);
141
142 if(tmp >= 0 && endptr != env && (*endptr) == '\0')
143 {
144 timeout_sec = timeout_sec * tmp;
145 }
146 }
147
148 tc->timeout.tv_sec = (time_t) floor(timeout_sec);
149 tc->timeout.tv_nsec =
150 (long)((timeout_sec -
151 floor(timeout_sec)) * (double)NANOS_PER_SECONDS);
152
153 tc->tflst = check_list_create();
154 tc->unch_sflst = check_list_create();
155 tc->ch_sflst = check_list_create();
156 tc->unch_tflst = check_list_create();
157 tc->ch_tflst = check_list_create();
158 tc->tags = check_list_create();
159
160 return tc;
161 }
162
163 /*
164 * Helper function to create a list of tags from
165 * a space separated string.
166 */
tag_string_to_list(const char * tags_string)167 List *tag_string_to_list(const char *tags_string)
168 {
169 List *list;
170 char *tags;
171 char *tag;
172
173 list = check_list_create();
174
175 if (NULL == tags_string)
176 {
177 return list;
178 }
179
180 tags = strdup(tags_string);
181 tag = strtok(tags, " ");
182 while (tag)
183 {
184 check_list_add_end(list, strdup(tag));
185 tag = strtok(NULL, " ");
186 }
187 free(tags);
188 return list;
189 }
190
tcase_set_tags(TCase * tc,const char * tags_orig)191 void tcase_set_tags(TCase * tc, const char *tags_orig)
192 {
193 /* replace any pre-existing list */
194 if (tc->tags)
195 {
196 check_list_apply(tc->tags, free);
197 check_list_free(tc->tags);
198 }
199 tc->tags = tag_string_to_list(tags_orig);
200 }
201
tcase_free(TCase * tc)202 static void tcase_free(TCase * tc)
203 {
204 check_list_apply(tc->tflst, free);
205 check_list_apply(tc->unch_sflst, free);
206 check_list_apply(tc->ch_sflst, free);
207 check_list_apply(tc->unch_tflst, free);
208 check_list_apply(tc->ch_tflst, free);
209 check_list_apply(tc->tags, free);
210 check_list_free(tc->tflst);
211 check_list_free(tc->unch_sflst);
212 check_list_free(tc->ch_sflst);
213 check_list_free(tc->unch_tflst);
214 check_list_free(tc->ch_tflst);
215 check_list_free(tc->tags);
216 free(tc);
217 }
218
tcase_matching_tag(TCase * tc,List * check_for)219 unsigned int tcase_matching_tag(TCase *tc, List *check_for)
220 {
221
222 if (NULL == check_for)
223 {
224 return 0;
225 }
226
227 for(check_list_front(check_for); !check_list_at_end(check_for);
228 check_list_advance(check_for))
229 {
230 for(check_list_front(tc->tags); !check_list_at_end(tc->tags);
231 check_list_advance(tc->tags))
232 {
233 if (0 == strcmp((const char *)check_list_val(tc->tags),
234 (const char *)check_list_val(check_for)))
235 {
236 return 1;
237 }
238 }
239 }
240 return 0;
241 }
242
suite_add_tcase(Suite * s,TCase * tc)243 void suite_add_tcase(Suite * s, TCase * tc)
244 {
245 if(s == NULL || tc == NULL || check_list_contains(s->tclst, tc))
246 {
247 return;
248 }
249
250 check_list_add_end(s->tclst, tc);
251 }
252
_tcase_add_test(TCase * tc,const TTest * ttest,int _signal,int allowed_exit_value,int start,int end)253 void _tcase_add_test(TCase * tc, const TTest * ttest,
254 int _signal, int allowed_exit_value,
255 int start, int end)
256 {
257 TF *tf;
258
259 if(tc == NULL || ttest == NULL)
260 return;
261 tf = (TF *)emalloc(sizeof(TF)); /* freed in tcase_free */
262 tf->ttest = ttest;
263 tf->loop_start = start;
264 tf->loop_end = end;
265 tf->signal = _signal; /* 0 means no signal expected */
266 tf->allowed_exit_value =
267 (WEXITSTATUS_MASK & allowed_exit_value); /* 0 is default successful exit */
268 check_list_add_end(tc->tflst, tf);
269 }
270
fixture_create(SFun fun,int ischecked)271 static Fixture *fixture_create(SFun fun, int ischecked)
272 {
273 Fixture *f;
274
275 f = (Fixture *)emalloc(sizeof(Fixture));
276 f->fun = fun;
277 f->ischecked = ischecked;
278
279 return f;
280 }
281
tcase_add_unchecked_fixture(TCase * tc,SFun setup,SFun teardown)282 void tcase_add_unchecked_fixture(TCase * tc, SFun setup, SFun teardown)
283 {
284 tcase_add_fixture(tc, setup, teardown, 0);
285 }
286
tcase_add_checked_fixture(TCase * tc,SFun setup,SFun teardown)287 void tcase_add_checked_fixture(TCase * tc, SFun setup, SFun teardown)
288 {
289 tcase_add_fixture(tc, setup, teardown, 1);
290 }
291
tcase_add_fixture(TCase * tc,SFun setup,SFun teardown,int ischecked)292 static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
293 int ischecked)
294 {
295 if(setup)
296 {
297 if(ischecked)
298 check_list_add_end(tc->ch_sflst,
299 fixture_create(setup, ischecked));
300 else
301 check_list_add_end(tc->unch_sflst,
302 fixture_create(setup, ischecked));
303 }
304
305 /* Add teardowns at front so they are run in reverse order. */
306 if(teardown)
307 {
308 if(ischecked)
309 check_list_add_front(tc->ch_tflst,
310 fixture_create(teardown, ischecked));
311 else
312 check_list_add_front(tc->unch_tflst,
313 fixture_create(teardown, ischecked));
314 }
315 }
316
tcase_set_timeout(TCase * tc,double timeout)317 void tcase_set_timeout(TCase * tc, double timeout)
318 {
319 #if defined(HAVE_FORK)
320 if(timeout >= 0)
321 {
322 char *env = getenv("CK_TIMEOUT_MULTIPLIER");
323
324 if(env != NULL)
325 {
326 char *endptr = NULL;
327 double tmp = strtod(env, &endptr);
328
329 if(tmp >= 0 && endptr != env && (*endptr) == '\0')
330 {
331 timeout = timeout * tmp;
332 }
333 }
334
335 tc->timeout.tv_sec = (time_t) floor(timeout);
336 tc->timeout.tv_nsec =
337 (long)((timeout - floor(timeout)) * (double)NANOS_PER_SECONDS);
338 }
339 #else
340 (void)tc;
341 (void)timeout;
342 /* Ignoring, as Check is not compiled with fork support. */
343 #endif /* HAVE_FORK */
344 }
345
tcase_fn_start(const char * fname,const char * file,int line)346 void tcase_fn_start(const char *fname, const char *file,
347 int line)
348 {
349 send_ctx_info(CK_CTX_TEST);
350 send_loc_info(file, line);
351
352 current_test_name = fname;
353 }
354
tcase_name(void)355 const char* tcase_name(void)
356 {
357 return current_test_name;
358 }
359
_mark_point(const char * file,int line)360 void _mark_point(const char *file, int line)
361 {
362 send_loc_info(file, line);
363 }
364
_ck_assert_failed(const char * file,int line,const char * expr,const char * msg,...)365 void _ck_assert_failed(const char *file, int line, const char *expr,
366 const char *msg, ...)
367 {
368 char buf[BUFSIZ];
369 const char *to_send;
370
371 send_loc_info(file, line);
372
373 /*
374 * If a message was passed, format it with vsnprintf.
375 * Otherwise, print the expression as is.
376 */
377 if(msg != NULL)
378 {
379 va_list ap;
380 va_start(ap, msg);
381 vsnprintf(buf, BUFSIZ, msg, ap);
382 va_end(ap);
383 to_send = buf;
384 }
385 else
386 {
387 to_send = expr;
388 }
389
390 send_failure_info(to_send);
391 if(cur_fork_status() == CK_FORK)
392 {
393 #if defined(HAVE_FORK) && HAVE_FORK==1
394 _exit(1);
395 #endif /* HAVE_FORK */
396 }
397 else
398 {
399 longjmp(error_jmp_buffer, 1);
400 }
401 }
402
srunner_create(Suite * s)403 SRunner *srunner_create(Suite * s)
404 {
405 SRunner *sr = (SRunner *)emalloc(sizeof(SRunner)); /* freed in srunner_free */
406
407 sr->slst = check_list_create();
408 if(s != NULL)
409 check_list_add_end(sr->slst, s);
410 sr->stats = (TestStats *)emalloc(sizeof(TestStats)); /* freed in srunner_free */
411 sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
412 sr->resultlst = check_list_create();
413 sr->log_fname = NULL;
414 sr->xml_fname = NULL;
415 sr->tap_fname = NULL;
416 sr->loglst = NULL;
417
418 #if defined(HAVE_FORK)
419 sr->fstat = CK_FORK_GETENV;
420 #else
421 /*
422 * Overriding the default of running tests in fork mode,
423 * as this system does not have fork()
424 */
425 sr->fstat = CK_NOFORK;
426 #endif /* HAVE_FORK */
427
428 return sr;
429 }
430
srunner_add_suite(SRunner * sr,Suite * s)431 void srunner_add_suite(SRunner * sr, Suite * s)
432 {
433 if(s == NULL)
434 return;
435
436 check_list_add_end(sr->slst, s);
437 }
438
srunner_free(SRunner * sr)439 void srunner_free(SRunner * sr)
440 {
441 List *l;
442
443 if(sr == NULL)
444 return;
445
446 free(sr->stats);
447 l = sr->slst;
448 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
449 {
450 suite_free((Suite *)check_list_val(l));
451 }
452 check_list_free(sr->slst);
453
454 l = sr->resultlst;
455 for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
456 {
457 TestResult *tr = (TestResult *)check_list_val(l);
458 tr_free(tr);
459 }
460 check_list_free(sr->resultlst);
461
462 free(sr);
463 }
464
srunner_ntests_failed(SRunner * sr)465 int srunner_ntests_failed(SRunner * sr)
466 {
467 return sr->stats->n_failed + sr->stats->n_errors;
468 }
469
srunner_ntests_run(SRunner * sr)470 int srunner_ntests_run(SRunner * sr)
471 {
472 return sr->stats->n_checked;
473 }
474
srunner_failures(SRunner * sr)475 TestResult **srunner_failures(SRunner * sr)
476 {
477 int i = 0;
478 TestResult **trarray;
479 List *rlst;
480
481 trarray = (TestResult **)emalloc(sizeof(trarray[0]) * srunner_ntests_failed(sr));
482
483 rlst = sr->resultlst;
484 for(check_list_front(rlst); !check_list_at_end(rlst);
485 check_list_advance(rlst))
486 {
487 TestResult *tr = (TestResult *)check_list_val(rlst);
488
489 if(non_pass(tr->rtype))
490 trarray[i++] = tr;
491
492 }
493 return trarray;
494 }
495
srunner_results(SRunner * sr)496 TestResult **srunner_results(SRunner * sr)
497 {
498 int i = 0;
499 TestResult **trarray;
500 List *rlst;
501
502 trarray =(TestResult **) emalloc(sizeof(trarray[0]) * srunner_ntests_run(sr));
503
504 rlst = sr->resultlst;
505 for(check_list_front(rlst); !check_list_at_end(rlst);
506 check_list_advance(rlst))
507 {
508 trarray[i++] = (TestResult *)check_list_val(rlst);
509 }
510 return trarray;
511 }
512
non_pass(enum test_result val)513 static int non_pass(enum test_result val)
514 {
515 return val != CK_PASS;
516 }
517
tr_create(void)518 TestResult *tr_create(void)
519 {
520 TestResult *tr;
521
522 tr = (TestResult *)emalloc(sizeof(TestResult));
523 tr_init(tr);
524 return tr;
525 }
526
tr_init(TestResult * tr)527 static void tr_init(TestResult * tr)
528 {
529 tr->ctx = CK_CTX_INVALID;
530 tr->line = -1;
531 tr->rtype = CK_TEST_RESULT_INVALID;
532 tr->msg = NULL;
533 tr->file = NULL;
534 tr->tcname = NULL;
535 tr->tname = NULL;
536 tr->duration = -1;
537 }
538
tr_free(TestResult * tr)539 void tr_free(TestResult * tr)
540 {
541 free(tr->file);
542 free(tr->msg);
543 free(tr);
544 }
545
546
tr_msg(TestResult * tr)547 const char *tr_msg(TestResult * tr)
548 {
549 return tr->msg;
550 }
551
tr_lno(TestResult * tr)552 int tr_lno(TestResult * tr)
553 {
554 return tr->line;
555 }
556
tr_lfile(TestResult * tr)557 const char *tr_lfile(TestResult * tr)
558 {
559 return tr->file;
560 }
561
tr_rtype(TestResult * tr)562 int tr_rtype(TestResult * tr)
563 {
564 return tr->rtype;
565 }
566
tr_ctx(TestResult * tr)567 enum ck_result_ctx tr_ctx(TestResult * tr)
568 {
569 return tr->ctx;
570 }
571
tr_tcname(TestResult * tr)572 const char *tr_tcname(TestResult * tr)
573 {
574 return tr->tcname;
575 }
576
577 static enum fork_status _fstat = CK_FORK;
578
set_fork_status(enum fork_status fstat)579 void set_fork_status(enum fork_status fstat)
580 {
581 if(fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
582 _fstat = fstat;
583 else
584 eprintf("Bad status in set_fork_status", __FILE__, __LINE__);
585 }
586
cur_fork_status(void)587 enum fork_status cur_fork_status(void)
588 {
589 return _fstat;
590 }
591
592 /**
593 * Not all systems support the same clockid_t's. This call checks
594 * if the CLOCK_MONOTONIC clockid_t is valid. If so, that is returned,
595 * otherwise, CLOCK_REALTIME is returned.
596 *
597 * The clockid_t that was found to work on the first call is
598 * cached for subsequent calls.
599 */
check_get_clockid()600 clockid_t check_get_clockid()
601 {
602 static clockid_t clockid = -1;
603
604 /*
605 * Only check if we have librt available. Otherwise, the clockid
606 * will be ignored anyway, as the clock_gettime() and
607 * generic_timer_create() functions will be re-implemented in libcompat.
608 * Worse, if librt and alarm() are unavailable, this check
609 * will result in an assert(0).
610 */
611 #ifdef HAVE_LIBRT
612 timer_t timerid;
613
614 if(generic_timer_create(CLOCK_MONOTONIC, NULL, &timerid) == 0)
615 {
616 timer_delete(timerid);
617 clockid = CLOCK_MONOTONIC;
618 }
619 else
620 {
621 clockid = CLOCK_REALTIME;
622 }
623 #else
624 clockid = CLOCK_MONOTONIC;
625 #endif
626
627 return clockid;
628 }
629