1 /* $NetBSD: tp_main.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.
32 #if defined(HAVE_CONFIG_H)
43 #include "atf-c/error.h"
46 #include "atf-c/utils.h"
54 #if defined(HAVE_GNU_GETOPT)
55 # define GETOPT_POSIX "+"
57 # define GETOPT_POSIX ""
60 static const char *progname
= NULL
;
62 /* This prototype is provided by macros.h during instantiation of the test
63 * program, so it can be kept private. Don't know if that's the best idea
65 int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t
*));
72 /* ---------------------------------------------------------------------
73 * The "usage" and "user" error types.
74 * --------------------------------------------------------------------- */
76 #define FREE_FORM_ERROR(name) \
77 struct name ## _error_data { \
83 name ## _format(const atf_error_t err, char *buf, size_t buflen) \
85 const struct name ## _error_data *data; \
87 PRE(atf_error_is(err, #name)); \
89 data = atf_error_data(err); \
90 snprintf(buf, buflen, "%s", data->m_what); \
95 name ## _error(const char *fmt, ...) \
98 struct name ## _error_data data; \
102 vsnprintf(data.m_what, sizeof(data.m_what), fmt, ap); \
105 err = atf_error_new(#name, &data, sizeof(data), name ## _format); \
110 FREE_FORM_ERROR(usage
);
111 FREE_FORM_ERROR(user
);
113 /* ---------------------------------------------------------------------
114 * Printing functions.
115 * --------------------------------------------------------------------- */
119 print_error(const atf_error_t err
)
123 PRE(atf_is_error(err
));
125 atf_error_format(err
, buf
, sizeof(buf
));
126 fprintf(stderr
, "%s: ERROR: %s\n", progname
, buf
);
128 if (atf_error_is(err
, "usage"))
129 fprintf(stderr
, "%s: See atf-test-program(1) for usage details.\n",
135 print_warning(const char *message
)
137 fprintf(stderr
, "%s: WARNING: %s\n", progname
, message
);
140 /* ---------------------------------------------------------------------
142 * --------------------------------------------------------------------- */
146 atf_fs_path_t m_srcdir
;
148 enum tc_part m_tcpart
;
149 atf_fs_path_t m_resfile
;
155 argv0_to_dir(const char *argv0
, atf_fs_path_t
*dir
)
160 err
= atf_fs_path_init_fmt(&temp
, "%s", argv0
);
161 if (atf_is_error(err
))
164 err
= atf_fs_path_branch_path(&temp
, dir
);
166 atf_fs_path_fini(&temp
);
173 params_init(struct params
*p
, const char *argv0
)
177 p
->m_do_list
= false;
181 err
= argv0_to_dir(argv0
, &p
->m_srcdir
);
182 if (atf_is_error(err
))
185 err
= atf_fs_path_init_fmt(&p
->m_resfile
, "/dev/stdout");
186 if (atf_is_error(err
)) {
187 atf_fs_path_fini(&p
->m_srcdir
);
191 err
= atf_map_init(&p
->m_config
);
192 if (atf_is_error(err
)) {
193 atf_fs_path_fini(&p
->m_resfile
);
194 atf_fs_path_fini(&p
->m_srcdir
);
203 params_fini(struct params
*p
)
205 atf_map_fini(&p
->m_config
);
206 atf_fs_path_fini(&p
->m_resfile
);
207 atf_fs_path_fini(&p
->m_srcdir
);
208 if (p
->m_tcname
!= NULL
)
214 parse_vflag(char *arg
, atf_map_t
*config
)
219 split
= strchr(arg
, '=');
221 err
= usage_error("-v requires an argument of the form var=value");
228 err
= atf_map_insert(config
, arg
, split
, false);
236 replace_path_param(atf_fs_path_t
*param
, const char *value
)
241 err
= atf_fs_path_init_fmt(&temp
, "%s", value
);
242 if (!atf_is_error(err
)) {
243 atf_fs_path_fini(param
);
250 /* ---------------------------------------------------------------------
252 * --------------------------------------------------------------------- */
256 list_tcs(const atf_tp_t
*tp
)
258 const atf_tc_t
*const *tcs
;
259 const atf_tc_t
*const *tcsptr
;
261 printf("Content-Type: application/X-atf-tp; version=\"1\"\n\n");
263 tcs
= atf_tp_get_tcs(tp
);
264 INV(tcs
!= NULL
); /* Should be checked. */
265 for (tcsptr
= tcs
; *tcsptr
!= NULL
; tcsptr
++) {
266 const atf_tc_t
*tc
= *tcsptr
;
267 char **vars
= atf_tc_get_md_vars(tc
);
270 INV(vars
!= NULL
); /* Should be checked. */
272 if (tcsptr
!= tcs
) /* Not first. */
275 for (ptr
= vars
; *ptr
!= NULL
; ptr
+= 2) {
276 if (strcmp(*ptr
, "ident") == 0) {
277 printf("ident: %s\n", *(ptr
+ 1));
282 for (ptr
= vars
; *ptr
!= NULL
; ptr
+= 2) {
283 if (strcmp(*ptr
, "ident") != 0) {
284 printf("%s: %s\n", *ptr
, *(ptr
+ 1));
288 atf_utils_free_charpp(vars
);
292 /* ---------------------------------------------------------------------
294 * --------------------------------------------------------------------- */
298 handle_tcarg(const char *tcarg
, char **tcname
, enum tc_part
*tcpart
)
302 err
= atf_no_error();
304 *tcname
= strdup(tcarg
);
305 if (*tcname
== NULL
) {
306 err
= atf_no_memory_error();
310 char *delim
= strchr(*tcname
, ':');
315 if (strcmp(delim
, "body") == 0) {
317 } else if (strcmp(delim
, "cleanup") == 0) {
320 err
= usage_error("Invalid test case part `%s'", delim
);
331 process_params(int argc
, char **argv
, struct params
*p
)
337 err
= params_init(p
, argv
[0]);
338 if (atf_is_error(err
))
343 while (!atf_is_error(err
) &&
344 (ch
= getopt(argc
, argv
, GETOPT_POSIX
":lr:s:v:")) != -1) {
351 err
= replace_path_param(&p
->m_resfile
, optarg
);
355 err
= replace_path_param(&p
->m_srcdir
, optarg
);
359 err
= parse_vflag(optarg
, &p
->m_config
);
363 err
= usage_error("Option -%c requires an argument.", optopt
);
368 err
= usage_error("Unknown option -%c.", optopt
);
374 /* Clear getopt state just in case the test wants to use it. */
377 #if defined(HAVE_OPTRESET)
381 if (!atf_is_error(err
)) {
384 err
= usage_error("Cannot provide test case names with -l");
387 err
= usage_error("Must provide a test case name");
389 err
= handle_tcarg(argv
[0], &p
->m_tcname
, &p
->m_tcpart
);
391 err
= usage_error("Cannot provide more than one test case "
397 if (atf_is_error(err
))
406 srcdir_strip_libtool(atf_fs_path_t
*srcdir
)
409 atf_fs_path_t parent
;
411 err
= atf_fs_path_branch_path(srcdir
, &parent
);
412 if (atf_is_error(err
))
415 atf_fs_path_fini(srcdir
);
418 INV(!atf_is_error(err
));
425 handle_srcdir(struct params
*p
)
428 atf_dynstr_t leafname
;
429 atf_fs_path_t exe
, srcdir
;
432 err
= atf_fs_path_copy(&srcdir
, &p
->m_srcdir
);
433 if (atf_is_error(err
))
436 if (!atf_fs_path_is_absolute(&srcdir
)) {
437 atf_fs_path_t srcdirabs
;
439 err
= atf_fs_path_to_absolute(&srcdir
, &srcdirabs
);
440 if (atf_is_error(err
))
443 atf_fs_path_fini(&srcdir
);
447 err
= atf_fs_path_leaf_name(&srcdir
, &leafname
);
448 if (atf_is_error(err
))
451 const bool libs
= atf_equal_dynstr_cstring(&leafname
, ".libs");
452 atf_dynstr_fini(&leafname
);
455 err
= srcdir_strip_libtool(&srcdir
);
456 if (atf_is_error(err
))
461 err
= atf_fs_path_copy(&exe
, &srcdir
);
462 if (atf_is_error(err
))
465 err
= atf_fs_path_append_fmt(&exe
, "%s", progname
);
466 if (atf_is_error(err
))
469 err
= atf_fs_exists(&exe
, &b
);
470 if (!atf_is_error(err
)) {
472 err
= atf_map_insert(&p
->m_config
, "srcdir",
473 strdup(atf_fs_path_cstring(&srcdir
)), true);
475 err
= user_error("Cannot find the test program in the source "
476 "directory `%s'", atf_fs_path_cstring(&srcdir
));
481 atf_fs_path_fini(&exe
);
483 atf_fs_path_fini(&srcdir
);
490 run_tc(const atf_tp_t
*tp
, struct params
*p
, int *exitcode
)
494 err
= atf_no_error();
496 if (!atf_tp_has_tc(tp
, p
->m_tcname
)) {
497 err
= usage_error("Unknown test case `%s'", p
->m_tcname
);
501 if (!atf_env_has("__RUNNING_INSIDE_ATF_RUN") || strcmp(atf_env_get(
502 "__RUNNING_INSIDE_ATF_RUN"), "internal-yes-value") != 0)
504 print_warning("Running test cases without atf-run(1) is unsupported");
505 print_warning("No isolation nor timeout control is being applied; you "
506 "may get unexpected failures; see atf-test-case(4)");
509 switch (p
->m_tcpart
) {
511 err
= atf_tp_run(tp
, p
->m_tcname
, atf_fs_path_cstring(&p
->m_resfile
));
512 if (atf_is_error(err
)) {
513 /* TODO: Handle error */
514 *exitcode
= EXIT_FAILURE
;
517 *exitcode
= EXIT_SUCCESS
;
523 err
= atf_tp_cleanup(tp
, p
->m_tcname
);
524 if (atf_is_error(err
)) {
525 /* TODO: Handle error */
526 *exitcode
= EXIT_FAILURE
;
529 *exitcode
= EXIT_SUCCESS
;
538 INV(!atf_is_error(err
));
545 controlled_main(int argc
, char **argv
,
546 atf_error_t (*add_tcs_hook
)(atf_tp_t
*),
554 err
= process_params(argc
, argv
, &p
);
555 if (atf_is_error(err
))
558 err
= handle_srcdir(&p
);
559 if (atf_is_error(err
))
562 raw_config
= atf_map_to_charpp(&p
.m_config
);
563 if (raw_config
== NULL
) {
564 err
= atf_no_memory_error();
567 err
= atf_tp_init(&tp
, (const char* const*)raw_config
);
568 atf_utils_free_charpp(raw_config
);
569 if (atf_is_error(err
))
572 err
= add_tcs_hook(&tp
);
573 if (atf_is_error(err
))
578 INV(!atf_is_error(err
));
579 *exitcode
= EXIT_SUCCESS
;
581 err
= run_tc(&tp
, &p
, exitcode
);
593 atf_tp_main(int argc
, char **argv
, atf_error_t (*add_tcs_hook
)(atf_tp_t
*))
598 progname
= strrchr(argv
[0], '/');
599 if (progname
== NULL
)
604 /* Libtool workaround: if running from within the source tree (binaries
605 * that are not installed yet), skip the "lt-" prefix added to files in
606 * the ".libs" directory to show the real (not temporary) name. */
607 if (strncmp(progname
, "lt-", 3) == 0)
610 exitcode
= EXIT_FAILURE
; /* Silence GCC warning. */
611 err
= controlled_main(argc
, argv
, add_tcs_hook
, &exitcode
);
612 if (atf_is_error(err
)) {
615 exitcode
= EXIT_FAILURE
;