diff --git a/tests/Makefile.am b/tests/Makefile.am index 4d24d9c2..72247781 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,7 +10,7 @@ check_PROGRAMS = \ event-loop-test \ client-test -test_runner_src = test-runner.c test-runner.h +test_runner_src = test-runner.c test-runner.h test-helpers.c sanity_test_SOURCES = sanity-test.c $(test_runner_src) map_test_SOURCES = map-test.c $(test_runner_src) diff --git a/tests/sanity-test.c b/tests/sanity-test.c index f2abe426..389ee4c5 100644 --- a/tests/sanity-test.c +++ b/tests/sanity-test.c @@ -85,3 +85,11 @@ FAIL_TEST(sanity_malloc_indirect) /* not freeing array, must leak */ } + +FAIL_TEST(sanity_fd_leak) +{ + int fd[2]; + + /* leak 2 file descriptors */ + pipe(fd); +} diff --git a/tests/test-helpers.c b/tests/test-helpers.c new file mode 100644 index 00000000..2cc5c7d6 --- /dev/null +++ b/tests/test-helpers.c @@ -0,0 +1,52 @@ +/* + * Copyright © 2012 Collabora, Ltd. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "test-runner.h" + +int +count_open_fds(void) +{ + DIR *dir; + struct dirent *ent; + int count = 0; + + dir = opendir("/proc/self/fd"); + assert(dir && "opening /proc/self/fd failed."); + + errno = 0; + while ((ent = readdir(dir))) { + const char *s = ent->d_name; + if (s[0] == '.' && (s[1] == 0 || (s[1] == '.' && s[2] == 0))) + continue; + count++; + } + assert(errno == 0 && "reading /proc/self/fd failed."); + + closedir(dir); + + return count; +} + diff --git a/tests/test-runner.c b/tests/test-runner.c index 24ae3170..c4a57a30 100644 --- a/tests/test-runner.c +++ b/tests/test-runner.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "test-runner.h" static int num_alloc; @@ -69,9 +70,12 @@ static void run_test(const struct test *t) { int cur_alloc = num_alloc; + int cur_fds; + cur_fds = count_open_fds(); t->run(); assert(cur_alloc == num_alloc && "memory leak detected in test."); + assert(cur_fds == count_open_fds() && "fd leak detected"); exit(EXIT_SUCCESS); } diff --git a/tests/test-runner.h b/tests/test-runner.h index 0614101f..edcf5925 100644 --- a/tests/test-runner.h +++ b/tests/test-runner.h @@ -31,4 +31,7 @@ struct test { \ static void name(void) +int +count_open_fds(void); + #endif