2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 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/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 * --------------------------------------------------------------------- */
58 create_tmpdir(atf_fs_path_t
*dir
)
62 err
= atf_fs_path_init_fmt(dir
, "%s/check.XXXXXX",
63 atf_config_get("atf_workdir"));
64 if (atf_is_error(err
))
67 err
= atf_fs_mkdtemp(dir
);
68 if (atf_is_error(err
)) {
69 atf_fs_path_fini(dir
);
73 INV(!atf_is_error(err
));
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
);
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
);
100 INV(!atf_is_error(err
));
104 #if defined(__minix) && !defined(NDEBUG)
106 #endif /* defined(__minix) && !defined(NDEBUG) */
108 INV(!atf_is_error(err
));
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
));
123 init_sb(const atf_fs_path_t
*path
, atf_process_stream_t
*sb
)
128 err
= atf_process_stream_init_inherit(sb
);
130 err
= atf_process_stream_init_redirect_path(sb
, path
);
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
)
142 err
= init_sb(outfile
, outsb
);
143 if (atf_is_error(err
))
146 err
= init_sb(errfile
, errsb
);
147 if (atf_is_error(err
)) {
148 atf_process_stream_fini(outsb
);
157 const char *const *m_argv
;
160 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN
;
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
));
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
)
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
))
187 err
= atf_process_fork(&child
, exec_child
, &outsb
, &errsb
, &ea
);
188 if (atf_is_error(err
))
191 err
= atf_process_child_wait(&child
, status
);
194 atf_process_stream_fini(&errsb
);
195 atf_process_stream_fini(&outsb
);
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
)
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
)) {
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)" : "");
223 fprintf(stderr
, "%s failed due to unknown reason\n", progname
);
231 array_to_list(const char *const *a
, atf_list_t
*l
)
235 err
= atf_list_init(l
);
236 if (atf_is_error(err
))
240 char *item
= strdup(*a
);
242 err
= atf_no_memory_error();
246 err
= atf_list_append(l
, item
, true);
247 if (atf_is_error(err
))
258 print_array(const char *const *array
, const char *pfx
)
260 const char *const *ptr
;
263 for (ptr
= array
; *ptr
!= NULL
; ptr
++)
270 check_build_run(const char *const *argv
, bool *success
)
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
))
281 update_success_from_status(argv
[0], &status
, success
);
282 atf_process_status_fini(&status
);
284 INV(!atf_is_error(err
));
289 /* ---------------------------------------------------------------------
290 * The "atf_check_result" type.
291 * --------------------------------------------------------------------- */
293 struct atf_check_result_impl
{
296 atf_fs_path_t m_stdout
;
297 atf_fs_path_t m_stderr
;
298 atf_process_status_t m_status
;
303 atf_check_result_init(atf_check_result_t
*r
, const char *const *argv
,
304 const atf_fs_path_t
*dir
)
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
))
316 err
= atf_fs_path_copy(&r
->pimpl
->m_dir
, dir
);
317 if (atf_is_error(err
))
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
))
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
))
330 INV(!atf_is_error(err
));
334 atf_fs_path_fini(&r
->pimpl
->m_stdout
);
336 atf_fs_path_fini(&r
->pimpl
->m_dir
);
338 atf_list_fini(&r
->pimpl
->m_argv
);
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
);
360 atf_check_result_stdout(const atf_check_result_t
*r
)
362 return atf_fs_path_cstring(&r
->pimpl
->m_stdout
);
366 atf_check_result_stderr(const atf_check_result_t
*r
)
368 return atf_fs_path_cstring(&r
->pimpl
->m_stderr
);
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
);
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 /* ---------------------------------------------------------------------
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). */
403 atf_check_build_c_o(const char *sfile
,
405 const char *const optargs
[],
411 err
= atf_build_c_o(sfile
, ofile
, optargs
, &argv
);
412 if (atf_is_error(err
))
415 err
= check_build_run((const char *const *)argv
, success
);
417 atf_utils_free_charpp(argv
);
423 atf_check_build_cpp(const char *sfile
,
425 const char *const optargs
[],
431 err
= atf_build_cpp(sfile
, ofile
, optargs
, &argv
);
432 if (atf_is_error(err
))
435 err
= check_build_run((const char *const *)argv
, success
);
437 atf_utils_free_charpp(argv
);
443 atf_check_build_cxx_o(const char *sfile
,
445 const char *const optargs
[],
451 err
= atf_build_cxx_o(sfile
, ofile
, optargs
, &argv
);
452 if (atf_is_error(err
))
455 err
= check_build_run((const char *const *)argv
, success
);
457 atf_utils_free_charpp(argv
);
463 atf_check_exec_array(const char *const *argv
, atf_check_result_t
*r
)
468 err
= create_tmpdir(&dir
);
469 if (atf_is_error(err
))
472 err
= atf_check_result_init(r
, argv
, &dir
);
473 if (atf_is_error(err
)) {
474 #if defined(__minix) && !defined(NDEBUG)
476 #endif /* defined(__minix) && !defined(NDEBUG) */
478 INV(!atf_is_error(err2
));
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
);
489 INV(!atf_is_error(err
));
491 atf_fs_path_fini(&dir
);