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