pipecmd: reverse order of args to match documentation; add pipecmd_preserve.
[ccan] / ccan / pipecmd / test / run-preserve.c
1 #include <ccan/pipecmd/pipecmd.h>
2 /* Include the C files directly. */
3 #include <ccan/pipecmd/pipecmd.c>
4 #include <ccan/tap/tap.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8
9 int main(int argc, char *argv[])
10 {
11         pid_t child;
12         int fd, oldfd, status;
13         char buf[5] = "test";
14         char template[] = "/tmp/run-preserve.XXXXXX";
15
16         /* We call ourselves, to test pipe. */
17         if (argc == 2) {
18                 if (strcmp(argv[1], "out") == 0) {
19                         if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf))
20                                 exit(2);
21                 } else if (strcmp(argv[1], "in") == 0) {
22                         if (read(STDIN_FILENO, buf, sizeof(buf)) != sizeof(buf))
23                                 exit(3);
24                         if (memcmp(buf, "test", sizeof(buf)) != 0)
25                                 exit(4);
26                 } else if (strcmp(argv[1], "err") == 0) {
27                         if (write(STDERR_FILENO, buf, sizeof(buf)) != sizeof(buf))
28                                 exit(5);
29                 } else
30                         abort();
31                 exit(0);
32         }
33
34         /* This is how many tests you plan to run */
35         plan_tests(25);
36
37         /* Preserve stdin test. */
38         fd = mkstemp(template);
39         ok1(write(fd, buf, sizeof(buf)) == sizeof(buf));
40         ok1(fd >= 0);
41         ok1(dup2(fd, STDIN_FILENO) == STDIN_FILENO);
42         ok1(lseek(STDIN_FILENO, 0, SEEK_SET) == 0);
43         child = pipecmd(&pipecmd_preserve, NULL, NULL, argv[0], "in", NULL);
44         if (!ok1(child > 0))
45                 exit(1);
46         ok1(waitpid(child, &status, 0) == child);
47         ok1(WIFEXITED(status));
48         ok1(WEXITSTATUS(status) == 0);
49
50         close(STDIN_FILENO);
51
52         /* Preserve stdout test */
53         fd = open(template, O_WRONLY|O_TRUNC);
54         ok1(fd >= 0);
55         oldfd = dup(STDOUT_FILENO);
56         /* Can't use OK after this, since we mug stdout */
57         if (dup2(fd, STDOUT_FILENO) != STDOUT_FILENO)
58                 exit(1);
59         child = pipecmd(NULL, &pipecmd_preserve, NULL, argv[0], "out", NULL);
60         if (child == -1)
61                 exit(1);
62         /* Restore stdout */
63         dup2(oldfd, STDOUT_FILENO);
64         close(oldfd);
65         ok1(waitpid(child, &status, 0) == child);
66         ok1(WIFEXITED(status));
67         ok1(WEXITSTATUS(status) == 0);
68
69         fd = open(template, O_RDONLY);
70         ok1(read(fd, buf, sizeof(buf)) == sizeof(buf));
71         ok1(close(fd) == 0);
72         ok1(memcmp(buf, "test", sizeof(buf)) == 0);
73
74         /* Preserve stderr test. */
75         fd = open(template, O_WRONLY|O_TRUNC);
76         ok1(fd >= 0);
77         oldfd = dup(STDERR_FILENO);
78         ok1(dup2(fd, STDERR_FILENO) == STDERR_FILENO);
79         child = pipecmd(NULL, NULL, &pipecmd_preserve, argv[0], "err", NULL);
80         if (!ok1(child > 0))
81                 exit(1);
82
83         /* Restore stderr. */
84         ok1(dup2(oldfd, STDERR_FILENO));
85         ok1(waitpid(child, &status, 0) == child);
86         ok1(WIFEXITED(status));
87         ok1(WEXITSTATUS(status) == 0);
88         close(oldfd);
89
90         fd = open(template, O_RDONLY);
91         ok1(read(fd, buf, sizeof(buf)) == sizeof(buf));
92         ok1(close(fd) == 0);
93         ok1(memcmp(buf, "test", sizeof(buf)) == 0);
94         unlink(template);
95
96         /* This exits depending on whether all tests passed */
97         return exit_status();
98 }