1 #include <ccan/pipecmd/pipecmd.h>
2 /* Include the C files directly. */
3 #include <ccan/pipecmd/pipecmd.c>
4 #include <ccan/tap/tap.h>
9 int main(int argc, char *argv[])
12 int infd, outfd, errfd, status;
15 /* We call ourselves, to test pipe. */
17 if (strcmp(argv[1], "out") == 0) {
18 if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf))
20 } else if (strcmp(argv[1], "in") == 0) {
21 if (read(STDIN_FILENO, buf, sizeof(buf)) != sizeof(buf))
23 if (memcmp(buf, "test", sizeof(buf)) != 0)
25 } else if (strcmp(argv[1], "inout") == 0) {
26 if (read(STDIN_FILENO, buf, sizeof(buf)) != sizeof(buf))
29 if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf))
31 } else if (strcmp(argv[1], "err") == 0) {
32 if (write(STDERR_FILENO, buf, sizeof(buf)) != sizeof(buf))
39 /* We assume no fd leaks, so close them now. */
49 /* This is how many tests you plan to run */
51 child = pipecmd(&infd, &outfd, &errfd, argv[0], "inout", NULL);
54 ok1(write(infd, buf, sizeof(buf)) == sizeof(buf));
55 ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
56 ok1(read(errfd, buf, sizeof(buf)) == 0);
57 ok1(close(infd) == 0);
58 ok1(close(outfd) == 0);
59 ok1(close(errfd) == 0);
61 ok1(memcmp(buf, "test", sizeof(buf)) == 0);
62 ok1(waitpid(child, &status, 0) == child);
63 ok1(WIFEXITED(status));
64 ok1(WEXITSTATUS(status) == 0);
66 child = pipecmd(&infd, NULL, NULL, argv[0], "in", NULL);
69 ok1(write(infd, buf, sizeof(buf)) == sizeof(buf));
70 ok1(close(infd) == 0);
71 ok1(waitpid(child, &status, 0) == child);
72 ok1(WIFEXITED(status));
73 ok1(WEXITSTATUS(status) == 0);
75 child = pipecmd(NULL, &outfd, NULL, argv[0], "out", NULL);
78 ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
79 ok1(close(outfd) == 0);
80 ok1(memcmp(buf, "test", sizeof(buf)) == 0);
81 ok1(waitpid(child, &status, 0) == child);
82 ok1(WIFEXITED(status));
83 ok1(WEXITSTATUS(status) == 0);
85 /* Errfd only should be fine. */
86 child = pipecmd(NULL, NULL, &errfd, argv[0], "err", NULL);
89 ok1(read(errfd, buf, sizeof(buf)) == sizeof(buf));
90 ok1(close(errfd) == 0);
91 ok1(memcmp(buf, "test", sizeof(buf)) == 0);
92 ok1(waitpid(child, &status, 0) == child);
93 ok1(WIFEXITED(status));
94 ok1(WEXITSTATUS(status) == 0);
96 /* errfd == outfd should work with both. */
97 child = pipecmd(NULL, &errfd, &errfd, argv[0], "err", NULL);
100 ok1(read(errfd, buf, sizeof(buf)) == sizeof(buf));
101 ok1(close(errfd) == 0);
102 ok1(memcmp(buf, "test", sizeof(buf)) == 0);
103 ok1(waitpid(child, &status, 0) == child);
104 ok1(WIFEXITED(status));
105 ok1(WEXITSTATUS(status) == 0);
107 child = pipecmd(NULL, &outfd, &outfd, argv[0], "out", NULL);
110 ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
111 ok1(close(outfd) == 0);
112 ok1(memcmp(buf, "test", sizeof(buf)) == 0);
113 ok1(waitpid(child, &status, 0) == child);
114 ok1(WIFEXITED(status));
115 ok1(WEXITSTATUS(status) == 0);
117 // Writing to /dev/null should be fine.
118 child = pipecmd(NULL, NULL, NULL, argv[0], "out", NULL);
121 ok1(waitpid(child, &status, 0) == child);
122 ok1(WIFEXITED(status));
123 ok1(WEXITSTATUS(status) == 0);
125 // Reading should fail.
126 child = pipecmd(NULL, NULL, NULL, argv[0], "in", NULL);
129 ok1(waitpid(child, &status, 0) == child);
130 ok1(WIFEXITED(status));
131 ok1(WEXITSTATUS(status) == 1);
133 child = pipecmd(NULL, NULL, NULL, argv[0], "err", NULL);
136 ok1(waitpid(child, &status, 0) == child);
137 ok1(WIFEXITED(status));
138 ok1(WEXITSTATUS(status) == 0);
140 // Can't run non-existent file, but errno set correctly.
141 child = pipecmd(NULL, NULL, NULL, "/doesnotexist", "in", NULL);
142 ok1(errno == ENOENT);
146 ok1(close(3) == -1 && errno == EBADF);
147 ok1(close(4) == -1 && errno == EBADF);
148 ok1(close(5) == -1 && errno == EBADF);
149 ok1(close(6) == -1 && errno == EBADF);
150 ok1(close(7) == -1 && errno == EBADF);
151 ok1(close(8) == -1 && errno == EBADF);
152 ok1(close(9) == -1 && errno == EBADF);
153 ok1(close(10) == -1 && errno == EBADF);
155 /* This exits depending on whether all tests passed */
156 return exit_status();