2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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"
46 #include "atf-c/list.h"
47 #include "atf-c/process.h"
48 #include "atf-c/sanity.h"
50 /* ---------------------------------------------------------------------
51 * Auxiliary functions.
52 * --------------------------------------------------------------------- */
56 create_tmpdir(atf_fs_path_t
*dir
)
60 err
= atf_fs_path_init_fmt(dir
, "%s/check.XXXXXX",
61 atf_config_get("atf_workdir"));
62 if (atf_is_error(err
))
65 err
= atf_fs_mkdtemp(dir
);
66 if (atf_is_error(err
)) {
67 atf_fs_path_fini(dir
);
71 INV(!atf_is_error(err
));
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
);
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
);
98 INV(!atf_is_error(err
));
102 atf_error_t err
= atf_fs_rmdir(dir
);
103 INV(!atf_is_error(err
));
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
));
118 init_sb(const atf_fs_path_t
*path
, atf_process_stream_t
*sb
)
123 err
= atf_process_stream_init_inherit(sb
);
125 err
= atf_process_stream_init_redirect_path(sb
, path
);
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
)
137 err
= init_sb(outfile
, outsb
);
138 if (atf_is_error(err
))
141 err
= init_sb(errfile
, errsb
);
142 if (atf_is_error(err
)) {
143 atf_process_stream_fini(outsb
);
152 const char *const *m_argv
;
155 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN
;
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
));
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
)
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
))
183 err
= atf_process_fork(&child
, exec_child
, &outsb
, &errsb
, &ea
);
184 if (atf_is_error(err
))
187 err
= atf_process_child_wait(&child
, status
);
190 atf_process_stream_fini(&errsb
);
191 atf_process_stream_fini(&outsb
);
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
)
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
)) {
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)" : "");
219 fprintf(stderr
, "%s failed due to unknown reason\n", progname
);
227 array_to_list(const char *const *a
, atf_list_t
*l
)
231 err
= atf_list_init(l
);
232 if (atf_is_error(err
))
236 char *item
= strdup(*a
);
238 err
= atf_no_memory_error();
242 err
= atf_list_append(l
, item
, true);
243 if (atf_is_error(err
))
255 list_to_array(const atf_list_t
*l
, const char ***ap
)
260 a
= (const char **)malloc((atf_list_size(l
) + 1) * sizeof(const char *));
262 err
= atf_no_memory_error();
265 atf_list_citer_t liter
;
268 atf_list_for_each_c(liter
, l
) {
269 *aiter
= (const char *)atf_list_citer_data(liter
);
274 err
= atf_no_error();
283 print_list(const atf_list_t
*l
, const char *pfx
)
285 atf_list_citer_t iter
;
288 atf_list_for_each_c(iter
, l
)
289 printf(" %s", (const char *)atf_list_citer_data(iter
));
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
)
301 err
= list_to_array(argvl
, &argva
);
302 if (atf_is_error(err
))
305 err
= fork_and_wait(argva
, outfile
, errfile
, status
);
314 check_build_run(const atf_list_t
*argv
, bool *success
)
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
))
325 update_success_from_status((const char *)atf_list_index_c(argv
, 0),
327 atf_process_status_fini(&status
);
329 INV(!atf_is_error(err
));
334 /* ---------------------------------------------------------------------
335 * The "atf_check_result" type.
336 * --------------------------------------------------------------------- */
340 atf_check_result_init(atf_check_result_t
*r
, const char *const *argv
,
341 const atf_fs_path_t
*dir
)
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
))
354 err
= atf_fs_path_copy(&r
->m_dir
, dir
);
355 if (atf_is_error(err
))
358 err
= atf_fs_path_init_fmt(&r
->m_stdout
, "%s/stdout",
359 atf_fs_path_cstring(dir
));
360 if (atf_is_error(err
))
363 err
= atf_fs_path_init_fmt(&r
->m_stderr
, "%s/stderr",
364 atf_fs_path_cstring(dir
));
365 if (atf_is_error(err
))
368 INV(!atf_is_error(err
));
372 atf_fs_path_fini(&r
->m_stdout
);
374 atf_fs_path_fini(&r
->m_dir
);
376 atf_list_fini(&r
->m_argv
);
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
);
397 atf_check_result_argv(const atf_check_result_t
*r
)
402 const atf_fs_path_t
*
403 atf_check_result_stdout(const atf_check_result_t
*r
)
408 const atf_fs_path_t
*
409 atf_check_result_stderr(const atf_check_result_t
*r
)
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 /* ---------------------------------------------------------------------
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). */
434 atf_check_build_c_o(const char *sfile
,
436 const char *const optargs
[],
442 err
= atf_build_c_o(sfile
, ofile
, optargs
, &argv
);
443 if (atf_is_error(err
))
446 err
= check_build_run(&argv
, success
);
448 atf_list_fini(&argv
);
454 atf_check_build_cpp(const char *sfile
,
456 const char *const optargs
[],
462 err
= atf_build_cpp(sfile
, ofile
, optargs
, &argv
);
463 if (atf_is_error(err
))
466 err
= check_build_run(&argv
, success
);
468 atf_list_fini(&argv
);
474 atf_check_build_cxx_o(const char *sfile
,
476 const char *const optargs
[],
482 err
= atf_build_cxx_o(sfile
, ofile
, optargs
, &argv
);
483 if (atf_is_error(err
))
486 err
= check_build_run(&argv
, success
);
488 atf_list_fini(&argv
);
494 atf_check_exec_array(const char *const *argv
, atf_check_result_t
*r
)
499 err
= create_tmpdir(&dir
);
500 if (atf_is_error(err
))
503 err
= atf_check_result_init(r
, argv
, &dir
);
504 if (atf_is_error(err
))
507 err
= fork_and_wait(argv
, &r
->m_stdout
, &r
->m_stderr
, &r
->m_status
);
508 if (atf_is_error(err
))
511 INV(!atf_is_error(err
));
515 atf_check_result_fini(r
);
518 atf_error_t err2
= atf_fs_rmdir(&dir
);
519 INV(!atf_is_error(err2
));
522 atf_fs_path_fini(&dir
);
528 atf_check_exec_list(const atf_list_t
*argv
, atf_check_result_t
*r
)
533 argv2
= NULL
; /* Silence GCC warning. */
534 err
= list_to_array(argv
, &argv2
);
535 if (atf_is_error(err
))
538 err
= atf_check_exec_array(argv2
, r
);