X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fpipecmd%2Ftest%2Frun-fdleak.c;fp=ccan%2Fpipecmd%2Ftest%2Frun-fdleak.c;h=9ab9d1bbbdbce977617cf9363fe5ab524234af99;hb=dd204de80d65e1ef22e90d207a816b7ef6a2e548;hp=0000000000000000000000000000000000000000;hpb=aae40e493625a07f4ac95476664447546b28661a;p=ccan diff --git a/ccan/pipecmd/test/run-fdleak.c b/ccan/pipecmd/test/run-fdleak.c new file mode 100644 index 00000000..9ab9d1bb --- /dev/null +++ b/ccan/pipecmd/test/run-fdleak.c @@ -0,0 +1,44 @@ +#include +/* Include the C files directly. */ +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + pid_t child; + int outfd, status; + char buf[5] = "test"; + + /* We call ourselves, to test pipe. */ + if (argc == 2) { + if (write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) + exit(1); + exit(0); + } + + /* This is how many tests you plan to run */ + plan_tests(13); + child = pipecmd(&outfd, NULL, argv[0], "out", NULL); + if (!ok1(child > 0)) + exit(1); + ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf)); + ok1(memcmp(buf, "test", sizeof(buf)) == 0); + ok1(waitpid(child, &status, 0) == child); + ok1(WIFEXITED(status)); + ok1(WEXITSTATUS(status) == 0); + + /* No leaks! */ + ok1(close(outfd) == 0); + ok1(close(outfd) == -1 && errno == EBADF); + ok1(close(++outfd) == -1 && errno == EBADF); + ok1(close(++outfd) == -1 && errno == EBADF); + ok1(close(++outfd) == -1 && errno == EBADF); + ok1(close(++outfd) == -1 && errno == EBADF); + ok1(close(++outfd) == -1 && errno == EBADF); + + /* This exits depending on whether all tests passed */ + return exit_status(); +}