Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / unit / atf-src / atf-c / detail / process_test.c
blob3780ee352db6a0306836fc3bf3a40be389f0ac75
1 /* $NetBSD: process_test.c,v 1.4 2014/12/10 04:38:03 christos Exp $ */
3 /*
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 #include <sys/wait.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include <atf-c.h>
47 #include "atf-c/defs.h"
49 #include "process.h"
50 #include "sanity.h"
51 #include "test_helpers.h"
53 atf_error_t atf_process_status_init(atf_process_status_t *, int);
55 /* ---------------------------------------------------------------------
56 * Auxiliary functions for testing of 'atf_process_fork'.
57 * --------------------------------------------------------------------- */
60 * Testing of atf_process_fork is quite messy. We want to be able to test
61 * all the possible combinations of stdout and stderr behavior to ensure
62 * that the streams are manipulated correctly.
64 * To do this, the do_fork function is a wrapper for atf_process_fork that
65 * issues stream-specific hooks before fork, while the child is running and
66 * after the child terminates. We then provide test cases that just call
67 * do_fork with different hooks.
69 * The hooks are described by base_stream, and we then have one *_stream
70 * type for ever possible stream behavior.
73 enum out_type { stdout_type, stderr_type };
75 struct base_stream {
76 void (*init)(void *);
77 void (*process)(void *, atf_process_child_t *);
78 void (*fini)(void *);
80 /* m_sb is initialized by subclasses that need it, but all consumers
81 * must use m_sb_ptr, which may or may not point to m_sb. This allows
82 * us to test the interface with a NULL value, which triggers a
83 * default behavior. */
84 atf_process_stream_t m_sb;
85 atf_process_stream_t *m_sb_ptr;
86 enum out_type m_type;
88 #define BASE_STREAM(ihook, phook, fhook, type) \
89 { .init = ihook, \
90 .process = phook, \
91 .fini = fhook, \
92 .m_type = type }
94 static
95 void
96 check_file(const enum out_type type)
98 switch (type) {
99 case stdout_type:
100 ATF_CHECK(atf_utils_grep_file("stdout: msg", "stdout"));
101 ATF_CHECK(!atf_utils_grep_file("stderr: msg", "stdout"));
102 break;
103 case stderr_type:
104 ATF_CHECK(atf_utils_grep_file("stderr: msg", "stderr"));
105 ATF_CHECK(!atf_utils_grep_file("stdout: msg", "stderr"));
106 break;
107 default:
108 UNREACHABLE;
112 struct capture_stream {
113 struct base_stream m_base;
115 char *m_msg;
117 #define CAPTURE_STREAM(type) \
118 { .m_base = BASE_STREAM(capture_stream_init, \
119 capture_stream_process, \
120 capture_stream_fini, \
121 type) }
123 static
124 void
125 capture_stream_init(void *v)
127 struct capture_stream *s = v;
129 s->m_base.m_sb_ptr = &s->m_base.m_sb;
130 RE(atf_process_stream_init_capture(&s->m_base.m_sb));
131 s->m_msg = NULL;
134 static
135 void
136 capture_stream_process(void *v, atf_process_child_t *c)
138 struct capture_stream *s = v;
140 switch (s->m_base.m_type) {
141 case stdout_type:
142 s->m_msg = atf_utils_readline(atf_process_child_stdout(c));
143 break;
144 case stderr_type:
145 s->m_msg = atf_utils_readline(atf_process_child_stderr(c));
146 break;
147 default:
148 UNREACHABLE;
152 static
153 void
154 capture_stream_fini(void *v)
156 struct capture_stream *s = v;
158 switch (s->m_base.m_type) {
159 case stdout_type:
160 ATF_CHECK(atf_utils_grep_string("stdout: msg", s->m_msg));
161 ATF_CHECK(!atf_utils_grep_string("stderr: msg", s->m_msg));
162 break;
163 case stderr_type:
164 ATF_CHECK(!atf_utils_grep_string("stdout: msg", s->m_msg));
165 ATF_CHECK(atf_utils_grep_string("stderr: msg", s->m_msg));
166 break;
167 default:
168 UNREACHABLE;
171 free(s->m_msg);
172 atf_process_stream_fini(&s->m_base.m_sb);
175 struct connect_stream {
176 struct base_stream m_base;
178 int m_fd;
180 #define CONNECT_STREAM(type) \
181 { .m_base = BASE_STREAM(connect_stream_init, \
182 NULL, \
183 connect_stream_fini, \
184 type) }
186 static
187 void
188 connect_stream_init(void *v)
190 struct connect_stream *s = v;
191 int src_fd;
193 switch (s->m_base.m_type) {
194 case stdout_type:
195 src_fd = STDOUT_FILENO;
196 s->m_fd = open("stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644);
197 break;
198 case stderr_type:
199 src_fd = STDERR_FILENO;
200 s->m_fd = open("stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
201 break;
202 default:
203 UNREACHABLE;
204 src_fd = -1;
206 ATF_REQUIRE(s->m_fd != -1);
208 s->m_base.m_sb_ptr = &s->m_base.m_sb;
209 RE(atf_process_stream_init_connect(&s->m_base.m_sb, src_fd, s->m_fd));
212 static
213 void
214 connect_stream_fini(void *v)
216 struct connect_stream *s = v;
218 ATF_REQUIRE(close(s->m_fd) != -1);
220 atf_process_stream_fini(&s->m_base.m_sb);
222 check_file(s->m_base.m_type);
225 struct inherit_stream {
226 struct base_stream m_base;
227 int m_fd;
229 int m_old_fd;
231 #define INHERIT_STREAM(type) \
232 { .m_base = BASE_STREAM(inherit_stream_init, \
233 NULL, \
234 inherit_stream_fini, \
235 type) }
237 static
238 void
239 inherit_stream_init(void *v)
241 struct inherit_stream *s = v;
242 const char *name;
244 s->m_base.m_sb_ptr = &s->m_base.m_sb;
245 RE(atf_process_stream_init_inherit(&s->m_base.m_sb));
247 switch (s->m_base.m_type) {
248 case stdout_type:
249 s->m_fd = STDOUT_FILENO;
250 name = "stdout";
251 break;
252 case stderr_type:
253 s->m_fd = STDERR_FILENO;
254 name = "stderr";
255 break;
256 default:
257 UNREACHABLE;
258 name = NULL;
261 s->m_old_fd = dup(s->m_fd);
262 ATF_REQUIRE(s->m_old_fd != -1);
263 ATF_REQUIRE(close(s->m_fd) != -1);
264 ATF_REQUIRE_EQ(open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644),
265 s->m_fd);
268 static
269 void
270 inherit_stream_fini(void *v)
272 struct inherit_stream *s = v;
274 ATF_REQUIRE(dup2(s->m_old_fd, s->m_fd) != -1);
275 ATF_REQUIRE(close(s->m_old_fd) != -1);
277 atf_process_stream_fini(&s->m_base.m_sb);
279 check_file(s->m_base.m_type);
282 #define default_stream inherit_stream
283 #define DEFAULT_STREAM(type) \
284 { .m_base = BASE_STREAM(default_stream_init, \
285 NULL, \
286 default_stream_fini, \
287 type) }
289 static
290 void
291 default_stream_init(void *v)
293 struct inherit_stream *s = v;
295 inherit_stream_init(v);
296 s->m_base.m_sb_ptr = NULL;
299 static
300 void
301 default_stream_fini(void *v)
303 inherit_stream_fini(v);
306 struct redirect_fd_stream {
307 struct base_stream m_base;
309 int m_fd;
311 #define REDIRECT_FD_STREAM(type) \
312 { .m_base = BASE_STREAM(redirect_fd_stream_init, \
313 NULL, \
314 redirect_fd_stream_fini, \
315 type) }
317 static
318 void
319 redirect_fd_stream_init(void *v)
321 struct redirect_fd_stream *s = v;
323 switch (s->m_base.m_type) {
324 case stdout_type:
325 s->m_fd = open("stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644);
326 break;
327 case stderr_type:
328 s->m_fd = open("stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
329 break;
330 default:
331 UNREACHABLE;
333 ATF_REQUIRE(s->m_fd != -1);
335 s->m_base.m_sb_ptr = &s->m_base.m_sb;
336 RE(atf_process_stream_init_redirect_fd(&s->m_base.m_sb, s->m_fd));
339 static
340 void
341 redirect_fd_stream_fini(void *v)
343 struct redirect_fd_stream *s = v;
345 ATF_REQUIRE(close(s->m_fd) != -1);
347 atf_process_stream_fini(&s->m_base.m_sb);
349 check_file(s->m_base.m_type);
352 struct redirect_path_stream {
353 struct base_stream m_base;
355 atf_fs_path_t m_path;
357 #define REDIRECT_PATH_STREAM(type) \
358 { .m_base = BASE_STREAM(redirect_path_stream_init, \
359 NULL, \
360 redirect_path_stream_fini, \
361 type) }
363 static
364 void
365 redirect_path_stream_init(void *v)
367 struct redirect_path_stream *s = v;
369 switch (s->m_base.m_type) {
370 case stdout_type:
371 RE(atf_fs_path_init_fmt(&s->m_path, "stdout"));
372 break;
373 case stderr_type:
374 RE(atf_fs_path_init_fmt(&s->m_path, "stderr"));
375 break;
376 default:
377 UNREACHABLE;
380 s->m_base.m_sb_ptr = &s->m_base.m_sb;
381 RE(atf_process_stream_init_redirect_path(&s->m_base.m_sb, &s->m_path));
384 static
385 void
386 redirect_path_stream_fini(void *v)
388 struct redirect_path_stream *s = v;
390 atf_process_stream_fini(&s->m_base.m_sb);
392 atf_fs_path_fini(&s->m_path);
394 check_file(s->m_base.m_type);
397 static void child_print(void *) ATF_DEFS_ATTRIBUTE_NORETURN;
399 struct child_print_data {
400 const char *m_msg;
403 static
404 void
405 child_print(void *v)
407 struct child_print_data *cpd = v;
409 fprintf(stdout, "stdout: %s\n", cpd->m_msg);
410 fprintf(stderr, "stderr: %s\n", cpd->m_msg);
412 exit(EXIT_SUCCESS);
415 static
416 void
417 do_fork(const struct base_stream *outfs, void *out,
418 const struct base_stream *errfs, void *err)
420 atf_process_child_t child;
421 atf_process_status_t status;
422 struct child_print_data cpd = { "msg" };
424 outfs->init(out);
425 errfs->init(err);
427 RE(atf_process_fork(&child, child_print, outfs->m_sb_ptr,
428 errfs->m_sb_ptr, &cpd));
429 if (outfs->process != NULL)
430 outfs->process(out, &child);
431 if (errfs->process != NULL)
432 errfs->process(err, &child);
433 RE(atf_process_child_wait(&child, &status));
435 outfs->fini(out);
436 errfs->fini(err);
438 atf_process_status_fini(&status);
441 /* ---------------------------------------------------------------------
442 * Test cases for the "stream" type.
443 * --------------------------------------------------------------------- */
445 ATF_TC(stream_init_capture);
446 ATF_TC_HEAD(stream_init_capture, tc)
448 atf_tc_set_md_var(tc, "descr", "Tests the "
449 "atf_process_stream_init_capture function");
451 ATF_TC_BODY(stream_init_capture, tc)
453 atf_process_stream_t sb;
455 RE(atf_process_stream_init_capture(&sb));
457 ATF_CHECK_EQ(atf_process_stream_type(&sb),
458 atf_process_stream_type_capture);
460 atf_process_stream_fini(&sb);
463 ATF_TC(stream_init_connect);
464 ATF_TC_HEAD(stream_init_connect, tc)
466 atf_tc_set_md_var(tc, "descr", "Tests the "
467 "atf_process_stream_init_connect function");
469 ATF_TC_BODY(stream_init_connect, tc)
471 atf_process_stream_t sb;
473 RE(atf_process_stream_init_connect(&sb, 1, 2));
475 ATF_CHECK_EQ(atf_process_stream_type(&sb),
476 atf_process_stream_type_connect);
478 atf_process_stream_fini(&sb);
481 ATF_TC(stream_init_inherit);
482 ATF_TC_HEAD(stream_init_inherit, tc)
484 atf_tc_set_md_var(tc, "descr", "Tests the "
485 "atf_process_stream_init_inherit function");
487 ATF_TC_BODY(stream_init_inherit, tc)
489 atf_process_stream_t sb;
491 RE(atf_process_stream_init_inherit(&sb));
493 ATF_CHECK_EQ(atf_process_stream_type(&sb),
494 atf_process_stream_type_inherit);
496 atf_process_stream_fini(&sb);
499 ATF_TC(stream_init_redirect_fd);
500 ATF_TC_HEAD(stream_init_redirect_fd, tc)
502 atf_tc_set_md_var(tc, "descr", "Tests the "
503 "atf_process_stream_init_redirect_fd function");
505 ATF_TC_BODY(stream_init_redirect_fd, tc)
507 atf_process_stream_t sb;
509 RE(atf_process_stream_init_redirect_fd(&sb, 1));
511 ATF_CHECK_EQ(atf_process_stream_type(&sb),
512 atf_process_stream_type_redirect_fd);
514 atf_process_stream_fini(&sb);
517 ATF_TC(stream_init_redirect_path);
518 ATF_TC_HEAD(stream_init_redirect_path, tc)
520 atf_tc_set_md_var(tc, "descr", "Tests the "
521 "atf_process_stream_init_redirect_path function");
523 ATF_TC_BODY(stream_init_redirect_path, tc)
525 atf_process_stream_t sb;
526 atf_fs_path_t path;
528 RE(atf_fs_path_init_fmt(&path, "foo"));
529 RE(atf_process_stream_init_redirect_path(&sb, &path));
531 ATF_CHECK_EQ(atf_process_stream_type(&sb),
532 atf_process_stream_type_redirect_path);
534 atf_process_stream_fini(&sb);
535 atf_fs_path_fini(&path);
538 /* ---------------------------------------------------------------------
539 * Test cases for the "status" type.
540 * --------------------------------------------------------------------- */
542 static void child_exit_success(void) ATF_DEFS_ATTRIBUTE_NORETURN;
543 static void child_exit_failure(void) ATF_DEFS_ATTRIBUTE_NORETURN;
544 static void child_sigkill(void) ATF_DEFS_ATTRIBUTE_NORETURN;
545 static void child_sigquit(void) ATF_DEFS_ATTRIBUTE_NORETURN;
546 static void child_sigterm(void) ATF_DEFS_ATTRIBUTE_NORETURN;
548 void
549 child_exit_success(void)
551 exit(EXIT_SUCCESS);
554 void
555 child_exit_failure(void)
557 exit(EXIT_FAILURE);
560 void
561 child_sigkill(void)
563 kill(getpid(), SIGKILL);
564 abort();
567 void
568 child_sigquit(void)
570 kill(getpid(), SIGQUIT);
571 abort();
574 void
575 child_sigterm(void)
577 kill(getpid(), SIGTERM);
578 abort();
581 static
583 fork_and_wait_child(void (*child_func)(void))
585 pid_t pid;
586 int status;
588 pid = fork();
589 ATF_REQUIRE(pid != -1);
590 if (pid == 0) {
591 status = 0; /* Silence compiler warnings */
592 child_func();
593 UNREACHABLE;
594 } else {
595 ATF_REQUIRE(waitpid(pid, &status, 0) != 0);
598 return status;
601 ATF_TC(status_exited);
602 ATF_TC_HEAD(status_exited, tc)
604 atf_tc_set_md_var(tc, "descr", "Tests the status type for processes "
605 "that exit cleanly");
607 ATF_TC_BODY(status_exited, tc)
610 const int rawstatus = fork_and_wait_child(child_exit_success);
611 atf_process_status_t s;
612 RE(atf_process_status_init(&s, rawstatus));
613 ATF_CHECK(atf_process_status_exited(&s));
614 ATF_CHECK_EQ(atf_process_status_exitstatus(&s), EXIT_SUCCESS);
615 ATF_CHECK(!atf_process_status_signaled(&s));
616 atf_process_status_fini(&s);
620 const int rawstatus = fork_and_wait_child(child_exit_failure);
621 atf_process_status_t s;
622 RE(atf_process_status_init(&s, rawstatus));
623 ATF_CHECK(atf_process_status_exited(&s));
624 ATF_CHECK_EQ(atf_process_status_exitstatus(&s), EXIT_FAILURE);
625 ATF_CHECK(!atf_process_status_signaled(&s));
626 atf_process_status_fini(&s);
630 ATF_TC(status_signaled);
631 ATF_TC_HEAD(status_signaled, tc)
633 atf_tc_set_md_var(tc, "descr", "Tests the status type for processes "
634 "that end due to a signal");
636 ATF_TC_BODY(status_signaled, tc)
639 const int rawstatus = fork_and_wait_child(child_sigkill);
640 atf_process_status_t s;
641 RE(atf_process_status_init(&s, rawstatus));
642 ATF_CHECK(!atf_process_status_exited(&s));
643 ATF_CHECK(atf_process_status_signaled(&s));
644 ATF_CHECK_EQ(atf_process_status_termsig(&s), SIGKILL);
645 ATF_CHECK(!atf_process_status_coredump(&s));
646 atf_process_status_fini(&s);
650 const int rawstatus = fork_and_wait_child(child_sigterm);
651 atf_process_status_t s;
652 RE(atf_process_status_init(&s, rawstatus));
653 ATF_CHECK(!atf_process_status_exited(&s));
654 ATF_CHECK(atf_process_status_signaled(&s));
655 ATF_CHECK_EQ(atf_process_status_termsig(&s), SIGTERM);
656 ATF_CHECK(!atf_process_status_coredump(&s));
657 atf_process_status_fini(&s);
661 ATF_TC(status_coredump);
662 ATF_TC_HEAD(status_coredump, tc)
664 atf_tc_set_md_var(tc, "descr", "Tests the status type for processes "
665 "that crash");
667 ATF_TC_BODY(status_coredump, tc)
669 struct rlimit rl;
670 rl.rlim_cur = RLIM_INFINITY;
671 rl.rlim_max = RLIM_INFINITY;
672 if (setrlimit(RLIMIT_CORE, &rl) == -1)
673 atf_tc_skip("Cannot unlimit the core file size; check limits "
674 "manually");
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;
692 static
693 void
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))
698 abort();
699 fprintf(stderr, "Reporting %d to parent\n", (int)getpid());
700 exit(EXIT_SUCCESS);
703 ATF_TC(child_pid);
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;
714 pid_t pid;
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)),
721 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);
733 static
734 void
735 child_loop(void *v ATF_DEFS_ATTRIBUTE_UNUSED)
737 for (;;)
738 sleep(1);
741 static
742 void
743 nop_signal(int sig ATF_DEFS_ATTRIBUTE_UNUSED)
747 static
748 void
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) \
756 do { \
757 atf_error_t _aux_err = expr; \
758 if (atf_is_error(_aux_err)) { \
759 atf_error_free(_aux_err); \
760 abort(); \
762 } while (/*CONSTCOND*/0)
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);
776 sighup.sa_flags = 0;
777 if (sigaction(SIGHUP, &sighup, &old_sighup) == -1)
778 abort();
780 printf("waiting\n");
781 fflush(stdout);
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");
788 abort();
790 if (!atf_error_is(err, "libc")) {
791 fprintf(stderr, "wait did not raise libc_error\n");
792 abort();
794 if (atf_libc_error_code(err) != EINTR) {
795 fprintf(stderr, "libc_error is not EINTR\n");
796 abort();
798 atf_error_free(err);
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);
808 #undef RE_ABORT
810 exit(EXIT_SUCCESS);
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 "
818 "an EINTR error");
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
841 * to do this. */
842 char buf[16];
843 printf("Waiting for child to issue wait(2)\n");
844 ATF_REQUIRE(read(atf_process_child_stdout(&child), buf,
845 sizeof(buf)) > 0);
846 sleep(1);
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 * --------------------------------------------------------------------- */
863 static
864 void
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;
869 const char *argv[3];
871 get_process_helpers_path(tc, true, &process_helpers);
873 argv[0] = atf_fs_path_cstring(&process_helpers);
874 argv[1] = helper_name;
875 argv[2] = NULL;
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);
882 static
883 void
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);
889 free(line);
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);
907 ATF_TC(exec_list);
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;
915 atf_list_t argv;
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,
931 NULL, NULL));
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);
942 ATF_CHECK(fd != -1);
943 check_line(fd, "test-message");
944 close(fd);
947 atf_process_status_fini(&status);
948 atf_fs_path_fini(&process_helpers);
951 static void
952 exit_early(void)
954 exit(80);
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;
990 static
991 void
992 child_cookie(void *v)
994 if (v == NULL)
995 exit(exit_v_null);
996 else
997 exit(exit_v_notnull);
999 UNREACHABLE;
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;
1031 int dummy_int;
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 /* ---------------------------------------------------------------------
1100 * Main.
1101 * --------------------------------------------------------------------- */
1103 ATF_TP_ADD_TCS(tp)
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();