Home | History | Annotate | Line # | Download | only in atf-c
tc.c revision 1.8
      1  1.1  jmmv /*
      2  1.1  jmmv  * Automated Testing Framework (atf)
      3  1.1  jmmv  *
      4  1.4  jmmv  * Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc.
      5  1.1  jmmv  * All rights reserved.
      6  1.1  jmmv  *
      7  1.1  jmmv  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmmv  * modification, are permitted provided that the following conditions
      9  1.1  jmmv  * are met:
     10  1.1  jmmv  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmmv  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmmv  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmmv  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmmv  *
     16  1.1  jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1  jmmv  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1  jmmv  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1  jmmv  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  jmmv  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1  jmmv  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  jmmv  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1  jmmv  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1  jmmv  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  jmmv  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1  jmmv  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  jmmv  */
     29  1.1  jmmv 
     30  1.7  jmmv #include <sys/types.h>
     31  1.7  jmmv #include <sys/stat.h>
     32  1.7  jmmv 
     33  1.1  jmmv #include <errno.h>
     34  1.7  jmmv #include <fcntl.h>
     35  1.1  jmmv #include <stdarg.h>
     36  1.1  jmmv #include <stdbool.h>
     37  1.1  jmmv #include <stdio.h>
     38  1.1  jmmv #include <stdlib.h>
     39  1.1  jmmv #include <string.h>
     40  1.1  jmmv #include <unistd.h>
     41  1.1  jmmv 
     42  1.3  jmmv #include "atf-c/defs.h"
     43  1.1  jmmv #include "atf-c/error.h"
     44  1.1  jmmv #include "atf-c/tc.h"
     45  1.7  jmmv 
     46  1.7  jmmv #include "detail/env.h"
     47  1.7  jmmv #include "detail/fs.h"
     48  1.7  jmmv #include "detail/map.h"
     49  1.7  jmmv #include "detail/sanity.h"
     50  1.7  jmmv #include "detail/text.h"
     51  1.1  jmmv 
     52  1.1  jmmv /* ---------------------------------------------------------------------
     53  1.6  jmmv  * Auxiliary functions.
     54  1.1  jmmv  * --------------------------------------------------------------------- */
     55  1.1  jmmv 
     56  1.6  jmmv enum expect_type {
     57  1.6  jmmv     EXPECT_PASS,
     58  1.6  jmmv     EXPECT_FAIL,
     59  1.6  jmmv     EXPECT_EXIT,
     60  1.6  jmmv     EXPECT_SIGNAL,
     61  1.6  jmmv     EXPECT_DEATH,
     62  1.6  jmmv     EXPECT_TIMEOUT,
     63  1.6  jmmv };
     64  1.6  jmmv 
     65  1.6  jmmv struct context {
     66  1.6  jmmv     const atf_tc_t *tc;
     67  1.7  jmmv     const char *resfile;
     68  1.6  jmmv     size_t fail_count;
     69  1.6  jmmv 
     70  1.6  jmmv     enum expect_type expect;
     71  1.6  jmmv     atf_dynstr_t expect_reason;
     72  1.6  jmmv     size_t expect_previous_fail_count;
     73  1.6  jmmv     size_t expect_fail_count;
     74  1.6  jmmv     int expect_exitcode;
     75  1.6  jmmv     int expect_signo;
     76  1.6  jmmv };
     77  1.6  jmmv 
     78  1.7  jmmv static void context_init(struct context *, const atf_tc_t *, const char *);
     79  1.6  jmmv static void check_fatal_error(atf_error_t);
     80  1.6  jmmv static void report_fatal_error(const char *, ...)
     81  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
     82  1.7  jmmv static atf_error_t write_resfile(const int, const char *, const int,
     83  1.6  jmmv                                  const atf_dynstr_t *);
     84  1.7  jmmv static void create_resfile(const char *, const char *, const int,
     85  1.6  jmmv                            atf_dynstr_t *);
     86  1.6  jmmv static void error_in_expect(struct context *, const char *, ...)
     87  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
     88  1.6  jmmv static void validate_expect(struct context *);
     89  1.6  jmmv static void expected_failure(struct context *, atf_dynstr_t *)
     90  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
     91  1.6  jmmv static void fail_requirement(struct context *, atf_dynstr_t *)
     92  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
     93  1.6  jmmv static void fail_check(struct context *, atf_dynstr_t *);
     94  1.6  jmmv static void pass(struct context *)
     95  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
     96  1.6  jmmv static void skip(struct context *, atf_dynstr_t *)
     97  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
     98  1.6  jmmv static void format_reason_ap(atf_dynstr_t *, const char *, const size_t,
     99  1.6  jmmv                              const char *, va_list);
    100  1.6  jmmv static void format_reason_fmt(atf_dynstr_t *, const char *, const size_t,
    101  1.6  jmmv                               const char *, ...);
    102  1.6  jmmv static void errno_test(struct context *, const char *, const size_t,
    103  1.6  jmmv                        const int, const char *, const bool,
    104  1.6  jmmv                        void (*)(struct context *, atf_dynstr_t *));
    105  1.1  jmmv static atf_error_t check_prog_in_dir(const char *, void *);
    106  1.6  jmmv static atf_error_t check_prog(struct context *, const char *, void *);
    107  1.6  jmmv 
    108  1.6  jmmv static void
    109  1.7  jmmv context_init(struct context *ctx, const atf_tc_t *tc, const char *resfile)
    110  1.6  jmmv {
    111  1.6  jmmv     ctx->tc = tc;
    112  1.6  jmmv     ctx->resfile = resfile;
    113  1.6  jmmv     ctx->fail_count = 0;
    114  1.6  jmmv     ctx->expect = EXPECT_PASS;
    115  1.6  jmmv     check_fatal_error(atf_dynstr_init(&ctx->expect_reason));
    116  1.6  jmmv     ctx->expect_previous_fail_count = 0;
    117  1.6  jmmv     ctx->expect_fail_count = 0;
    118  1.6  jmmv     ctx->expect_exitcode = 0;
    119  1.6  jmmv     ctx->expect_signo = 0;
    120  1.6  jmmv }
    121  1.6  jmmv 
    122  1.6  jmmv static void
    123  1.6  jmmv check_fatal_error(atf_error_t err)
    124  1.6  jmmv {
    125  1.6  jmmv     if (atf_is_error(err)) {
    126  1.6  jmmv         char buf[1024];
    127  1.6  jmmv         atf_error_format(err, buf, sizeof(buf));
    128  1.6  jmmv         fprintf(stderr, "FATAL ERROR: %s\n", buf);
    129  1.6  jmmv         atf_error_free(err);
    130  1.6  jmmv         abort();
    131  1.6  jmmv     }
    132  1.6  jmmv }
    133  1.6  jmmv 
    134  1.6  jmmv static void
    135  1.6  jmmv report_fatal_error(const char *msg, ...)
    136  1.6  jmmv {
    137  1.6  jmmv     va_list ap;
    138  1.6  jmmv     fprintf(stderr, "FATAL ERROR: ");
    139  1.6  jmmv 
    140  1.6  jmmv     va_start(ap, msg);
    141  1.6  jmmv     vfprintf(stderr, msg, ap);
    142  1.6  jmmv     va_end(ap);
    143  1.6  jmmv 
    144  1.6  jmmv     fprintf(stderr, "\n");
    145  1.6  jmmv     abort();
    146  1.6  jmmv }
    147  1.6  jmmv 
    148  1.6  jmmv /** Writes to a results file.
    149  1.6  jmmv  *
    150  1.6  jmmv  * The results file is supposed to be already open.
    151  1.6  jmmv  *
    152  1.6  jmmv  * This function returns an error code instead of exiting in case of error
    153  1.6  jmmv  * because the caller needs to clean up the reason object before terminating.
    154  1.6  jmmv  */
    155  1.6  jmmv static atf_error_t
    156  1.7  jmmv write_resfile(const int fd, const char *result, const int arg,
    157  1.6  jmmv               const atf_dynstr_t *reason)
    158  1.6  jmmv {
    159  1.7  jmmv     char buffer[1024];
    160  1.7  jmmv     int ret;
    161  1.7  jmmv 
    162  1.6  jmmv     if (arg == -1 && reason == NULL) {
    163  1.7  jmmv         if (snprintf(buffer, sizeof(buffer), "%s\n", result) <= 0)
    164  1.6  jmmv             goto err;
    165  1.6  jmmv     } else if (arg == -1 && reason != NULL) {
    166  1.7  jmmv         if (snprintf(buffer, sizeof(buffer), "%s: %s\n", result,
    167  1.7  jmmv                      atf_dynstr_cstring(reason)) <= 0)
    168  1.6  jmmv             goto err;
    169  1.6  jmmv     } else if (arg != -1 && reason != NULL) {
    170  1.7  jmmv         if (snprintf(buffer, sizeof(buffer), "%s(%d): %s\n", result,
    171  1.7  jmmv                      arg, atf_dynstr_cstring(reason)) <= 0)
    172  1.6  jmmv             goto err;
    173  1.7  jmmv     } else {
    174  1.6  jmmv         UNREACHABLE;
    175  1.7  jmmv     }
    176  1.6  jmmv 
    177  1.7  jmmv     while ((ret = write(fd, buffer, strlen(buffer))) == -1 && errno == EINTR)
    178  1.7  jmmv         ; /* Retry. */
    179  1.7  jmmv     if (ret != -1)
    180  1.7  jmmv         return atf_no_error();
    181  1.6  jmmv 
    182  1.6  jmmv err:
    183  1.7  jmmv     return atf_libc_error(
    184  1.7  jmmv         errno, "Failed to write results file; result %s, reason %s", result,
    185  1.6  jmmv         reason == NULL ? "null" : atf_dynstr_cstring(reason));
    186  1.6  jmmv }
    187  1.6  jmmv 
    188  1.6  jmmv /** Creates a results file.
    189  1.6  jmmv  *
    190  1.6  jmmv  * The input reason is released in all cases.
    191  1.6  jmmv  *
    192  1.6  jmmv  * An error in this function is considered to be fatal, hence why it does
    193  1.6  jmmv  * not return any error code.
    194  1.6  jmmv  */
    195  1.6  jmmv static void
    196  1.7  jmmv create_resfile(const char *resfile, const char *result, const int arg,
    197  1.6  jmmv                atf_dynstr_t *reason)
    198  1.6  jmmv {
    199  1.6  jmmv     atf_error_t err;
    200  1.6  jmmv 
    201  1.7  jmmv     if (strcmp("/dev/stdout", resfile) == 0) {
    202  1.7  jmmv         err = write_resfile(STDOUT_FILENO, result, arg, reason);
    203  1.7  jmmv     } else if (strcmp("/dev/stderr", resfile) == 0) {
    204  1.7  jmmv         err = write_resfile(STDERR_FILENO, result, arg, reason);
    205  1.6  jmmv     } else {
    206  1.7  jmmv         const int fd = open(resfile, O_WRONLY | O_CREAT | O_TRUNC,
    207  1.7  jmmv             S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    208  1.7  jmmv         if (fd == -1) {
    209  1.6  jmmv             err = atf_libc_error(errno, "Cannot create results file '%s'",
    210  1.7  jmmv                                  resfile);
    211  1.6  jmmv         } else {
    212  1.7  jmmv             err = write_resfile(fd, result, arg, reason);
    213  1.7  jmmv             close(fd);
    214  1.6  jmmv         }
    215  1.6  jmmv     }
    216  1.6  jmmv 
    217  1.6  jmmv     if (reason != NULL)
    218  1.6  jmmv         atf_dynstr_fini(reason);
    219  1.6  jmmv 
    220  1.6  jmmv     check_fatal_error(err);
    221  1.6  jmmv }
    222  1.6  jmmv 
    223  1.6  jmmv /** Fails a test case if validate_expect fails. */
    224  1.6  jmmv static void
    225  1.6  jmmv error_in_expect(struct context *ctx, const char *fmt, ...)
    226  1.6  jmmv {
    227  1.6  jmmv     atf_dynstr_t reason;
    228  1.6  jmmv     va_list ap;
    229  1.6  jmmv 
    230  1.6  jmmv     va_start(ap, fmt);
    231  1.6  jmmv     format_reason_ap(&reason, NULL, 0, fmt, ap);
    232  1.6  jmmv     va_end(ap);
    233  1.6  jmmv 
    234  1.6  jmmv     ctx->expect = EXPECT_PASS;  /* Ensure fail_requirement really fails. */
    235  1.6  jmmv     fail_requirement(ctx, &reason);
    236  1.6  jmmv }
    237  1.6  jmmv 
    238  1.6  jmmv /** Ensures that the "expect" state is correct.
    239  1.6  jmmv  *
    240  1.6  jmmv  * Call this function before modifying the current value of expect.
    241  1.6  jmmv  */
    242  1.6  jmmv static void
    243  1.6  jmmv validate_expect(struct context *ctx)
    244  1.6  jmmv {
    245  1.6  jmmv     if (ctx->expect == EXPECT_DEATH) {
    246  1.6  jmmv         error_in_expect(ctx, "Test case was expected to terminate abruptly "
    247  1.6  jmmv             "but it continued execution");
    248  1.6  jmmv     } else if (ctx->expect == EXPECT_EXIT) {
    249  1.6  jmmv         error_in_expect(ctx, "Test case was expected to exit cleanly but it "
    250  1.6  jmmv             "continued execution");
    251  1.6  jmmv     } else if (ctx->expect == EXPECT_FAIL) {
    252  1.6  jmmv         if (ctx->expect_fail_count == ctx->expect_previous_fail_count)
    253  1.6  jmmv             error_in_expect(ctx, "Test case was expecting a failure but none "
    254  1.6  jmmv                 "were raised");
    255  1.6  jmmv         else
    256  1.6  jmmv             INV(ctx->expect_fail_count > ctx->expect_previous_fail_count);
    257  1.6  jmmv     } else if (ctx->expect == EXPECT_PASS) {
    258  1.6  jmmv         /* Nothing to validate. */
    259  1.6  jmmv     } else if (ctx->expect == EXPECT_SIGNAL) {
    260  1.6  jmmv         error_in_expect(ctx, "Test case was expected to receive a termination "
    261  1.6  jmmv             "signal but it continued execution");
    262  1.6  jmmv     } else if (ctx->expect == EXPECT_TIMEOUT) {
    263  1.6  jmmv         error_in_expect(ctx, "Test case was expected to hang but it continued "
    264  1.6  jmmv             "execution");
    265  1.6  jmmv     } else
    266  1.6  jmmv         UNREACHABLE;
    267  1.6  jmmv }
    268  1.6  jmmv 
    269  1.6  jmmv static void
    270  1.6  jmmv expected_failure(struct context *ctx, atf_dynstr_t *reason)
    271  1.6  jmmv {
    272  1.6  jmmv     check_fatal_error(atf_dynstr_prepend_fmt(reason, "%s: ",
    273  1.6  jmmv         atf_dynstr_cstring(&ctx->expect_reason)));
    274  1.6  jmmv     create_resfile(ctx->resfile, "expected_failure", -1, reason);
    275  1.6  jmmv     exit(EXIT_SUCCESS);
    276  1.6  jmmv }
    277  1.6  jmmv 
    278  1.6  jmmv static void
    279  1.6  jmmv fail_requirement(struct context *ctx, atf_dynstr_t *reason)
    280  1.6  jmmv {
    281  1.6  jmmv     if (ctx->expect == EXPECT_FAIL) {
    282  1.6  jmmv         expected_failure(ctx, reason);
    283  1.6  jmmv     } else if (ctx->expect == EXPECT_PASS) {
    284  1.6  jmmv         create_resfile(ctx->resfile, "failed", -1, reason);
    285  1.6  jmmv         exit(EXIT_FAILURE);
    286  1.6  jmmv     } else {
    287  1.6  jmmv         error_in_expect(ctx, "Test case raised a failure but was not "
    288  1.6  jmmv             "expecting one; reason was %s", atf_dynstr_cstring(reason));
    289  1.6  jmmv     }
    290  1.6  jmmv     UNREACHABLE;
    291  1.6  jmmv }
    292  1.6  jmmv 
    293  1.6  jmmv static void
    294  1.6  jmmv fail_check(struct context *ctx, atf_dynstr_t *reason)
    295  1.6  jmmv {
    296  1.6  jmmv     if (ctx->expect == EXPECT_FAIL) {
    297  1.6  jmmv         fprintf(stderr, "*** Expected check failure: %s: %s\n",
    298  1.6  jmmv             atf_dynstr_cstring(&ctx->expect_reason),
    299  1.6  jmmv             atf_dynstr_cstring(reason));
    300  1.6  jmmv         ctx->expect_fail_count++;
    301  1.6  jmmv     } else if (ctx->expect == EXPECT_PASS) {
    302  1.6  jmmv         fprintf(stderr, "*** Check failed: %s\n", atf_dynstr_cstring(reason));
    303  1.6  jmmv         ctx->fail_count++;
    304  1.6  jmmv     } else {
    305  1.6  jmmv         error_in_expect(ctx, "Test case raised a failure but was not "
    306  1.6  jmmv             "expecting one; reason was %s", atf_dynstr_cstring(reason));
    307  1.6  jmmv     }
    308  1.6  jmmv 
    309  1.6  jmmv     atf_dynstr_fini(reason);
    310  1.6  jmmv }
    311  1.6  jmmv 
    312  1.6  jmmv static void
    313  1.6  jmmv pass(struct context *ctx)
    314  1.6  jmmv {
    315  1.6  jmmv     if (ctx->expect == EXPECT_FAIL) {
    316  1.6  jmmv         error_in_expect(ctx, "Test case was expecting a failure but got "
    317  1.6  jmmv             "a pass instead");
    318  1.6  jmmv     } else if (ctx->expect == EXPECT_PASS) {
    319  1.6  jmmv         create_resfile(ctx->resfile, "passed", -1, NULL);
    320  1.6  jmmv         exit(EXIT_SUCCESS);
    321  1.6  jmmv     } else {
    322  1.6  jmmv         error_in_expect(ctx, "Test case asked to explicitly pass but was "
    323  1.6  jmmv             "not expecting such condition");
    324  1.6  jmmv     }
    325  1.6  jmmv     UNREACHABLE;
    326  1.6  jmmv }
    327  1.6  jmmv 
    328  1.6  jmmv static void
    329  1.6  jmmv skip(struct context *ctx, atf_dynstr_t *reason)
    330  1.6  jmmv {
    331  1.6  jmmv     if (ctx->expect == EXPECT_PASS) {
    332  1.6  jmmv         create_resfile(ctx->resfile, "skipped", -1, reason);
    333  1.6  jmmv         exit(EXIT_SUCCESS);
    334  1.6  jmmv     } else {
    335  1.6  jmmv         error_in_expect(ctx, "Can only skip a test case when running in "
    336  1.6  jmmv             "expect pass mode");
    337  1.6  jmmv     }
    338  1.6  jmmv     UNREACHABLE;
    339  1.6  jmmv }
    340  1.6  jmmv 
    341  1.6  jmmv /** Formats a failure/skip reason message.
    342  1.6  jmmv  *
    343  1.6  jmmv  * The formatted reason is stored in out_reason.  out_reason is initialized
    344  1.6  jmmv  * in this function and is supposed to be released by the caller.  In general,
    345  1.6  jmmv  * the reason will eventually be fed to create_resfile, which will release
    346  1.6  jmmv  * it.
    347  1.6  jmmv  *
    348  1.6  jmmv  * Errors in this function are fatal.  Rationale being: reasons are used to
    349  1.6  jmmv  * create results files; if we can't format the reason correctly, the result
    350  1.6  jmmv  * of the test program will be bogus.  So it's better to just exit with a
    351  1.6  jmmv  * fatal error.
    352  1.6  jmmv  */
    353  1.6  jmmv static void
    354  1.6  jmmv format_reason_ap(atf_dynstr_t *out_reason,
    355  1.6  jmmv                  const char *source_file, const size_t source_line,
    356  1.6  jmmv                  const char *reason, va_list ap)
    357  1.6  jmmv {
    358  1.6  jmmv     atf_error_t err;
    359  1.6  jmmv 
    360  1.6  jmmv     if (source_file != NULL) {
    361  1.6  jmmv         err = atf_dynstr_init_fmt(out_reason, "%s:%zd: ", source_file,
    362  1.6  jmmv                                   source_line);
    363  1.6  jmmv     } else {
    364  1.6  jmmv         PRE(source_line == 0);
    365  1.6  jmmv         err = atf_dynstr_init(out_reason);
    366  1.6  jmmv     }
    367  1.6  jmmv 
    368  1.6  jmmv     if (!atf_is_error(err)) {
    369  1.6  jmmv         va_list ap2;
    370  1.6  jmmv         va_copy(ap2, ap);
    371  1.6  jmmv         err = atf_dynstr_append_ap(out_reason, reason, ap2);
    372  1.6  jmmv         va_end(ap2);
    373  1.6  jmmv     }
    374  1.6  jmmv 
    375  1.6  jmmv     check_fatal_error(err);
    376  1.6  jmmv }
    377  1.6  jmmv 
    378  1.6  jmmv static void
    379  1.6  jmmv format_reason_fmt(atf_dynstr_t *out_reason,
    380  1.6  jmmv                   const char *source_file, const size_t source_line,
    381  1.6  jmmv                   const char *reason, ...)
    382  1.6  jmmv {
    383  1.6  jmmv     va_list ap;
    384  1.6  jmmv 
    385  1.6  jmmv     va_start(ap, reason);
    386  1.6  jmmv     format_reason_ap(out_reason, source_file, source_line, reason, ap);
    387  1.6  jmmv     va_end(ap);
    388  1.6  jmmv }
    389  1.6  jmmv 
    390  1.6  jmmv static void
    391  1.6  jmmv errno_test(struct context *ctx, const char *file, const size_t line,
    392  1.6  jmmv            const int exp_errno, const char *expr_str,
    393  1.6  jmmv            const bool expr_result,
    394  1.6  jmmv            void (*fail_func)(struct context *, atf_dynstr_t *))
    395  1.6  jmmv {
    396  1.6  jmmv     const int actual_errno = errno;
    397  1.6  jmmv 
    398  1.6  jmmv     if (expr_result) {
    399  1.6  jmmv         if (exp_errno != actual_errno) {
    400  1.6  jmmv             atf_dynstr_t reason;
    401  1.6  jmmv 
    402  1.6  jmmv             format_reason_fmt(&reason, file, line, "Expected errno %d, got %d, "
    403  1.6  jmmv                 "in %s", exp_errno, actual_errno, expr_str);
    404  1.6  jmmv             fail_func(ctx, &reason);
    405  1.6  jmmv         }
    406  1.6  jmmv     } else {
    407  1.6  jmmv         atf_dynstr_t reason;
    408  1.6  jmmv 
    409  1.6  jmmv         format_reason_fmt(&reason, file, line, "Expected true value in %s",
    410  1.6  jmmv             expr_str);
    411  1.6  jmmv         fail_func(ctx, &reason);
    412  1.6  jmmv     }
    413  1.6  jmmv }
    414  1.6  jmmv 
    415  1.6  jmmv struct prog_found_pair {
    416  1.6  jmmv     const char *prog;
    417  1.6  jmmv     bool found;
    418  1.6  jmmv };
    419  1.6  jmmv 
    420  1.6  jmmv static atf_error_t
    421  1.6  jmmv check_prog_in_dir(const char *dir, void *data)
    422  1.6  jmmv {
    423  1.6  jmmv     struct prog_found_pair *pf = data;
    424  1.6  jmmv     atf_error_t err;
    425  1.6  jmmv 
    426  1.6  jmmv     if (pf->found)
    427  1.6  jmmv         err = atf_no_error();
    428  1.6  jmmv     else {
    429  1.6  jmmv         atf_fs_path_t p;
    430  1.6  jmmv 
    431  1.6  jmmv         err = atf_fs_path_init_fmt(&p, "%s/%s", dir, pf->prog);
    432  1.6  jmmv         if (atf_is_error(err))
    433  1.6  jmmv             goto out_p;
    434  1.6  jmmv 
    435  1.6  jmmv         err = atf_fs_eaccess(&p, atf_fs_access_x);
    436  1.6  jmmv         if (!atf_is_error(err))
    437  1.6  jmmv             pf->found = true;
    438  1.6  jmmv         else {
    439  1.6  jmmv             atf_error_free(err);
    440  1.6  jmmv             INV(!pf->found);
    441  1.6  jmmv             err = atf_no_error();
    442  1.6  jmmv         }
    443  1.6  jmmv 
    444  1.6  jmmv out_p:
    445  1.6  jmmv         atf_fs_path_fini(&p);
    446  1.6  jmmv     }
    447  1.6  jmmv 
    448  1.6  jmmv     return err;
    449  1.6  jmmv }
    450  1.6  jmmv 
    451  1.6  jmmv static atf_error_t
    452  1.6  jmmv check_prog(struct context *ctx, const char *prog, void *data)
    453  1.6  jmmv {
    454  1.6  jmmv     atf_error_t err;
    455  1.6  jmmv     atf_fs_path_t p;
    456  1.6  jmmv 
    457  1.6  jmmv     err = atf_fs_path_init_fmt(&p, "%s", prog);
    458  1.6  jmmv     if (atf_is_error(err))
    459  1.6  jmmv         goto out;
    460  1.6  jmmv 
    461  1.6  jmmv     if (atf_fs_path_is_absolute(&p)) {
    462  1.6  jmmv         err = atf_fs_eaccess(&p, atf_fs_access_x);
    463  1.6  jmmv         if (atf_is_error(err)) {
    464  1.6  jmmv             atf_dynstr_t reason;
    465  1.6  jmmv 
    466  1.6  jmmv             atf_error_free(err);
    467  1.6  jmmv             atf_fs_path_fini(&p);
    468  1.6  jmmv             format_reason_fmt(&reason, NULL, 0, "The required program %s could "
    469  1.6  jmmv                 "not be found", prog);
    470  1.6  jmmv             skip(ctx, &reason);
    471  1.6  jmmv         }
    472  1.6  jmmv     } else {
    473  1.6  jmmv         const char *path = atf_env_get("PATH");
    474  1.6  jmmv         struct prog_found_pair pf;
    475  1.6  jmmv         atf_fs_path_t bp;
    476  1.6  jmmv 
    477  1.6  jmmv         err = atf_fs_path_branch_path(&p, &bp);
    478  1.6  jmmv         if (atf_is_error(err))
    479  1.6  jmmv             goto out_p;
    480  1.6  jmmv 
    481  1.6  jmmv         if (strcmp(atf_fs_path_cstring(&bp), ".") != 0) {
    482  1.6  jmmv             atf_fs_path_fini(&bp);
    483  1.6  jmmv             atf_fs_path_fini(&p);
    484  1.6  jmmv 
    485  1.6  jmmv             report_fatal_error("Relative paths are not allowed when searching "
    486  1.6  jmmv                 "for a program (%s)", prog);
    487  1.6  jmmv             UNREACHABLE;
    488  1.6  jmmv         }
    489  1.6  jmmv 
    490  1.6  jmmv         pf.prog = prog;
    491  1.6  jmmv         pf.found = false;
    492  1.6  jmmv         err = atf_text_for_each_word(path, ":", check_prog_in_dir, &pf);
    493  1.6  jmmv         if (atf_is_error(err))
    494  1.6  jmmv             goto out_bp;
    495  1.6  jmmv 
    496  1.6  jmmv         if (!pf.found) {
    497  1.6  jmmv             atf_dynstr_t reason;
    498  1.6  jmmv 
    499  1.6  jmmv             atf_fs_path_fini(&bp);
    500  1.6  jmmv             atf_fs_path_fini(&p);
    501  1.6  jmmv             format_reason_fmt(&reason, NULL, 0, "The required program %s could "
    502  1.6  jmmv                 "not be found in the PATH", prog);
    503  1.6  jmmv             fail_requirement(ctx, &reason);
    504  1.6  jmmv         }
    505  1.6  jmmv 
    506  1.6  jmmv out_bp:
    507  1.6  jmmv         atf_fs_path_fini(&bp);
    508  1.6  jmmv     }
    509  1.6  jmmv 
    510  1.6  jmmv out_p:
    511  1.6  jmmv     atf_fs_path_fini(&p);
    512  1.6  jmmv out:
    513  1.6  jmmv     return err;
    514  1.6  jmmv }
    515  1.1  jmmv 
    516  1.1  jmmv /* ---------------------------------------------------------------------
    517  1.1  jmmv  * The "atf_tc" type.
    518  1.1  jmmv  * --------------------------------------------------------------------- */
    519  1.1  jmmv 
    520  1.7  jmmv struct atf_tc_impl {
    521  1.7  jmmv     const char *m_ident;
    522  1.7  jmmv 
    523  1.7  jmmv     atf_map_t m_vars;
    524  1.7  jmmv     atf_map_t m_config;
    525  1.7  jmmv 
    526  1.7  jmmv     atf_tc_head_t m_head;
    527  1.7  jmmv     atf_tc_body_t m_body;
    528  1.7  jmmv     atf_tc_cleanup_t m_cleanup;
    529  1.7  jmmv };
    530  1.7  jmmv 
    531  1.1  jmmv /*
    532  1.1  jmmv  * Constructors/destructors.
    533  1.1  jmmv  */
    534  1.1  jmmv 
    535  1.1  jmmv atf_error_t
    536  1.1  jmmv atf_tc_init(atf_tc_t *tc, const char *ident, atf_tc_head_t head,
    537  1.1  jmmv             atf_tc_body_t body, atf_tc_cleanup_t cleanup,
    538  1.7  jmmv             const char *const *config)
    539  1.1  jmmv {
    540  1.1  jmmv     atf_error_t err;
    541  1.1  jmmv 
    542  1.7  jmmv     tc->pimpl = malloc(sizeof(struct atf_tc_impl));
    543  1.7  jmmv     if (tc->pimpl == NULL) {
    544  1.7  jmmv         err = atf_no_memory_error();
    545  1.7  jmmv         goto err;
    546  1.7  jmmv     }
    547  1.7  jmmv 
    548  1.7  jmmv     tc->pimpl->m_ident = ident;
    549  1.7  jmmv     tc->pimpl->m_head = head;
    550  1.7  jmmv     tc->pimpl->m_body = body;
    551  1.7  jmmv     tc->pimpl->m_cleanup = cleanup;
    552  1.1  jmmv 
    553  1.7  jmmv     err = atf_map_init_charpp(&tc->pimpl->m_config, config);
    554  1.1  jmmv     if (atf_is_error(err))
    555  1.5  jmmv         goto err;
    556  1.1  jmmv 
    557  1.7  jmmv     err = atf_map_init(&tc->pimpl->m_vars);
    558  1.7  jmmv     if (atf_is_error(err))
    559  1.7  jmmv         goto err_vars;
    560  1.7  jmmv 
    561  1.1  jmmv     err = atf_tc_set_md_var(tc, "ident", ident);
    562  1.1  jmmv     if (atf_is_error(err))
    563  1.1  jmmv         goto err_map;
    564  1.1  jmmv 
    565  1.6  jmmv     if (cleanup != NULL) {
    566  1.6  jmmv         err = atf_tc_set_md_var(tc, "has.cleanup", "true");
    567  1.6  jmmv         if (atf_is_error(err))
    568  1.6  jmmv             goto err_map;
    569  1.6  jmmv     }
    570  1.6  jmmv 
    571  1.1  jmmv     /* XXX Should the head be able to return error codes? */
    572  1.7  jmmv     if (tc->pimpl->m_head != NULL)
    573  1.7  jmmv         tc->pimpl->m_head(tc);
    574  1.1  jmmv 
    575  1.6  jmmv     if (strcmp(atf_tc_get_md_var(tc, "ident"), ident) != 0) {
    576  1.6  jmmv         report_fatal_error("Test case head modified the read-only 'ident' "
    577  1.6  jmmv             "property");
    578  1.6  jmmv         UNREACHABLE;
    579  1.6  jmmv     }
    580  1.1  jmmv 
    581  1.1  jmmv     INV(!atf_is_error(err));
    582  1.1  jmmv     return err;
    583  1.1  jmmv 
    584  1.1  jmmv err_map:
    585  1.7  jmmv     atf_map_fini(&tc->pimpl->m_vars);
    586  1.7  jmmv err_vars:
    587  1.7  jmmv     atf_map_fini(&tc->pimpl->m_config);
    588  1.5  jmmv err:
    589  1.1  jmmv     return err;
    590  1.1  jmmv }
    591  1.1  jmmv 
    592  1.1  jmmv atf_error_t
    593  1.1  jmmv atf_tc_init_pack(atf_tc_t *tc, const atf_tc_pack_t *pack,
    594  1.7  jmmv                  const char *const *config)
    595  1.1  jmmv {
    596  1.1  jmmv     return atf_tc_init(tc, pack->m_ident, pack->m_head, pack->m_body,
    597  1.1  jmmv                        pack->m_cleanup, config);
    598  1.1  jmmv }
    599  1.1  jmmv 
    600  1.1  jmmv void
    601  1.1  jmmv atf_tc_fini(atf_tc_t *tc)
    602  1.1  jmmv {
    603  1.7  jmmv     atf_map_fini(&tc->pimpl->m_vars);
    604  1.7  jmmv     free(tc->pimpl);
    605  1.1  jmmv }
    606  1.1  jmmv 
    607  1.1  jmmv /*
    608  1.1  jmmv  * Getters.
    609  1.1  jmmv  */
    610  1.1  jmmv 
    611  1.1  jmmv const char *
    612  1.1  jmmv atf_tc_get_ident(const atf_tc_t *tc)
    613  1.1  jmmv {
    614  1.7  jmmv     return tc->pimpl->m_ident;
    615  1.1  jmmv }
    616  1.1  jmmv 
    617  1.1  jmmv const char *
    618  1.1  jmmv atf_tc_get_config_var(const atf_tc_t *tc, const char *name)
    619  1.1  jmmv {
    620  1.1  jmmv     const char *val;
    621  1.1  jmmv     atf_map_citer_t iter;
    622  1.1  jmmv 
    623  1.1  jmmv     PRE(atf_tc_has_config_var(tc, name));
    624  1.7  jmmv     iter = atf_map_find_c(&tc->pimpl->m_config, name);
    625  1.1  jmmv     val = atf_map_citer_data(iter);
    626  1.1  jmmv     INV(val != NULL);
    627  1.1  jmmv 
    628  1.1  jmmv     return val;
    629  1.1  jmmv }
    630  1.1  jmmv 
    631  1.1  jmmv const char *
    632  1.1  jmmv atf_tc_get_config_var_wd(const atf_tc_t *tc, const char *name,
    633  1.1  jmmv                          const char *defval)
    634  1.1  jmmv {
    635  1.1  jmmv     const char *val;
    636  1.1  jmmv 
    637  1.1  jmmv     if (!atf_tc_has_config_var(tc, name))
    638  1.1  jmmv         val = defval;
    639  1.1  jmmv     else
    640  1.1  jmmv         val = atf_tc_get_config_var(tc, name);
    641  1.1  jmmv 
    642  1.1  jmmv     return val;
    643  1.1  jmmv }
    644  1.1  jmmv 
    645  1.8  jmmv bool
    646  1.8  jmmv atf_tc_get_config_var_as_bool(const atf_tc_t *tc, const char *name)
    647  1.8  jmmv {
    648  1.8  jmmv     bool val;
    649  1.8  jmmv     const char *strval;
    650  1.8  jmmv     atf_error_t err;
    651  1.8  jmmv 
    652  1.8  jmmv     strval = atf_tc_get_config_var(tc, name);
    653  1.8  jmmv     err = atf_text_to_bool(strval, &val);
    654  1.8  jmmv     if (atf_is_error(err)) {
    655  1.8  jmmv         atf_error_free(err);
    656  1.8  jmmv         atf_tc_fail("Configuration variable %s does not have a valid "
    657  1.8  jmmv                     "boolean value; found %s", name, strval);
    658  1.8  jmmv     }
    659  1.8  jmmv 
    660  1.8  jmmv     return val;
    661  1.8  jmmv }
    662  1.8  jmmv 
    663  1.8  jmmv bool
    664  1.8  jmmv atf_tc_get_config_var_as_bool_wd(const atf_tc_t *tc, const char *name,
    665  1.8  jmmv                                  const bool defval)
    666  1.8  jmmv {
    667  1.8  jmmv     bool val;
    668  1.8  jmmv 
    669  1.8  jmmv     if (!atf_tc_has_config_var(tc, name))
    670  1.8  jmmv         val = defval;
    671  1.8  jmmv     else
    672  1.8  jmmv         val = atf_tc_get_config_var_as_bool(tc, name);
    673  1.8  jmmv 
    674  1.8  jmmv     return val;
    675  1.8  jmmv }
    676  1.8  jmmv 
    677  1.8  jmmv long
    678  1.8  jmmv atf_tc_get_config_var_as_long(const atf_tc_t *tc, const char *name)
    679  1.8  jmmv {
    680  1.8  jmmv     long val;
    681  1.8  jmmv     const char *strval;
    682  1.8  jmmv     atf_error_t err;
    683  1.8  jmmv 
    684  1.8  jmmv     strval = atf_tc_get_config_var(tc, name);
    685  1.8  jmmv     err = atf_text_to_long(strval, &val);
    686  1.8  jmmv     if (atf_is_error(err)) {
    687  1.8  jmmv         atf_error_free(err);
    688  1.8  jmmv         atf_tc_fail("Configuration variable %s does not have a valid "
    689  1.8  jmmv                     "long value; found %s", name, strval);
    690  1.8  jmmv     }
    691  1.8  jmmv 
    692  1.8  jmmv     return val;
    693  1.8  jmmv }
    694  1.8  jmmv 
    695  1.8  jmmv long
    696  1.8  jmmv atf_tc_get_config_var_as_long_wd(const atf_tc_t *tc, const char *name,
    697  1.8  jmmv                                  const long defval)
    698  1.8  jmmv {
    699  1.8  jmmv     long val;
    700  1.8  jmmv 
    701  1.8  jmmv     if (!atf_tc_has_config_var(tc, name))
    702  1.8  jmmv         val = defval;
    703  1.8  jmmv     else
    704  1.8  jmmv         val = atf_tc_get_config_var_as_long(tc, name);
    705  1.8  jmmv 
    706  1.8  jmmv     return val;
    707  1.8  jmmv }
    708  1.8  jmmv 
    709  1.1  jmmv const char *
    710  1.1  jmmv atf_tc_get_md_var(const atf_tc_t *tc, const char *name)
    711  1.1  jmmv {
    712  1.1  jmmv     const char *val;
    713  1.1  jmmv     atf_map_citer_t iter;
    714  1.1  jmmv 
    715  1.1  jmmv     PRE(atf_tc_has_md_var(tc, name));
    716  1.7  jmmv     iter = atf_map_find_c(&tc->pimpl->m_vars, name);
    717  1.1  jmmv     val = atf_map_citer_data(iter);
    718  1.1  jmmv     INV(val != NULL);
    719  1.1  jmmv 
    720  1.1  jmmv     return val;
    721  1.1  jmmv }
    722  1.1  jmmv 
    723  1.7  jmmv char **
    724  1.4  jmmv atf_tc_get_md_vars(const atf_tc_t *tc)
    725  1.4  jmmv {
    726  1.7  jmmv     return atf_map_to_charpp(&tc->pimpl->m_vars);
    727  1.4  jmmv }
    728  1.4  jmmv 
    729  1.1  jmmv bool
    730  1.1  jmmv atf_tc_has_config_var(const atf_tc_t *tc, const char *name)
    731  1.1  jmmv {
    732  1.1  jmmv     atf_map_citer_t end, iter;
    733  1.1  jmmv 
    734  1.7  jmmv     iter = atf_map_find_c(&tc->pimpl->m_config, name);
    735  1.7  jmmv     end = atf_map_end_c(&tc->pimpl->m_config);
    736  1.7  jmmv     return !atf_equal_map_citer_map_citer(iter, end);
    737  1.1  jmmv }
    738  1.1  jmmv 
    739  1.1  jmmv bool
    740  1.1  jmmv atf_tc_has_md_var(const atf_tc_t *tc, const char *name)
    741  1.1  jmmv {
    742  1.1  jmmv     atf_map_citer_t end, iter;
    743  1.1  jmmv 
    744  1.7  jmmv     iter = atf_map_find_c(&tc->pimpl->m_vars, name);
    745  1.7  jmmv     end = atf_map_end_c(&tc->pimpl->m_vars);
    746  1.1  jmmv     return !atf_equal_map_citer_map_citer(iter, end);
    747  1.1  jmmv }
    748  1.1  jmmv 
    749  1.1  jmmv /*
    750  1.1  jmmv  * Modifiers.
    751  1.1  jmmv  */
    752  1.1  jmmv 
    753  1.1  jmmv atf_error_t
    754  1.1  jmmv atf_tc_set_md_var(atf_tc_t *tc, const char *name, const char *fmt, ...)
    755  1.1  jmmv {
    756  1.1  jmmv     atf_error_t err;
    757  1.1  jmmv     char *value;
    758  1.1  jmmv     va_list ap;
    759  1.1  jmmv 
    760  1.1  jmmv     va_start(ap, fmt);
    761  1.1  jmmv     err = atf_text_format_ap(&value, fmt, ap);
    762  1.1  jmmv     va_end(ap);
    763  1.1  jmmv 
    764  1.1  jmmv     if (!atf_is_error(err))
    765  1.7  jmmv         err = atf_map_insert(&tc->pimpl->m_vars, name, value, true);
    766  1.1  jmmv     else
    767  1.1  jmmv         free(value);
    768  1.1  jmmv 
    769  1.1  jmmv     return err;
    770  1.1  jmmv }
    771  1.1  jmmv 
    772  1.1  jmmv /* ---------------------------------------------------------------------
    773  1.6  jmmv  * Free functions, as they should be publicly but they can't.
    774  1.1  jmmv  * --------------------------------------------------------------------- */
    775  1.1  jmmv 
    776  1.6  jmmv static void _atf_tc_fail(struct context *, const char *, va_list)
    777  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
    778  1.6  jmmv static void _atf_tc_fail_nonfatal(struct context *, const char *, va_list);
    779  1.6  jmmv static void _atf_tc_fail_check(struct context *, const char *, const size_t,
    780  1.6  jmmv     const char *, va_list);
    781  1.6  jmmv static void _atf_tc_fail_requirement(struct context *, const char *,
    782  1.6  jmmv     const size_t, const char *, va_list) ATF_DEFS_ATTRIBUTE_NORETURN;
    783  1.6  jmmv static void _atf_tc_pass(struct context *) ATF_DEFS_ATTRIBUTE_NORETURN;
    784  1.6  jmmv static void _atf_tc_require_prog(struct context *, const char *);
    785  1.6  jmmv static void _atf_tc_skip(struct context *, const char *, va_list)
    786  1.6  jmmv     ATF_DEFS_ATTRIBUTE_NORETURN;
    787  1.6  jmmv static void _atf_tc_check_errno(struct context *, const char *, const size_t,
    788  1.6  jmmv     const int, const char *, const bool);
    789  1.6  jmmv static void _atf_tc_require_errno(struct context *, const char *, const size_t,
    790  1.6  jmmv     const int, const char *, const bool);
    791  1.6  jmmv static void _atf_tc_expect_pass(struct context *);
    792  1.6  jmmv static void _atf_tc_expect_fail(struct context *, const char *, va_list);
    793  1.6  jmmv static void _atf_tc_expect_exit(struct context *, const int, const char *,
    794  1.6  jmmv     va_list);
    795  1.6  jmmv static void _atf_tc_expect_signal(struct context *, const int, const char *,
    796  1.6  jmmv     va_list);
    797  1.6  jmmv static void _atf_tc_expect_death(struct context *, const char *,
    798  1.6  jmmv     va_list);
    799  1.6  jmmv 
    800  1.6  jmmv static void
    801  1.6  jmmv _atf_tc_fail(struct context *ctx, const char *fmt, va_list ap)
    802  1.6  jmmv {
    803  1.6  jmmv     va_list ap2;
    804  1.6  jmmv     atf_dynstr_t reason;
    805  1.6  jmmv 
    806  1.6  jmmv     va_copy(ap2, ap);
    807  1.6  jmmv     format_reason_ap(&reason, NULL, 0, fmt, ap2);
    808  1.6  jmmv     va_end(ap2);
    809  1.6  jmmv 
    810  1.6  jmmv     fail_requirement(ctx, &reason);
    811  1.6  jmmv     UNREACHABLE;
    812  1.6  jmmv }
    813  1.6  jmmv 
    814  1.6  jmmv static void
    815  1.6  jmmv _atf_tc_fail_nonfatal(struct context *ctx, const char *fmt, va_list ap)
    816  1.6  jmmv {
    817  1.6  jmmv     va_list ap2;
    818  1.6  jmmv     atf_dynstr_t reason;
    819  1.6  jmmv 
    820  1.6  jmmv     va_copy(ap2, ap);
    821  1.6  jmmv     format_reason_ap(&reason, NULL, 0, fmt, ap2);
    822  1.6  jmmv     va_end(ap2);
    823  1.6  jmmv 
    824  1.6  jmmv     fail_check(ctx, &reason);
    825  1.6  jmmv }
    826  1.1  jmmv 
    827  1.6  jmmv static void
    828  1.6  jmmv _atf_tc_fail_check(struct context *ctx, const char *file, const size_t line,
    829  1.6  jmmv                    const char *fmt, va_list ap)
    830  1.1  jmmv {
    831  1.6  jmmv     va_list ap2;
    832  1.6  jmmv     atf_dynstr_t reason;
    833  1.1  jmmv 
    834  1.6  jmmv     va_copy(ap2, ap);
    835  1.6  jmmv     format_reason_ap(&reason, file, line, fmt, ap2);
    836  1.6  jmmv     va_end(ap2);
    837  1.6  jmmv 
    838  1.6  jmmv     fail_check(ctx, &reason);
    839  1.6  jmmv }
    840  1.1  jmmv 
    841  1.6  jmmv static void
    842  1.6  jmmv _atf_tc_fail_requirement(struct context *ctx, const char *file,
    843  1.6  jmmv                          const size_t line, const char *fmt, va_list ap)
    844  1.6  jmmv {
    845  1.6  jmmv     va_list ap2;
    846  1.6  jmmv     atf_dynstr_t reason;
    847  1.1  jmmv 
    848  1.6  jmmv     va_copy(ap2, ap);
    849  1.6  jmmv     format_reason_ap(&reason, file, line, fmt, ap2);
    850  1.6  jmmv     va_end(ap2);
    851  1.1  jmmv 
    852  1.6  jmmv     fail_requirement(ctx, &reason);
    853  1.6  jmmv     UNREACHABLE;
    854  1.1  jmmv }
    855  1.1  jmmv 
    856  1.6  jmmv static void
    857  1.6  jmmv _atf_tc_pass(struct context *ctx)
    858  1.1  jmmv {
    859  1.6  jmmv     pass(ctx);
    860  1.6  jmmv     UNREACHABLE;
    861  1.1  jmmv }
    862  1.1  jmmv 
    863  1.6  jmmv static void
    864  1.6  jmmv _atf_tc_require_prog(struct context *ctx, const char *prog)
    865  1.6  jmmv {
    866  1.6  jmmv     check_fatal_error(check_prog(ctx, prog, NULL));
    867  1.6  jmmv }
    868  1.1  jmmv 
    869  1.6  jmmv static void
    870  1.6  jmmv _atf_tc_skip(struct context *ctx, const char *fmt, va_list ap)
    871  1.1  jmmv {
    872  1.6  jmmv     atf_dynstr_t reason;
    873  1.6  jmmv     va_list ap2;
    874  1.6  jmmv 
    875  1.6  jmmv     va_copy(ap2, ap);
    876  1.6  jmmv     format_reason_ap(&reason, NULL, 0, fmt, ap2);
    877  1.6  jmmv     va_end(ap2);
    878  1.1  jmmv 
    879  1.6  jmmv     skip(ctx, &reason);
    880  1.6  jmmv }
    881  1.1  jmmv 
    882  1.6  jmmv static void
    883  1.6  jmmv _atf_tc_check_errno(struct context *ctx, const char *file, const size_t line,
    884  1.6  jmmv                     const int exp_errno, const char *expr_str,
    885  1.6  jmmv                     const bool expr_result)
    886  1.6  jmmv {
    887  1.6  jmmv     errno_test(ctx, file, line, exp_errno, expr_str, expr_result, fail_check);
    888  1.6  jmmv }
    889  1.1  jmmv 
    890  1.6  jmmv static void
    891  1.6  jmmv _atf_tc_require_errno(struct context *ctx, const char *file, const size_t line,
    892  1.6  jmmv                       const int exp_errno, const char *expr_str,
    893  1.6  jmmv                       const bool expr_result)
    894  1.6  jmmv {
    895  1.6  jmmv     errno_test(ctx, file, line, exp_errno, expr_str, expr_result,
    896  1.6  jmmv         fail_requirement);
    897  1.6  jmmv }
    898  1.1  jmmv 
    899  1.6  jmmv static void
    900  1.6  jmmv _atf_tc_expect_pass(struct context *ctx)
    901  1.6  jmmv {
    902  1.6  jmmv     validate_expect(ctx);
    903  1.1  jmmv 
    904  1.6  jmmv     ctx->expect = EXPECT_PASS;
    905  1.6  jmmv }
    906  1.1  jmmv 
    907  1.6  jmmv static void
    908  1.6  jmmv _atf_tc_expect_fail(struct context *ctx, const char *reason, va_list ap)
    909  1.6  jmmv {
    910  1.6  jmmv     va_list ap2;
    911  1.1  jmmv 
    912  1.6  jmmv     validate_expect(ctx);
    913  1.1  jmmv 
    914  1.6  jmmv     ctx->expect = EXPECT_FAIL;
    915  1.6  jmmv     atf_dynstr_fini(&ctx->expect_reason);
    916  1.6  jmmv     va_copy(ap2, ap);
    917  1.6  jmmv     check_fatal_error(atf_dynstr_init_ap(&ctx->expect_reason, reason, ap2));
    918  1.6  jmmv     va_end(ap2);
    919  1.6  jmmv     ctx->expect_previous_fail_count = ctx->expect_fail_count;
    920  1.1  jmmv }
    921  1.1  jmmv 
    922  1.6  jmmv static void
    923  1.6  jmmv _atf_tc_expect_exit(struct context *ctx, const int exitcode, const char *reason,
    924  1.6  jmmv                     va_list ap)
    925  1.1  jmmv {
    926  1.6  jmmv     va_list ap2;
    927  1.6  jmmv     atf_dynstr_t formatted;
    928  1.6  jmmv 
    929  1.6  jmmv     validate_expect(ctx);
    930  1.6  jmmv 
    931  1.6  jmmv     ctx->expect = EXPECT_EXIT;
    932  1.6  jmmv     va_copy(ap2, ap);
    933  1.6  jmmv     check_fatal_error(atf_dynstr_init_ap(&formatted, reason, ap2));
    934  1.6  jmmv     va_end(ap2);
    935  1.1  jmmv 
    936  1.6  jmmv     create_resfile(ctx->resfile, "expected_exit", exitcode, &formatted);
    937  1.6  jmmv }
    938  1.1  jmmv 
    939  1.6  jmmv static void
    940  1.6  jmmv _atf_tc_expect_signal(struct context *ctx, const int signo, const char *reason,
    941  1.6  jmmv                       va_list ap)
    942  1.6  jmmv {
    943  1.6  jmmv     va_list ap2;
    944  1.6  jmmv     atf_dynstr_t formatted;
    945  1.1  jmmv 
    946  1.6  jmmv     validate_expect(ctx);
    947  1.1  jmmv 
    948  1.6  jmmv     ctx->expect = EXPECT_SIGNAL;
    949  1.6  jmmv     va_copy(ap2, ap);
    950  1.6  jmmv     check_fatal_error(atf_dynstr_init_ap(&formatted, reason, ap2));
    951  1.6  jmmv     va_end(ap2);
    952  1.1  jmmv 
    953  1.6  jmmv     create_resfile(ctx->resfile, "expected_signal", signo, &formatted);
    954  1.1  jmmv }
    955  1.1  jmmv 
    956  1.6  jmmv static void
    957  1.6  jmmv _atf_tc_expect_death(struct context *ctx, const char *reason, va_list ap)
    958  1.3  jmmv {
    959  1.6  jmmv     va_list ap2;
    960  1.6  jmmv     atf_dynstr_t formatted;
    961  1.6  jmmv 
    962  1.6  jmmv     validate_expect(ctx);
    963  1.6  jmmv 
    964  1.6  jmmv     ctx->expect = EXPECT_DEATH;
    965  1.6  jmmv     va_copy(ap2, ap);
    966  1.6  jmmv     check_fatal_error(atf_dynstr_init_ap(&formatted, reason, ap2));
    967  1.6  jmmv     va_end(ap2);
    968  1.3  jmmv 
    969  1.6  jmmv     create_resfile(ctx->resfile, "expected_death", -1, &formatted);
    970  1.6  jmmv }
    971  1.3  jmmv 
    972  1.6  jmmv static void
    973  1.6  jmmv _atf_tc_expect_timeout(struct context *ctx, const char *reason, va_list ap)
    974  1.6  jmmv {
    975  1.6  jmmv     va_list ap2;
    976  1.6  jmmv     atf_dynstr_t formatted;
    977  1.3  jmmv 
    978  1.6  jmmv     validate_expect(ctx);
    979  1.3  jmmv 
    980  1.6  jmmv     ctx->expect = EXPECT_TIMEOUT;
    981  1.6  jmmv     va_copy(ap2, ap);
    982  1.6  jmmv     check_fatal_error(atf_dynstr_init_ap(&formatted, reason, ap2));
    983  1.6  jmmv     va_end(ap2);
    984  1.3  jmmv 
    985  1.6  jmmv     create_resfile(ctx->resfile, "expected_timeout", -1, &formatted);
    986  1.3  jmmv }
    987  1.3  jmmv 
    988  1.6  jmmv /* ---------------------------------------------------------------------
    989  1.6  jmmv  * Free functions.
    990  1.6  jmmv  * --------------------------------------------------------------------- */
    991  1.6  jmmv 
    992  1.6  jmmv static struct context Current;
    993  1.6  jmmv 
    994  1.6  jmmv atf_error_t
    995  1.7  jmmv atf_tc_run(const atf_tc_t *tc, const char *resfile)
    996  1.3  jmmv {
    997  1.6  jmmv     context_init(&Current, tc, resfile);
    998  1.6  jmmv 
    999  1.7  jmmv     tc->pimpl->m_body(tc);
   1000  1.6  jmmv 
   1001  1.6  jmmv     validate_expect(&Current);
   1002  1.6  jmmv 
   1003  1.6  jmmv     if (Current.fail_count > 0) {
   1004  1.6  jmmv         atf_dynstr_t reason;
   1005  1.3  jmmv 
   1006  1.6  jmmv         format_reason_fmt(&reason, NULL, 0, "%d checks failed; see output for "
   1007  1.6  jmmv             "more details", Current.fail_count);
   1008  1.6  jmmv         fail_requirement(&Current, &reason);
   1009  1.6  jmmv     } else if (Current.expect_fail_count > 0) {
   1010  1.6  jmmv         atf_dynstr_t reason;
   1011  1.6  jmmv 
   1012  1.6  jmmv         format_reason_fmt(&reason, NULL, 0, "%d checks failed as expected; "
   1013  1.6  jmmv             "see output for more details", Current.expect_fail_count);
   1014  1.6  jmmv         expected_failure(&Current, &reason);
   1015  1.6  jmmv     } else {
   1016  1.6  jmmv         pass(&Current);
   1017  1.6  jmmv     }
   1018  1.6  jmmv     UNREACHABLE;
   1019  1.6  jmmv     return atf_no_error();
   1020  1.6  jmmv }
   1021  1.6  jmmv 
   1022  1.6  jmmv atf_error_t
   1023  1.6  jmmv atf_tc_cleanup(const atf_tc_t *tc)
   1024  1.6  jmmv {
   1025  1.7  jmmv     if (tc->pimpl->m_cleanup != NULL)
   1026  1.7  jmmv         tc->pimpl->m_cleanup(tc);
   1027  1.6  jmmv     return atf_no_error(); /* XXX */
   1028  1.1  jmmv }
   1029  1.1  jmmv 
   1030  1.6  jmmv /* ---------------------------------------------------------------------
   1031  1.6  jmmv  * Free functions that depend on Current.
   1032  1.6  jmmv  * --------------------------------------------------------------------- */
   1033  1.6  jmmv 
   1034  1.6  jmmv /*
   1035  1.6  jmmv  * All the functions below provide delegates to other internal functions
   1036  1.6  jmmv  * (prefixed by _) that take the current test case as an argument to
   1037  1.6  jmmv  * prevent them from accessing global state.  This is to keep the side-
   1038  1.6  jmmv  * effects of the internal functions clearer and easier to understand.
   1039  1.6  jmmv  *
   1040  1.6  jmmv  * The public API should never have hid the fact that it needs access to
   1041  1.6  jmmv  * the current test case (other than maybe in the macros), but changing it
   1042  1.6  jmmv  * is hard.  TODO: Revisit in the future.
   1043  1.6  jmmv  */
   1044  1.6  jmmv 
   1045  1.1  jmmv void
   1046  1.1  jmmv atf_tc_fail(const char *fmt, ...)
   1047  1.1  jmmv {
   1048  1.1  jmmv     va_list ap;
   1049  1.1  jmmv 
   1050  1.6  jmmv     PRE(Current.tc != NULL);
   1051  1.1  jmmv 
   1052  1.1  jmmv     va_start(ap, fmt);
   1053  1.6  jmmv     _atf_tc_fail(&Current, fmt, ap);
   1054  1.1  jmmv     va_end(ap);
   1055  1.6  jmmv }
   1056  1.1  jmmv 
   1057  1.6  jmmv void
   1058  1.6  jmmv atf_tc_fail_nonfatal(const char *fmt, ...)
   1059  1.6  jmmv {
   1060  1.6  jmmv     va_list ap;
   1061  1.1  jmmv 
   1062  1.6  jmmv     PRE(Current.tc != NULL);
   1063  1.1  jmmv 
   1064  1.6  jmmv     va_start(ap, fmt);
   1065  1.6  jmmv     _atf_tc_fail_nonfatal(&Current, fmt, ap);
   1066  1.6  jmmv     va_end(ap);
   1067  1.1  jmmv }
   1068  1.1  jmmv 
   1069  1.1  jmmv void
   1070  1.6  jmmv atf_tc_fail_check(const char *file, const size_t line, const char *fmt, ...)
   1071  1.1  jmmv {
   1072  1.1  jmmv     va_list ap;
   1073  1.1  jmmv 
   1074  1.6  jmmv     PRE(Current.tc != NULL);
   1075  1.6  jmmv 
   1076  1.1  jmmv     va_start(ap, fmt);
   1077  1.6  jmmv     _atf_tc_fail_check(&Current, file, line, fmt, ap);
   1078  1.1  jmmv     va_end(ap);
   1079  1.1  jmmv }
   1080  1.1  jmmv 
   1081  1.1  jmmv void
   1082  1.6  jmmv atf_tc_fail_requirement(const char *file, const size_t line,
   1083  1.6  jmmv                         const char *fmt, ...)
   1084  1.1  jmmv {
   1085  1.1  jmmv     va_list ap;
   1086  1.1  jmmv 
   1087  1.6  jmmv     PRE(Current.tc != NULL);
   1088  1.6  jmmv 
   1089  1.1  jmmv     va_start(ap, fmt);
   1090  1.6  jmmv     _atf_tc_fail_requirement(&Current, file, line, fmt, ap);
   1091  1.1  jmmv     va_end(ap);
   1092  1.1  jmmv }
   1093  1.1  jmmv 
   1094  1.1  jmmv void
   1095  1.6  jmmv atf_tc_pass(void)
   1096  1.6  jmmv {
   1097  1.6  jmmv     PRE(Current.tc != NULL);
   1098  1.6  jmmv 
   1099  1.6  jmmv     _atf_tc_pass(&Current);
   1100  1.6  jmmv }
   1101  1.6  jmmv 
   1102  1.6  jmmv void
   1103  1.6  jmmv atf_tc_require_prog(const char *prog)
   1104  1.6  jmmv {
   1105  1.6  jmmv     PRE(Current.tc != NULL);
   1106  1.6  jmmv 
   1107  1.6  jmmv     _atf_tc_require_prog(&Current, prog);
   1108  1.6  jmmv }
   1109  1.6  jmmv 
   1110  1.6  jmmv void
   1111  1.6  jmmv atf_tc_skip(const char *fmt, ...)
   1112  1.1  jmmv {
   1113  1.1  jmmv     va_list ap;
   1114  1.1  jmmv 
   1115  1.6  jmmv     PRE(Current.tc != NULL);
   1116  1.6  jmmv 
   1117  1.1  jmmv     va_start(ap, fmt);
   1118  1.6  jmmv     _atf_tc_skip(&Current, fmt, ap);
   1119  1.1  jmmv     va_end(ap);
   1120  1.6  jmmv }
   1121  1.6  jmmv 
   1122  1.6  jmmv void
   1123  1.6  jmmv atf_tc_check_errno(const char *file, const size_t line, const int exp_errno,
   1124  1.6  jmmv                    const char *expr_str, const bool expr_result)
   1125  1.6  jmmv {
   1126  1.6  jmmv     PRE(Current.tc != NULL);
   1127  1.1  jmmv 
   1128  1.6  jmmv     _atf_tc_check_errno(&Current, file, line, exp_errno, expr_str,
   1129  1.6  jmmv                         expr_result);
   1130  1.1  jmmv }
   1131  1.1  jmmv 
   1132  1.1  jmmv void
   1133  1.6  jmmv atf_tc_require_errno(const char *file, const size_t line, const int exp_errno,
   1134  1.6  jmmv                      const char *expr_str, const bool expr_result)
   1135  1.1  jmmv {
   1136  1.6  jmmv     PRE(Current.tc != NULL);
   1137  1.1  jmmv 
   1138  1.6  jmmv     _atf_tc_require_errno(&Current, file, line, exp_errno, expr_str,
   1139  1.6  jmmv                           expr_result);
   1140  1.6  jmmv }
   1141  1.1  jmmv 
   1142  1.6  jmmv void
   1143  1.6  jmmv atf_tc_expect_pass(void)
   1144  1.6  jmmv {
   1145  1.6  jmmv     PRE(Current.tc != NULL);
   1146  1.1  jmmv 
   1147  1.6  jmmv     _atf_tc_expect_pass(&Current);
   1148  1.1  jmmv }
   1149  1.1  jmmv 
   1150  1.1  jmmv void
   1151  1.6  jmmv atf_tc_expect_fail(const char *reason, ...)
   1152  1.1  jmmv {
   1153  1.6  jmmv     va_list ap;
   1154  1.1  jmmv 
   1155  1.6  jmmv     PRE(Current.tc != NULL);
   1156  1.1  jmmv 
   1157  1.6  jmmv     va_start(ap, reason);
   1158  1.6  jmmv     _atf_tc_expect_fail(&Current, reason, ap);
   1159  1.6  jmmv     va_end(ap);
   1160  1.6  jmmv }
   1161  1.1  jmmv 
   1162  1.6  jmmv void
   1163  1.6  jmmv atf_tc_expect_exit(const int exitcode, const char *reason, ...)
   1164  1.6  jmmv {
   1165  1.6  jmmv     va_list ap;
   1166  1.1  jmmv 
   1167  1.6  jmmv     PRE(Current.tc != NULL);
   1168  1.1  jmmv 
   1169  1.6  jmmv     va_start(ap, reason);
   1170  1.6  jmmv     _atf_tc_expect_exit(&Current, exitcode, reason, ap);
   1171  1.6  jmmv     va_end(ap);
   1172  1.1  jmmv }
   1173  1.1  jmmv 
   1174  1.1  jmmv void
   1175  1.6  jmmv atf_tc_expect_signal(const int signo, const char *reason, ...)
   1176  1.1  jmmv {
   1177  1.6  jmmv     va_list ap;
   1178  1.6  jmmv 
   1179  1.6  jmmv     PRE(Current.tc != NULL);
   1180  1.1  jmmv 
   1181  1.6  jmmv     va_start(ap, reason);
   1182  1.6  jmmv     _atf_tc_expect_signal(&Current, signo, reason, ap);
   1183  1.6  jmmv     va_end(ap);
   1184  1.1  jmmv }
   1185  1.1  jmmv 
   1186  1.1  jmmv void
   1187  1.6  jmmv atf_tc_expect_death(const char *reason, ...)
   1188  1.1  jmmv {
   1189  1.1  jmmv     va_list ap;
   1190  1.1  jmmv 
   1191  1.6  jmmv     PRE(Current.tc != NULL);
   1192  1.1  jmmv 
   1193  1.6  jmmv     va_start(ap, reason);
   1194  1.6  jmmv     _atf_tc_expect_death(&Current, reason, ap);
   1195  1.1  jmmv     va_end(ap);
   1196  1.6  jmmv }
   1197  1.1  jmmv 
   1198  1.6  jmmv void
   1199  1.6  jmmv atf_tc_expect_timeout(const char *reason, ...)
   1200  1.6  jmmv {
   1201  1.6  jmmv     va_list ap;
   1202  1.1  jmmv 
   1203  1.6  jmmv     PRE(Current.tc != NULL);
   1204  1.1  jmmv 
   1205  1.6  jmmv     va_start(ap, reason);
   1206  1.6  jmmv     _atf_tc_expect_timeout(&Current, reason, ap);
   1207  1.6  jmmv     va_end(ap);
   1208  1.1  jmmv }
   1209