mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2025-10-31 22:25:25 -04:00
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>
This commit is contained in:
parent
2896b6a220
commit
e0561ac68d
5 changed files with 68 additions and 1 deletions
|
|
@ -10,7 +10,7 @@ check_PROGRAMS = \
|
||||||
event-loop-test \
|
event-loop-test \
|
||||||
client-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)
|
sanity_test_SOURCES = sanity-test.c $(test_runner_src)
|
||||||
map_test_SOURCES = map-test.c $(test_runner_src)
|
map_test_SOURCES = map-test.c $(test_runner_src)
|
||||||
|
|
|
||||||
|
|
@ -85,3 +85,11 @@ FAIL_TEST(sanity_malloc_indirect)
|
||||||
|
|
||||||
/* not freeing array, must leak */
|
/* not freeing array, must leak */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FAIL_TEST(sanity_fd_leak)
|
||||||
|
{
|
||||||
|
int fd[2];
|
||||||
|
|
||||||
|
/* leak 2 file descriptors */
|
||||||
|
pipe(fd);
|
||||||
|
}
|
||||||
|
|
|
||||||
52
tests/test-helpers.c
Normal file
52
tests/test-helpers.c
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -30,6 +30,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
#include <errno.h>
|
||||||
#include "test-runner.h"
|
#include "test-runner.h"
|
||||||
|
|
||||||
static int num_alloc;
|
static int num_alloc;
|
||||||
|
|
@ -69,9 +70,12 @@ static void
|
||||||
run_test(const struct test *t)
|
run_test(const struct test *t)
|
||||||
{
|
{
|
||||||
int cur_alloc = num_alloc;
|
int cur_alloc = num_alloc;
|
||||||
|
int cur_fds;
|
||||||
|
|
||||||
|
cur_fds = count_open_fds();
|
||||||
t->run();
|
t->run();
|
||||||
assert(cur_alloc == num_alloc && "memory leak detected in test.");
|
assert(cur_alloc == num_alloc && "memory leak detected in test.");
|
||||||
|
assert(cur_fds == count_open_fds() && "fd leak detected");
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,4 +31,7 @@ struct test {
|
||||||
\
|
\
|
||||||
static void name(void)
|
static void name(void)
|
||||||
|
|
||||||
|
int
|
||||||
|
count_open_fds(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue