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
)
668 rl
.rlim_cur
= RLIM_INFINITY
;
669 rl
.rlim_max
= RLIM_INFINITY
;
670 if (setrlimit(RLIMIT_CORE
, &rl
) == -1)
671 atf_tc_skip("Cannot unlimit the core file size; check limits "
674 const int rawstatus
= fork_and_wait_child(child_sigquit
);
675 atf_process_status_t s
;
676 RE(atf_process_status_init(&s
, rawstatus
));
677 ATF_CHECK(!atf_process_status_exited(&s
));
678 ATF_CHECK(atf_process_status_signaled(&s
));
679 ATF_CHECK_EQ(atf_process_status_termsig(&s
), SIGQUIT
);
680 ATF_CHECK(atf_process_status_coredump(&s
));
681 atf_process_status_fini(&s
);
684 /* ---------------------------------------------------------------------
685 * Test cases for the "child" type.
686 * --------------------------------------------------------------------- */
688 static void child_report_pid(void *) ATF_DEFS_ATTRIBUTE_NORETURN
;
692 child_report_pid(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
694 const pid_t pid
= getpid();
695 if (write(STDOUT_FILENO
, &pid
, sizeof(pid
)) != sizeof(pid
))
697 fprintf(stderr
, "Reporting %d to parent\n", (int)getpid());
702 ATF_TC_HEAD(child_pid
, tc
)
704 atf_tc_set_md_var(tc
, "descr", "Tests the correctness of the pid "
705 "stored in the child type");
707 ATF_TC_BODY(child_pid
, tc
)
709 atf_process_stream_t outsb
, errsb
;
710 atf_process_child_t child
;
711 atf_process_status_t status
;
714 RE(atf_process_stream_init_capture(&outsb
));
715 RE(atf_process_stream_init_inherit(&errsb
));
717 RE(atf_process_fork(&child
, child_report_pid
, &outsb
, &errsb
, NULL
));
718 ATF_CHECK_EQ(read(atf_process_child_stdout(&child
), &pid
, sizeof(pid
)),
720 printf("Expected PID: %d\n", (int)atf_process_child_pid(&child
));
721 printf("Actual PID: %d\n", (int)pid
);
722 ATF_CHECK_EQ(atf_process_child_pid(&child
), pid
);
724 RE(atf_process_child_wait(&child
, &status
));
725 atf_process_status_fini(&status
);
727 atf_process_stream_fini(&outsb
);
728 atf_process_stream_fini(&errsb
);
733 child_loop(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
741 nop_signal(int sig ATF_DEFS_ATTRIBUTE_UNUSED
)
747 child_spawn_loop_and_wait_eintr(void *v ATF_DEFS_ATTRIBUTE_UNUSED
)
749 atf_process_child_t child
;
750 atf_process_status_t status
;
751 struct sigaction sighup
, old_sighup
;
753 #define RE_ABORT(expr) \
755 atf_error_t _aux_err = expr; \
756 if (atf_is_error(_aux_err)) { \
757 atf_error_free(_aux_err); \
763 atf_process_stream_t outsb
, errsb
;
765 RE_ABORT(atf_process_stream_init_capture(&outsb
));
766 RE_ABORT(atf_process_stream_init_inherit(&errsb
));
767 RE_ABORT(atf_process_fork(&child
, child_loop
, &outsb
, &errsb
, NULL
));
768 atf_process_stream_fini(&outsb
);
769 atf_process_stream_fini(&errsb
);
772 sighup
.sa_handler
= nop_signal
;
773 sigemptyset(&sighup
.sa_mask
);
775 if (sigaction(SIGHUP
, &sighup
, &old_sighup
) == -1)
781 fprintf(stderr
, "Child entering wait(2)\n");
782 atf_error_t err
= atf_process_child_wait(&child
, &status
);
783 fprintf(stderr
, "Child's wait(2) terminated\n");
784 if (!atf_is_error(err
)) {
785 fprintf(stderr
, "wait completed successfully (not interrupted)\n");
788 if (!atf_error_is(err
, "libc")) {
789 fprintf(stderr
, "wait did not raise libc_error\n");
792 if (atf_libc_error_code(err
) != EINTR
) {
793 fprintf(stderr
, "libc_error is not EINTR\n");
798 sigaction(SIGHUP
, &old_sighup
, NULL
);
800 fprintf(stderr
, "Child is killing subchild\n");
801 kill(atf_process_child_pid(&child
), SIGTERM
);
803 RE_ABORT(atf_process_child_wait(&child
, &status
));
804 atf_process_status_fini(&status
);
811 ATF_TC(child_wait_eintr
);
812 ATF_TC_HEAD(child_wait_eintr
, tc
)
814 atf_tc_set_md_var(tc
, "descr", "Tests the interruption of the wait "
815 "method by an external signal, and the return of "
817 atf_tc_set_md_var(tc
, "timeout", "30");
819 ATF_TC_BODY(child_wait_eintr
, tc
)
821 atf_process_child_t child
;
822 atf_process_status_t status
;
825 atf_process_stream_t outsb
, errsb
;
827 RE(atf_process_stream_init_capture(&outsb
));
828 RE(atf_process_stream_init_inherit(&errsb
));
829 RE(atf_process_fork(&child
, child_spawn_loop_and_wait_eintr
,
830 &outsb
, &errsb
, NULL
));
831 atf_process_stream_fini(&outsb
);
832 atf_process_stream_fini(&errsb
);
836 /* Wait until the child process performs the wait call. This is
837 * racy, because the message we get from it is sent *before*
838 * doing the real system call... but I can't figure any other way
841 printf("Waiting for child to issue wait(2)\n");
842 ATF_REQUIRE(read(atf_process_child_stdout(&child
), buf
,
847 printf("Interrupting child's wait(2) call\n");
848 kill(atf_process_child_pid(&child
), SIGHUP
);
850 printf("Waiting for child's completion\n");
851 RE(atf_process_child_wait(&child
, &status
));
852 ATF_REQUIRE(atf_process_status_exited(&status
));
853 ATF_REQUIRE_EQ(atf_process_status_exitstatus(&status
), EXIT_SUCCESS
);
854 atf_process_status_fini(&status
);
857 /* ---------------------------------------------------------------------
858 * Tests cases for the free functions.
859 * --------------------------------------------------------------------- */
863 do_exec(const atf_tc_t
*tc
, const char *helper_name
, atf_process_status_t
*s
,
864 void (*prehook
)(void))
866 atf_fs_path_t process_helpers
;
869 get_process_helpers_path(tc
, true, &process_helpers
);
871 argv
[0] = atf_fs_path_cstring(&process_helpers
);
872 argv
[1] = helper_name
;
874 printf("Executing %s %s\n", argv
[0], argv
[1]);
876 RE(atf_process_exec_array(s
, &process_helpers
, argv
, NULL
, NULL
, prehook
));
877 atf_fs_path_fini(&process_helpers
);
882 check_line(int fd
, const char *exp
)
884 char *line
= atf_utils_readline(fd
);
885 ATF_CHECK(line
!= NULL
);
886 ATF_CHECK_STREQ_MSG(exp
, line
, "read: '%s', expected: '%s'", line
, exp
);
890 ATF_TC(exec_failure
);
891 ATF_TC_HEAD(exec_failure
, tc
)
893 atf_tc_set_md_var(tc
, "descr", "Tests execing a command");
895 ATF_TC_BODY(exec_failure
, tc
)
897 atf_process_status_t status
;
899 do_exec(tc
, "exit-failure", &status
, NULL
);
900 ATF_CHECK(atf_process_status_exited(&status
));
901 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), EXIT_FAILURE
);
902 atf_process_status_fini(&status
);
906 ATF_TC_HEAD(exec_list
, tc
)
908 atf_tc_set_md_var(tc
, "descr", "Tests execing a command");
910 ATF_TC_BODY(exec_list
, tc
)
912 atf_fs_path_t process_helpers
;
914 atf_process_status_t status
;
916 RE(atf_list_init(&argv
));
918 get_process_helpers_path(tc
, true, &process_helpers
);
919 atf_list_append(&argv
, strdup(atf_fs_path_cstring(&process_helpers
)), true);
920 atf_list_append(&argv
, strdup("echo"), true);
921 atf_list_append(&argv
, strdup("test-message"), true);
923 atf_fs_path_t outpath
;
924 atf_process_stream_t outsb
;
926 RE(atf_fs_path_init_fmt(&outpath
, "stdout"));
927 RE(atf_process_stream_init_redirect_path(&outsb
, &outpath
));
928 RE(atf_process_exec_list(&status
, &process_helpers
, &argv
, &outsb
,
930 atf_process_stream_fini(&outsb
);
931 atf_fs_path_fini(&outpath
);
933 atf_list_fini(&argv
);
935 ATF_CHECK(atf_process_status_exited(&status
));
936 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), EXIT_SUCCESS
);
939 int fd
= open("stdout", O_RDONLY
);
941 check_line(fd
, "test-message");
945 atf_process_status_fini(&status
);
946 atf_fs_path_fini(&process_helpers
);
955 ATF_TC(exec_prehook
);
956 ATF_TC_HEAD(exec_prehook
, tc
)
958 atf_tc_set_md_var(tc
, "descr", "Tests execing a command with a prehook");
960 ATF_TC_BODY(exec_prehook
, tc
)
962 atf_process_status_t status
;
964 do_exec(tc
, "exit-success", &status
, exit_early
);
965 ATF_CHECK(atf_process_status_exited(&status
));
966 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), 80);
967 atf_process_status_fini(&status
);
970 ATF_TC(exec_success
);
971 ATF_TC_HEAD(exec_success
, tc
)
973 atf_tc_set_md_var(tc
, "descr", "Tests execing a command");
975 ATF_TC_BODY(exec_success
, tc
)
977 atf_process_status_t status
;
979 do_exec(tc
, "exit-success", &status
, NULL
);
980 ATF_CHECK(atf_process_status_exited(&status
));
981 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), EXIT_SUCCESS
);
982 atf_process_status_fini(&status
);
985 static const int exit_v_null
= 1;
986 static const int exit_v_notnull
= 2;
990 child_cookie(void *v
)
995 exit(exit_v_notnull
);
1000 ATF_TC(fork_cookie
);
1001 ATF_TC_HEAD(fork_cookie
, tc
)
1003 atf_tc_set_md_var(tc
, "descr", "Tests forking a child, with "
1004 "a null and non-null data cookie");
1006 ATF_TC_BODY(fork_cookie
, tc
)
1008 atf_process_stream_t outsb
, errsb
;
1010 RE(atf_process_stream_init_inherit(&outsb
));
1011 RE(atf_process_stream_init_inherit(&errsb
));
1014 atf_process_child_t child
;
1015 atf_process_status_t status
;
1017 RE(atf_process_fork(&child
, child_cookie
, &outsb
, &errsb
, NULL
));
1018 RE(atf_process_child_wait(&child
, &status
));
1020 ATF_CHECK(atf_process_status_exited(&status
));
1021 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), exit_v_null
);
1023 atf_process_status_fini(&status
);
1027 atf_process_child_t child
;
1028 atf_process_status_t status
;
1031 RE(atf_process_fork(&child
, child_cookie
, &outsb
, &errsb
, &dummy_int
));
1032 RE(atf_process_child_wait(&child
, &status
));
1034 ATF_CHECK(atf_process_status_exited(&status
));
1035 ATF_CHECK_EQ(atf_process_status_exitstatus(&status
), exit_v_notnull
);
1037 atf_process_status_fini(&status
);
1040 atf_process_stream_fini(&errsb
);
1041 atf_process_stream_fini(&outsb
);
1044 #define TC_FORK_STREAMS(outlc, outuc, errlc, erruc) \
1045 ATF_TC(fork_out_ ## outlc ## _err_ ## errlc); \
1046 ATF_TC_HEAD(fork_out_ ## outlc ## _err_ ## errlc, tc) \
1048 atf_tc_set_md_var(tc, "descr", "Tests forking a child, with " \
1049 "stdout " #outlc " and stderr " #errlc); \
1051 ATF_TC_BODY(fork_out_ ## outlc ## _err_ ## errlc, tc) \
1053 struct outlc ## _stream out = outuc ## _STREAM(stdout_type); \
1054 struct errlc ## _stream err = erruc ## _STREAM(stderr_type); \
1055 do_fork(&out.m_base, &out, &err.m_base, &err); \
1058 TC_FORK_STREAMS(capture
, CAPTURE
, capture
, CAPTURE
);
1059 TC_FORK_STREAMS(capture
, CAPTURE
, connect
, CONNECT
);
1060 TC_FORK_STREAMS(capture
, CAPTURE
, default, DEFAULT
);
1061 TC_FORK_STREAMS(capture
, CAPTURE
, inherit
, INHERIT
);
1062 TC_FORK_STREAMS(capture
, CAPTURE
, redirect_fd
, REDIRECT_FD
);
1063 TC_FORK_STREAMS(capture
, CAPTURE
, redirect_path
, REDIRECT_PATH
);
1064 TC_FORK_STREAMS(connect
, CONNECT
, capture
, CAPTURE
);
1065 TC_FORK_STREAMS(connect
, CONNECT
, connect
, CONNECT
);
1066 TC_FORK_STREAMS(connect
, CONNECT
, default, DEFAULT
);
1067 TC_FORK_STREAMS(connect
, CONNECT
, inherit
, INHERIT
);
1068 TC_FORK_STREAMS(connect
, CONNECT
, redirect_fd
, REDIRECT_FD
);
1069 TC_FORK_STREAMS(connect
, CONNECT
, redirect_path
, REDIRECT_PATH
);
1070 TC_FORK_STREAMS(default, DEFAULT
, capture
, CAPTURE
);
1071 TC_FORK_STREAMS(default, DEFAULT
, connect
, CONNECT
);
1072 TC_FORK_STREAMS(default, DEFAULT
, default, DEFAULT
);
1073 TC_FORK_STREAMS(default, DEFAULT
, inherit
, INHERIT
);
1074 TC_FORK_STREAMS(default, DEFAULT
, redirect_fd
, REDIRECT_FD
);
1075 TC_FORK_STREAMS(default, DEFAULT
, redirect_path
, REDIRECT_PATH
);
1076 TC_FORK_STREAMS(inherit
, INHERIT
, capture
, CAPTURE
);
1077 TC_FORK_STREAMS(inherit
, INHERIT
, connect
, CONNECT
);
1078 TC_FORK_STREAMS(inherit
, INHERIT
, default, DEFAULT
);
1079 TC_FORK_STREAMS(inherit
, INHERIT
, inherit
, INHERIT
);
1080 TC_FORK_STREAMS(inherit
, INHERIT
, redirect_fd
, REDIRECT_FD
);
1081 TC_FORK_STREAMS(inherit
, INHERIT
, redirect_path
, REDIRECT_PATH
);
1082 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, capture
, CAPTURE
);
1083 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, connect
, CONNECT
);
1084 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, default, DEFAULT
);
1085 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, inherit
, INHERIT
);
1086 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, redirect_fd
, REDIRECT_FD
);
1087 TC_FORK_STREAMS(redirect_fd
, REDIRECT_FD
, redirect_path
, REDIRECT_PATH
);
1088 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, capture
, CAPTURE
);
1089 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, connect
, CONNECT
);
1090 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, default, DEFAULT
);
1091 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, inherit
, INHERIT
);
1092 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, redirect_fd
, REDIRECT_FD
);
1093 TC_FORK_STREAMS(redirect_path
, REDIRECT_PATH
, redirect_path
, REDIRECT_PATH
);
1095 #undef TC_FORK_STREAMS
1097 /* ---------------------------------------------------------------------
1099 * --------------------------------------------------------------------- */
1103 /* Add the tests for the "stream" type. */
1104 ATF_TP_ADD_TC(tp
, stream_init_capture
);
1105 ATF_TP_ADD_TC(tp
, stream_init_connect
);
1106 ATF_TP_ADD_TC(tp
, stream_init_inherit
);
1107 ATF_TP_ADD_TC(tp
, stream_init_redirect_fd
);
1108 ATF_TP_ADD_TC(tp
, stream_init_redirect_path
);
1110 /* Add the tests for the "status" type. */
1111 ATF_TP_ADD_TC(tp
, status_exited
);
1112 ATF_TP_ADD_TC(tp
, status_signaled
);
1113 ATF_TP_ADD_TC(tp
, status_coredump
);
1115 /* Add the tests for the "child" type. */
1116 ATF_TP_ADD_TC(tp
, child_pid
);
1117 ATF_TP_ADD_TC(tp
, child_wait_eintr
);
1119 /* Add the tests for the free functions. */
1120 ATF_TP_ADD_TC(tp
, exec_failure
);
1121 ATF_TP_ADD_TC(tp
, exec_list
);
1122 ATF_TP_ADD_TC(tp
, exec_prehook
);
1123 ATF_TP_ADD_TC(tp
, exec_success
);
1124 ATF_TP_ADD_TC(tp
, fork_cookie
);
1125 ATF_TP_ADD_TC(tp
, fork_out_capture_err_capture
);
1126 ATF_TP_ADD_TC(tp
, fork_out_capture_err_connect
);
1127 ATF_TP_ADD_TC(tp
, fork_out_capture_err_default
);
1128 ATF_TP_ADD_TC(tp
, fork_out_capture_err_inherit
);
1129 ATF_TP_ADD_TC(tp
, fork_out_capture_err_redirect_fd
);
1130 ATF_TP_ADD_TC(tp
, fork_out_capture_err_redirect_path
);
1131 ATF_TP_ADD_TC(tp
, fork_out_connect_err_capture
);
1132 ATF_TP_ADD_TC(tp
, fork_out_connect_err_connect
);
1133 ATF_TP_ADD_TC(tp
, fork_out_connect_err_default
);
1134 ATF_TP_ADD_TC(tp
, fork_out_connect_err_inherit
);
1135 ATF_TP_ADD_TC(tp
, fork_out_connect_err_redirect_fd
);
1136 ATF_TP_ADD_TC(tp
, fork_out_connect_err_redirect_path
);
1137 ATF_TP_ADD_TC(tp
, fork_out_default_err_capture
);
1138 ATF_TP_ADD_TC(tp
, fork_out_default_err_connect
);
1139 ATF_TP_ADD_TC(tp
, fork_out_default_err_default
);
1140 ATF_TP_ADD_TC(tp
, fork_out_default_err_inherit
);
1141 ATF_TP_ADD_TC(tp
, fork_out_default_err_redirect_fd
);
1142 ATF_TP_ADD_TC(tp
, fork_out_default_err_redirect_path
);
1143 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_capture
);
1144 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_connect
);
1145 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_default
);
1146 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_inherit
);
1147 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_redirect_fd
);
1148 ATF_TP_ADD_TC(tp
, fork_out_inherit_err_redirect_path
);
1149 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_capture
);
1150 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_connect
);
1151 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_default
);
1152 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_inherit
);
1153 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_redirect_fd
);
1154 ATF_TP_ADD_TC(tp
, fork_out_redirect_fd_err_redirect_path
);
1155 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_capture
);
1156 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_connect
);
1157 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_default
);
1158 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_inherit
);
1159 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_redirect_fd
);
1160 ATF_TP_ADD_TC(tp
, fork_out_redirect_path_err_redirect_path
);
1162 return atf_no_error();