ccan/err: new err.h-replacing module.
[ccan] / ccan / err / test / run.c
1 #include <ccan/err/err.h>
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(void)
14 {
15         int pfd[2];
16
17         plan_tests(20);
18         fflush(stdout);
19
20         /* Test err() in child */
21         pipe(pfd);
22         if (fork()) {
23                 char buffer[BUFFER_MAX+1];
24                 unsigned int i;
25                 int status;
26
27                 /* We are parent. */
28                 close(pfd[1]);
29                 for (i = 0; i < BUFFER_MAX; i++) {
30                         if (read(pfd[0], buffer + i, 1) == 0) {
31                                 buffer[i] = '\0';
32                                 ok1(strstr(buffer, "running err:"));
33                                 ok1(strstr(buffer, strerror(ENOENT)));
34                                 ok1(buffer[i-1] == '\n');
35                                 break;
36                         }
37                 }
38                 close(pfd[0]);
39                 ok1(wait(&status) != -1);
40                 ok1(WIFEXITED(status));
41                 ok1(WEXITSTATUS(status) == 17);
42         } else {
43                 close(pfd[0]);
44                 dup2(pfd[1], STDERR_FILENO);
45                 errno = ENOENT;
46                 err(17, "running %s", "err");
47                 abort();
48         }
49
50         /* Test errx() in child */
51         pipe(pfd);
52         fflush(stdout);
53         if (fork()) {
54                 char buffer[BUFFER_MAX+1];
55                 unsigned int i;
56                 int status;
57
58                 /* We are parent. */
59                 close(pfd[1]);
60                 for (i = 0; i < BUFFER_MAX; i++) {
61                         if (read(pfd[0], buffer + i, 1) == 0) {
62                                 buffer[i] = '\0';
63                                 ok1(strstr(buffer, "running errx\n"));
64                                 break;
65                         }
66                 }
67                 close(pfd[0]);
68                 ok1(wait(&status) != -1);
69                 ok1(WIFEXITED(status));
70                 ok1(WEXITSTATUS(status) == 17);
71         } else {
72                 close(pfd[0]);
73                 dup2(pfd[1], STDERR_FILENO);
74                 errx(17, "running %s", "errx");
75                 abort();
76         }
77
78
79         /* Test warn() in child */
80         pipe(pfd);
81         fflush(stdout);
82         if (fork()) {
83                 char buffer[BUFFER_MAX+1];
84                 unsigned int i;
85                 int status;
86
87                 /* We are parent. */
88                 close(pfd[1]);
89                 for (i = 0; i < BUFFER_MAX; i++) {
90                         if (read(pfd[0], buffer + i, 1) == 0) {
91                                 buffer[i] = '\0';
92                                 ok1(strstr(buffer, "running warn:"));
93                                 ok1(strstr(buffer, strerror(ENOENT)));
94                                 ok1(buffer[i-1] == '\n');
95                                 break;
96                         }
97                 }
98                 close(pfd[0]);
99                 ok1(wait(&status) != -1);
100                 ok1(WIFEXITED(status));
101                 ok1(WEXITSTATUS(status) == 17);
102         } else {
103                 close(pfd[0]);
104                 dup2(pfd[1], STDERR_FILENO);
105                 errno = ENOENT;
106                 warn("running %s", "warn");
107                 exit(17);
108         }
109
110         /* Test warnx() in child */
111         pipe(pfd);
112         fflush(stdout);
113         if (fork()) {
114                 char buffer[BUFFER_MAX+1];
115                 unsigned int i;
116                 int status;
117
118                 /* We are parent. */
119                 close(pfd[1]);
120                 for (i = 0; i < BUFFER_MAX; i++) {
121                         if (read(pfd[0], buffer + i, 1) == 0) {
122                                 buffer[i] = '\0';
123                                 ok1(strstr(buffer, "running warnx\n"));
124                                 break;
125                         }
126                 }
127                 close(pfd[0]);
128                 ok1(wait(&status) != -1);
129                 ok1(WIFEXITED(status));
130                 ok1(WEXITSTATUS(status) == 17);
131         } else {
132                 close(pfd[0]);
133                 dup2(pfd[1], STDERR_FILENO);
134                 warnx("running %s", "warnx");
135                 exit(17);
136         }
137         return exit_status();
138 }
139