9 #include <CUnit/CUnit.h>
13 #include "test_process.h"
15 void test_wait_for_death(void)
21 execl("/bin/true", "/bin/true", NULL
);
25 CU_ASSERT(!wait_for_death(pid
, 0, 2000));
26 CU_ASSERT(wait_for_death(pid
, 0, 20000));
29 void test_ensure_death(void)
34 signal(SIGTERM
, SIG_IGN
);
35 signal(SIGHUP
, SIG_IGN
);
36 execl("/bin/true", "/bin/true", NULL
);
42 CU_ASSERT(waitpid(pid
, NULL
, WNOHANG
) < 0);
43 CU_ASSERT(errno
== ECHILD
);
46 int child_function(void *a
)
49 ssize_t l
= strlen(s
);
50 char buffer
[LINE_MAX
];
54 if (write(STDOUT_FILENO
, s
, l
) < l
)
57 l_in
= read(STDIN_FILENO
, buffer
, sizeof buffer
);
62 l_out
= write(STDOUT_FILENO
, buffer
, l_in
);
70 void test_create_child_function(void)
74 ssize_t l1
= strlen(s1
);
75 ssize_t l2
= strlen(s2
);
76 struct child_process child
= {
77 .function
= child_function
,
79 .stdin_fd
= REDIRECT_PIPE
,
80 .stdout_fd
= REDIRECT_PIPE
,
81 .stderr_fd
= REDIRECT_DEV_NULL
,
84 char buffer
[LINE_MAX
];
86 CU_ASSERT(create_child(&child
));
88 CU_ASSERT(child
.pid
> 0);
90 CU_ASSERT(read(child
.stdout_fd
, buffer
, sizeof buffer
) == l1
);
91 CU_ASSERT(strncmp(buffer
, s1
, l1
) == 0);
93 CU_ASSERT(waitpid(child
.pid
, NULL
, WNOHANG
) == 0);
95 CU_ASSERT(write(child
.stdin_fd
, s2
, l2
) == l2
);
96 CU_ASSERT(read(child
.stdout_fd
, buffer
, sizeof buffer
) == l2
);
98 CU_ASSERT(strncmp(buffer
, s2
, l2
) == 0);
100 CU_ASSERT(waitpid(child
.pid
, &status
, 0) == child
.pid
);
102 CU_ASSERT(WIFEXITED(status
));
103 CU_ASSERT(WEXITSTATUS(status
) == 0);
105 (void) close(child
.stdin_fd
);
106 (void) close(child
.stdout_fd
);
109 void test_create_child_process(void)
111 const char *s1
= "hello\n";
112 const char *s2
= "olleh\n";
113 ssize_t l1
= strlen(s1
);
114 ssize_t l2
= strlen(s2
);
115 const char *argv
[] = { "sh", "-c", "rev", NULL
};
116 struct child_process child
= {
119 .stdin_fd
= REDIRECT_PIPE
,
120 .stdout_fd
= REDIRECT_PIPE
,
121 .stderr_fd
= REDIRECT_DEV_NULL
,
124 char buffer
[LINE_MAX
];
126 CU_ASSERT(create_child(&child
));
128 CU_ASSERT(write(child
.stdin_fd
, s1
, l1
) == l1
);
129 (void) close(child
.stdin_fd
);
131 CU_ASSERT(read(child
.stdout_fd
, buffer
, sizeof buffer
) == l2
);
132 (void) close(child
.stdout_fd
);
134 CU_ASSERT(strncmp(buffer
, s2
, l2
) == 0);
136 CU_ASSERT(wait_for_death(child
.pid
, 0, 0));
139 CU_TestInfo process_tests
[] = {
140 { "test_wait_for_death", test_wait_for_death
},
141 { "test_ensure_death", test_ensure_death
},
142 { "test_create_child_function", test_create_child_function
},
143 { "test_create_child_process", test_create_child_process
},