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 #include <sys/types.h>
32 #include <sys/resource.h>
45 #include "atf-c/defs.h"
49 #include "test_helpers.h"
51 atf_error_t
atf_process_status_init(atf_process_status_t
*, int);
53 /* ---------------------------------------------------------------------
54 * Auxiliary functions for testing of 'atf_process_fork'.
55 * --------------------------------------------------------------------- */
58 * Testing of atf_process_fork is quite messy. We want to be able to test
59 * all the possible combinations of stdout and stderr behavior to ensure
60 * that the streams are manipulated correctly.
62 * To do this, the do_fork function is a wrapper for atf_process_fork that
63 * issues stream-specific hooks before fork, while the child is running and
64 * after the child terminates. We then provide test cases that just call
65 * do_fork with different hooks.
67 * The hooks are described by base_stream, and we then have one *_stream
68 * type for ever possible stream behavior.
71 enum out_type
{ stdout_type
, stderr_type
};
75 void (*process
)(void *, atf_process_child_t
*);
78 /* m_sb is initialized by subclasses that need it, but all consumers
79 * must use m_sb_ptr, which may or may not point to m_sb. This allows
80 * us to test the interface with a NULL value, which triggers a
81 * default behavior. */
82 atf_process_stream_t m_sb
;
83 atf_process_stream_t
*m_sb_ptr
;
86 #define BASE_STREAM(ihook, phook, fhook, type) \
94 check_file(const enum out_type type
)
98 ATF_CHECK(atf_utils_grep_file("stdout: msg", "stdout"));
99 ATF_CHECK(!atf_utils_grep_file("stderr: msg", "stdout"));
102 ATF_CHECK(atf_utils_grep_file("stderr: msg", "stderr"));
103 ATF_CHECK(!atf_utils_grep_file("stdout: msg", "stderr"));
110 struct capture_stream
{
111 struct base_stream m_base
;
115 #define CAPTURE_STREAM(type) \
116 { .m_base = BASE_STREAM(capture_stream_init, \
117 capture_stream_process, \
118 capture_stream_fini, \
123 capture_stream_init(void *v
)
125 struct capture_stream
*s
= v
;
127 s
->m_base
.m_sb_ptr
= &s
->m_base
.m_sb
;
128 RE(atf_process_stream_init_capture(&s
->m_base
.m_sb
));
134 capture_stream_process(void *v
, atf_process_child_t
*c
)
136 struct capture_stream
*s
= v
;
138 switch (s
->m_base
.m_type
) {
140 s
->m_msg
= atf_utils_readline(atf_process_child_stdout(c
));
143 s
->m_msg
= atf_utils_readline(atf_process_child_stderr(c
));
152 capture_stream_fini(void *v
)
154 struct capture_stream
*s
= v
;
156 switch (s
->m_base
.m_type
) {
158 ATF_CHECK(atf_utils_grep_string("stdout: msg", s
->m_msg
));
159 ATF_CHECK(!atf_utils_grep_string("stderr: msg", s
->m_msg
));
162 ATF_CHECK(!atf_utils_grep_string("stdout: msg", s
->m_msg
));
163 ATF_CHECK(atf_utils_grep_string("stderr: msg", s
->m_msg
));
170 atf_process_stream_fini(&s
->m_base
.m_sb
);
173 struct connect_stream
{
174 struct base_stream m_base
;
178 #define CONNECT_STREAM(type) \
179 { .m_base = BASE_STREAM(connect_stream_init, \
181 connect_stream_fini, \
186 connect_stream_init(void *v
)
188 struct connect_stream
*s
= v
;
191 switch (s
->m_base
.m_type
) {
193 src_fd
= STDOUT_FILENO
;
194 s
->m_fd
= open("stdout", O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
197 src_fd
= STDERR_FILENO
;
198 s
->m_fd
= open("stderr", O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
204 ATF_REQUIRE(s
->m_fd
!= -1);
206 s
->m_base
.m_sb_ptr
= &s
->m_base
.m_sb
;
207 RE(atf_process_stream_init_connect(&s
->m_base
.m_sb
, src_fd
, s
->m_fd
));
212 connect_stream_fini(void *v
)
214 struct connect_stream
*s
= v
;
216 ATF_REQUIRE(close(s
->m_fd
) != -1);
218 atf_process_stream_fini(&s
->m_base
.m_sb
);
220 check_file(s
->m_base
.m_type
);
223 struct inherit_stream
{
224 struct base_stream m_base
;
229 #define INHERIT_STREAM(type) \
230 { .m_base = BASE_STREAM(inherit_stream_init, \
232 inherit_stream_fini, \
237 inherit_stream_init(void *v
)
239 struct inherit_stream
*s
= v
;
242 s
->m_base
.m_sb_ptr
= &s
->m_base
.m_sb
;
243 RE(atf_process_stream_init_inherit(&s
->m_base
.m_sb
));
245 switch (s
->m_base
.m_type
) {
247 s
->m_fd
= STDOUT_FILENO
;
251 s
->m_fd
= STDERR_FILENO
;
259 s
->m_old_fd
= dup(s
->m_fd
);
260 ATF_REQUIRE(s
->m_old_fd
!= -1);
261 ATF_REQUIRE(close(s
->m_fd
) != -1);
262 ATF_REQUIRE_EQ(open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644),
268 inherit_stream_fini(void *v
)
270 struct inherit_stream
*s
= v
;
272 ATF_REQUIRE(dup2(s
->m_old_fd
, s
->m_fd
) != -1);
273 ATF_REQUIRE(close(s
->m_old_fd
) != -1);
275 atf_process_stream_fini(&s
->m_base
.m_sb
);
277 check_file(s
->m_base
.m_type
);
280 #define default_stream inherit_stream
281 #define DEFAULT_STREAM(type) \
282 { .m_base = BASE_STREAM(default_stream_init, \
284 default_stream_fini, \
289 default_stream_init(void *v
)
291 struct inherit_stream
*s
= v
;
293 inherit_stream_init(v
);
294 s
->m_base
.m_sb_ptr
= NULL
;
299 default_stream_fini(void *v
)
301 inherit_stream_fini(v
);
304 struct redirect_fd_stream
{
305 struct base_stream m_base
;
309 #define REDIRECT_FD_STREAM(type) \
310 { .m_base = BASE_STREAM(redirect_fd_stream_init, \
312 redirect_fd_stream_fini, \
317 redirect_fd_stream_init(void *v
)
319 struct redirect_fd_stream
*s
= v
;
321 switch (s
->m_base
.m_type
) {
323 s
->m_fd
= open("stdout", O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
326 s
->m_fd
= open("stderr", O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
331 ATF_REQUIRE(s
->m_fd
!= -1);
333 s
->m_base
.m_sb_ptr
= &s
->m_base
.m_sb
;
334 RE(atf_process_stream_init_redirect_fd(&s
->m_base
.m_sb
, s
->m_fd
));
339 redirect_fd_stream_fini(void *v
)
341 struct redirect_fd_stream
*s
= v
;
343 ATF_REQUIRE(close(s
->m_fd
) != -1);
345 atf_process_stream_fini(&s
->m_base
.m_sb
);
347 check_file(s
->m_base
.m_type
);
350 struct redirect_path_stream
{
351 struct base_stream m_base
;
353 atf_fs_path_t m_path
;
355 #define REDIRECT_PATH_STREAM(type) \
356 { .m_base = BASE_STREAM(redirect_path_stream_init, \
358 redirect_path_stream_fini, \
363 redirect_path_stream_init(void *v
)
365 struct redirect_path_stream
*s
= v
;
367 switch (s
->m_base
.m_type
) {
369 RE(atf_fs_path_init_fmt(&s
->m_path
, "stdout"));
372 RE(atf_fs_path_init_fmt(&s
->m_path
, "stderr"));
378 s
->m_base
.m_sb_ptr
= &s
->m_base
.m_sb
;
379 RE(atf_process_stream_init_redirect_path(&s
->m_base
.m_sb
, &s
->m_path
));
384 redirect_path_stream_fini(void *v
)
386 struct redirect_path_stream
*s
= v
;
388 atf_process_stream_fini(&s
->m_base
.m_sb
);
390 atf_fs_path_fini(&s
->m_path
);
392 check_file(s
->m_base
.m_type
);
395 static void child_print(void *) ATF_DEFS_ATTRIBUTE_NORETURN
;
397 struct child_print_data
{
405 struct child_print_data
*cpd
= v
;
407 fprintf(stdout
, "stdout: %s\n", cpd
->m_msg
);
408 fprintf(stderr
, "stderr: %s\n", cpd
->m_msg
);
415 do_fork(const struct base_stream
*outfs
, void *out
,
416 const struct base_stream
*errfs
, void *err
)
418 atf_process_child_t child
;
419 atf_process_status_t status
;
420 struct child_print_data cpd
= { "msg" };
425 RE(atf_process_fork(&child
, child_print
, outfs
->m_sb_ptr
,
426 errfs
->m_sb_ptr
, &cpd
));
427 if (outfs
->process
!= NULL
)
428 outfs
->process(out
, &child
);
429 if (errfs
->process
!= NULL
)
430 errfs
->process(err
, &child
);
431 RE(atf_process_child_wait(&child
, &status
));
436 atf_process_status_fini(&status
);
439 /* ---------------------------------------------------------------------
440 * Test cases for the "stream" type.
441 * --------------------------------------------------------------------- */
443 ATF_TC(stream_init_capture
);
444 ATF_TC_HEAD(stream_init_capture
, tc
)
446 atf_tc_set_md_var(tc
, "descr", "Tests the "
447 "atf_process_stream_init_capture function");
449 ATF_TC_BODY(stream_init_capture
, tc
)
451 atf_process_stream_t sb
;
453 RE(atf_process_stream_init_capture(&sb
));
455 ATF_CHECK_EQ(atf_process_stream_type(&sb
),
456 atf_process_stream_type_capture
);
458 atf_process_stream_fini(&sb
);
461 ATF_TC(stream_init_connect
);
462 ATF_TC_HEAD(stream_init_connect
, tc
)
464 atf_tc_set_md_var(tc
, "descr", "Tests the "
465 "atf_process_stream_init_connect function");
467 ATF_TC_BODY(stream_init_connect
, tc
)
469 atf_process_stream_t sb
;
471 RE(atf_process_stream_init_connect(&sb
, 1, 2));
473 ATF_CHECK_EQ(atf_process_stream_type(&sb
),
474 atf_process_stream_type_connect
);
476 atf_process_stream_fini(&sb
);
479 ATF_TC(stream_init_inherit
);
480 ATF_TC_HEAD(stream_init_inherit
, tc
)
482 atf_tc_set_md_var(tc
, "descr", "Tests the "
483 "atf_process_stream_init_inherit function");
485 ATF_TC_BODY(stream_init_inherit
, tc
)
487 atf_process_stream_t sb
;
489 RE(atf_process_stream_init_inherit(&sb
));
491 ATF_CHECK_EQ(atf_process_stream_type(&sb
),
492 atf_process_stream_type_inherit
);
494 atf_process_stream_fini(&sb
);
497 ATF_TC(stream_init_redirect_fd
);
498 ATF_TC_HEAD(stream_init_redirect_fd
, tc
)
500 atf_tc_set_md_var(tc
, "descr", "Tests the "
501 "atf_process_stream_init_redirect_fd function");
503 ATF_TC_BODY(stream_init_redirect_fd
, tc
)
505 atf_process_stream_t sb
;
507 RE(atf_process_stream_init_redirect_fd(&sb
, 1));
509 ATF_CHECK_EQ(atf_process_stream_type(&sb
),
510 atf_process_stream_type_redirect_fd
);
512 atf_process_stream_fini(&sb
);
515 ATF_TC(stream_init_redirect_path
);
516 ATF_TC_HEAD(stream_init_redirect_path
, tc
)
518 atf_tc_set_md_var(tc
, "descr", "Tests the "
519 "atf_process_stream_init_redirect_path function");
521 ATF_TC_BODY(stream_init_redirect_path
, tc
)
523 atf_process_stream_t sb
;
526 RE(atf_fs_path_init_fmt(&path
, "foo"));
527 RE(atf_process_stream_init_redirect_path(&sb
, &path
));
529 ATF_CHECK_EQ(atf_process_stream_type(&sb
),
530 atf_process_stream_type_redirect_path
);
532 atf_process_stream_fini(&sb
);
533 atf_fs_path_fini(&path
);
536 /* ---------------------------------------------------------------------
537 * Test cases for the "status" type.
538 * --------------------------------------------------------------------- */
540 static void child_exit_success(void) ATF_DEFS_ATTRIBUTE_NORETURN
;
541 static void child_exit_failure(void) ATF_DEFS_ATTRIBUTE_NORETURN
;
542 static void child_sigkill(void) ATF_DEFS_ATTRIBUTE_NORETURN
;
543 static void child_sigquit(void) ATF_DEFS_ATTRIBUTE_NORETURN
;
544 static void child_sigterm(void) ATF_DEFS_ATTRIBUTE_NORETURN
;
547 child_exit_success(void)
553 child_exit_failure(void)
561 kill(getpid(), SIGKILL
);
568 kill(getpid(), SIGQUIT
);
575 kill(getpid(), SIGTERM
);
581 fork_and_wait_child(void (*child_func
)(void))
587 ATF_REQUIRE(pid
!= -1);
589 status
= 0; /* Silence compiler warnings */
593 ATF_REQUIRE(waitpid(pid
, &status
, 0) != 0);
599 ATF_TC(status_exited
);
600 ATF_TC_HEAD(status_exited
, tc
)
602 atf_tc_set_md_var(tc
, "descr", "Tests the status type for processes "
603 "that exit cleanly");
605 ATF_TC_BODY(status_exited
, tc
)
608 const int rawstatus
= fork_and_wait_child(child_exit_success
);
609 atf_process_status_t s
;
610 RE(atf_process_status_init(&s
, rawstatus
));
611 ATF_CHECK(atf_process_status_exited(&s
));
612 ATF_CHECK_EQ(atf_process_status_exitstatus(&s
), EXIT_SUCCESS
);
613 ATF_CHECK(!atf_process_status_signaled(&s
));
614 atf_process_status_fini(&s
);
618 const int rawstatus
= fork_and_wait_child(child_exit_failure
);
619 atf_process_status_t s
;
620 RE(atf_process_status_init(&s
, rawstatus
));
621 ATF_CHECK(atf_process_status_exited(&s
));
622 ATF_CHECK_EQ(atf_process_status_exitstatus(&s
), EXIT_FAILURE
);
623 ATF_CHECK(!atf_process_status_signaled(&s
));
624 atf_process_status_fini(&s
);
628 ATF_TC(status_signaled
);
629 ATF_TC_HEAD(status_signaled
, tc
)
631 atf_tc_set_md_var(tc
, "descr", "Tests the status type for processes "
632 "that end due to a signal");
634 ATF_TC_BODY(status_signaled
, tc
)
637 const int rawstatus
= fork_and_wait_child(child_sigkill
);
638 atf_process_status_t s
;
639 RE(atf_process_status_init(&s
, rawstatus
));
640 ATF_CHECK(!atf_process_status_exited(&s
));
641 ATF_CHECK(atf_process_status_signaled(&s
));
642 ATF_CHECK_EQ(atf_process_status_termsig(&s
), SIGKILL
);
643 ATF_CHECK(!atf_process_status_coredump(&s
));
644 atf_process_status_fini(&s
);
648 const int rawstatus
= fork_and_wait_child(child_sigterm
);
649 atf_process_status_t s
;
650 RE(atf_process_status_init(&s
, rawstatus
));
651 ATF_CHECK(!atf_process_status_exited(&s
));
652 ATF_CHECK(atf_process_status_signaled(&s
));
653 ATF_CHECK_EQ(atf_process_status_termsig(&s
), SIGTERM
);
654 ATF_CHECK(!atf_process_status_coredump(&s
));
655 atf_process_status_fini(&s
);
659 ATF_TC(status_coredump
);
660 ATF_TC_HEAD(status_coredump
, tc
)
662 atf_tc_set_md_var(tc
, "descr", "Tests the status type for processes "
665 ATF_TC_BODY(status_coredump
, tc
)
667 #if !defined(__minix)
669 rl
.rlim_cur
= RLIM_INFINITY
;
670 rl
.rlim_max
= RLIM_INFINITY
;
671 if (setrlimit(RLIMIT_CORE
, &rl
) == -1)
672 atf_tc_skip("Cannot unlimit the core file size; check limits "
676 const int rawstatus
= fork_and_wait_child(child_sigquit
);
677 atf_process_status_t s
;
678 RE(atf_process_status_init(&s
, rawstatus
));
679 ATF_CHECK(!atf_process_status_exited(&s
));
680 ATF_CHECK(atf_process_status_signaled(&s
));
681 ATF_CHECK_EQ(atf_process_status_termsig(&s
), SIGQUIT
);
682 ATF_CHECK(atf_process_status_coredump(&s
));
683 atf_process_status_fini(&s
);
686 /* ---------------------------------------------------------------------
687 * Test cases for the "child" type.
688 * --------------------------------------------------------------------- */
690 static void child_report_pid(void *) ATF_DEFS_ATTRIBUTE_NORETURN
;
694 child_report_pid(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
696 const pid_t pid
= getpid();
697 if (write(STDOUT_FILENO
, &pid
, sizeof(pid
)) != sizeof(pid
))
699 fprintf(stderr
, "Reporting %d to parent\n", (int)getpid());
704 ATF_TC_HEAD(child_pid
, tc
)
706 atf_tc_set_md_var(tc
, "descr", "Tests the correctness of the pid "
707 "stored in the child type");
709 ATF_TC_BODY(child_pid
, tc
)
711 atf_process_stream_t outsb
, errsb
;
712 atf_process_child_t child
;
713 atf_process_status_t status
;
716 RE(atf_process_stream_init_capture(&outsb
));
717 RE(atf_process_stream_init_inherit(&errsb
));
719 RE(atf_process_fork(&child
, child_report_pid
, &outsb
, &errsb
, NULL
));
720 ATF_CHECK_EQ(read(atf_process_child_stdout(&child
), &pid
, sizeof(pid
)),
722 printf("Expected PID: %d\n", (int)atf_process_child_pid(&child
));
723 printf("Actual PID: %d\n", (int)pid
);
724 ATF_CHECK_EQ(atf_process_child_pid(&child
), pid
);
726 RE(atf_process_child_wait(&child
, &status
));
727 atf_process_status_fini(&status
);
729 atf_process_stream_fini(&outsb
);
730 atf_process_stream_fini(&errsb
);
735 child_loop(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
743 nop_signal(int sig ATF_DEFS_ATTRIBUTE_UNUSED
)
749 child_spawn_loop_and_wait_eintr(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
751 atf_process_child_t child
;
752 atf_process_status_t status
;
753 struct sigaction sighup
, old_sighup
;
755 #define RE_ABORT(expr) \
757 atf_error_t _aux_err = expr; \
758 if (atf_is_error(_aux_err)) { \
759 atf_error_free(_aux_err); \
765 atf_process_stream_t outsb
, errsb
;
767 RE_ABORT(atf_process_stream_init_capture(&outsb
));
768 RE_ABORT(atf_process_stream_init_inherit(&errsb
));
769 RE_ABORT(atf_process_fork(&child
, child_loop
, &outsb
, &errsb
, NULL
));
770 atf_process_stream_fini(&outsb
);
771 atf_process_stream_fini(&errsb
);
774 sighup
.sa_handler
= nop_signal
;
775 sigemptyset(&sighup
.sa_mask
);
777 if (sigaction(SIGHUP
, &sighup
, &old_sighup
) == -1)
783 fprintf(stderr
, "Child entering wait(2)\n");
784 atf_error_t err
= atf_process_child_wait(&child
, &status
);
785 fprintf(stderr
, "Child's wait(2) terminated\n");
786 if (!atf_is_error(err
)) {
787 fprintf(stderr
, "wait completed successfully (not interrupted)\n");
790 if (!atf_error_is(err
, "libc")) {
791 fprintf(stderr
, "wait did not raise libc_error\n");
794 if (atf_libc_error_code(err
) != EINTR
) {
795 fprintf(stderr
, "libc_error is not EINTR\n");
800 sigaction(SIGHUP
, &old_sighup
, NULL
);
802 fprintf(stderr
, "Child is killing subchild\n");
803 kill(atf_process_child_pid(&child
), SIGTERM
);
805 RE_ABORT(atf_process_child_wait(&child
, &status
));
806 atf_process_status_fini(&status
);
813 ATF_TC(child_wait_eintr
);
814 ATF_TC_HEAD(child_wait_eintr
, tc
)
816 atf_tc_set_md_var(tc
, "descr", "Tests the interruption of the wait "
817 "method by an external signal, and the return of "
819 atf_tc_set_md_var(tc
, "timeout", "30");
821 ATF_TC_BODY(child_wait_eintr
, tc
)
823 atf_process_child_t child
;
824 atf_process_status_t status
;
827 atf_process_stream_t outsb
, errsb
;
829 RE(atf_process_stream_init_capture(&outsb
));
830 RE(atf_process_stream_init_inherit(&errsb
));
831 RE(atf_process_fork(&child
, child_spawn_loop_and_wait_eintr
,
832 &outsb
, &errsb
, NULL
));
833 atf_process_stream_fini(&outsb
);
834 atf_process_stream_fini(&errsb
);
838 /* Wait until the child process performs the wait call. This is
839 * racy, because the message we get from it is sent *before*
840 * doing the real system call... but I can't figure any other way
843 printf("Waiting for child to issue wait(2)\n");
844 ATF_REQUIRE(read(atf_process_child_stdout(&child
), buf
,
849 printf("Interrupting child's wait(2) call\n");
850 kill(atf_process_child_pid(&child
), SIGHUP
);
852 printf("Waiting for child's completion\n");
853 RE(atf_process_child_wait(&child
, &status
));
854 ATF_REQUIRE(atf_process_status_exited(&status
));
855 ATF_REQUIRE_EQ(atf_process_status_exitstatus(&status
), EXIT_SUCCESS
);
856 atf_process_status_fini(&status
);
859 /* ---------------------------------------------------------------------
860 * Tests cases for the free functions.
861 * --------------------------------------------------------------------- */
865 do_exec(const atf_tc_t
*tc
, const char *helper_name
, atf_process_status_t
*s
,
866 void (*prehook
)(void))
868 atf_fs_path_t process_helpers
;
871 get_process_helpers_path(tc
, true, &process_helpers
);
873 argv
[0] = atf_fs_path_cstring(&process_helpers
);
874 argv
[1] = helper_name
;
876 printf("Executing %s %s\n", argv
[0], argv
[1]);
878 RE(atf_process_exec_array(s
, &process_helpers
, argv
, NULL
, NULL
, prehook
));
879 atf_fs_path_fini(&process_helpers
);
884 check_line(int fd
, const char *exp
)
886 char *line
= atf_utils_readline(fd
);
887 ATF_CHECK(line
!= NULL
);
888 ATF_CHECK_STREQ_MSG(exp
, line
, "read: '%s', expected: '%s'", line
, exp
);
892 ATF_TC(exec_failure
);
893 ATF_TC_HEAD(exec_failure
, tc
)
895 atf_tc_set_md_var(tc
, "descr", "Tests execing a command");
897 ATF_TC_BODY(exec_failure
, tc
)
899 atf_process_status_t status
;
901 do_exec(tc
, "exit-failure", &status
, NULL
);
902 ATF_CHECK(atf_process_status_exited(&status
));
903 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), EXIT_FAILURE
);
904 atf_process_status_fini(&status
);
908 ATF_TC_HEAD(exec_list
, tc
)
910 atf_tc_set_md_var(tc
, "descr", "Tests execing a command");
912 ATF_TC_BODY(exec_list
, tc
)
914 atf_fs_path_t process_helpers
;
916 atf_process_status_t status
;
918 RE(atf_list_init(&argv
));
920 get_process_helpers_path(tc
, true, &process_helpers
);
921 atf_list_append(&argv
, strdup(atf_fs_path_cstring(&process_helpers
)), true);
922 atf_list_append(&argv
, strdup("echo"), true);
923 atf_list_append(&argv
, strdup("test-message"), true);
925 atf_fs_path_t outpath
;
926 atf_process_stream_t outsb
;
928 RE(atf_fs_path_init_fmt(&outpath
, "stdout"));
929 RE(atf_process_stream_init_redirect_path(&outsb
, &outpath
));
930 RE(atf_process_exec_list(&status
, &process_helpers
, &argv
, &outsb
,
932 atf_process_stream_fini(&outsb
);
933 atf_fs_path_fini(&outpath
);
935 atf_list_fini(&argv
);
937 ATF_CHECK(atf_process_status_exited(&status
));
938 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), EXIT_SUCCESS
);
941 int fd
= open("stdout", O_RDONLY
);
943 check_line(fd
, "test-message");
947 atf_process_status_fini(&status
);
948 atf_fs_path_fini(&process_helpers
);
957 ATF_TC(exec_prehook
);
958 ATF_TC_HEAD(exec_prehook
, tc
)
960 atf_tc_set_md_var(tc
, "descr", "Tests execing a command with a prehook");
962 ATF_TC_BODY(exec_prehook
, tc
)
964 atf_process_status_t status
;
966 do_exec(tc
, "exit-success", &status
, exit_early
);
967 ATF_CHECK(atf_process_status_exited(&status
));
968 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), 80);
969 atf_process_status_fini(&status
);
972 ATF_TC(exec_success
);
973 ATF_TC_HEAD(exec_success
, tc
)
975 atf_tc_set_md_var(tc
, "descr", "Tests execing a command");
977 ATF_TC_BODY(exec_success
, tc
)
979 atf_process_status_t status
;
981 do_exec(tc
, "exit-success", &status
, NULL
);
982 ATF_CHECK(atf_process_status_exited(&status
));
983 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), EXIT_SUCCESS
);
984 atf_process_status_fini(&status
);
987 static const int exit_v_null
= 1;
988 static const int exit_v_notnull
= 2;
992 child_cookie(void *v
)
997 exit(exit_v_notnull
);
1002 ATF_TC(fork_cookie
);
1003 ATF_TC_HEAD(fork_cookie
, tc
)
1005 atf_tc_set_md_var(tc
, "descr", "Tests forking a child, with "
1006 "a null and non-null data cookie");
1008 ATF_TC_BODY(fork_cookie
, tc
)
1010 atf_process_stream_t outsb
, errsb
;
1012 RE(atf_process_stream_init_inherit(&outsb
));
1013 RE(atf_process_stream_init_inherit(&errsb
));
1016 atf_process_child_t child
;
1017 atf_process_status_t status
;
1019 RE(atf_process_fork(&child
, child_cookie
, &outsb
, &errsb
, NULL
));
1020 RE(atf_process_child_wait(&child
, &status
));
1022 ATF_CHECK(atf_process_status_exited(&status
));
1023 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), exit_v_null
);
1025 atf_process_status_fini(&status
);
1029 atf_process_child_t child
;
1030 atf_process_status_t status
;
1033 RE(atf_process_fork(&child
, child_cookie
, &outsb
, &errsb
, &dummy_int
));
1034 RE(atf_process_child_wait(&child
, &status
));
1036 ATF_CHECK(atf_process_status_exited(&status
));
1037 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), exit_v_notnull
);
1039 atf_process_status_fini(&status
);
1042 atf_process_stream_fini(&errsb
);
1043 atf_process_stream_fini(&outsb
);
1046 #define TC_FORK_STREAMS(outlc, outuc, errlc, erruc) \
1047 ATF_TC(fork_out_ ## outlc ## _err_ ## errlc); \
1048 ATF_TC_HEAD(fork_out_ ## outlc ## _err_ ## errlc, tc) \
1050 atf_tc_set_md_var(tc, "descr", "Tests forking a child, with " \
1051 "stdout " #outlc " and stderr " #errlc); \
1053 ATF_TC_BODY(fork_out_ ## outlc ## _err_ ## errlc, tc) \
1055 struct outlc ## _stream out = outuc ## _STREAM(stdout_type); \
1056 struct errlc ## _stream err = erruc ## _STREAM(stderr_type); \
1057 do_fork(&out.m_base, &out, &err.m_base, &err); \
1060 TC_FORK_STREAMS(capture
, CAPTURE
, capture
, CAPTURE
);
1061 TC_FORK_STREAMS(capture
, CAPTURE
, connect
, CONNECT
);
1062 TC_FORK_STREAMS(capture
, CAPTURE
, default, DEFAULT
);
1063 TC_FORK_STREAMS(capture
, CAPTURE
, inherit
, INHERIT
);
1064 TC_FORK_STREAMS(capture
, CAPTURE
, redirect_fd
, REDIRECT_FD
);
1065 TC_FORK_STREAMS(capture
, CAPTURE
, redirect_path
, REDIRECT_PATH
);
1066 TC_FORK_STREAMS(connect
, CONNECT
, capture
, CAPTURE
);
1067 TC_FORK_STREAMS(connect
, CONNECT
, connect
, CONNECT
);
1068 TC_FORK_STREAMS(connect
, CONNECT
, default, DEFAULT
);
1069 TC_FORK_STREAMS(connect
, CONNECT
, inherit
, INHERIT
);
1070 TC_FORK_STREAMS(connect
, CONNECT
, redirect_fd
, REDIRECT_FD
);
1071 TC_FORK_STREAMS(connect
, CONNECT
, redirect_path
, REDIRECT_PATH
);
1072 TC_FORK_STREAMS(default, DEFAULT
, capture
, CAPTURE
);
1073 TC_FORK_STREAMS(default, DEFAULT
, connect
, CONNECT
);
1074 TC_FORK_STREAMS(default, DEFAULT
, default, DEFAULT
);
1075 TC_FORK_STREAMS(default, DEFAULT
, inherit
, INHERIT
);
1076 TC_FORK_STREAMS(default, DEFAULT
, redirect_fd
, REDIRECT_FD
);
1077 TC_FORK_STREAMS(default, DEFAULT
, redirect_path
, REDIRECT_PATH
);
1078 TC_FORK_STREAMS(inherit
, INHERIT
, capture
, CAPTURE
);
1079 TC_FORK_STREAMS(inherit
, INHERIT
, connect
, CONNECT
);
1080 TC_FORK_STREAMS(inherit
, INHERIT
, default, DEFAULT
);
1081 TC_FORK_STREAMS(inherit
, INHERIT
, inherit
, INHERIT
);
1082 TC_FORK_STREAMS(inherit
, INHERIT
, redirect_fd
, REDIRECT_FD
);
1083 TC_FORK_STREAMS(inherit
, INHERIT
, redirect_path
, REDIRECT_PATH
);
1084 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, capture
, CAPTURE
);
1085 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, connect
, CONNECT
);
1086 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, default, DEFAULT
);
1087 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, inherit
, INHERIT
);
1088 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, redirect_fd
, REDIRECT_FD
);
1089 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, redirect_path
, REDIRECT_PATH
);
1090 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, capture
, CAPTURE
);
1091 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, connect
, CONNECT
);
1092 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, default, DEFAULT
);
1093 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, inherit
, INHERIT
);
1094 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, redirect_fd
, REDIRECT_FD
);
1095 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, redirect_path
, REDIRECT_PATH
);
1097 #undef TC_FORK_STREAMS
1099 /* ---------------------------------------------------------------------
1101 * --------------------------------------------------------------------- */
1105 /* Add the tests for the "stream" type. */
1106 ATF_TP_ADD_TC(tp
, stream_init_capture
);
1107 ATF_TP_ADD_TC(tp
, stream_init_connect
);
1108 ATF_TP_ADD_TC(tp
, stream_init_inherit
);
1109 ATF_TP_ADD_TC(tp
, stream_init_redirect_fd
);
1110 ATF_TP_ADD_TC(tp
, stream_init_redirect_path
);
1112 /* Add the tests for the "status" type. */
1113 ATF_TP_ADD_TC(tp
, status_exited
);
1114 ATF_TP_ADD_TC(tp
, status_signaled
);
1115 ATF_TP_ADD_TC(tp
, status_coredump
);
1117 /* Add the tests for the "child" type. */
1118 ATF_TP_ADD_TC(tp
, child_pid
);
1119 ATF_TP_ADD_TC(tp
, child_wait_eintr
);
1121 /* Add the tests for the free functions. */
1122 ATF_TP_ADD_TC(tp
, exec_failure
);
1123 ATF_TP_ADD_TC(tp
, exec_list
);
1124 ATF_TP_ADD_TC(tp
, exec_prehook
);
1125 ATF_TP_ADD_TC(tp
, exec_success
);
1126 ATF_TP_ADD_TC(tp
, fork_cookie
);
1127 ATF_TP_ADD_TC(tp
, fork_out_capture_err_capture
);
1128 ATF_TP_ADD_TC(tp
, fork_out_capture_err_connect
);
1129 ATF_TP_ADD_TC(tp
, fork_out_capture_err_default
);
1130 ATF_TP_ADD_TC(tp
, fork_out_capture_err_inherit
);
1131 ATF_TP_ADD_TC(tp
, fork_out_capture_err_redirect_fd
);
1132 ATF_TP_ADD_TC(tp
, fork_out_capture_err_redirect_path
);
1133 ATF_TP_ADD_TC(tp
, fork_out_connect_err_capture
);
1134 ATF_TP_ADD_TC(tp
, fork_out_connect_err_connect
);
1135 ATF_TP_ADD_TC(tp
, fork_out_connect_err_default
);
1136 ATF_TP_ADD_TC(tp
, fork_out_connect_err_inherit
);
1137 ATF_TP_ADD_TC(tp
, fork_out_connect_err_redirect_fd
);
1138 ATF_TP_ADD_TC(tp
, fork_out_connect_err_redirect_path
);
1139 ATF_TP_ADD_TC(tp
, fork_out_default_err_capture
);
1140 ATF_TP_ADD_TC(tp
, fork_out_default_err_connect
);
1141 ATF_TP_ADD_TC(tp
, fork_out_default_err_default
);
1142 ATF_TP_ADD_TC(tp
, fork_out_default_err_inherit
);
1143 ATF_TP_ADD_TC(tp
, fork_out_default_err_redirect_fd
);
1144 ATF_TP_ADD_TC(tp
, fork_out_default_err_redirect_path
);
1145 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_capture
);
1146 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_connect
);
1147 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_default
);
1148 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_inherit
);
1149 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_redirect_fd
);
1150 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_redirect_path
);
1151 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_capture
);
1152 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_connect
);
1153 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_default
);
1154 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_inherit
);
1155 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_redirect_fd
);
1156 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_redirect_path
);
1157 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_capture
);
1158 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_connect
);
1159 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_default
);
1160 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_inherit
);
1161 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_redirect_fd
);
1162 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_redirect_path
);
1164 return atf_no_error();