tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / atf / dist / atf-c / check.c
blob5a012e1859e76736a634107bb4d933bc4f5734b9
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 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/error.h"
44 #include "atf-c/utils.h"
46 #include "detail/dynstr.h"
47 #include "detail/fs.h"
48 #include "detail/list.h"
49 #include "detail/process.h"
50 #include "detail/sanity.h"
52 /* ---------------------------------------------------------------------
53 * Auxiliary functions.
54 * --------------------------------------------------------------------- */
56 static
57 atf_error_t
58 create_tmpdir(atf_fs_path_t *dir)
60 atf_error_t err;
62 err = atf_fs_path_init_fmt(dir, "%s/check.XXXXXX",
63 atf_config_get("atf_workdir"));
64 if (atf_is_error(err))
65 goto out;
67 err = atf_fs_mkdtemp(dir);
68 if (atf_is_error(err)) {
69 atf_fs_path_fini(dir);
70 goto out;
73 INV(!atf_is_error(err));
74 out:
75 return err;
78 static
79 void
80 cleanup_tmpdir(const atf_fs_path_t *dir, const atf_fs_path_t *outfile,
81 const atf_fs_path_t *errfile)
84 atf_error_t err = atf_fs_unlink(outfile);
85 if (atf_is_error(err)) {
86 INV(atf_error_is(err, "libc") &&
87 atf_libc_error_code(err) == ENOENT);
88 atf_error_free(err);
89 } else
90 INV(!atf_is_error(err));
94 atf_error_t err = atf_fs_unlink(errfile);
95 if (atf_is_error(err)) {
96 INV(atf_error_is(err, "libc") &&
97 atf_libc_error_code(err) == ENOENT);
98 atf_error_free(err);
99 } else
100 INV(!atf_is_error(err));
104 #if defined(__minix) && !defined(NDEBUG)
105 atf_error_t err =
106 #endif /* defined(__minix) && !defined(NDEBUG) */
107 atf_fs_rmdir(dir);
108 INV(!atf_is_error(err));
112 static
114 const_execvp(const char *file, const char *const *argv)
116 #define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
117 return execvp(file, UNCONST(argv));
118 #undef UNCONST
121 static
122 atf_error_t
123 init_sb(const atf_fs_path_t *path, atf_process_stream_t *sb)
125 atf_error_t err;
127 if (path == NULL)
128 err = atf_process_stream_init_inherit(sb);
129 else
130 err = atf_process_stream_init_redirect_path(sb, path);
132 return err;
135 static
136 atf_error_t
137 init_sbs(const atf_fs_path_t *outfile, atf_process_stream_t *outsb,
138 const atf_fs_path_t *errfile, atf_process_stream_t *errsb)
140 atf_error_t err;
142 err = init_sb(outfile, outsb);
143 if (atf_is_error(err))
144 goto out;
146 err = init_sb(errfile, errsb);
147 if (atf_is_error(err)) {
148 atf_process_stream_fini(outsb);
149 goto out;
152 out:
153 return err;
156 struct exec_data {
157 const char *const *m_argv;
160 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
162 static
163 void
164 exec_child(void *v)
166 struct exec_data *ea = v;
168 const_execvp(ea->m_argv[0], ea->m_argv);
169 fprintf(stderr, "execvp(%s) failed: %s\n", ea->m_argv[0], strerror(errno));
170 exit(127);
173 static
174 atf_error_t
175 fork_and_wait(const char *const *argv, const atf_fs_path_t *outfile,
176 const atf_fs_path_t *errfile, atf_process_status_t *status)
178 atf_error_t err;
179 atf_process_child_t child;
180 atf_process_stream_t outsb, errsb;
181 struct exec_data ea = { argv };
183 err = init_sbs(outfile, &outsb, errfile, &errsb);
184 if (atf_is_error(err))
185 goto out;
187 err = atf_process_fork(&child, exec_child, &outsb, &errsb, &ea);
188 if (atf_is_error(err))
189 goto out_sbs;
191 err = atf_process_child_wait(&child, status);
193 out_sbs:
194 atf_process_stream_fini(&errsb);
195 atf_process_stream_fini(&outsb);
196 out:
197 return err;
200 static
201 void
202 update_success_from_status(const char *progname,
203 const atf_process_status_t *status, bool *success)
205 bool s = atf_process_status_exited(status) &&
206 atf_process_status_exitstatus(status) == EXIT_SUCCESS;
208 if (atf_process_status_exited(status)) {
209 if (atf_process_status_exitstatus(status) == EXIT_SUCCESS)
210 INV(s);
211 else {
212 INV(!s);
213 fprintf(stderr, "%s failed with exit code %d\n", progname,
214 atf_process_status_exitstatus(status));
216 } else if (atf_process_status_signaled(status)) {
217 INV(!s);
218 fprintf(stderr, "%s failed due to signal %d%s\n", progname,
219 atf_process_status_termsig(status),
220 atf_process_status_coredump(status) ? " (core dumped)" : "");
221 } else {
222 INV(!s);
223 fprintf(stderr, "%s failed due to unknown reason\n", progname);
226 *success = s;
229 static
230 atf_error_t
231 array_to_list(const char *const *a, atf_list_t *l)
233 atf_error_t err;
235 err = atf_list_init(l);
236 if (atf_is_error(err))
237 goto out;
239 while (*a != NULL) {
240 char *item = strdup(*a);
241 if (item == NULL) {
242 err = atf_no_memory_error();
243 goto out;
246 err = atf_list_append(l, item, true);
247 if (atf_is_error(err))
248 goto out;
250 a++;
253 out:
254 return err;
257 static void
258 print_array(const char *const *array, const char *pfx)
260 const char *const *ptr;
262 printf("%s", pfx);
263 for (ptr = array; *ptr != NULL; ptr++)
264 printf(" %s", *ptr);
265 printf("\n");
268 static
269 atf_error_t
270 check_build_run(const char *const *argv, bool *success)
272 atf_error_t err;
273 atf_process_status_t status;
275 print_array(argv, ">");
277 err = fork_and_wait(argv, NULL, NULL, &status);
278 if (atf_is_error(err))
279 goto out;
281 update_success_from_status(argv[0], &status, success);
282 atf_process_status_fini(&status);
284 INV(!atf_is_error(err));
285 out:
286 return err;
289 /* ---------------------------------------------------------------------
290 * The "atf_check_result" type.
291 * --------------------------------------------------------------------- */
293 struct atf_check_result_impl {
294 atf_list_t m_argv;
295 atf_fs_path_t m_dir;
296 atf_fs_path_t m_stdout;
297 atf_fs_path_t m_stderr;
298 atf_process_status_t m_status;
301 static
302 atf_error_t
303 atf_check_result_init(atf_check_result_t *r, const char *const *argv,
304 const atf_fs_path_t *dir)
306 atf_error_t err;
308 r->pimpl = malloc(sizeof(struct atf_check_result_impl));
309 if (r->pimpl == NULL)
310 return atf_no_memory_error();
312 err = array_to_list(argv, &r->pimpl->m_argv);
313 if (atf_is_error(err))
314 goto out;
316 err = atf_fs_path_copy(&r->pimpl->m_dir, dir);
317 if (atf_is_error(err))
318 goto err_argv;
320 err = atf_fs_path_init_fmt(&r->pimpl->m_stdout, "%s/stdout",
321 atf_fs_path_cstring(dir));
322 if (atf_is_error(err))
323 goto err_dir;
325 err = atf_fs_path_init_fmt(&r->pimpl->m_stderr, "%s/stderr",
326 atf_fs_path_cstring(dir));
327 if (atf_is_error(err))
328 goto err_stdout;
330 INV(!atf_is_error(err));
331 goto out;
333 err_stdout:
334 atf_fs_path_fini(&r->pimpl->m_stdout);
335 err_dir:
336 atf_fs_path_fini(&r->pimpl->m_dir);
337 err_argv:
338 atf_list_fini(&r->pimpl->m_argv);
339 out:
340 return err;
343 void
344 atf_check_result_fini(atf_check_result_t *r)
346 atf_process_status_fini(&r->pimpl->m_status);
348 cleanup_tmpdir(&r->pimpl->m_dir, &r->pimpl->m_stdout,
349 &r->pimpl->m_stderr);
350 atf_fs_path_fini(&r->pimpl->m_stdout);
351 atf_fs_path_fini(&r->pimpl->m_stderr);
352 atf_fs_path_fini(&r->pimpl->m_dir);
354 atf_list_fini(&r->pimpl->m_argv);
356 free(r->pimpl);
359 const char *
360 atf_check_result_stdout(const atf_check_result_t *r)
362 return atf_fs_path_cstring(&r->pimpl->m_stdout);
365 const char *
366 atf_check_result_stderr(const atf_check_result_t *r)
368 return atf_fs_path_cstring(&r->pimpl->m_stderr);
371 bool
372 atf_check_result_exited(const atf_check_result_t *r)
374 return atf_process_status_exited(&r->pimpl->m_status);
378 atf_check_result_exitcode(const atf_check_result_t *r)
380 return atf_process_status_exitstatus(&r->pimpl->m_status);
383 bool
384 atf_check_result_signaled(const atf_check_result_t *r)
386 return atf_process_status_signaled(&r->pimpl->m_status);
390 atf_check_result_termsig(const atf_check_result_t *r)
392 return atf_process_status_termsig(&r->pimpl->m_status);
395 /* ---------------------------------------------------------------------
396 * Free functions.
397 * --------------------------------------------------------------------- */
399 /* XXX: This function shouldn't be in this module. It messes with stdout
400 * and stderr, and it provides a very high-end interface. This belongs,
401 * probably, somewhere related to test cases (such as in the tc module). */
402 atf_error_t
403 atf_check_build_c_o(const char *sfile,
404 const char *ofile,
405 const char *const optargs[],
406 bool *success)
408 atf_error_t err;
409 char **argv;
411 err = atf_build_c_o(sfile, ofile, optargs, &argv);
412 if (atf_is_error(err))
413 goto out;
415 err = check_build_run((const char *const *)argv, success);
417 atf_utils_free_charpp(argv);
418 out:
419 return err;
422 atf_error_t
423 atf_check_build_cpp(const char *sfile,
424 const char *ofile,
425 const char *const optargs[],
426 bool *success)
428 atf_error_t err;
429 char **argv;
431 err = atf_build_cpp(sfile, ofile, optargs, &argv);
432 if (atf_is_error(err))
433 goto out;
435 err = check_build_run((const char *const *)argv, success);
437 atf_utils_free_charpp(argv);
438 out:
439 return err;
442 atf_error_t
443 atf_check_build_cxx_o(const char *sfile,
444 const char *ofile,
445 const char *const optargs[],
446 bool *success)
448 atf_error_t err;
449 char **argv;
451 err = atf_build_cxx_o(sfile, ofile, optargs, &argv);
452 if (atf_is_error(err))
453 goto out;
455 err = check_build_run((const char *const *)argv, success);
457 atf_utils_free_charpp(argv);
458 out:
459 return err;
462 atf_error_t
463 atf_check_exec_array(const char *const *argv, atf_check_result_t *r)
465 atf_error_t err;
466 atf_fs_path_t dir;
468 err = create_tmpdir(&dir);
469 if (atf_is_error(err))
470 goto out;
472 err = atf_check_result_init(r, argv, &dir);
473 if (atf_is_error(err)) {
474 #if defined(__minix) && !defined(NDEBUG)
475 atf_error_t err2 =
476 #endif /* defined(__minix) && !defined(NDEBUG) */
477 atf_fs_rmdir(&dir);
478 INV(!atf_is_error(err2));
479 goto out;
482 err = fork_and_wait(argv, &r->pimpl->m_stdout, &r->pimpl->m_stderr,
483 &r->pimpl->m_status);
484 if (atf_is_error(err)) {
485 atf_check_result_fini(r);
486 goto out;
489 INV(!atf_is_error(err));
491 atf_fs_path_fini(&dir);
492 out:
493 return err;