242e93f8061c246e344a26079f0b0fd2808bb813
[ccan] / ccan / err / test / run.c
1 #include <ccan/err/err.c>
2 #include <ccan/tap/tap.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10
11 #define BUFFER_MAX 1024
12
13 int main(int argc, char *argv[])
14 {
15         int pfd[2];
16         const char *base;
17
18         plan_tests(24);
19
20         err_set_progname(argv[0]);
21
22         /* In case it only prints out the basename of argv[0]. */
23         base = strrchr(argv[0], '/');
24         if (base)
25                 base++;
26         else
27                 base = argv[0];
28
29         /* Test err() in child */
30         pipe(pfd);
31         fflush(stdout);
32         if (fork()) {
33                 char buffer[BUFFER_MAX+1];
34                 unsigned int i;
35                 int status;
36
37                 /* We are parent. */
38                 close(pfd[1]);
39                 for (i = 0; i < BUFFER_MAX; i++) {
40                         if (read(pfd[0], buffer + i, 1) == 0) {
41                                 buffer[i] = '\0';
42                                 ok1(strstr(buffer, "running err:"));
43                                 ok1(strstr(buffer, strerror(ENOENT)));
44                                 ok1(strstr(buffer, base));
45                                 ok1(buffer[i-1] == '\n');
46                                 break;
47                         }
48                 }
49                 close(pfd[0]);
50                 ok1(wait(&status) != -1);
51                 ok1(WIFEXITED(status));
52                 ok1(WEXITSTATUS(status) == 17);
53         } else {
54                 close(pfd[0]);
55                 dup2(pfd[1], STDERR_FILENO);
56                 errno = ENOENT;
57                 err(17, "running %s", "err");
58                 abort();
59         }
60
61         /* Test errx() in child */
62         pipe(pfd);
63         fflush(stdout);
64         if (fork()) {
65                 char buffer[BUFFER_MAX+1];
66                 unsigned int i;
67                 int status;
68
69                 /* We are parent. */
70                 close(pfd[1]);
71                 for (i = 0; i < BUFFER_MAX; i++) {
72                         if (read(pfd[0], buffer + i, 1) == 0) {
73                                 buffer[i] = '\0';
74                                 ok1(strstr(buffer, "running errx\n"));
75                                 ok1(strstr(buffer, base));
76                                 break;
77                         }
78                 }
79                 close(pfd[0]);
80                 ok1(wait(&status) != -1);
81                 ok1(WIFEXITED(status));
82                 ok1(WEXITSTATUS(status) == 17);
83         } else {
84                 close(pfd[0]);
85                 dup2(pfd[1], STDERR_FILENO);
86                 errx(17, "running %s", "errx");
87                 abort();
88         }
89
90
91         /* Test warn() in child */
92         pipe(pfd);
93         fflush(stdout);
94         if (fork()) {
95                 char buffer[BUFFER_MAX+1];
96                 unsigned int i;
97                 int status;
98
99                 /* We are parent. */
100                 close(pfd[1]);
101                 for (i = 0; i < BUFFER_MAX; i++) {
102                         if (read(pfd[0], buffer + i, 1) == 0) {
103                                 buffer[i] = '\0';
104                                 ok1(strstr(buffer, "running warn:"));
105                                 ok1(strstr(buffer, strerror(ENOENT)));
106                                 ok1(strstr(buffer, base));
107                                 ok1(buffer[i-1] == '\n');
108                                 break;
109                         }
110                 }
111                 close(pfd[0]);
112                 ok1(wait(&status) != -1);
113                 ok1(WIFEXITED(status));
114                 ok1(WEXITSTATUS(status) == 17);
115         } else {
116                 close(pfd[0]);
117                 dup2(pfd[1], STDERR_FILENO);
118                 errno = ENOENT;
119                 warn("running %s", "warn");
120                 exit(17);
121         }
122
123         /* Test warnx() in child */
124         pipe(pfd);
125         fflush(stdout);
126         if (fork()) {
127                 char buffer[BUFFER_MAX+1];
128                 unsigned int i;
129                 int status;
130
131                 /* We are parent. */
132                 close(pfd[1]);
133                 for (i = 0; i < BUFFER_MAX; i++) {
134                         if (read(pfd[0], buffer + i, 1) == 0) {
135                                 buffer[i] = '\0';
136                                 ok1(strstr(buffer, "running warnx\n"));
137                                 ok1(strstr(buffer, base));
138                                 break;
139                         }
140                 }
141                 close(pfd[0]);
142                 ok1(wait(&status) != -1);
143                 ok1(WIFEXITED(status));
144                 ok1(WEXITSTATUS(status) == 17);
145         } else {
146                 close(pfd[0]);
147                 dup2(pfd[1], STDERR_FILENO);
148                 warnx("running %s", "warnx");
149                 exit(17);
150         }
151         return exit_status();
152 }
153