document the change in prompt timeout handling
[vlock.git] / tests / test_process.c
blob59e16c533e67f0ec749a025307fc0b3ccc3cfa65
1 #include <sys/types.h>
2 #include <sys/wait.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <signal.h>
6 #include <errno.h>
7 #include <limits.h>
9 #include <CUnit/CUnit.h>
11 #include "process.h"
13 #include "test_process.h"
15 void test_wait_for_death(void)
17 pid_t pid = fork();
19 if (pid == 0) {
20 usleep(20000);
21 execl("/bin/true", "/bin/true", NULL);
22 _exit(1);
25 CU_ASSERT(!wait_for_death(pid, 0, 2000));
26 CU_ASSERT(wait_for_death(pid, 0, 20000));
29 void test_ensure_death(void)
31 pid_t pid = fork();
33 if (pid == 0) {
34 signal(SIGTERM, SIG_IGN);
35 signal(SIGHUP, SIG_IGN);
36 execl("/bin/true", "/bin/true", NULL);
37 _exit(0);
40 ensure_death(pid);
42 CU_ASSERT(waitpid(pid, NULL, WNOHANG) < 0);
43 CU_ASSERT(errno == ECHILD);
46 int child_function(void *a)
48 char *s = a;
49 ssize_t l = strlen(s);
50 char buffer[LINE_MAX];
51 ssize_t l_in;
52 ssize_t l_out;
54 if (write(STDOUT_FILENO, s, l) < l)
55 return 1;
57 l_in = read(STDIN_FILENO, buffer, sizeof buffer);
59 if (l_in <= 0)
60 return 1;
62 l_out = write(STDOUT_FILENO, buffer, l_in);
64 if (l_out != l_in)
65 return 1;
67 return 0;
70 void test_create_child_function(void)
72 char *s1 = "hello";
73 char *s2 = "world";
74 ssize_t l1 = strlen(s1);
75 ssize_t l2 = strlen(s2);
76 struct child_process child = {
77 .function = child_function,
78 .argument = s1,
79 .stdin_fd = REDIRECT_PIPE,
80 .stdout_fd = REDIRECT_PIPE,
81 .stderr_fd = REDIRECT_DEV_NULL,
83 int status;
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 = {
117 .path = "/bin/sh",
118 .argv = argv,
119 .stdin_fd = REDIRECT_PIPE,
120 .stdout_fd = REDIRECT_PIPE,
121 .stderr_fd = REDIRECT_DEV_NULL,
122 .function = 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 },
144 CU_TEST_INFO_NULL,