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); \
93 name ## _error(const char *fmt, ...) \
96 struct name ## _error_data data; \
100 vsnprintf(data.m_what, sizeof(data.m_what), fmt, ap); \
103 err = atf_error_new(#name, &data, sizeof(data), name ## _format); \
108 FREE_FORM_ERROR(usage
);
109 FREE_FORM_ERROR(user
);
111 /* ---------------------------------------------------------------------
112 * Printing functions.
113 * --------------------------------------------------------------------- */
117 print_error(const atf_error_t err
)
121 PRE(atf_is_error(err
));
123 atf_error_format(err
, buf
, sizeof(buf
));
124 fprintf(stderr
, "%s: ERROR: %s\n", progname
, buf
);
126 if (atf_error_is(err
, "usage"))
127 fprintf(stderr
, "%s: See atf-test-program(1) for usage details.\n",
133 print_warning(const char *message
)
135 fprintf(stderr
, "%s: WARNING: %s\n", progname
, message
);
138 /* ---------------------------------------------------------------------
140 * --------------------------------------------------------------------- */
144 atf_fs_path_t m_srcdir
;
146 enum tc_part m_tcpart
;
147 atf_fs_path_t m_resfile
;
153 argv0_to_dir(const char *argv0
, atf_fs_path_t
*dir
)
158 err
= atf_fs_path_init_fmt(&temp
, "%s", argv0
);
159 if (atf_is_error(err
))
162 err
= atf_fs_path_branch_path(&temp
, dir
);
164 atf_fs_path_fini(&temp
);
171 params_init(struct params
*p
, const char *argv0
)
175 p
->m_do_list
= false;
179 err
= argv0_to_dir(argv0
, &p
->m_srcdir
);
180 if (atf_is_error(err
))
183 err
= atf_fs_path_init_fmt(&p
->m_resfile
, "/dev/stdout");
184 if (atf_is_error(err
)) {
185 atf_fs_path_fini(&p
->m_srcdir
);
189 err
= atf_map_init(&p
->m_config
);
190 if (atf_is_error(err
)) {
191 atf_fs_path_fini(&p
->m_resfile
);
192 atf_fs_path_fini(&p
->m_srcdir
);
201 params_fini(struct params
*p
)
203 atf_map_fini(&p
->m_config
);
204 atf_fs_path_fini(&p
->m_resfile
);
205 atf_fs_path_fini(&p
->m_srcdir
);
206 if (p
->m_tcname
!= NULL
)
212 parse_vflag(char *arg
, atf_map_t
*config
)
217 split
= strchr(arg
, '=');
219 err
= usage_error("-v requires an argument of the form var=value");
226 err
= atf_map_insert(config
, arg
, split
, false);
234 replace_path_param(atf_fs_path_t
*param
, const char *value
)
239 err
= atf_fs_path_init_fmt(&temp
, "%s", value
);
240 if (!atf_is_error(err
)) {
241 atf_fs_path_fini(param
);
248 /* ---------------------------------------------------------------------
250 * --------------------------------------------------------------------- */
254 list_tcs(const atf_tp_t
*tp
)
256 const atf_tc_t
*const *tcs
;
257 const atf_tc_t
*const *tcsptr
;
259 printf("Content-Type: application/X-atf-tp; version=\"1\"\n\n");
261 tcs
= atf_tp_get_tcs(tp
);
262 INV(tcs
!= NULL
); /* Should be checked. */
263 for (tcsptr
= tcs
; *tcsptr
!= NULL
; tcsptr
++) {
264 const atf_tc_t
*tc
= *tcsptr
;
265 char **vars
= atf_tc_get_md_vars(tc
);
268 INV(vars
!= NULL
); /* Should be checked. */
270 if (tcsptr
!= tcs
) /* Not first. */
273 for (ptr
= vars
; *ptr
!= NULL
; ptr
+= 2) {
274 if (strcmp(*ptr
, "ident") == 0) {
275 printf("ident: %s\n", *(ptr
+ 1));
280 for (ptr
= vars
; *ptr
!= NULL
; ptr
+= 2) {
281 if (strcmp(*ptr
, "ident") != 0) {
282 printf("%s: %s\n", *ptr
, *(ptr
+ 1));
286 atf_utils_free_charpp(vars
);
290 /* ---------------------------------------------------------------------
292 * --------------------------------------------------------------------- */
296 handle_tcarg(const char *tcarg
, char **tcname
, enum tc_part
*tcpart
)
300 err
= atf_no_error();
302 *tcname
= strdup(tcarg
);
303 if (*tcname
== NULL
) {
304 err
= atf_no_memory_error();
308 char *delim
= strchr(*tcname
, ':');
313 if (strcmp(delim
, "body") == 0) {
315 } else if (strcmp(delim
, "cleanup") == 0) {
318 err
= usage_error("Invalid test case part `%s'", delim
);
329 process_params(int argc
, char **argv
, struct params
*p
)
335 err
= params_init(p
, argv
[0]);
336 if (atf_is_error(err
))
341 while (!atf_is_error(err
) &&
342 (ch
= getopt(argc
, argv
, GETOPT_POSIX
":lr:s:v:")) != -1) {
349 err
= replace_path_param(&p
->m_resfile
, optarg
);
353 err
= replace_path_param(&p
->m_srcdir
, optarg
);
357 err
= parse_vflag(optarg
, &p
->m_config
);
361 err
= usage_error("Option -%c requires an argument.", optopt
);
366 err
= usage_error("Unknown option -%c.", optopt
);
372 /* Clear getopt state just in case the test wants to use it. */
375 #if defined(HAVE_OPTRESET)
379 if (!atf_is_error(err
)) {
382 err
= usage_error("Cannot provide test case names with -l");
385 err
= usage_error("Must provide a test case name");
387 err
= handle_tcarg(argv
[0], &p
->m_tcname
, &p
->m_tcpart
);
389 err
= usage_error("Cannot provide more than one test case "
395 if (atf_is_error(err
))
404 srcdir_strip_libtool(atf_fs_path_t
*srcdir
)
407 atf_fs_path_t parent
;
409 err
= atf_fs_path_branch_path(srcdir
, &parent
);
410 if (atf_is_error(err
))
413 atf_fs_path_fini(srcdir
);
416 INV(!atf_is_error(err
));
423 handle_srcdir(struct params
*p
)
426 atf_dynstr_t leafname
;
427 atf_fs_path_t exe
, srcdir
;
430 err
= atf_fs_path_copy(&srcdir
, &p
->m_srcdir
);
431 if (atf_is_error(err
))
434 if (!atf_fs_path_is_absolute(&srcdir
)) {
435 atf_fs_path_t srcdirabs
;
437 err
= atf_fs_path_to_absolute(&srcdir
, &srcdirabs
);
438 if (atf_is_error(err
))
441 atf_fs_path_fini(&srcdir
);
445 err
= atf_fs_path_leaf_name(&srcdir
, &leafname
);
446 if (atf_is_error(err
))
449 const bool libs
= atf_equal_dynstr_cstring(&leafname
, ".libs");
450 atf_dynstr_fini(&leafname
);
453 err
= srcdir_strip_libtool(&srcdir
);
454 if (atf_is_error(err
))
459 err
= atf_fs_path_copy(&exe
, &srcdir
);
460 if (atf_is_error(err
))
463 err
= atf_fs_path_append_fmt(&exe
, "%s", progname
);
464 if (atf_is_error(err
))
467 err
= atf_fs_exists(&exe
, &b
);
468 if (!atf_is_error(err
)) {
470 err
= atf_map_insert(&p
->m_config
, "srcdir",
471 strdup(atf_fs_path_cstring(&srcdir
)), true);
473 err
= user_error("Cannot find the test program in the source "
474 "directory `%s'", atf_fs_path_cstring(&srcdir
));
479 atf_fs_path_fini(&exe
);
481 atf_fs_path_fini(&srcdir
);
488 run_tc(const atf_tp_t
*tp
, struct params
*p
, int *exitcode
)
492 err
= atf_no_error();
494 if (!atf_tp_has_tc(tp
, p
->m_tcname
)) {
495 err
= usage_error("Unknown test case `%s'", p
->m_tcname
);
499 if (!atf_env_has("__RUNNING_INSIDE_ATF_RUN") || strcmp(atf_env_get(
500 "__RUNNING_INSIDE_ATF_RUN"), "internal-yes-value") != 0)
502 print_warning("Running test cases without atf-run(1) is unsupported");
503 print_warning("No isolation nor timeout control is being applied; you "
504 "may get unexpected failures; see atf-test-case(4)");
507 switch (p
->m_tcpart
) {
509 err
= atf_tp_run(tp
, p
->m_tcname
, atf_fs_path_cstring(&p
->m_resfile
));
510 if (atf_is_error(err
)) {
511 /* TODO: Handle error */
512 *exitcode
= EXIT_FAILURE
;
515 *exitcode
= EXIT_SUCCESS
;
521 err
= atf_tp_cleanup(tp
, p
->m_tcname
);
522 if (atf_is_error(err
)) {
523 /* TODO: Handle error */
524 *exitcode
= EXIT_FAILURE
;
527 *exitcode
= EXIT_SUCCESS
;
536 INV(!atf_is_error(err
));
543 controlled_main(int argc
, char **argv
,
544 atf_error_t (*add_tcs_hook
)(atf_tp_t
*),
552 err
= process_params(argc
, argv
, &p
);
553 if (atf_is_error(err
))
556 err
= handle_srcdir(&p
);
557 if (atf_is_error(err
))
560 raw_config
= atf_map_to_charpp(&p
.m_config
);
561 if (raw_config
== NULL
) {
562 err
= atf_no_memory_error();
565 err
= atf_tp_init(&tp
, (const char* const*)raw_config
);
566 atf_utils_free_charpp(raw_config
);
567 if (atf_is_error(err
))
570 err
= add_tcs_hook(&tp
);
571 if (atf_is_error(err
))
576 INV(!atf_is_error(err
));
577 *exitcode
= EXIT_SUCCESS
;
579 err
= run_tc(&tp
, &p
, exitcode
);
591 atf_tp_main(int argc
, char **argv
, atf_error_t (*add_tcs_hook
)(atf_tp_t
*))
596 progname
= strrchr(argv
[0], '/');
597 if (progname
== NULL
)
602 /* Libtool workaround: if running from within the source tree (binaries
603 * that are not installed yet), skip the "lt-" prefix added to files in
604 * the ".libs" directory to show the real (not temporary) name. */
605 if (strncmp(progname
, "lt-", 3) == 0)
608 exitcode
= EXIT_FAILURE
; /* Silence GCC warning. */
609 err
= controlled_main(argc
, argv
, add_tcs_hook
, &exitcode
);
610 if (atf_is_error(err
)) {
613 exitcode
= EXIT_FAILURE
;