tests: detect fd leaks
authorPekka Paalanen <ppaalanen@gmail.com>
Thu, 19 Apr 2012 13:52:32 +0000 (16:52 +0300)
committerPekka Paalanen <ppaalanen@gmail.com>
Fri, 20 Apr 2012 11:58:31 +0000 (14:58 +0300)
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>
tests/Makefile.am
tests/sanity-test.c
tests/test-helpers.c [new file with mode: 0644]
tests/test-runner.c
tests/test-runner.h

index 4d24d9c..7224778 100644 (file)
@@ -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)
index f2abe42..389ee4c 100644 (file)
@@ -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 (file)
index 0000000..2cc5c7d
--- /dev/null
@@ -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;
+}
+
index 24ae317..c4a57a3 100644 (file)
@@ -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);
 }
 
index 0614101..edcf592 100644 (file)
@@ -31,4 +31,7 @@ struct test {
                                                                \
        static void name(void)
 
+int
+count_open_fds(void);
+
 #endif