summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPekka Paalanen <ppaalanen@gmail.com>2012-04-19 16:52:32 +0300
committerPekka Paalanen <ppaalanen@gmail.com>2012-04-20 14:58:31 +0300
commite0561ac68d4db23906ca7e61bf191da4451711fd (patch)
treef1c35d58239fe67859cb47022e0fb17b1d27bc06 /tests
parent2896b6a22040dab70655bc271025f99ba92e6bb1 (diff)
tests: detect fd leaks
Detect file descriptor leaks in tests. Add a sanity test to verify that we catch the leaks. Signed-off-by: Pekka Paalanen <ppaalanen@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/sanity-test.c8
-rw-r--r--tests/test-helpers.c52
-rw-r--r--tests/test-runner.c4
-rw-r--r--tests/test-runner.h3
5 files changed, 68 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4d24d9c..7224778 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 f2abe42..389ee4c 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 0000000..2cc5c7d
--- /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 <assert.h>
+#include <errno.h>
+#include <dirent.h>
+
+#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 24ae317..c4a57a3 100644
--- a/tests/test-runner.c
+++ b/tests/test-runner.c
@@ -30,6 +30,7 @@
#include <string.h>
#include <assert.h>
#include <dlfcn.h>
+#include <errno.h>
#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 0614101..edcf592 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