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.
30 #if defined(HAVE_CONFIG_H)
41 #include "atf-c/error.h"
44 #include "atf-c/utils.h"
52 #if defined(HAVE_GNU_GETOPT)
53 # define GETOPT_POSIX "+"
55 # define GETOPT_POSIX ""
58 static const char *progname
= NULL
;
60 /* This prototype is provided by macros.h during instantiation of the test
61 * program, so it can be kept private. Don't know if that's the best idea
63 int atf_tp_main(int, char **, atf_error_t (*)(atf_tp_t
*));
70 /* ---------------------------------------------------------------------
71 * The "usage" and "user" error types.
72 * --------------------------------------------------------------------- */
74 #define FREE_FORM_ERROR(name) \
75 struct name ## _error_data { \
81 name ## _format(const atf_error_t err, char *buf, size_t buflen) \
83 const struct name ## _error_data *data; \
85 PRE(atf_error_is(err, #name)); \
87 data = atf_error_data(err); \
88 snprintf(buf, buflen, "%s", data->m_what); \
91 ATF_DEFS_ATTRIBUTE_FORMAT_PRINTF(1, 2) \
94 name ## _error(const char *fmt, ...) \
97 struct name ## _error_data data; \
101 vsnprintf(data.m_what, sizeof(data.m_what), fmt, ap); \
104 err = atf_error_new(#name, &data, sizeof(data), name ## _format); \
109 FREE_FORM_ERROR(usage
);
110 FREE_FORM_ERROR(user
);
112 /* ---------------------------------------------------------------------
113 * Printing functions.
114 * --------------------------------------------------------------------- */
118 print_error(const atf_error_t err
)
122 PRE(atf_is_error(err
));
124 atf_error_format(err
, buf
, sizeof(buf
));
125 fprintf(stderr
, "%s: ERROR: %s\n", progname
, buf
);
127 if (atf_error_is(err
, "usage"))
128 fprintf(stderr
, "%s: See atf-test-program(1) for usage details.\n",
134 print_warning(const char *message
)
136 fprintf(stderr
, "%s: WARNING: %s\n", progname
, message
);
139 /* ---------------------------------------------------------------------
141 * --------------------------------------------------------------------- */
145 atf_fs_path_t m_srcdir
;
147 enum tc_part m_tcpart
;
148 atf_fs_path_t m_resfile
;
154 argv0_to_dir(const char *argv0
, atf_fs_path_t
*dir
)
159 err
= atf_fs_path_init_fmt(&temp
, "%s", argv0
);
160 if (atf_is_error(err
))
163 err
= atf_fs_path_branch_path(&temp
, dir
);
165 atf_fs_path_fini(&temp
);
172 params_init(struct params
*p
, const char *argv0
)
176 p
->m_do_list
= false;
180 err
= argv0_to_dir(argv0
, &p
->m_srcdir
);
181 if (atf_is_error(err
))
184 err
= atf_fs_path_init_fmt(&p
->m_resfile
, "/dev/stdout");
185 if (atf_is_error(err
)) {
186 atf_fs_path_fini(&p
->m_srcdir
);
190 err
= atf_map_init(&p
->m_config
);
191 if (atf_is_error(err
)) {
192 atf_fs_path_fini(&p
->m_resfile
);
193 atf_fs_path_fini(&p
->m_srcdir
);
202 params_fini(struct params
*p
)
204 atf_map_fini(&p
->m_config
);
205 atf_fs_path_fini(&p
->m_resfile
);
206 atf_fs_path_fini(&p
->m_srcdir
);
207 if (p
->m_tcname
!= NULL
)
213 parse_vflag(char *arg
, atf_map_t
*config
)
218 split
= strchr(arg
, '=');
220 err
= usage_error("-v requires an argument of the form var=value");
227 err
= atf_map_insert(config
, arg
, split
, false);
235 replace_path_param(atf_fs_path_t
*param
, const char *value
)
240 err
= atf_fs_path_init_fmt(&temp
, "%s", value
);
241 if (!atf_is_error(err
)) {
242 atf_fs_path_fini(param
);
249 /* ---------------------------------------------------------------------
251 * --------------------------------------------------------------------- */
255 list_tcs(const atf_tp_t
*tp
)
257 const atf_tc_t
*const *tcs
;
258 const atf_tc_t
*const *tcsptr
;
260 printf("Content-Type: application/X-atf-tp; version=\"1\"\n\n");
262 tcs
= atf_tp_get_tcs(tp
);
263 INV(tcs
!= NULL
); /* Should be checked. */
264 for (tcsptr
= tcs
; *tcsptr
!= NULL
; tcsptr
++) {
265 const atf_tc_t
*tc
= *tcsptr
;
266 char **vars
= atf_tc_get_md_vars(tc
);
269 INV(vars
!= NULL
); /* Should be checked. */
271 if (tcsptr
!= tcs
) /* Not first. */
274 for (ptr
= vars
; *ptr
!= NULL
; ptr
+= 2) {
275 if (strcmp(*ptr
, "ident") == 0) {
276 printf("ident: %s\n", *(ptr
+ 1));
281 for (ptr
= vars
; *ptr
!= NULL
; ptr
+= 2) {
282 if (strcmp(*ptr
, "ident") != 0) {
283 printf("%s: %s\n", *ptr
, *(ptr
+ 1));
287 atf_utils_free_charpp(vars
);
291 /* ---------------------------------------------------------------------
293 * --------------------------------------------------------------------- */
297 handle_tcarg(const char *tcarg
, char **tcname
, enum tc_part
*tcpart
)
301 err
= atf_no_error();
303 *tcname
= strdup(tcarg
);
304 if (*tcname
== NULL
) {
305 err
= atf_no_memory_error();
309 char *delim
= strchr(*tcname
, ':');
314 if (strcmp(delim
, "body") == 0) {
316 } else if (strcmp(delim
, "cleanup") == 0) {
319 err
= usage_error("Invalid test case part `%s'", delim
);
330 process_params(int argc
, char **argv
, struct params
*p
)
336 err
= params_init(p
, argv
[0]);
337 if (atf_is_error(err
))
342 while (!atf_is_error(err
) &&
343 (ch
= getopt(argc
, argv
, GETOPT_POSIX
":lr:s:v:")) != -1) {
350 err
= replace_path_param(&p
->m_resfile
, optarg
);
354 err
= replace_path_param(&p
->m_srcdir
, optarg
);
358 err
= parse_vflag(optarg
, &p
->m_config
);
362 err
= usage_error("Option -%c requires an argument.", optopt
);
367 err
= usage_error("Unknown option -%c.", optopt
);
373 /* Clear getopt state just in case the test wants to use it. */
376 #if defined(HAVE_OPTRESET)
380 if (!atf_is_error(err
)) {
383 err
= usage_error("Cannot provide test case names with -l");
386 err
= usage_error("Must provide a test case name");
388 err
= handle_tcarg(argv
[0], &p
->m_tcname
, &p
->m_tcpart
);
390 err
= usage_error("Cannot provide more than one test case "
396 if (atf_is_error(err
))
405 srcdir_strip_libtool(atf_fs_path_t
*srcdir
)
408 atf_fs_path_t parent
;
410 err
= atf_fs_path_branch_path(srcdir
, &parent
);
411 if (atf_is_error(err
))
414 atf_fs_path_fini(srcdir
);
417 INV(!atf_is_error(err
));
424 handle_srcdir(struct params
*p
)
427 atf_dynstr_t leafname
;
428 atf_fs_path_t exe
, srcdir
;
431 err
= atf_fs_path_copy(&srcdir
, &p
->m_srcdir
);
432 if (atf_is_error(err
))
435 if (!atf_fs_path_is_absolute(&srcdir
)) {
436 atf_fs_path_t srcdirabs
;
438 err
= atf_fs_path_to_absolute(&srcdir
, &srcdirabs
);
439 if (atf_is_error(err
))
442 atf_fs_path_fini(&srcdir
);
446 err
= atf_fs_path_leaf_name(&srcdir
, &leafname
);
447 if (atf_is_error(err
))
450 const bool libs
= atf_equal_dynstr_cstring(&leafname
, ".libs");
451 atf_dynstr_fini(&leafname
);
454 err
= srcdir_strip_libtool(&srcdir
);
455 if (atf_is_error(err
))
460 err
= atf_fs_path_copy(&exe
, &srcdir
);
461 if (atf_is_error(err
))
464 err
= atf_fs_path_append_fmt(&exe
, "%s", progname
);
465 if (atf_is_error(err
))
468 err
= atf_fs_exists(&exe
, &b
);
469 if (!atf_is_error(err
)) {
471 err
= atf_map_insert(&p
->m_config
, "srcdir",
472 strdup(atf_fs_path_cstring(&srcdir
)), true);
474 err
= user_error("Cannot find the test program in the source "
475 "directory `%s'", atf_fs_path_cstring(&srcdir
));
480 atf_fs_path_fini(&exe
);
482 atf_fs_path_fini(&srcdir
);
489 run_tc(const atf_tp_t
*tp
, struct params
*p
, int *exitcode
)
493 err
= atf_no_error();
495 if (!atf_tp_has_tc(tp
, p
->m_tcname
)) {
496 err
= usage_error("Unknown test case `%s'", p
->m_tcname
);
500 if (!atf_env_has("__RUNNING_INSIDE_ATF_RUN") || strcmp(atf_env_get(
501 "__RUNNING_INSIDE_ATF_RUN"), "internal-yes-value") != 0)
503 print_warning("Running test cases without atf-run(1) is unsupported");
504 print_warning("No isolation nor timeout control is being applied; you "
505 "may get unexpected failures; see atf-test-case(4)");
508 switch (p
->m_tcpart
) {
510 err
= atf_tp_run(tp
, p
->m_tcname
, atf_fs_path_cstring(&p
->m_resfile
));
511 if (atf_is_error(err
)) {
512 /* TODO: Handle error */
513 *exitcode
= EXIT_FAILURE
;
516 *exitcode
= EXIT_SUCCESS
;
522 err
= atf_tp_cleanup(tp
, p
->m_tcname
);
523 if (atf_is_error(err
)) {
524 /* TODO: Handle error */
525 *exitcode
= EXIT_FAILURE
;
528 *exitcode
= EXIT_SUCCESS
;
537 INV(!atf_is_error(err
));
544 controlled_main(int argc
, char **argv
,
545 atf_error_t (*add_tcs_hook
)(atf_tp_t
*),
553 err
= process_params(argc
, argv
, &p
);
554 if (atf_is_error(err
))
557 err
= handle_srcdir(&p
);
558 if (atf_is_error(err
))
561 raw_config
= atf_map_to_charpp(&p
.m_config
);
562 if (raw_config
== NULL
) {
563 err
= atf_no_memory_error();
566 err
= atf_tp_init(&tp
, (const char* const*)raw_config
);
567 atf_utils_free_charpp(raw_config
);
568 if (atf_is_error(err
))
571 err
= add_tcs_hook(&tp
);
572 if (atf_is_error(err
))
577 INV(!atf_is_error(err
));
578 *exitcode
= EXIT_SUCCESS
;
580 err
= run_tc(&tp
, &p
, exitcode
);
592 atf_tp_main(int argc
, char **argv
, atf_error_t (*add_tcs_hook
)(atf_tp_t
*))
597 progname
= strrchr(argv
[0], '/');
598 if (progname
== NULL
)
603 /* Libtool workaround: if running from within the source tree (binaries
604 * that are not installed yet), skip the "lt-" prefix added to files in
605 * the ".libs" directory to show the real (not temporary) name. */
606 if (strncmp(progname
, "lt-", 3) == 0)
609 exitcode
= EXIT_FAILURE
; /* Silence GCC warning. */
610 err
= controlled_main(argc
, argv
, add_tcs_hook
, &exitcode
);
611 if (atf_is_error(err
)) {
614 exitcode
= EXIT_FAILURE
;