1 /* $NetBSD: check.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "atf-c/build.h"
42 #include "atf-c/check.h"
43 #include "atf-c/config.h"
44 #include "atf-c/defs.h"
45 #include "atf-c/error.h"
46 #include "atf-c/utils.h"
48 #include "detail/dynstr.h"
49 #include "detail/fs.h"
50 #include "detail/list.h"
51 #include "detail/process.h"
52 #include "detail/sanity.h"
54 /* ---------------------------------------------------------------------
55 * Auxiliary functions.
56 * --------------------------------------------------------------------- */
60 create_tmpdir(atf_fs_path_t
*dir
)
64 err
= atf_fs_path_init_fmt(dir
, "%s/check.XXXXXX",
65 atf_config_get("atf_workdir"));
66 if (atf_is_error(err
))
69 err
= atf_fs_mkdtemp(dir
);
70 if (atf_is_error(err
)) {
71 atf_fs_path_fini(dir
);
75 INV(!atf_is_error(err
));
82 cleanup_tmpdir(const atf_fs_path_t
*dir
, const atf_fs_path_t
*outfile
,
83 const atf_fs_path_t
*errfile
)
86 atf_error_t err
= atf_fs_unlink(outfile
);
87 if (atf_is_error(err
)) {
88 INV(atf_error_is(err
, "libc") &&
89 atf_libc_error_code(err
) == ENOENT
);
92 INV(!atf_is_error(err
));
96 atf_error_t err
= atf_fs_unlink(errfile
);
97 if (atf_is_error(err
)) {
98 INV(atf_error_is(err
, "libc") &&
99 atf_libc_error_code(err
) == ENOENT
);
102 INV(!atf_is_error(err
));
106 atf_error_t err
= atf_fs_rmdir(dir
);
107 INV(!atf_is_error(err
));
113 const_execvp(const char *file
, const char *const *argv
)
115 #define UNCONST(a) ((void *)(unsigned long)(const void *)(a))
116 return execvp(file
, UNCONST(argv
));
122 init_sb(const atf_fs_path_t
*path
, atf_process_stream_t
*sb
)
127 err
= atf_process_stream_init_inherit(sb
);
129 err
= atf_process_stream_init_redirect_path(sb
, path
);
136 init_sbs(const atf_fs_path_t
*outfile
, atf_process_stream_t
*outsb
,
137 const atf_fs_path_t
*errfile
, atf_process_stream_t
*errsb
)
141 err
= init_sb(outfile
, outsb
);
142 if (atf_is_error(err
))
145 err
= init_sb(errfile
, errsb
);
146 if (atf_is_error(err
)) {
147 atf_process_stream_fini(outsb
);
156 const char *const *m_argv
;
159 static void exec_child(void *) ATF_DEFS_ATTRIBUTE_NORETURN
;
165 struct exec_data
*ea
= v
;
167 const_execvp(ea
->m_argv
[0], ea
->m_argv
);
168 fprintf(stderr
, "execvp(%s) failed: %s\n", ea
->m_argv
[0], strerror(errno
));
174 fork_and_wait(const char *const *argv
, const atf_fs_path_t
*outfile
,
175 const atf_fs_path_t
*errfile
, atf_process_status_t
*status
)
178 atf_process_child_t child
;
179 atf_process_stream_t outsb
, errsb
;
180 struct exec_data ea
= { argv
};
182 err
= init_sbs(outfile
, &outsb
, errfile
, &errsb
);
183 if (atf_is_error(err
))
186 err
= atf_process_fork(&child
, exec_child
, &outsb
, &errsb
, &ea
);
187 if (atf_is_error(err
))
190 err
= atf_process_child_wait(&child
, status
);
193 atf_process_stream_fini(&errsb
);
194 atf_process_stream_fini(&outsb
);
201 update_success_from_status(const char *progname
,
202 const atf_process_status_t
*status
, bool *success
)
204 bool s
= atf_process_status_exited(status
) &&
205 atf_process_status_exitstatus(status
) == EXIT_SUCCESS
;
207 if (atf_process_status_exited(status
)) {
208 if (atf_process_status_exitstatus(status
) == EXIT_SUCCESS
)
212 fprintf(stderr
, "%s failed with exit code %d\n", progname
,
213 atf_process_status_exitstatus(status
));
215 } else if (atf_process_status_signaled(status
)) {
217 fprintf(stderr
, "%s failed due to signal %d%s\n", progname
,
218 atf_process_status_termsig(status
),
219 atf_process_status_coredump(status
) ? " (core dumped)" : "");
222 fprintf(stderr
, "%s failed due to unknown reason\n", progname
);
230 array_to_list(const char *const *a
, atf_list_t
*l
)
234 err
= atf_list_init(l
);
235 if (atf_is_error(err
))
239 char *item
= strdup(*a
);
241 err
= atf_no_memory_error();
245 err
= atf_list_append(l
, item
, true);
246 if (atf_is_error(err
))
257 print_array(const char *const *array
, const char *pfx
)
259 const char *const *ptr
;
262 for (ptr
= array
; *ptr
!= NULL
; ptr
++)
269 check_build_run(const char *const *argv
, bool *success
)
272 atf_process_status_t status
;
274 print_array(argv
, ">");
276 err
= fork_and_wait(argv
, NULL
, NULL
, &status
);
277 if (atf_is_error(err
))
280 update_success_from_status(argv
[0], &status
, success
);
281 atf_process_status_fini(&status
);
283 INV(!atf_is_error(err
));
288 /* ---------------------------------------------------------------------
289 * The "atf_check_result" type.
290 * --------------------------------------------------------------------- */
292 struct atf_check_result_impl
{
295 atf_fs_path_t m_stdout
;
296 atf_fs_path_t m_stderr
;
297 atf_process_status_t m_status
;
302 atf_check_result_init(atf_check_result_t
*r
, const char *const *argv
,
303 const atf_fs_path_t
*dir
)
307 r
->pimpl
= malloc(sizeof(struct atf_check_result_impl
));
308 if (r
->pimpl
== NULL
)
309 return atf_no_memory_error();
311 err
= array_to_list(argv
, &r
->pimpl
->m_argv
);
312 if (atf_is_error(err
))
315 err
= atf_fs_path_copy(&r
->pimpl
->m_dir
, dir
);
316 if (atf_is_error(err
))
319 err
= atf_fs_path_init_fmt(&r
->pimpl
->m_stdout
, "%s/stdout",
320 atf_fs_path_cstring(dir
));
321 if (atf_is_error(err
))
324 err
= atf_fs_path_init_fmt(&r
->pimpl
->m_stderr
, "%s/stderr",
325 atf_fs_path_cstring(dir
));
326 if (atf_is_error(err
))
329 INV(!atf_is_error(err
));
333 atf_fs_path_fini(&r
->pimpl
->m_stdout
);
335 atf_fs_path_fini(&r
->pimpl
->m_dir
);
337 atf_list_fini(&r
->pimpl
->m_argv
);
343 atf_check_result_fini(atf_check_result_t
*r
)
345 atf_process_status_fini(&r
->pimpl
->m_status
);
347 cleanup_tmpdir(&r
->pimpl
->m_dir
, &r
->pimpl
->m_stdout
,
348 &r
->pimpl
->m_stderr
);
349 atf_fs_path_fini(&r
->pimpl
->m_stdout
);
350 atf_fs_path_fini(&r
->pimpl
->m_stderr
);
351 atf_fs_path_fini(&r
->pimpl
->m_dir
);
353 atf_list_fini(&r
->pimpl
->m_argv
);
359 atf_check_result_stdout(const atf_check_result_t
*r
)
361 return atf_fs_path_cstring(&r
->pimpl
->m_stdout
);
365 atf_check_result_stderr(const atf_check_result_t
*r
)
367 return atf_fs_path_cstring(&r
->pimpl
->m_stderr
);
371 atf_check_result_exited(const atf_check_result_t
*r
)
373 return atf_process_status_exited(&r
->pimpl
->m_status
);
377 atf_check_result_exitcode(const atf_check_result_t
*r
)
379 return atf_process_status_exitstatus(&r
->pimpl
->m_status
);
383 atf_check_result_signaled(const atf_check_result_t
*r
)
385 return atf_process_status_signaled(&r
->pimpl
->m_status
);
389 atf_check_result_termsig(const atf_check_result_t
*r
)
391 return atf_process_status_termsig(&r
->pimpl
->m_status
);
394 /* ---------------------------------------------------------------------
396 * --------------------------------------------------------------------- */
398 /* XXX: This function shouldn't be in this module. It messes with stdout
399 * and stderr, and it provides a very high-end interface. This belongs,
400 * probably, somewhere related to test cases (such as in the tc module). */
402 atf_check_build_c_o(const char *sfile
,
404 const char *const optargs
[],
410 err
= atf_build_c_o(sfile
, ofile
, optargs
, &argv
);
411 if (atf_is_error(err
))
414 err
= check_build_run((const char *const *)argv
, success
);
416 atf_utils_free_charpp(argv
);
422 atf_check_build_cpp(const char *sfile
,
424 const char *const optargs
[],
430 err
= atf_build_cpp(sfile
, ofile
, optargs
, &argv
);
431 if (atf_is_error(err
))
434 err
= check_build_run((const char *const *)argv
, success
);
436 atf_utils_free_charpp(argv
);
442 atf_check_build_cxx_o(const char *sfile
,
444 const char *const optargs
[],
450 err
= atf_build_cxx_o(sfile
, ofile
, optargs
, &argv
);
451 if (atf_is_error(err
))
454 err
= check_build_run((const char *const *)argv
, success
);
456 atf_utils_free_charpp(argv
);
462 atf_check_exec_array(const char *const *argv
, atf_check_result_t
*r
)
467 err
= create_tmpdir(&dir
);
468 if (atf_is_error(err
))
471 err
= atf_check_result_init(r
, argv
, &dir
);
472 if (atf_is_error(err
)) {
473 atf_error_t err2
= atf_fs_rmdir(&dir
);
474 INV(!atf_is_error(err2
));
478 err
= fork_and_wait(argv
, &r
->pimpl
->m_stdout
, &r
->pimpl
->m_stderr
,
479 &r
->pimpl
->m_status
);
480 if (atf_is_error(err
)) {
481 atf_check_result_fini(r
);
485 INV(!atf_is_error(err
));
487 atf_fs_path_fini(&dir
);