No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / check.c
blobeddbf35032c964415d435c88014320bf56218cce
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/wait.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "atf-c/build.h"
40 #include "atf-c/check.h"
41 #include "atf-c/config.h"
42 #include "atf-c/defs.h"
43 #include "atf-c/dynstr.h"
44 #include "atf-c/error.h"
45 #include "atf-c/fs.h"
46 #include "atf-c/list.h"
47 #include "atf-c/process.h"
48 #include "atf-c/sanity.h"
50 /* ---------------------------------------------------------------------
51 * Auxiliary functions.
52 * --------------------------------------------------------------------- */
54 static
55 atf_error_t
56 create_tmpdir(atf_fs_path_t *dir)
58 atf_error_t err;
60 err = atf_fs_path_init_fmt(dir, "%s/check.XXXXXX",
61 atf_config_get("atf_workdir"));
62 if (atf_is_error(err))
63 goto out;
65 err = atf_fs_mkdtemp(dir);
66 if (atf_is_error(err)) {
67 atf_fs_path_fini(dir);
68 goto out;
71 INV(!atf_is_error(err));
72 out:
73 return err;
76 static
77 void
78 cleanup_tmpdir(const atf_fs_path_t *dir, const atf_fs_path_t *outfile,
79 const atf_fs_path_t *errfile)
82 atf_error_t err = atf_fs_unlink(outfile);
83 if (atf_is_error(err)) {
84 INV(atf_error_is(err, "libc") &&
85 atf_libc_error_code(err) == ENOENT);
86 atf_error_free(err);
87 } else
88 INV(!atf_is_error(err));
92 atf_error_t err = atf_fs_unlink(errfile);
93 if (atf_is_error(err)) {
94 INV(atf_error_is(err, "libc") &&
95 atf_libc_error_code(err) == ENOENT);
96 atf_error_free(err);
97 } else
98 INV(!atf_is_error(err));
102 atf_error_t err = atf_fs_rmdir(dir);
103 INV(!atf_is_error(err));
107 static
109 const_execvp(const char *file, const char *const *argv)
111 #define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
112 return execvp(file, UNCONST(argv));
113 #undef UNCONST
116 static
117 atf_error_t
118 init_sb(const atf_fs_path_t *path, atf_process_stream_t *sb)
120 atf_error_t err;
122 if (path == NULL)
123 err = atf_process_stream_init_inherit(sb);
124 else
125 err = atf_process_stream_init_redirect_path(sb, path);
127 return err;
130 static
131 atf_error_t
132 init_sbs(const atf_fs_path_t *outfile, atf_process_stream_t *outsb,
133 const atf_fs_path_t *errfile, atf_process_stream_t *errsb)
135 atf_error_t err;
137 err = init_sb(outfile, outsb);
138 if (atf_is_error(err))
139 goto out;
141 err = init_sb(errfile, errsb);
142 if (atf_is_error(err)) {
143 atf_process_stream_fini(outsb);
144 goto out;
147 out:
148 return err;
151 struct exec_data {
152 const char *const *m_argv;
155 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
157 static
158 void
159 exec_child(void *v)
161 struct exec_data *ea = v;
163 atf_reset_exit_checks();
164 const_execvp(ea->m_argv[0], ea->m_argv);
165 fprintf(stderr, "execvp(%s) failed: %s\n", ea->m_argv[0], strerror(errno));
166 exit(127);
169 static
170 atf_error_t
171 fork_and_wait(const char *const *argv, const atf_fs_path_t *outfile,
172 const atf_fs_path_t *errfile, atf_process_status_t *status)
174 atf_error_t err;
175 atf_process_child_t child;
176 atf_process_stream_t outsb, errsb;
177 struct exec_data ea = { argv };
179 err = init_sbs(outfile, &outsb, errfile, &errsb);
180 if (atf_is_error(err))
181 goto out;
183 err = atf_process_fork(&child, exec_child, &outsb, &errsb, &ea);
184 if (atf_is_error(err))
185 goto out_sbs;
187 err = atf_process_child_wait(&child, status);
189 out_sbs:
190 atf_process_stream_fini(&errsb);
191 atf_process_stream_fini(&outsb);
192 out:
193 return err;
196 static
197 void
198 update_success_from_status(const char *progname,
199 const atf_process_status_t *status, bool *success)
201 bool s = atf_process_status_exited(status) &&
202 atf_process_status_exitstatus(status) == EXIT_SUCCESS;
204 if (atf_process_status_exited(status)) {
205 if (atf_process_status_exitstatus(status) == EXIT_SUCCESS)
206 INV(s);
207 else {
208 INV(!s);
209 fprintf(stderr, "%s failed with exit code %d\n", progname,
210 atf_process_status_exitstatus(status));
212 } else if (atf_process_status_signaled(status)) {
213 INV(!s);
214 fprintf(stderr, "%s failed due to signal %d%s\n", progname,
215 atf_process_status_termsig(status),
216 atf_process_status_coredump(status) ? " (core dumped)" : "");
217 } else {
218 INV(!s);
219 fprintf(stderr, "%s failed due to unknown reason\n", progname);
222 *success = s;
225 static
226 atf_error_t
227 array_to_list(const char *const *a, atf_list_t *l)
229 atf_error_t err;
231 err = atf_list_init(l);
232 if (atf_is_error(err))
233 goto out;
235 while (*a != NULL) {
236 char *item = strdup(*a);
237 if (item == NULL) {
238 err = atf_no_memory_error();
239 goto out;
242 err = atf_list_append(l, item, true);
243 if (atf_is_error(err))
244 goto out;
246 a++;
249 out:
250 return err;
253 static
254 atf_error_t
255 list_to_array(const atf_list_t *l, const char ***ap)
257 atf_error_t err;
258 const char **a;
260 a = (const char **)malloc((atf_list_size(l) + 1) * sizeof(const char *));
261 if (a == NULL)
262 err = atf_no_memory_error();
263 else {
264 const char **aiter;
265 atf_list_citer_t liter;
267 aiter = a;
268 atf_list_for_each_c(liter, l) {
269 *aiter = (const char *)atf_list_citer_data(liter);
270 aiter++;
272 *aiter = NULL;
274 err = atf_no_error();
275 *ap = a;
278 return err;
281 static
282 void
283 print_list(const atf_list_t *l, const char *pfx)
285 atf_list_citer_t iter;
287 printf("%s", pfx);
288 atf_list_for_each_c(iter, l)
289 printf(" %s", (const char *)atf_list_citer_data(iter));
290 printf("\n");
293 static
294 atf_error_t
295 fork_and_wait_list(const atf_list_t *argvl, const atf_fs_path_t *outfile,
296 const atf_fs_path_t *errfile, atf_process_status_t *status)
298 atf_error_t err;
299 const char **argva;
301 err = list_to_array(argvl, &argva);
302 if (atf_is_error(err))
303 goto out;
305 err = fork_and_wait(argva, outfile, errfile, status);
307 free(argva);
308 out:
309 return err;
312 static
313 atf_error_t
314 check_build_run(const atf_list_t *argv, bool *success)
316 atf_error_t err;
317 atf_process_status_t status;
319 print_list(argv, ">");
321 err = fork_and_wait_list(argv, NULL, NULL, &status);
322 if (atf_is_error(err))
323 goto out;
325 update_success_from_status((const char *)atf_list_index_c(argv, 0),
326 &status, success);
327 atf_process_status_fini(&status);
329 INV(!atf_is_error(err));
330 out:
331 return err;
334 /* ---------------------------------------------------------------------
335 * The "atf_check_result" type.
336 * --------------------------------------------------------------------- */
338 static
339 atf_error_t
340 atf_check_result_init(atf_check_result_t *r, const char *const *argv,
341 const atf_fs_path_t *dir)
343 atf_error_t err;
344 const char *workdir;
346 atf_object_init(&r->m_object);
348 workdir = atf_config_get("atf_workdir");
350 err = array_to_list(argv, &r->m_argv);
351 if (atf_is_error(err))
352 goto out;
354 err = atf_fs_path_copy(&r->m_dir, dir);
355 if (atf_is_error(err))
356 goto err_argv;
358 err = atf_fs_path_init_fmt(&r->m_stdout, "%s/stdout",
359 atf_fs_path_cstring(dir));
360 if (atf_is_error(err))
361 goto err_dir;
363 err = atf_fs_path_init_fmt(&r->m_stderr, "%s/stderr",
364 atf_fs_path_cstring(dir));
365 if (atf_is_error(err))
366 goto err_stdout;
368 INV(!atf_is_error(err));
369 goto out;
371 err_stdout:
372 atf_fs_path_fini(&r->m_stdout);
373 err_dir:
374 atf_fs_path_fini(&r->m_dir);
375 err_argv:
376 atf_list_fini(&r->m_argv);
377 out:
378 return err;
381 void
382 atf_check_result_fini(atf_check_result_t *r)
384 atf_process_status_fini(&r->m_status);
386 cleanup_tmpdir(&r->m_dir, &r->m_stdout, &r->m_stderr);
387 atf_fs_path_fini(&r->m_stdout);
388 atf_fs_path_fini(&r->m_stderr);
389 atf_fs_path_fini(&r->m_dir);
391 atf_list_fini(&r->m_argv);
393 atf_object_fini(&r->m_object);
396 const atf_list_t *
397 atf_check_result_argv(const atf_check_result_t *r)
399 return &r->m_argv;
402 const atf_fs_path_t *
403 atf_check_result_stdout(const atf_check_result_t *r)
405 return &r->m_stdout;
408 const atf_fs_path_t *
409 atf_check_result_stderr(const atf_check_result_t *r)
411 return &r->m_stderr;
414 bool
415 atf_check_result_exited(const atf_check_result_t *r)
417 return atf_process_status_exited(&r->m_status);
421 atf_check_result_exitcode(const atf_check_result_t *r)
423 return atf_process_status_exitstatus(&r->m_status);
426 /* ---------------------------------------------------------------------
427 * Free functions.
428 * --------------------------------------------------------------------- */
430 /* XXX: This function shouldn't be in this module. It messes with stdout
431 * and stderr, and it provides a very high-end interface. This belongs,
432 * probably, somewhere related to test cases (such as in the tc module). */
433 atf_error_t
434 atf_check_build_c_o(const char *sfile,
435 const char *ofile,
436 const char *const optargs[],
437 bool *success)
439 atf_error_t err;
440 atf_list_t argv;
442 err = atf_build_c_o(sfile, ofile, optargs, &argv);
443 if (atf_is_error(err))
444 goto out;
446 err = check_build_run(&argv, success);
448 atf_list_fini(&argv);
449 out:
450 return err;
453 atf_error_t
454 atf_check_build_cpp(const char *sfile,
455 const char *ofile,
456 const char *const optargs[],
457 bool *success)
459 atf_error_t err;
460 atf_list_t argv;
462 err = atf_build_cpp(sfile, ofile, optargs, &argv);
463 if (atf_is_error(err))
464 goto out;
466 err = check_build_run(&argv, success);
468 atf_list_fini(&argv);
469 out:
470 return err;
473 atf_error_t
474 atf_check_build_cxx_o(const char *sfile,
475 const char *ofile,
476 const char *const optargs[],
477 bool *success)
479 atf_error_t err;
480 atf_list_t argv;
482 err = atf_build_cxx_o(sfile, ofile, optargs, &argv);
483 if (atf_is_error(err))
484 goto out;
486 err = check_build_run(&argv, success);
488 atf_list_fini(&argv);
489 out:
490 return err;
493 atf_error_t
494 atf_check_exec_array(const char *const *argv, atf_check_result_t *r)
496 atf_error_t err;
497 atf_fs_path_t dir;
499 err = create_tmpdir(&dir);
500 if (atf_is_error(err))
501 goto out;
503 err = atf_check_result_init(r, argv, &dir);
504 if (atf_is_error(err))
505 goto err_dir;
507 err = fork_and_wait(argv, &r->m_stdout, &r->m_stderr, &r->m_status);
508 if (atf_is_error(err))
509 goto err_r;
511 INV(!atf_is_error(err));
512 goto out_dir;
514 err_r:
515 atf_check_result_fini(r);
516 err_dir:
518 atf_error_t err2 = atf_fs_rmdir(&dir);
519 INV(!atf_is_error(err2));
521 out_dir:
522 atf_fs_path_fini(&dir);
523 out:
524 return err;
527 atf_error_t
528 atf_check_exec_list(const atf_list_t *argv, atf_check_result_t *r)
530 atf_error_t err;
531 const char **argv2;
533 argv2 = NULL; /* Silence GCC warning. */
534 err = list_to_array(argv, &argv2);
535 if (atf_is_error(err))
536 goto out;
538 err = atf_check_exec_array(argv2, r);
540 free(argv2);
541 out:
542 return err;