Remove building with NOCRYPTO option
[minix3.git] / external / bsd / atf / dist / atf-c / detail / process_test.c
blob9e55f70e321498a281c6affc3e482b7c6ce5cb42
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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>
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 #include <sys/wait.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include <atf-c.h>
45 #include "atf-c/defs.h"
47 #include "process.h"
48 #include "sanity.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 };
73 struct base_stream {
74 void (*init)(void *);
75 void (*process)(void *, atf_process_child_t *);
76 void (*fini)(void *);
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;
84 enum out_type m_type;
86 #define BASE_STREAM(ihook, phook, fhook, type) \
87 { .init = ihook, \
88 .process = phook, \
89 .fini = fhook, \
90 .m_type = type }
92 static
93 void
94 check_file(const enum out_type type)
96 switch (type) {
97 case stdout_type:
98 ATF_CHECK(atf_utils_grep_file("stdout: msg", "stdout"));
99 ATF_CHECK(!atf_utils_grep_file("stderr: msg", "stdout"));
100 break;
101 case stderr_type:
102 ATF_CHECK(atf_utils_grep_file("stderr: msg", "stderr"));
103 ATF_CHECK(!atf_utils_grep_file("stdout: msg", "stderr"));
104 break;
105 default:
106 UNREACHABLE;
110 struct capture_stream {
111 struct base_stream m_base;
113 char *m_msg;
115 #define CAPTURE_STREAM(type) \
116 { .m_base = BASE_STREAM(capture_stream_init, \
117 capture_stream_process, \
118 capture_stream_fini, \
119 type) }
121 static
122 void
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));
129 s->m_msg = NULL;
132 static
133 void
134 capture_stream_process(void *v, atf_process_child_t *c)
136 struct capture_stream *s = v;
138 switch (s->m_base.m_type) {
139 case stdout_type:
140 s->m_msg = atf_utils_readline(atf_process_child_stdout(c));
141 break;
142 case stderr_type:
143 s->m_msg = atf_utils_readline(atf_process_child_stderr(c));
144 break;
145 default:
146 UNREACHABLE;
150 static
151 void
152 capture_stream_fini(void *v)
154 struct capture_stream *s = v;
156 switch (s->m_base.m_type) {
157 case stdout_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));
160 break;
161 case stderr_type:
162 ATF_CHECK(!atf_utils_grep_string("stdout: msg", s->m_msg));
163 ATF_CHECK(atf_utils_grep_string("stderr: msg", s->m_msg));
164 break;
165 default:
166 UNREACHABLE;
169 free(s->m_msg);
170 atf_process_stream_fini(&s->m_base.m_sb);
173 struct connect_stream {
174 struct base_stream m_base;
176 int m_fd;
178 #define CONNECT_STREAM(type) \
179 { .m_base = BASE_STREAM(connect_stream_init, \
180 NULL, \
181 connect_stream_fini, \
182 type) }
184 static
185 void
186 connect_stream_init(void *v)
188 struct connect_stream *s = v;
189 int src_fd;
191 switch (s->m_base.m_type) {
192 case stdout_type:
193 src_fd = STDOUT_FILENO;
194 s->m_fd = open("stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644);
195 break;
196 case stderr_type:
197 src_fd = STDERR_FILENO;
198 s->m_fd = open("stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
199 break;
200 default:
201 UNREACHABLE;
202 src_fd = -1;
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));
210 static
211 void
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;
225 int m_fd;
227 int m_old_fd;
229 #define INHERIT_STREAM(type) \
230 { .m_base = BASE_STREAM(inherit_stream_init, \
231 NULL, \
232 inherit_stream_fini, \
233 type) }
235 static
236 void
237 inherit_stream_init(void *v)
239 struct inherit_stream *s = v;
240 const char *name;
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) {
246 case stdout_type:
247 s->m_fd = STDOUT_FILENO;
248 name = "stdout";
249 break;
250 case stderr_type:
251 s->m_fd = STDERR_FILENO;
252 name = "stderr";
253 break;
254 default:
255 UNREACHABLE;
256 name = NULL;
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),
263 s->m_fd);
266 static
267 void
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, \
283 NULL, \
284 default_stream_fini, \
285 type) }
287 static
288 void
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;
297 static
298 void
299 default_stream_fini(void *v)
301 inherit_stream_fini(v);
304 struct redirect_fd_stream {
305 struct base_stream m_base;
307 int m_fd;
309 #define REDIRECT_FD_STREAM(type) \
310 { .m_base = BASE_STREAM(redirect_fd_stream_init, \
311 NULL, \
312 redirect_fd_stream_fini, \
313 type) }
315 static
316 void
317 redirect_fd_stream_init(void *v)
319 struct redirect_fd_stream *s = v;
321 switch (s->m_base.m_type) {
322 case stdout_type:
323 s->m_fd = open("stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644);
324 break;
325 case stderr_type:
326 s->m_fd = open("stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
327 break;
328 default:
329 UNREACHABLE;
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));
337 static
338 void
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, \
357 NULL, \
358 redirect_path_stream_fini, \
359 type) }
361 static
362 void
363 redirect_path_stream_init(void *v)
365 struct redirect_path_stream *s = v;
367 switch (s->m_base.m_type) {
368 case stdout_type:
369 RE(atf_fs_path_init_fmt(&s->m_path, "stdout"));
370 break;
371 case stderr_type:
372 RE(atf_fs_path_init_fmt(&s->m_path, "stderr"));
373 break;
374 default:
375 UNREACHABLE;
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));
382 static
383 void
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 {
398 const char *m_msg;
401 static
402 void
403 child_print(void *v)
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);
410 exit(EXIT_SUCCESS);
413 static
414 void
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" };
422 outfs->init(out);
423 errfs->init(err);
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));
433 outfs->fini(out);
434 errfs->fini(err);
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;
524 atf_fs_path_t path;
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;
546 void
547 child_exit_success(void)
549 exit(EXIT_SUCCESS);
552 void
553 child_exit_failure(void)
555 exit(EXIT_FAILURE);
558 void
559 child_sigkill(void)
561 kill(getpid(), SIGKILL);
562 abort();
565 void
566 child_sigquit(void)
568 kill(getpid(), SIGQUIT);
569 abort();
572 void
573 child_sigterm(void)
575 kill(getpid(), SIGTERM);
576 abort();
579 static
581 fork_and_wait_child(void (*child_func)(void))
583 pid_t pid;
584 int status;
586 pid = fork();
587 ATF_REQUIRE(pid != -1);
588 if (pid == 0) {
589 status = 0; /* Silence compiler warnings */
590 child_func();
591 UNREACHABLE;
592 } else {
593 ATF_REQUIRE(waitpid(pid, &status, 0) != 0);
596 return status;
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 "
663 "that crash");
665 ATF_TC_BODY(status_coredump, tc)
667 struct rlimit rl;
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 "
672 "manually");
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;
690 static
691 void
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))
696 abort();
697 fprintf(stderr, "Reporting %d to parent\n", (int)getpid());
698 exit(EXIT_SUCCESS);
701 ATF_TC(child_pid);
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;
712 pid_t pid;
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)),
719 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);
731 static
732 void
733 child_loop(void *v ATF_DEFS_ATTRIBUTE_UNUSED)
735 for (;;)
736 sleep(1);
739 static
740 void
741 nop_signal(int sig ATF_DEFS_ATTRIBUTE_UNUSED)
745 static
746 void
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) \
754 do { \
755 atf_error_t _aux_err = expr; \
756 if (atf_is_error(_aux_err)) { \
757 atf_error_free(_aux_err); \
758 abort(); \
760 } while (0)
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);
774 sighup.sa_flags = 0;
775 if (sigaction(SIGHUP, &sighup, &old_sighup) == -1)
776 abort();
778 printf("waiting\n");
779 fflush(stdout);
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");
786 abort();
788 if (!atf_error_is(err, "libc")) {
789 fprintf(stderr, "wait did not raise libc_error\n");
790 abort();
792 if (atf_libc_error_code(err) != EINTR) {
793 fprintf(stderr, "libc_error is not EINTR\n");
794 abort();
796 atf_error_free(err);
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);
806 #undef RE_ABORT
808 exit(EXIT_SUCCESS);
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 "
816 "an EINTR error");
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
839 * to do this. */
840 char buf[16];
841 printf("Waiting for child to issue wait(2)\n");
842 ATF_REQUIRE(read(atf_process_child_stdout(&child), buf,
843 sizeof(buf)) > 0);
844 sleep(1);
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 * --------------------------------------------------------------------- */
861 static
862 void
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;
867 const char *argv[3];
869 get_process_helpers_path(tc, true, &process_helpers);
871 argv[0] = atf_fs_path_cstring(&process_helpers);
872 argv[1] = helper_name;
873 argv[2] = NULL;
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);
880 static
881 void
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);
887 free(line);
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);
905 ATF_TC(exec_list);
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;
913 atf_list_t argv;
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,
929 NULL, NULL));
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);
940 ATF_CHECK(fd != -1);
941 check_line(fd, "test-message");
942 close(fd);
945 atf_process_status_fini(&status);
946 atf_fs_path_fini(&process_helpers);
949 static void
950 exit_early(void)
952 exit(80);
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;
988 static
989 void
990 child_cookie(void *v)
992 if (v == NULL)
993 exit(exit_v_null);
994 else
995 exit(exit_v_notnull);
997 UNREACHABLE;
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;
1029 int dummy_int;
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 /* ---------------------------------------------------------------------
1098 * Main.
1099 * --------------------------------------------------------------------- */
1101 ATF_TP_ADD_TCS(tp)
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();