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, 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))
36 /* This is how many tests you plan to run */
38 child = pipecmd(&outfd, &infd, argv[0], "inout", NULL);
41 ok1(write(infd, buf, sizeof(buf)) == sizeof(buf));
42 ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
44 ok1(memcmp(buf, "test", sizeof(buf)) == 0);
45 ok1(waitpid(child, &status, 0) == child);
46 ok1(WIFEXITED(status));
47 ok1(WEXITSTATUS(status) == 0);
49 child = pipecmd(NULL, &infd, argv[0], "in", NULL);
52 ok1(write(infd, buf, sizeof(buf)) == sizeof(buf));
53 ok1(waitpid(child, &status, 0) == child);
54 ok1(WIFEXITED(status));
55 ok1(WEXITSTATUS(status) == 0);
57 child = pipecmd(&outfd, NULL, argv[0], "out", NULL);
60 ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
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 // Writing to /dev/null should be fine.
67 child = pipecmd(NULL, NULL, argv[0], "out", NULL);
70 ok1(waitpid(child, &status, 0) == child);
71 ok1(WIFEXITED(status));
72 ok1(WEXITSTATUS(status) == 0);
74 // Reading should fail.
75 child = pipecmd(NULL, NULL, argv[0], "in", NULL);
78 ok1(waitpid(child, &status, 0) == child);
79 ok1(WIFEXITED(status));
80 ok1(WEXITSTATUS(status) == 1);
82 /* This exits depending on whether all tests passed */