1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/detail/process.h"
28 #include <sys/types.h>
30 #include <sys/resource.h>
43 #include "atf-c/defs.h"
44 #include "atf-c/detail/sanity.h"
45 #include "atf-c/detail/test_helpers.h"
47 atf_error_t atf_process_status_init(atf_process_status_t *, int);
49 /* ---------------------------------------------------------------------
50 * Auxiliary functions for testing of 'atf_process_fork'.
51 * --------------------------------------------------------------------- */
54 * Testing of atf_process_fork is quite messy. We want to be able to test
55 * all the possible combinations of stdout and stderr behavior to ensure
56 * that the streams are manipulated correctly.
58 * To do this, the do_fork function is a wrapper for atf_process_fork that
59 * issues stream-specific hooks before fork, while the child is running and
60 * after the child terminates. We then provide test cases that just call
61 * do_fork with different hooks.
63 * The hooks are described by base_stream, and we then have one *_stream
64 * type for ever possible stream behavior.
67 enum out_type { stdout_type, stderr_type };
71 void (*process)(void *, atf_process_child_t *);
74 /* m_sb is initialized by subclasses that need it, but all consumers
75 * must use m_sb_ptr, which may or may not point to m_sb. This allows
76 * us to test the interface with a NULL value, which triggers a
77 * default behavior. */
78 atf_process_stream_t m_sb;
79 atf_process_stream_t *m_sb_ptr;
82 #define BASE_STREAM(ihook, phook, fhook, type) \
90 check_file(const enum out_type type)
94 ATF_CHECK(atf_utils_grep_file("stdout: msg", "stdout"));
95 ATF_CHECK(!atf_utils_grep_file("stderr: msg", "stdout"));
98 ATF_CHECK(atf_utils_grep_file("stderr: msg", "stderr"));
99 ATF_CHECK(!atf_utils_grep_file("stdout: msg", "stderr"));
106 struct capture_stream {
107 struct base_stream m_base;
111 #define CAPTURE_STREAM(type) \
112 { .m_base = BASE_STREAM(capture_stream_init, \
113 capture_stream_process, \
114 capture_stream_fini, \
119 capture_stream_init(void *v)
121 struct capture_stream *s = v;
123 s->m_base.m_sb_ptr = &s->m_base.m_sb;
124 RE(atf_process_stream_init_capture(&s->m_base.m_sb));
130 capture_stream_process(void *v, atf_process_child_t *c)
132 struct capture_stream *s = v;
134 switch (s->m_base.m_type) {
136 s->m_msg = atf_utils_readline(atf_process_child_stdout(c));
139 s->m_msg = atf_utils_readline(atf_process_child_stderr(c));
148 capture_stream_fini(void *v)
150 struct capture_stream *s = v;
152 switch (s->m_base.m_type) {
154 ATF_CHECK(atf_utils_grep_string("stdout: msg", s->m_msg));
155 ATF_CHECK(!atf_utils_grep_string("stderr: msg", s->m_msg));
158 ATF_CHECK(!atf_utils_grep_string("stdout: msg", s->m_msg));
159 ATF_CHECK(atf_utils_grep_string("stderr: msg", s->m_msg));
166 atf_process_stream_fini(&s->m_base.m_sb);
169 struct connect_stream {
170 struct base_stream m_base;
174 #define CONNECT_STREAM(type) \
175 { .m_base = BASE_STREAM(connect_stream_init, \
177 connect_stream_fini, \
182 connect_stream_init(void *v)
184 struct connect_stream *s = v;
187 switch (s->m_base.m_type) {
189 src_fd = STDOUT_FILENO;
190 s->m_fd = open("stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644);
193 src_fd = STDERR_FILENO;
194 s->m_fd = open("stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
200 ATF_REQUIRE(s->m_fd != -1);
202 s->m_base.m_sb_ptr = &s->m_base.m_sb;
203 RE(atf_process_stream_init_connect(&s->m_base.m_sb, src_fd, s->m_fd));
208 connect_stream_fini(void *v)
210 struct connect_stream *s = v;
212 ATF_REQUIRE(close(s->m_fd) != -1);
214 atf_process_stream_fini(&s->m_base.m_sb);
216 check_file(s->m_base.m_type);
219 struct inherit_stream {
220 struct base_stream m_base;
225 #define INHERIT_STREAM(type) \
226 { .m_base = BASE_STREAM(inherit_stream_init, \
228 inherit_stream_fini, \
233 inherit_stream_init(void *v)
235 struct inherit_stream *s = v;
238 s->m_base.m_sb_ptr = &s->m_base.m_sb;
239 RE(atf_process_stream_init_inherit(&s->m_base.m_sb));
241 switch (s->m_base.m_type) {
243 s->m_fd = STDOUT_FILENO;
247 s->m_fd = STDERR_FILENO;
255 s->m_old_fd = dup(s->m_fd);
256 ATF_REQUIRE(s->m_old_fd != -1);
257 ATF_REQUIRE(close(s->m_fd) != -1);
258 ATF_REQUIRE_EQ(open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644),
264 inherit_stream_fini(void *v)
266 struct inherit_stream *s = v;
268 ATF_REQUIRE(dup2(s->m_old_fd, s->m_fd) != -1);
269 ATF_REQUIRE(close(s->m_old_fd) != -1);
271 atf_process_stream_fini(&s->m_base.m_sb);
273 check_file(s->m_base.m_type);
276 #define default_stream inherit_stream
277 #define DEFAULT_STREAM(type) \
278 { .m_base = BASE_STREAM(default_stream_init, \
280 default_stream_fini, \
285 default_stream_init(void *v)
287 struct inherit_stream *s = v;
289 inherit_stream_init(v);
290 s->m_base.m_sb_ptr = NULL;
295 default_stream_fini(void *v)
297 inherit_stream_fini(v);
300 struct redirect_fd_stream {
301 struct base_stream m_base;
305 #define REDIRECT_FD_STREAM(type) \
306 { .m_base = BASE_STREAM(redirect_fd_stream_init, \
308 redirect_fd_stream_fini, \
313 redirect_fd_stream_init(void *v)
315 struct redirect_fd_stream *s = v;
317 switch (s->m_base.m_type) {
319 s->m_fd = open("stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644);
322 s->m_fd = open("stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
327 ATF_REQUIRE(s->m_fd != -1);
329 s->m_base.m_sb_ptr = &s->m_base.m_sb;
330 RE(atf_process_stream_init_redirect_fd(&s->m_base.m_sb, s->m_fd));
335 redirect_fd_stream_fini(void *v)
337 struct redirect_fd_stream *s = v;
339 ATF_REQUIRE(close(s->m_fd) != -1);
341 atf_process_stream_fini(&s->m_base.m_sb);
343 check_file(s->m_base.m_type);
346 struct redirect_path_stream {
347 struct base_stream m_base;
349 atf_fs_path_t m_path;
351 #define REDIRECT_PATH_STREAM(type) \
352 { .m_base = BASE_STREAM(redirect_path_stream_init, \
354 redirect_path_stream_fini, \
359 redirect_path_stream_init(void *v)
361 struct redirect_path_stream *s = v;
363 switch (s->m_base.m_type) {
365 RE(atf_fs_path_init_fmt(&s->m_path, "stdout"));
368 RE(atf_fs_path_init_fmt(&s->m_path, "stderr"));
374 s->m_base.m_sb_ptr = &s->m_base.m_sb;
375 RE(atf_process_stream_init_redirect_path(&s->m_base.m_sb, &s->m_path));
380 redirect_path_stream_fini(void *v)
382 struct redirect_path_stream *s = v;
384 atf_process_stream_fini(&s->m_base.m_sb);
386 atf_fs_path_fini(&s->m_path);
388 check_file(s->m_base.m_type);
391 static void child_print(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
393 struct child_print_data {
401 struct child_print_data *cpd = v;
403 fprintf(stdout, "stdout: %s\n", cpd->m_msg);
404 fprintf(stderr, "stderr: %s\n", cpd->m_msg);
411 do_fork(const struct base_stream *outfs, void *out,
412 const struct base_stream *errfs, void *err)
414 atf_process_child_t child;
415 atf_process_status_t status;
416 struct child_print_data cpd = { "msg" };
421 RE(atf_process_fork(&child, child_print, outfs->m_sb_ptr,
422 errfs->m_sb_ptr, &cpd));
423 if (outfs->process != NULL)
424 outfs->process(out, &child);
425 if (errfs->process != NULL)
426 errfs->process(err, &child);
427 RE(atf_process_child_wait(&child, &status));
432 atf_process_status_fini(&status);
435 /* ---------------------------------------------------------------------
436 * Test cases for the "stream" type.
437 * --------------------------------------------------------------------- */
439 ATF_TC(stream_init_capture);
440 ATF_TC_HEAD(stream_init_capture, tc)
442 atf_tc_set_md_var(tc, "descr", "Tests the "
443 "atf_process_stream_init_capture function");
445 ATF_TC_BODY(stream_init_capture, tc)
447 atf_process_stream_t sb;
449 RE(atf_process_stream_init_capture(&sb));
451 ATF_CHECK_EQ(atf_process_stream_type(&sb),
452 atf_process_stream_type_capture);
454 atf_process_stream_fini(&sb);
457 ATF_TC(stream_init_connect);
458 ATF_TC_HEAD(stream_init_connect, tc)
460 atf_tc_set_md_var(tc, "descr", "Tests the "
461 "atf_process_stream_init_connect function");
463 ATF_TC_BODY(stream_init_connect, tc)
465 atf_process_stream_t sb;
467 RE(atf_process_stream_init_connect(&sb, 1, 2));
469 ATF_CHECK_EQ(atf_process_stream_type(&sb),
470 atf_process_stream_type_connect);
472 atf_process_stream_fini(&sb);
475 ATF_TC(stream_init_inherit);
476 ATF_TC_HEAD(stream_init_inherit, tc)
478 atf_tc_set_md_var(tc, "descr", "Tests the "
479 "atf_process_stream_init_inherit function");
481 ATF_TC_BODY(stream_init_inherit, tc)
483 atf_process_stream_t sb;
485 RE(atf_process_stream_init_inherit(&sb));
487 ATF_CHECK_EQ(atf_process_stream_type(&sb),
488 atf_process_stream_type_inherit);
490 atf_process_stream_fini(&sb);
493 ATF_TC(stream_init_redirect_fd);
494 ATF_TC_HEAD(stream_init_redirect_fd, tc)
496 atf_tc_set_md_var(tc, "descr", "Tests the "
497 "atf_process_stream_init_redirect_fd function");
499 ATF_TC_BODY(stream_init_redirect_fd, tc)
501 atf_process_stream_t sb;
503 RE(atf_process_stream_init_redirect_fd(&sb, 1));
505 ATF_CHECK_EQ(atf_process_stream_type(&sb),
506 atf_process_stream_type_redirect_fd);
508 atf_process_stream_fini(&sb);
511 ATF_TC(stream_init_redirect_path);
512 ATF_TC_HEAD(stream_init_redirect_path, tc)
514 atf_tc_set_md_var(tc, "descr", "Tests the "
515 "atf_process_stream_init_redirect_path function");
517 ATF_TC_BODY(stream_init_redirect_path, tc)
519 atf_process_stream_t sb;
522 RE(atf_fs_path_init_fmt(&path, "foo"));
523 RE(atf_process_stream_init_redirect_path(&sb, &path));
525 ATF_CHECK_EQ(atf_process_stream_type(&sb),
526 atf_process_stream_type_redirect_path);
528 atf_process_stream_fini(&sb);
529 atf_fs_path_fini(&path);
532 /* ---------------------------------------------------------------------
533 * Test cases for the "status" type.
534 * --------------------------------------------------------------------- */
536 static void child_exit_success(void) ATF_DEFS_ATTRIBUTE_NORETURN;
537 static void child_exit_failure(void) ATF_DEFS_ATTRIBUTE_NORETURN;
538 static void child_sigkill(void) ATF_DEFS_ATTRIBUTE_NORETURN;
539 static void child_sigquit(void) ATF_DEFS_ATTRIBUTE_NORETURN;
540 static void child_sigterm(void) ATF_DEFS_ATTRIBUTE_NORETURN;
543 child_exit_success(void)
549 child_exit_failure(void)
557 kill(getpid(), SIGKILL);
564 kill(getpid(), SIGQUIT);
571 kill(getpid(), SIGTERM);
577 fork_and_wait_child(void (*child_func)(void))
583 ATF_REQUIRE(pid != -1);
585 status = 0; /* Silence compiler warnings */
589 ATF_REQUIRE(waitpid(pid, &status, 0) != 0);
595 ATF_TC(status_exited);
596 ATF_TC_HEAD(status_exited, tc)
598 atf_tc_set_md_var(tc, "descr", "Tests the status type for processes "
599 "that exit cleanly");
601 ATF_TC_BODY(status_exited, tc)
604 const int rawstatus = fork_and_wait_child(child_exit_success);
605 atf_process_status_t s;
606 RE(atf_process_status_init(&s, rawstatus));
607 ATF_CHECK(atf_process_status_exited(&s));
608 ATF_CHECK_EQ(atf_process_status_exitstatus(&s), EXIT_SUCCESS);
609 ATF_CHECK(!atf_process_status_signaled(&s));
610 atf_process_status_fini(&s);
614 const int rawstatus = fork_and_wait_child(child_exit_failure);
615 atf_process_status_t s;
616 RE(atf_process_status_init(&s, rawstatus));
617 ATF_CHECK(atf_process_status_exited(&s));
618 ATF_CHECK_EQ(atf_process_status_exitstatus(&s), EXIT_FAILURE);
619 ATF_CHECK(!atf_process_status_signaled(&s));
620 atf_process_status_fini(&s);
624 ATF_TC(status_signaled);
625 ATF_TC_HEAD(status_signaled, tc)
627 atf_tc_set_md_var(tc, "descr", "Tests the status type for processes "
628 "that end due to a signal");
630 ATF_TC_BODY(status_signaled, tc)
633 const int rawstatus = fork_and_wait_child(child_sigkill);
634 atf_process_status_t s;
635 RE(atf_process_status_init(&s, rawstatus));
636 ATF_CHECK(!atf_process_status_exited(&s));
637 ATF_CHECK(atf_process_status_signaled(&s));
638 ATF_CHECK_EQ(atf_process_status_termsig(&s), SIGKILL);
639 ATF_CHECK(!atf_process_status_coredump(&s));
640 atf_process_status_fini(&s);
644 const int rawstatus = fork_and_wait_child(child_sigterm);
645 atf_process_status_t s;
646 RE(atf_process_status_init(&s, rawstatus));
647 ATF_CHECK(!atf_process_status_exited(&s));
648 ATF_CHECK(atf_process_status_signaled(&s));
649 ATF_CHECK_EQ(atf_process_status_termsig(&s), SIGTERM);
650 ATF_CHECK(!atf_process_status_coredump(&s));
651 atf_process_status_fini(&s);
655 ATF_TC(status_coredump);
656 ATF_TC_HEAD(status_coredump, tc)
658 atf_tc_set_md_var(tc, "descr", "Tests the status type for processes "
661 ATF_TC_BODY(status_coredump, tc)
664 rl.rlim_cur = RLIM_INFINITY;
665 rl.rlim_max = RLIM_INFINITY;
666 if (setrlimit(RLIMIT_CORE, &rl) == -1)
667 atf_tc_skip("Cannot unlimit the core file size; check limits "
670 const int rawstatus = fork_and_wait_child(child_sigquit);
671 atf_process_status_t s;
672 RE(atf_process_status_init(&s, rawstatus));
673 ATF_CHECK(!atf_process_status_exited(&s));
674 ATF_CHECK(atf_process_status_signaled(&s));
675 ATF_CHECK_EQ(atf_process_status_termsig(&s), SIGQUIT);
676 ATF_CHECK(atf_process_status_coredump(&s));
677 atf_process_status_fini(&s);
680 /* ---------------------------------------------------------------------
681 * Test cases for the "child" type.
682 * --------------------------------------------------------------------- */
684 static void child_report_pid(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
688 child_report_pid(void *v ATF_DEFS_ATTRIBUTE_UNUSED)
690 const pid_t pid = getpid();
691 if (write(STDOUT_FILENO, &pid, sizeof(pid)) != sizeof(pid))
693 fprintf(stderr, "Reporting %d to parent\n", (int)getpid());
698 ATF_TC_HEAD(child_pid, tc)
700 atf_tc_set_md_var(tc, "descr", "Tests the correctness of the pid "
701 "stored in the child type");
703 ATF_TC_BODY(child_pid, tc)
705 atf_process_stream_t outsb, errsb;
706 atf_process_child_t child;
707 atf_process_status_t status;
710 RE(atf_process_stream_init_capture(&outsb));
711 RE(atf_process_stream_init_inherit(&errsb));
713 RE(atf_process_fork(&child, child_report_pid, &outsb, &errsb, NULL));
714 ATF_CHECK_EQ(read(atf_process_child_stdout(&child), &pid, sizeof(pid)),
716 printf("Expected PID: %d\n", (int)atf_process_child_pid(&child));
717 printf("Actual PID: %d\n", (int)pid);
718 ATF_CHECK_EQ(atf_process_child_pid(&child), pid);
720 RE(atf_process_child_wait(&child, &status));
721 atf_process_status_fini(&status);
723 atf_process_stream_fini(&outsb);
724 atf_process_stream_fini(&errsb);
729 child_loop(void *v ATF_DEFS_ATTRIBUTE_UNUSED)
737 nop_signal(int sig ATF_DEFS_ATTRIBUTE_UNUSED)
743 child_spawn_loop_and_wait_eintr(void *v ATF_DEFS_ATTRIBUTE_UNUSED)
745 atf_process_child_t child;
746 atf_process_status_t status;
747 struct sigaction sighup, old_sighup;
749 #define RE_ABORT(expr) \
751 atf_error_t _aux_err = expr; \
752 if (atf_is_error(_aux_err)) { \
753 atf_error_free(_aux_err); \
759 atf_process_stream_t outsb, errsb;
761 RE_ABORT(atf_process_stream_init_capture(&outsb));
762 RE_ABORT(atf_process_stream_init_inherit(&errsb));
763 RE_ABORT(atf_process_fork(&child, child_loop, &outsb, &errsb, NULL));
764 atf_process_stream_fini(&outsb);
765 atf_process_stream_fini(&errsb);
768 sighup.sa_handler = nop_signal;
769 sigemptyset(&sighup.sa_mask);
771 if (sigaction(SIGHUP, &sighup, &old_sighup) == -1)
777 fprintf(stderr, "Child entering wait(2)\n");
778 atf_error_t err = atf_process_child_wait(&child, &status);
779 fprintf(stderr, "Child's wait(2) terminated\n");
780 if (!atf_is_error(err)) {
781 fprintf(stderr, "wait completed successfully (not interrupted)\n");
784 if (!atf_error_is(err, "libc")) {
785 fprintf(stderr, "wait did not raise libc_error\n");
788 if (atf_libc_error_code(err) != EINTR) {
789 fprintf(stderr, "libc_error is not EINTR\n");
794 sigaction(SIGHUP, &old_sighup, NULL);
796 fprintf(stderr, "Child is killing subchild\n");
797 kill(atf_process_child_pid(&child), SIGTERM);
799 RE_ABORT(atf_process_child_wait(&child, &status));
800 atf_process_status_fini(&status);
807 ATF_TC(child_wait_eintr);
808 ATF_TC_HEAD(child_wait_eintr, tc)
810 atf_tc_set_md_var(tc, "descr", "Tests the interruption of the wait "
811 "method by an external signal, and the return of "
813 atf_tc_set_md_var(tc, "timeout", "30");
815 ATF_TC_BODY(child_wait_eintr, tc)
817 atf_process_child_t child;
818 atf_process_status_t status;
821 atf_process_stream_t outsb, errsb;
823 RE(atf_process_stream_init_capture(&outsb));
824 RE(atf_process_stream_init_inherit(&errsb));
825 RE(atf_process_fork(&child, child_spawn_loop_and_wait_eintr,
826 &outsb, &errsb, NULL));
827 atf_process_stream_fini(&outsb);
828 atf_process_stream_fini(&errsb);
832 /* Wait until the child process performs the wait call. This is
833 * racy, because the message we get from it is sent *before*
834 * doing the real system call... but I can't figure any other way
837 printf("Waiting for child to issue wait(2)\n");
838 ATF_REQUIRE(read(atf_process_child_stdout(&child), buf,
843 printf("Interrupting child's wait(2) call\n");
844 kill(atf_process_child_pid(&child), SIGHUP);
846 printf("Waiting for child's completion\n");
847 RE(atf_process_child_wait(&child, &status));
848 ATF_REQUIRE(atf_process_status_exited(&status));
849 ATF_REQUIRE_EQ(atf_process_status_exitstatus(&status), EXIT_SUCCESS);
850 atf_process_status_fini(&status);
853 /* ---------------------------------------------------------------------
854 * Tests cases for the free functions.
855 * --------------------------------------------------------------------- */
859 do_exec(const atf_tc_t *tc, const char *helper_name, atf_process_status_t *s,
860 void (*prehook)(void))
862 atf_fs_path_t process_helpers;
865 get_process_helpers_path(tc, true, &process_helpers);
867 argv[0] = atf_fs_path_cstring(&process_helpers);
868 argv[1] = helper_name;
870 printf("Executing %s %s\n", argv[0], argv[1]);
872 RE(atf_process_exec_array(s, &process_helpers, argv, NULL, NULL, prehook));
873 atf_fs_path_fini(&process_helpers);
878 check_line(int fd, const char *exp)
880 char *line = atf_utils_readline(fd);
881 ATF_CHECK(line != NULL);
882 ATF_CHECK_STREQ_MSG(exp, line, "read: '%s', expected: '%s'", line, exp);
886 ATF_TC(exec_failure);
887 ATF_TC_HEAD(exec_failure, tc)
889 atf_tc_set_md_var(tc, "descr", "Tests execing a command");
891 ATF_TC_BODY(exec_failure, tc)
893 atf_process_status_t status;
895 do_exec(tc, "exit-failure", &status, NULL);
896 ATF_CHECK(atf_process_status_exited(&status));
897 ATF_CHECK_EQ(atf_process_status_exitstatus(&status), EXIT_FAILURE);
898 atf_process_status_fini(&status);
902 ATF_TC_HEAD(exec_list, tc)
904 atf_tc_set_md_var(tc, "descr", "Tests execing a command");
906 ATF_TC_BODY(exec_list, tc)
908 atf_fs_path_t process_helpers;
910 atf_process_status_t status;
912 RE(atf_list_init(&argv));
914 get_process_helpers_path(tc, true, &process_helpers);
915 atf_list_append(&argv, strdup(atf_fs_path_cstring(&process_helpers)), true);
916 atf_list_append(&argv, strdup("echo"), true);
917 atf_list_append(&argv, strdup("test-message"), true);
919 atf_fs_path_t outpath;
920 atf_process_stream_t outsb;
922 RE(atf_fs_path_init_fmt(&outpath, "stdout"));
923 RE(atf_process_stream_init_redirect_path(&outsb, &outpath));
924 RE(atf_process_exec_list(&status, &process_helpers, &argv, &outsb,
926 atf_process_stream_fini(&outsb);
927 atf_fs_path_fini(&outpath);
929 atf_list_fini(&argv);
931 ATF_CHECK(atf_process_status_exited(&status));
932 ATF_CHECK_EQ(atf_process_status_exitstatus(&status), EXIT_SUCCESS);
935 int fd = open("stdout", O_RDONLY);
937 check_line(fd, "test-message");
941 atf_process_status_fini(&status);
942 atf_fs_path_fini(&process_helpers);
951 ATF_TC(exec_prehook);
952 ATF_TC_HEAD(exec_prehook, tc)
954 atf_tc_set_md_var(tc, "descr", "Tests execing a command with a prehook");
956 ATF_TC_BODY(exec_prehook, tc)
958 atf_process_status_t status;
960 do_exec(tc, "exit-success", &status, exit_early);
961 ATF_CHECK(atf_process_status_exited(&status));
962 ATF_CHECK_EQ(atf_process_status_exitstatus(&status), 80);
963 atf_process_status_fini(&status);
966 ATF_TC(exec_success);
967 ATF_TC_HEAD(exec_success, tc)
969 atf_tc_set_md_var(tc, "descr", "Tests execing a command");
971 ATF_TC_BODY(exec_success, tc)
973 atf_process_status_t status;
975 do_exec(tc, "exit-success", &status, NULL);
976 ATF_CHECK(atf_process_status_exited(&status));
977 ATF_CHECK_EQ(atf_process_status_exitstatus(&status), EXIT_SUCCESS);
978 atf_process_status_fini(&status);
981 static const int exit_v_null = 1;
982 static const int exit_v_notnull = 2;
986 child_cookie(void *v)
991 exit(exit_v_notnull);
997 ATF_TC_HEAD(fork_cookie, tc)
999 atf_tc_set_md_var(tc, "descr", "Tests forking a child, with "
1000 "a null and non-null data cookie");
1002 ATF_TC_BODY(fork_cookie, tc)
1004 atf_process_stream_t outsb, errsb;
1006 RE(atf_process_stream_init_inherit(&outsb));
1007 RE(atf_process_stream_init_inherit(&errsb));
1010 atf_process_child_t child;
1011 atf_process_status_t status;
1013 RE(atf_process_fork(&child, child_cookie, &outsb, &errsb, NULL));
1014 RE(atf_process_child_wait(&child, &status));
1016 ATF_CHECK(atf_process_status_exited(&status));
1017 ATF_CHECK_EQ(atf_process_status_exitstatus(&status), exit_v_null);
1019 atf_process_status_fini(&status);
1023 atf_process_child_t child;
1024 atf_process_status_t status;
1027 RE(atf_process_fork(&child, child_cookie, &outsb, &errsb, &dummy_int));
1028 RE(atf_process_child_wait(&child, &status));
1030 ATF_CHECK(atf_process_status_exited(&status));
1031 ATF_CHECK_EQ(atf_process_status_exitstatus(&status), exit_v_notnull);
1033 atf_process_status_fini(&status);
1036 atf_process_stream_fini(&errsb);
1037 atf_process_stream_fini(&outsb);
1040 #define TC_FORK_STREAMS(outlc, outuc, errlc, erruc) \
1041 ATF_TC(fork_out_ ## outlc ## _err_ ## errlc); \
1042 ATF_TC_HEAD(fork_out_ ## outlc ## _err_ ## errlc, tc) \
1044 atf_tc_set_md_var(tc, "descr", "Tests forking a child, with " \
1045 "stdout " #outlc " and stderr " #errlc); \
1047 ATF_TC_BODY(fork_out_ ## outlc ## _err_ ## errlc, tc) \
1049 struct outlc ## _stream out = outuc ## _STREAM(stdout_type); \
1050 struct errlc ## _stream err = erruc ## _STREAM(stderr_type); \
1051 do_fork(&out.m_base, &out, &err.m_base, &err); \
1054 TC_FORK_STREAMS(capture, CAPTURE, capture, CAPTURE);
1055 TC_FORK_STREAMS(capture, CAPTURE, connect, CONNECT);
1056 TC_FORK_STREAMS(capture, CAPTURE, default, DEFAULT);
1057 TC_FORK_STREAMS(capture, CAPTURE, inherit, INHERIT);
1058 TC_FORK_STREAMS(capture, CAPTURE, redirect_fd, REDIRECT_FD);
1059 TC_FORK_STREAMS(capture, CAPTURE, redirect_path, REDIRECT_PATH);
1060 TC_FORK_STREAMS(connect, CONNECT, capture, CAPTURE);
1061 TC_FORK_STREAMS(connect, CONNECT, connect, CONNECT);
1062 TC_FORK_STREAMS(connect, CONNECT, default, DEFAULT);
1063 TC_FORK_STREAMS(connect, CONNECT, inherit, INHERIT);
1064 TC_FORK_STREAMS(connect, CONNECT, redirect_fd, REDIRECT_FD);
1065 TC_FORK_STREAMS(connect, CONNECT, redirect_path, REDIRECT_PATH);
1066 TC_FORK_STREAMS(default, DEFAULT, capture, CAPTURE);
1067 TC_FORK_STREAMS(default, DEFAULT, connect, CONNECT);
1068 TC_FORK_STREAMS(default, DEFAULT, default, DEFAULT);
1069 TC_FORK_STREAMS(default, DEFAULT, inherit, INHERIT);
1070 TC_FORK_STREAMS(default, DEFAULT, redirect_fd, REDIRECT_FD);
1071 TC_FORK_STREAMS(default, DEFAULT, redirect_path, REDIRECT_PATH);
1072 TC_FORK_STREAMS(inherit, INHERIT, capture, CAPTURE);
1073 TC_FORK_STREAMS(inherit, INHERIT, connect, CONNECT);
1074 TC_FORK_STREAMS(inherit, INHERIT, default, DEFAULT);
1075 TC_FORK_STREAMS(inherit, INHERIT, inherit, INHERIT);
1076 TC_FORK_STREAMS(inherit, INHERIT, redirect_fd, REDIRECT_FD);
1077 TC_FORK_STREAMS(inherit, INHERIT, redirect_path, REDIRECT_PATH);
1078 TC_FORK_STREAMS(redirect_fd, REDIRECT_FD, capture, CAPTURE);
1079 TC_FORK_STREAMS(redirect_fd, REDIRECT_FD, connect, CONNECT);
1080 TC_FORK_STREAMS(redirect_fd, REDIRECT_FD, default, DEFAULT);
1081 TC_FORK_STREAMS(redirect_fd, REDIRECT_FD, inherit, INHERIT);
1082 TC_FORK_STREAMS(redirect_fd, REDIRECT_FD, redirect_fd, REDIRECT_FD);
1083 TC_FORK_STREAMS(redirect_fd, REDIRECT_FD, redirect_path, REDIRECT_PATH);
1084 TC_FORK_STREAMS(redirect_path, REDIRECT_PATH, capture, CAPTURE);
1085 TC_FORK_STREAMS(redirect_path, REDIRECT_PATH, connect, CONNECT);
1086 TC_FORK_STREAMS(redirect_path, REDIRECT_PATH, default, DEFAULT);
1087 TC_FORK_STREAMS(redirect_path, REDIRECT_PATH, inherit, INHERIT);
1088 TC_FORK_STREAMS(redirect_path, REDIRECT_PATH, redirect_fd, REDIRECT_FD);
1089 TC_FORK_STREAMS(redirect_path, REDIRECT_PATH, redirect_path, REDIRECT_PATH);
1091 #undef TC_FORK_STREAMS
1093 /* ---------------------------------------------------------------------
1095 * --------------------------------------------------------------------- */
1099 /* Add the tests for the "stream" type. */
1100 ATF_TP_ADD_TC(tp, stream_init_capture);
1101 ATF_TP_ADD_TC(tp, stream_init_connect);
1102 ATF_TP_ADD_TC(tp, stream_init_inherit);
1103 ATF_TP_ADD_TC(tp, stream_init_redirect_fd);
1104 ATF_TP_ADD_TC(tp, stream_init_redirect_path);
1106 /* Add the tests for the "status" type. */
1107 ATF_TP_ADD_TC(tp, status_exited);
1108 ATF_TP_ADD_TC(tp, status_signaled);
1109 ATF_TP_ADD_TC(tp, status_coredump);
1111 /* Add the tests for the "child" type. */
1112 ATF_TP_ADD_TC(tp, child_pid);
1113 ATF_TP_ADD_TC(tp, child_wait_eintr);
1115 /* Add the tests for the free functions. */
1116 ATF_TP_ADD_TC(tp, exec_failure);
1117 ATF_TP_ADD_TC(tp, exec_list);
1118 ATF_TP_ADD_TC(tp, exec_prehook);
1119 ATF_TP_ADD_TC(tp, exec_success);
1120 ATF_TP_ADD_TC(tp, fork_cookie);
1121 ATF_TP_ADD_TC(tp, fork_out_capture_err_capture);
1122 ATF_TP_ADD_TC(tp, fork_out_capture_err_connect);
1123 ATF_TP_ADD_TC(tp, fork_out_capture_err_default);
1124 ATF_TP_ADD_TC(tp, fork_out_capture_err_inherit);
1125 ATF_TP_ADD_TC(tp, fork_out_capture_err_redirect_fd);
1126 ATF_TP_ADD_TC(tp, fork_out_capture_err_redirect_path);
1127 ATF_TP_ADD_TC(tp, fork_out_connect_err_capture);
1128 ATF_TP_ADD_TC(tp, fork_out_connect_err_connect);
1129 ATF_TP_ADD_TC(tp, fork_out_connect_err_default);
1130 ATF_TP_ADD_TC(tp, fork_out_connect_err_inherit);
1131 ATF_TP_ADD_TC(tp, fork_out_connect_err_redirect_fd);
1132 ATF_TP_ADD_TC(tp, fork_out_connect_err_redirect_path);
1133 ATF_TP_ADD_TC(tp, fork_out_default_err_capture);
1134 ATF_TP_ADD_TC(tp, fork_out_default_err_connect);
1135 ATF_TP_ADD_TC(tp, fork_out_default_err_default);
1136 ATF_TP_ADD_TC(tp, fork_out_default_err_inherit);
1137 ATF_TP_ADD_TC(tp, fork_out_default_err_redirect_fd);
1138 ATF_TP_ADD_TC(tp, fork_out_default_err_redirect_path);
1139 ATF_TP_ADD_TC(tp, fork_out_inherit_err_capture);
1140 ATF_TP_ADD_TC(tp, fork_out_inherit_err_connect);
1141 ATF_TP_ADD_TC(tp, fork_out_inherit_err_default);
1142 ATF_TP_ADD_TC(tp, fork_out_inherit_err_inherit);
1143 ATF_TP_ADD_TC(tp, fork_out_inherit_err_redirect_fd);
1144 ATF_TP_ADD_TC(tp, fork_out_inherit_err_redirect_path);
1145 ATF_TP_ADD_TC(tp, fork_out_redirect_fd_err_capture);
1146 ATF_TP_ADD_TC(tp, fork_out_redirect_fd_err_connect);
1147 ATF_TP_ADD_TC(tp, fork_out_redirect_fd_err_default);
1148 ATF_TP_ADD_TC(tp, fork_out_redirect_fd_err_inherit);
1149 ATF_TP_ADD_TC(tp, fork_out_redirect_fd_err_redirect_fd);
1150 ATF_TP_ADD_TC(tp, fork_out_redirect_fd_err_redirect_path);
1151 ATF_TP_ADD_TC(tp, fork_out_redirect_path_err_capture);
1152 ATF_TP_ADD_TC(tp, fork_out_redirect_path_err_connect);
1153 ATF_TP_ADD_TC(tp, fork_out_redirect_path_err_default);
1154 ATF_TP_ADD_TC(tp, fork_out_redirect_path_err_inherit);
1155 ATF_TP_ADD_TC(tp, fork_out_redirect_path_err_redirect_fd);
1156 ATF_TP_ADD_TC(tp, fork_out_redirect_path_err_redirect_path);
1158 return atf_no_error();