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